-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Place command (currently unfinished)
- Loading branch information
1 parent
f90d2ad
commit 58b9e4e
Showing
12 changed files
with
262 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/net/pedroricardo/commander/CommanderReflectionHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package net.pedroricardo.commander; | ||
|
||
import com.b100.utils.FileUtils; | ||
import com.b100.utils.interfaces.Condition; | ||
import net.fabricmc.loader.impl.launch.knot.Knot; | ||
import net.minecraft.core.util.helper.ReflectionHelper; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Enumeration; | ||
import java.util.List; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipFile; | ||
|
||
public class CommanderReflectionHelper { | ||
public static List<Class<?>> getAllClasses(Condition<String> classNameCondition) { | ||
String[] classPathEntries; | ||
ArrayList classes = new ArrayList(); | ||
for (String classPathEntry : classPathEntries = System.getProperty("java.class.path").split(";")) { | ||
File file = new File(classPathEntry); | ||
if (file.isDirectory()) { | ||
int l = file.getAbsolutePath().length() + 1; | ||
List<File> files = FileUtils.getAllFiles(file); | ||
for (File file2 : files) { | ||
if (!file2.getName().endsWith(".class")) continue; | ||
tryAddClass(classes, file2.getAbsolutePath().substring(l), classNameCondition); | ||
} | ||
} | ||
if (!file.isFile()) continue; | ||
try { | ||
ZipFile zip = new ZipFile(file); | ||
Enumeration<? extends ZipEntry> entries = zip.entries(); | ||
while (entries.hasMoreElements()) { | ||
String entry = ((Object)entries.nextElement()).toString(); | ||
if (!entry.endsWith(".class")) continue; | ||
tryAddClass(classes, entry, classNameCondition); | ||
} | ||
zip.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
return classes; | ||
} | ||
|
||
private static void tryAddClass(List<Class<?>> classes, String fileName, Condition<String> classNameCondition) { | ||
String className = fileName.substring(0, fileName.length() - 6); | ||
className = className.replace('\\', '.'); | ||
className = className.replace('/', '.'); | ||
if (classNameCondition == null || classNameCondition.isTrue(className)) { | ||
Class<?> clazz = null; | ||
try { | ||
clazz = Knot.getClass(className); | ||
} catch (Throwable e) { | ||
System.err.println(e.getClass().getName() + ": " + e.getMessage()); | ||
} | ||
if (clazz != null) { | ||
classes.add(clazz); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
src/main/java/net/pedroricardo/commander/content/arguments/WorldFeatureArgumentType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package net.pedroricardo.commander.content.arguments; | ||
|
||
import com.b100.utils.Utils; | ||
import com.mojang.brigadier.StringReader; | ||
import com.mojang.brigadier.arguments.ArgumentType; | ||
import com.mojang.brigadier.context.CommandContext; | ||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import com.mojang.brigadier.suggestion.Suggestions; | ||
import com.mojang.brigadier.suggestion.SuggestionsBuilder; | ||
import net.fabricmc.loader.impl.lib.sat4j.specs.Constr; | ||
import net.minecraft.core.achievement.Achievement; | ||
import net.minecraft.core.achievement.AchievementList; | ||
import net.minecraft.core.block.Block; | ||
import net.minecraft.core.lang.I18n; | ||
import net.minecraft.core.net.command.commands.GenerateCommand; | ||
import net.minecraft.core.util.collection.Pair; | ||
import net.minecraft.core.util.helper.ReflectionHelper; | ||
import net.minecraft.core.world.generate.feature.WorldFeature; | ||
import net.pedroricardo.commander.Commander; | ||
import net.pedroricardo.commander.CommanderHelper; | ||
import net.pedroricardo.commander.mixin.StatNameAccessor; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Parameter; | ||
import java.util.*; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class WorldFeatureArgumentType implements ArgumentType<WorldFeature> { | ||
private static final Collection<String> EXAMPLES = Arrays.asList("achievement.acquireIron", "acquireIron"); | ||
|
||
public static ArgumentType<WorldFeature> worldFeature() { | ||
return new WorldFeatureArgumentType(); | ||
} | ||
|
||
@Override | ||
public WorldFeature parse(StringReader reader) throws CommandSyntaxException { | ||
final String string = reader.readString(); | ||
Class<? extends WorldFeature> worldFeatureClass = null; | ||
|
||
for (Map.Entry<String, Class<? extends WorldFeature>> entry : CommanderHelper.WORLD_FEATURES.entrySet()) { | ||
if (CommanderHelper.matchesKeyString(entry.getKey(), string)) { | ||
worldFeatureClass = entry.getValue(); | ||
} | ||
} | ||
|
||
if (worldFeatureClass == null) throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().createWithContext(reader); | ||
|
||
boolean hasParameters = false; | ||
Constructor<?> constructor = null; | ||
|
||
for (Constructor<?> c : worldFeatureClass.getConstructors()) { | ||
constructor = c; | ||
if (c.getParameters().length != 0) hasParameters = true; | ||
} | ||
|
||
if (!hasParameters && constructor != null) { | ||
try { | ||
return (WorldFeature) constructor.newInstance(); | ||
} catch (Exception e) { | ||
throw new CommandSyntaxException(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument(), () -> I18n.getInstance().translateKey("argument_types.commander.world_feature.invalid_world_feature")); | ||
} | ||
} | ||
|
||
if (reader.canRead() && reader.peek() == '[') { | ||
List<Object> parameters = new ArrayList<>(); | ||
constructor = null; | ||
|
||
int cursor = reader.getCursor(); | ||
|
||
for (Constructor<?> c : worldFeatureClass.getConstructors()) { | ||
reader.setCursor(cursor); | ||
for (Parameter parameter : c.getParameters()) { | ||
reader.skip(); | ||
reader.skipWhitespace(); | ||
int cursor2 = reader.getCursor(); | ||
if (parameter.getType() == Integer.TYPE) { | ||
parameters.add(reader.readInt()); | ||
} else if (parameter.getType() == Block.class) { | ||
parameters.add(parseBlock(reader)); | ||
} else if (parameter.getType() == String.class) { | ||
parameters.add(reader.readString()); | ||
} else { | ||
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand().createWithContext(reader); | ||
} | ||
reader.skipWhitespace(); | ||
if (!reader.canRead() || (reader.peek() != ',' && reader.peek() != ']')) { | ||
reader.setCursor(cursor2); | ||
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().createWithContext(reader); | ||
} | ||
} | ||
constructor = c; | ||
} | ||
if (constructor != null && reader.canRead() && reader.peek() == ']') { | ||
reader.skip(); | ||
try { | ||
return (WorldFeature) constructor.newInstance(Utils.toArray(Object.class, parameters)); | ||
} catch (Exception e) { | ||
Commander.LOGGER.info(e.toString()); | ||
throw new CommandSyntaxException(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument(), () -> I18n.getInstance().translateKey("argument_types.commander.world_feature.invalid_world_feature")); | ||
} | ||
} | ||
} | ||
throw new CommandSyntaxException(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument(), () -> I18n.getInstance().translateKey("argument_types.commander.world_feature.invalid_world_feature")); | ||
} | ||
|
||
private static Block parseBlock(StringReader reader) throws CommandSyntaxException { | ||
final String string = reader.readString(); | ||
|
||
for (Block blockInList : Block.blocksList) { | ||
if (blockInList == null) continue; | ||
if (CommanderHelper.matchesKeyString(blockInList.getKey(), string)) { | ||
return blockInList; | ||
} | ||
} | ||
throw new CommandSyntaxException(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument(), () -> I18n.getInstance().translateKey("argument_types.commander.block.invalid_block")); | ||
} | ||
|
||
@Override | ||
public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) { | ||
String remaining = builder.getRemainingLowerCase(); | ||
for (Map.Entry<String, Class<? extends WorldFeature>> entry : CommanderHelper.WORLD_FEATURES.entrySet()) { | ||
Optional<String> optional = CommanderHelper.getStringToSuggest(entry.getKey(), remaining); | ||
optional.ifPresent(builder::suggest); | ||
} | ||
return builder.buildFuture(); | ||
} | ||
|
||
@Override | ||
public Collection<String> getExamples() { | ||
return EXAMPLES; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/net/pedroricardo/commander/content/commands/PlaceCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package net.pedroricardo.commander.content.commands; | ||
|
||
import com.mojang.brigadier.Command; | ||
import com.mojang.brigadier.CommandDispatcher; | ||
import com.mojang.brigadier.builder.LiteralArgumentBuilder; | ||
import com.mojang.brigadier.builder.RequiredArgumentBuilder; | ||
import com.mojang.brigadier.tree.CommandNode; | ||
import net.minecraft.core.util.phys.Vec3d; | ||
import net.minecraft.core.world.generate.feature.WorldFeature; | ||
import net.pedroricardo.commander.content.CommanderCommandSource; | ||
import net.pedroricardo.commander.content.arguments.IntegerCoordinatesArgumentType; | ||
import net.pedroricardo.commander.content.arguments.WorldFeatureArgumentType; | ||
import net.pedroricardo.commander.content.exceptions.CommanderExceptions; | ||
import net.pedroricardo.commander.content.helpers.IntegerCoordinates; | ||
|
||
import java.util.Collections; | ||
|
||
@SuppressWarnings("unchecked") | ||
public class PlaceCommand { | ||
public static void register(CommandDispatcher<CommanderCommandSource> dispatcher) { | ||
CommandNode<Object> command = dispatcher.register((LiteralArgumentBuilder) LiteralArgumentBuilder.literal("place") | ||
.then((RequiredArgumentBuilder) RequiredArgumentBuilder.argument("feature", WorldFeatureArgumentType.worldFeature()) | ||
.executes(c -> { | ||
CommanderCommandSource source = (CommanderCommandSource) c.getSource(); | ||
WorldFeature feature = c.getArgument("feature", WorldFeature.class); | ||
Vec3d coordinates = ((CommanderCommandSource) c.getSource()).getBlockCoordinates(); | ||
|
||
if (coordinates == null) throw CommanderExceptions.notInWorld().create(); | ||
|
||
feature.generate(source.getWorld(), source.getWorld().rand, (int)coordinates.xCoord, (int)coordinates.yCoord, (int)coordinates.zCoord); | ||
return Command.SINGLE_SUCCESS; | ||
}))); | ||
dispatcher.register((LiteralArgumentBuilder) LiteralArgumentBuilder.literal("generate") | ||
.redirect(command)); | ||
dispatcher.register((LiteralArgumentBuilder) LiteralArgumentBuilder.literal("gen") | ||
.redirect(command)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters