From 1441dec3675ee2498285d70fbf28d16060f50ac0 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Thu, 23 Apr 2020 16:19:49 -0400 Subject: [PATCH] Implement brigadier:long argument type, fixes #295 --- api/build.gradle | 1 + proxy/build.gradle | 2 +- .../brigadier/ArgumentPropertyRegistry.java | 3 ++ .../packet/brigadier/DummyProperty.java | 10 ++--- .../LongArgumentPropertySerializer.java | 40 +++++++++++++++++++ 5 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/LongArgumentPropertySerializer.java diff --git a/api/build.gradle b/api/build.gradle index edf3dc198..b156d61fc 100644 --- a/api/build.gradle +++ b/api/build.gradle @@ -63,6 +63,7 @@ artifacts { javadoc { options.encoding = 'UTF-8' options.charSet = 'UTF-8' + options.source = '8' options.links( 'http://www.slf4j.org/apidocs/', 'https://google.github.io/guava/releases/25.1-jre/api/docs/', diff --git a/proxy/build.gradle b/proxy/build.gradle index 216e58cc8..5498db10b 100644 --- a/proxy/build.gradle +++ b/proxy/build.gradle @@ -65,7 +65,7 @@ dependencies { compile 'it.unimi.dsi:fastutil:8.2.3' compile 'net.kyori:event-method-asm:3.0.0' - compile 'com.mojang:brigadier:1.0.15' + compile 'com.mojang:brigadier:1.0.17' compile 'org.asynchttpclient:async-http-client:2.10.4' diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ArgumentPropertyRegistry.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ArgumentPropertyRegistry.java index 9560a8b29..e7847ec8e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ArgumentPropertyRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ArgumentPropertyRegistry.java @@ -4,6 +4,7 @@ import static com.velocitypowered.proxy.protocol.packet.brigadier.DoubleArgument import static com.velocitypowered.proxy.protocol.packet.brigadier.DummyVoidArgumentPropertySerializer.DUMMY; import static com.velocitypowered.proxy.protocol.packet.brigadier.FloatArgumentPropertySerializer.FLOAT; import static com.velocitypowered.proxy.protocol.packet.brigadier.IntegerArgumentPropertySerializer.INTEGER; +import static com.velocitypowered.proxy.protocol.packet.brigadier.LongArgumentPropertySerializer.LONG; import static com.velocitypowered.proxy.protocol.packet.brigadier.StringArgumentPropertySerializer.STRING; import com.mojang.brigadier.arguments.ArgumentType; @@ -11,6 +12,7 @@ import com.mojang.brigadier.arguments.BoolArgumentType; import com.mojang.brigadier.arguments.DoubleArgumentType; import com.mojang.brigadier.arguments.FloatArgumentType; import com.mojang.brigadier.arguments.IntegerArgumentType; +import com.mojang.brigadier.arguments.LongArgumentType; import com.mojang.brigadier.arguments.StringArgumentType; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; @@ -90,6 +92,7 @@ public class ArgumentPropertyRegistry { register("brigadier:double", DoubleArgumentType.class, DOUBLE); register("brigadier:bool", BoolArgumentType.class, VoidArgumentPropertySerializer.create(BoolArgumentType::bool)); + register("brigadier:long", LongArgumentType.class, LONG); // Minecraft argument types with extra properties dummy("minecraft:entity", ByteArgumentPropertySerializer.BYTE); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/DummyProperty.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/DummyProperty.java index 762b52686..9b514f903 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/DummyProperty.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/DummyProperty.java @@ -17,11 +17,6 @@ class DummyProperty implements ArgumentType { this.result = result; } - @Override - public T parse(StringReader reader) throws CommandSyntaxException { - throw new UnsupportedOperationException(); - } - public String getIdentifier() { return identifier; } @@ -33,4 +28,9 @@ class DummyProperty implements ArgumentType { public @Nullable T getResult() { return result; } + + @Override + public T parse(StringReader reader) { + throw new UnsupportedOperationException(); + } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/LongArgumentPropertySerializer.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/LongArgumentPropertySerializer.java new file mode 100644 index 000000000..9d3b40b6d --- /dev/null +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/LongArgumentPropertySerializer.java @@ -0,0 +1,40 @@ +package com.velocitypowered.proxy.protocol.packet.brigadier; + +import static com.velocitypowered.proxy.protocol.packet.brigadier.IntegerArgumentPropertySerializer.HAS_MAXIMUM; +import static com.velocitypowered.proxy.protocol.packet.brigadier.IntegerArgumentPropertySerializer.HAS_MINIMUM; +import static com.velocitypowered.proxy.protocol.packet.brigadier.IntegerArgumentPropertySerializer.getFlags; + +import com.mojang.brigadier.arguments.LongArgumentType; +import io.netty.buffer.ByteBuf; + +class LongArgumentPropertySerializer implements ArgumentPropertySerializer { + + static final LongArgumentPropertySerializer LONG = new LongArgumentPropertySerializer(); + + private LongArgumentPropertySerializer() { + + } + + @Override + public LongArgumentType deserialize(ByteBuf buf) { + byte flags = buf.readByte(); + long minimum = (flags & HAS_MINIMUM) != 0 ? buf.readLong() : Long.MIN_VALUE; + long maximum = (flags & HAS_MAXIMUM) != 0 ? buf.readLong() : Long.MAX_VALUE; + return LongArgumentType.longArg(minimum, maximum); + } + + @Override + public void serialize(LongArgumentType object, ByteBuf buf) { + boolean hasMinimum = object.getMinimum() != Long.MIN_VALUE; + boolean hasMaximum = object.getMaximum() != Long.MAX_VALUE; + byte flag = getFlags(hasMinimum, hasMaximum); + + buf.writeByte(flag); + if (hasMinimum) { + buf.writeLong(object.getMinimum()); + } + if (hasMaximum) { + buf.writeLong(object.getMaximum()); + } + } +}