Skip to content

Commit

Permalink
Fix Brigadier exposure when using IntegerArgument
Browse files Browse the repository at this point in the history
  • Loading branch information
willkroboth committed Aug 12, 2023
1 parent 04c5c05 commit a841cd3
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,7 @@ <T, EI> ArgumentType<T> wrapArgumentType(Argument argument, ArgumentType<T> rawT

if (!(argument instanceof InitialParseExceptionArgument)) return rawType;

InitialParseExceptionArgument<T, ArgumentType<T>, EI, ?> iPEA =
(InitialParseExceptionArgument<T, ArgumentType<T>, EI, ?>) argument.instance();
InitialParseExceptionArgument<T, EI, ?> iPEA = (InitialParseExceptionArgument<T, EI, ?>) argument.instance();

Optional<InitialParseExceptionHandler<T, EI>> handler = iPEA.getInitialParseExceptionHandler();
if (handler.isEmpty()) return rawType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
* {@link ExceptionInformation} object.
*
* @param <T> The object returned when the wrapped {@link ArgumentType} is parsed.
* @param <BaseType> The class of the {@link ArgumentType} this object is wrapping.
* @param <ExceptionInformation> The class that holds information about the exception.
*/
public record ExceptionHandlingArgumentType<T, BaseType extends ArgumentType<T>, ExceptionInformation>(
public record ExceptionHandlingArgumentType<T, ExceptionInformation>(
/**
* @param baseType The {@link ArgumentType} this object is wrapping
*/
BaseType baseType,
ArgumentType<T> baseType,
/**
* @param exceptionHandler The {@link InitialParseExceptionHandler} that handles intercepted {@link CommandSyntaxException}
*/
Expand All @@ -38,7 +37,7 @@ public record ExceptionHandlingArgumentType<T, BaseType extends ArgumentType<T>,
* @param exceptionParser A function that parses the information in a {@link CommandSyntaxException} to create an
* {@link ExceptionInformation} object.
*/
InitialParseExceptionParser<T, BaseType, ExceptionInformation> exceptionParser
InitialParseExceptionParser<T, ExceptionInformation> exceptionParser
) implements ArgumentType<T> {

@Override
Expand All @@ -49,7 +48,7 @@ public T parse(StringReader stringReader) throws CommandSyntaxException {
try {
return exceptionHandler.handleException(new InitialParseExceptionContext<>(
new WrapperCommandSyntaxException(original),
exceptionParser.parse(original, stringReader, baseType),
exceptionParser.parse(original, stringReader),
new WrapperStringReader(stringReader)
));
} catch (WrapperCommandSyntaxException newException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
*
* @param <T> The class of the object returned when the {@link ArgumentType}
* used by this Argument parses its raw input.
* @param <BaseType> The implementation of {@link ArgumentType} being used for this Argument.
* @param <ExceptionInformation> The class that holds information about the exception.
* @param <Impl> The class extending this class, used as the return type in chained calls.
*/
public interface InitialParseExceptionArgument<T, BaseType extends ArgumentType<T>, ExceptionInformation, Impl extends AbstractArgument<?, Impl, ?, ?>> extends ChainableBuilder<Impl> {
public interface InitialParseExceptionArgument<T, ExceptionInformation, Impl extends AbstractArgument<?, Impl, ?, ?>> extends ChainableBuilder<Impl> {
/**
* A map that links Arguments to their ExceptionHandlers. This is basically
* equivalent to putting one instance variable in this interface, but Java
Expand All @@ -30,7 +29,7 @@ public interface InitialParseExceptionArgument<T, BaseType extends ArgumentType<
// TODO: Maybe this can be a WeakHashMap, so once the Argument objects aren't being used anywhere else we can forget about
// them and not store them anymore. I'm not entirely sure that is what WeakHashMap does though. Are Arguments ever GC'd
// anyway, or do they stick around somewhere?
Map<InitialParseExceptionArgument<?, ?, ?, ?>, InitialParseExceptionHandler<?, ?>> exceptionHandlers = new HashMap<>();
Map<InitialParseExceptionArgument<?, ?, ?>, InitialParseExceptionHandler<?, ?>> exceptionHandlers = new HashMap<>();

/**
* Sets the {@link InitialParseExceptionHandler} this Argument should
Expand Down Expand Up @@ -59,8 +58,7 @@ default Optional<InitialParseExceptionHandler<T, ExceptionInformation>> getIniti
*
* @param exception The {@link CommandSyntaxException} that was thrown.
* @param reader The {@link StringReader} that was reading this Argument.
* @param baseType The {@link ArgumentType} object was parsing.
* @return An {@link ExceptionInformation} object that holds information about the given exception.
*/
ExceptionInformation parseInitialParseException(CommandSyntaxException exception, StringReader reader, BaseType baseType);
ExceptionInformation parseInitialParseException(CommandSyntaxException exception, StringReader reader);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@

/**
* Extracts information about an exception that was thrown when an {@link ArgumentType} fails to parse.
* See {@link InitialParseExceptionArgument#parseInitialParseException(CommandSyntaxException, StringReader, ArgumentType)}.
* See {@link InitialParseExceptionArgument#parseInitialParseException(CommandSyntaxException, StringReader)}.
*
* @param <T> The object returned when the wrapped {@link ArgumentType} is parsed.
* @param <BaseType> The class of the {@link ArgumentType} this object is wrapping.
* @param <ExceptionInformation> The class that holds information about the exception.
*/
@FunctionalInterface
public interface InitialParseExceptionParser<T, BaseType extends ArgumentType<T>, ExceptionInformation> {
public interface InitialParseExceptionParser<T, ExceptionInformation> {
/**
* Extracts information about an exception that was thrown when an {@link ArgumentType} fails to parse.
* See {@link InitialParseExceptionArgument#parseInitialParseException(CommandSyntaxException, StringReader, ArgumentType)}.
* See {@link InitialParseExceptionArgument#parseInitialParseException(CommandSyntaxException, StringReader)}.
*
* @param exception The {@link CommandSyntaxException} that was thrown.
* @param reader The {@link StringReader} that was reading this Argument.
* @param baseType The {@link ArgumentType} object was parsing.
* @return An {@link ExceptionInformation} object that holds information about the given exception.
*/
ExceptionInformation parse(CommandSyntaxException exception, StringReader reader, BaseType baseType);
ExceptionInformation parse(CommandSyntaxException exception, StringReader reader);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
* @since 1.1
*/
public class IntegerArgument extends SafeOverrideableArgument<Integer, Integer>
implements InitialParseExceptionArgument<Integer, IntegerArgumentType,
IntegerArgument.InitialParseExceptionInformation, Argument<Integer>> {
implements InitialParseExceptionArgument<Integer, IntegerArgument.InitialParseExceptionInformation, Argument<Integer>> {
/**
* An integer argument
*
Expand Down Expand Up @@ -150,13 +149,12 @@ private static String getRawIntInput(StringReader reader) {
}

@Override
public InitialParseExceptionInformation parseInitialParseException(
CommandSyntaxException exception, StringReader reader, IntegerArgumentType baseType
) {
public InitialParseExceptionInformation parseInitialParseException(CommandSyntaxException exception, StringReader reader) {
String key = CommandAPIBukkit.get().extractTranslationKey(exception);
if (key == null) {
throw new IllegalStateException("Unexpected null translation key for IntegerArgument initial parse", exception);
}
IntegerArgumentType baseType = (IntegerArgumentType) this.getRawType();
int min = baseType.getMinimum();
int max = baseType.getMaximum();
return switch (key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
* Command Data: Node Format (pre 1.19)</a>.
*
* @param <T> The same type parameter T for {@link ExceptionHandlingArgumentType}. This fixes a silly generics issue.
* @param <BT> The same type parameter as BaseType in {@link ExceptionHandlingArgumentType}. This fixes a silly generics issue.
* @param <EI> The same type parameter as ExceptionInformation in {@link ExceptionHandlingArgumentType}. This fixes a silly generics issue.
* @param <WRITER> The netty {@link ByteBuf} subclass used to write packets in the version of Minecraft this is being used for.
*/
public abstract class ExceptionHandlingArgumentSerializer_Common<T, BT extends ArgumentType<T>, EI, WRITER extends ByteBuf> {
public abstract class ExceptionHandlingArgumentSerializer_Common<T, EI, WRITER extends ByteBuf> {
/**
* This method is expected to write the properties section for an argument node that uses the
* {@link ExceptionHandlingArgumentType}, so that the argument can be reconstructed when received by a client.
Expand All @@ -31,7 +30,7 @@ public abstract class ExceptionHandlingArgumentSerializer_Common<T, BT extends A
* @param argument The {@link ExceptionHandlingArgumentType} to serialize
* @param packetWriter The netty {@link ByteBuf} that contains the data
*/
protected void commonSerializeToNetwork(ExceptionHandlingArgumentType<T, BT, EI> argument, WRITER packetWriter) {
protected void commonSerializeToNetwork(ExceptionHandlingArgumentType<T, EI> argument, WRITER packetWriter) {
// REMOVE MY KEY FROM THE PACKET
Object myInfo = getArgumentTypeInformation(argument);

Expand Down Expand Up @@ -61,7 +60,7 @@ protected void commonSerializeToNetwork(ExceptionHandlingArgumentType<T, BT, EI>
* @param argument The {@link ExceptionHandlingArgumentType} to serialize
* @param properties The {@link JsonObject} to put the properties in
*/
protected void commonSerializeToJson(ExceptionHandlingArgumentType<T, BT, EI> argument, JsonObject properties) {
protected void commonSerializeToJson(ExceptionHandlingArgumentType<T, EI> argument, JsonObject properties) {
ArgumentType<T> baseType = argument.baseType();

Object baseInfo = getArgumentTypeInformation(baseType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import net.minecraft.server.v1_15_R1.MinecraftKey;
import net.minecraft.server.v1_15_R1.PacketDataSerializer;

public class ExceptionHandlingArgumentSerializer_1_15<T, BT extends ArgumentType<T>, EI>
extends ExceptionHandlingArgumentSerializer_Common<T, BT, EI, PacketDataSerializer>
implements ArgumentSerializer<ExceptionHandlingArgumentType<T, BT, EI>> {
public class ExceptionHandlingArgumentSerializer_1_15<T, EI>
extends ExceptionHandlingArgumentSerializer_Common<T, EI, PacketDataSerializer>
implements ArgumentSerializer<ExceptionHandlingArgumentType<T, EI>> {
// All the ? here should actually be ArgumentRegistry.a, but that is a private inner class. That makes everything really annoying.
// TODO: We want to check this reflection, but we can't give ArgumentRegistry.a to the @RequireField annotation
// Hopefully something works out, but the preprocessor needs to be expanded first
Expand Down Expand Up @@ -60,19 +60,19 @@ protected void serializeBaseTypeToJson(ArgumentType<T> baseType, Object baseInfo
// ArgumentSerializer methods
@Override
// serializeToNetwork
public void a(ExceptionHandlingArgumentType<T, BT, EI> argument, PacketDataSerializer packetDataSerializer) {
public void a(ExceptionHandlingArgumentType<T, EI> argument, PacketDataSerializer packetDataSerializer) {
commonSerializeToNetwork(argument, packetDataSerializer);
}

@Override
// serializeToJson
public void a(ExceptionHandlingArgumentType<T, BT, EI> argument, JsonObject properties) {
public void a(ExceptionHandlingArgumentType<T, EI> argument, JsonObject properties) {
commonSerializeToJson(argument, properties);
}

@Override
// deserializeFromNetwork
public ExceptionHandlingArgumentType<T, BT, EI> b(PacketDataSerializer packetDataSerializer) {
public ExceptionHandlingArgumentType<T, EI> b(PacketDataSerializer packetDataSerializer) {
// Since this class overrides its ArgumentRegistry key with the baseType's,
// this class's key should never show up in a packet and this method should never
// be called to deserialize the ArgumentType info that wasn't put into the packet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import net.minecraft.server.v1_16_R1.MinecraftKey;
import net.minecraft.server.v1_16_R1.PacketDataSerializer;

public class ExceptionHandlingArgumentSerializer_1_16_R1<T, BT extends ArgumentType<T>, EI>
extends ExceptionHandlingArgumentSerializer_Common<T, BT, EI, PacketDataSerializer>
implements ArgumentSerializer<ExceptionHandlingArgumentType<T, BT, EI>> {
public class ExceptionHandlingArgumentSerializer_1_16_R1<T, EI>
extends ExceptionHandlingArgumentSerializer_Common<T, EI, PacketDataSerializer>
implements ArgumentSerializer<ExceptionHandlingArgumentType<T, EI>> {
// All the ? here should actually be ArgumentRegistry.a, but that is a private inner class. That makes everything really annoying.
// TODO: We want to check this reflection, but we can't give ArgumentRegistry.a to the @RequireField annotation
// Hopefully something works out, but the preprocessor needs to be expanded first
Expand Down Expand Up @@ -60,19 +60,19 @@ protected void serializeBaseTypeToJson(ArgumentType<T> baseType, Object baseInfo
// ArgumentSerializer methods
@Override
// serializeToNetwork
public void a(ExceptionHandlingArgumentType<T, BT, EI> argument, PacketDataSerializer packetDataSerializer) {
public void a(ExceptionHandlingArgumentType<T, EI> argument, PacketDataSerializer packetDataSerializer) {
commonSerializeToNetwork(argument, packetDataSerializer);
}

@Override
// serializeToJson
public void a(ExceptionHandlingArgumentType<T, BT, EI> argument, JsonObject properties) {
public void a(ExceptionHandlingArgumentType<T, EI> argument, JsonObject properties) {
commonSerializeToJson(argument, properties);
}

@Override
// deserializeFromNetwork
public ExceptionHandlingArgumentType<T, BT, EI> b(PacketDataSerializer packetDataSerializer) {
public ExceptionHandlingArgumentType<T, EI> b(PacketDataSerializer packetDataSerializer) {
// Since this class overrides its ArgumentRegistry key with the baseType's,
// this class's key should never show up in a packet and this method should never
// be called to deserialize the ArgumentType info that wasn't put into the packet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import net.minecraft.server.v1_16_R2.PacketDataSerializer;

@Differs(from = {"1.15", "1.16.1"}, by = "Renamed ArgumentRegistry#a(ArgumentType) to ArgumentRegistry#b(ArgumentType)")
public class ExceptionHandlingArgumentSerializer_1_16_R2<T, BT extends ArgumentType<T>, EI>
extends ExceptionHandlingArgumentSerializer_Common<T, BT, EI, PacketDataSerializer>
implements ArgumentSerializer<ExceptionHandlingArgumentType<T, BT, EI>> {
public class ExceptionHandlingArgumentSerializer_1_16_R2<T, EI>
extends ExceptionHandlingArgumentSerializer_Common<T, EI, PacketDataSerializer>
implements ArgumentSerializer<ExceptionHandlingArgumentType<T, EI>> {
// All the ? here should actually be ArgumentRegistry.a, but that is a private inner class. That makes everything really annoying.
// TODO: We want to check this reflection, but we can't give ArgumentRegistry.a to the @RequireField annotation
// Hopefully something works out, but the preprocessor needs to be expanded first
Expand Down Expand Up @@ -62,19 +62,19 @@ protected void serializeBaseTypeToJson(ArgumentType<T> baseType, Object baseInfo
// ArgumentSerializer methods
@Override
// serializeToNetwork
public void a(ExceptionHandlingArgumentType<T, BT, EI> argument, PacketDataSerializer packetDataSerializer) {
public void a(ExceptionHandlingArgumentType<T, EI> argument, PacketDataSerializer packetDataSerializer) {
commonSerializeToNetwork(argument, packetDataSerializer);
}

@Override
// serializeToJson
public void a(ExceptionHandlingArgumentType<T, BT, EI> argument, JsonObject properties) {
public void a(ExceptionHandlingArgumentType<T, EI> argument, JsonObject properties) {
commonSerializeToJson(argument, properties);
}

@Override
// deserializeFromNetwork
public ExceptionHandlingArgumentType<T, BT, EI> b(PacketDataSerializer packetDataSerializer) {
public ExceptionHandlingArgumentType<T, EI> b(PacketDataSerializer packetDataSerializer) {
// Since this class overrides its ArgumentRegistry key with the baseType's,
// this class's key should never show up in a packet and this method should never
// be called to deserialize the ArgumentType info that wasn't put into the packet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import net.minecraft.server.v1_16_R3.MinecraftKey;
import net.minecraft.server.v1_16_R3.PacketDataSerializer;

public class ExceptionHandlingArgumentSerializer_1_16_4_R3<T, BT extends ArgumentType<T>, EI>
extends ExceptionHandlingArgumentSerializer_Common<T, BT, EI, PacketDataSerializer>
implements ArgumentSerializer<ExceptionHandlingArgumentType<T, BT, EI>> {
public class ExceptionHandlingArgumentSerializer_1_16_4_R3<T, EI>
extends ExceptionHandlingArgumentSerializer_Common<T, EI, PacketDataSerializer>
implements ArgumentSerializer<ExceptionHandlingArgumentType<T, EI>> {
// All the ? here should actually be ArgumentRegistry.a, but that is a private inner class. That makes everything really annoying.
// TODO: We want to check this reflection, but we can't give ArgumentRegistry.a to the @RequireField annotation
// Hopefully something works out, but the preprocessor needs to be expanded first
Expand Down Expand Up @@ -60,19 +60,19 @@ protected void serializeBaseTypeToJson(ArgumentType<T> baseType, Object baseInfo
// ArgumentSerializer methods
@Override
// serializeToNetwork
public void a(ExceptionHandlingArgumentType<T, BT, EI> argument, PacketDataSerializer packetDataSerializer) {
public void a(ExceptionHandlingArgumentType<T, EI> argument, PacketDataSerializer packetDataSerializer) {
commonSerializeToNetwork(argument, packetDataSerializer);
}

@Override
// serializeToJson
public void a(ExceptionHandlingArgumentType<T, BT, EI> argument, JsonObject properties) {
public void a(ExceptionHandlingArgumentType<T, EI> argument, JsonObject properties) {
commonSerializeToJson(argument, properties);
}

@Override
// deserializeFromNetwork
public ExceptionHandlingArgumentType<T, BT, EI> b(PacketDataSerializer packetDataSerializer) {
public ExceptionHandlingArgumentType<T, EI> b(PacketDataSerializer packetDataSerializer) {
// Since this class overrides its ArgumentRegistry key with the baseType's,
// this class's key should never show up in a packet and this method should never
// be called to deserialize the ArgumentType info that wasn't put into the packet
Expand Down
Loading

0 comments on commit a841cd3

Please sign in to comment.