Renamed directory to anticope
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package anticope.rejects.arguments;
|
||||
|
||||
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.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.command.CommandSource;
|
||||
import net.minecraft.command.argument.*;
|
||||
import net.minecraft.server.command.CommandManager;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class ClientPosArgumentType implements ArgumentType<Vec3d> {
|
||||
private static final Collection<String> EXAMPLES = Arrays.asList("0 0 0", "~ ~ ~", "~0.5 ~1 ~-5");
|
||||
private static final MinecraftClient mc = MinecraftClient.getInstance();
|
||||
|
||||
public ClientPosArgumentType() {
|
||||
}
|
||||
|
||||
public static ClientPosArgumentType pos() {
|
||||
return new ClientPosArgumentType();
|
||||
}
|
||||
|
||||
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
|
||||
if (!(context.getSource() instanceof CommandSource)) {
|
||||
return Suggestions.empty();
|
||||
} else {
|
||||
String string = builder.getRemaining();
|
||||
Object collection2 = ((CommandSource)context.getSource()).getBlockPositionSuggestions();
|
||||
|
||||
return CommandSource.suggestPositions(string, (Collection)collection2, builder, CommandManager.getCommandValidator(this::parse));
|
||||
}
|
||||
}
|
||||
|
||||
public static Vec3d getPos(final CommandContext<?> context, final String name) {
|
||||
return context.getArgument(name, Vec3d.class);
|
||||
}
|
||||
|
||||
|
||||
public Vec3d parse(StringReader reader) throws CommandSyntaxException {
|
||||
int i = reader.getCursor();
|
||||
double x,y,z;
|
||||
CoordinateArgument coordinateArgument = CoordinateArgument.parse(reader);
|
||||
CoordinateArgument coordinateArgument2;
|
||||
CoordinateArgument coordinateArgument3;
|
||||
if (reader.canRead() && reader.peek() == ' ') {
|
||||
reader.skip();
|
||||
coordinateArgument2 = CoordinateArgument.parse(reader);
|
||||
if (reader.canRead() && reader.peek() == ' ') {
|
||||
reader.skip();
|
||||
coordinateArgument3 = CoordinateArgument.parse(reader);
|
||||
} else {
|
||||
reader.setCursor(i);
|
||||
throw Vec3ArgumentType.INCOMPLETE_EXCEPTION.createWithContext(reader);
|
||||
}
|
||||
} else {
|
||||
reader.setCursor(i);
|
||||
throw Vec3ArgumentType.INCOMPLETE_EXCEPTION.createWithContext(reader);
|
||||
}
|
||||
|
||||
x = coordinateArgument.toAbsoluteCoordinate(mc.player.getX());
|
||||
y = coordinateArgument2.toAbsoluteCoordinate(mc.player.getY());
|
||||
z = coordinateArgument3.toAbsoluteCoordinate(mc.player.getZ());
|
||||
|
||||
return new Vec3d(x,y,z);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package anticope.rejects.arguments;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
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.exceptions.DynamicCommandExceptionType;
|
||||
import com.mojang.brigadier.suggestion.Suggestions;
|
||||
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import net.minecraft.command.CommandSource;
|
||||
import net.minecraft.text.LiteralText;
|
||||
|
||||
public class EnumArgumentType<T extends Enum<?>> implements ArgumentType<T> {
|
||||
private static final DynamicCommandExceptionType NO_SUCH_TYPE = new DynamicCommandExceptionType(o ->
|
||||
new LiteralText(o + " is not a valid argument."));
|
||||
|
||||
private T[] values;
|
||||
|
||||
public EnumArgumentType(T defaultValue) {
|
||||
super();
|
||||
|
||||
try {
|
||||
values = (T[]) defaultValue.getClass().getMethod("values").invoke(null);
|
||||
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static <T extends Enum<?>> EnumArgumentType<T> enumArgument(T defaultValue) {
|
||||
return new EnumArgumentType<T>(defaultValue);
|
||||
}
|
||||
|
||||
public static <T extends Enum<?>> T getEnum(CommandContext<?> context, String name, T defaultValue) {
|
||||
return (T) context.getArgument(name, defaultValue.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public T parse(StringReader reader) throws CommandSyntaxException {
|
||||
String argument = reader.readString();
|
||||
for (T t : values) {
|
||||
if (t.toString().equals(argument)) return t;
|
||||
}
|
||||
throw NO_SUCH_TYPE.create(argument);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
|
||||
return CommandSource.suggestMatching(Arrays.stream(values).map(T::toString), builder);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package anticope.rejects.arguments;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
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.exceptions.DynamicCommandExceptionType;
|
||||
import com.mojang.brigadier.suggestion.Suggestions;
|
||||
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
||||
|
||||
import net.minecraft.command.CommandSource;
|
||||
import net.minecraft.text.LiteralText;
|
||||
|
||||
public class EnumStringArgumentType implements ArgumentType<String> {
|
||||
|
||||
private Collection<String> EXAMPLES;
|
||||
private static final DynamicCommandExceptionType INVALID_OPTION = new DynamicCommandExceptionType(name ->
|
||||
new LiteralText(name+" is not a valid option."));
|
||||
public EnumStringArgumentType(Collection<String> examples) {
|
||||
this.EXAMPLES = examples;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parse(StringReader reader) throws CommandSyntaxException {
|
||||
String arg = reader.readUnquotedString();
|
||||
if (!EXAMPLES.contains(arg)) throw INVALID_OPTION.create(arg);
|
||||
return arg;
|
||||
}
|
||||
|
||||
public static String getString(final CommandContext<?> context, final String name) {
|
||||
return context.getArgument(name, String.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> getExamples() {
|
||||
return EXAMPLES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "string()";
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
|
||||
return CommandSource.suggestMatching(EXAMPLES, builder);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user