diff --git a/api/build.gradle b/api/build.gradle index 100bca6b1..6b8bf79c9 100644 --- a/api/build.gradle +++ b/api/build.gradle @@ -91,6 +91,10 @@ javadoc { options.source = '8' } +test { + useJUnitPlatform() +} + publishing { publications { mavenJava(MavenPublication) { diff --git a/native/build.gradle b/native/build.gradle index 0a348523f..b5b260486 100644 --- a/native/build.gradle +++ b/native/build.gradle @@ -21,6 +21,10 @@ dependencies { testImplementation "org.junit.jupiter:junit-jupiter-engine:${junitVersion}" } +test { + useJUnitPlatform() +} + publishing { publications { mavenJava(MavenPublication) { diff --git a/proxy/build.gradle b/proxy/build.gradle index ff2e1fe0a..e7dbaf011 100644 --- a/proxy/build.gradle +++ b/proxy/build.gradle @@ -13,6 +13,10 @@ java { targetCompatibility = JavaVersion.VERSION_1_8 } +test { + useJUnitPlatform() +} + jar { manifest { def buildNumber = System.getenv("BUILD_NUMBER") ?: "unknown" diff --git a/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java b/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java index 87a554a9b..a21b93bfa 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java @@ -76,11 +76,8 @@ public class VelocityCommandManager implements CommandManager { } if (!(command instanceof BrigadierCommand)) { - if (!meta.getHints().isEmpty()) { - // If the user specified a hint, then add the hints to the command node directly. - for (CommandNode hint : meta.getHints()) { - node.addChild(hint); - } + for (CommandNode hint : meta.getHints()) { + node.addChild(BrigadierUtils.wrapForHinting(hint, node.getCommand())); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/util/BrigadierUtils.java b/proxy/src/main/java/com/velocitypowered/proxy/util/BrigadierUtils.java index 1bad01978..73bf67f84 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/util/BrigadierUtils.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/util/BrigadierUtils.java @@ -1,8 +1,10 @@ package com.velocitypowered.proxy.util; +import com.google.common.base.Preconditions; import com.google.common.base.Splitter; import com.mojang.brigadier.Command; import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.ArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.context.CommandContext; @@ -11,6 +13,7 @@ import com.mojang.brigadier.tree.CommandNode; import com.mojang.brigadier.tree.LiteralCommandNode; import com.velocitypowered.api.command.CommandSource; import java.util.Locale; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Provides utilities for working with Brigadier commands. @@ -124,6 +127,25 @@ public final class BrigadierUtils { return command.toLowerCase(Locale.ENGLISH); } + /** + * Prepares the given command node prior for hinting metadata to + * a {@link com.velocitypowered.api.command.Command}. + * + * @param node the command node to be wrapped + * @param command the command to execute + * @return the wrapped command node + */ + public static CommandNode wrapForHinting( + final CommandNode node, final @Nullable Command command) { + Preconditions.checkNotNull(node, "node"); + ArgumentBuilder builder = node.createBuilder(); + builder.executes(command); + for (CommandNode child : node.getChildren()) { + builder.then(wrapForHinting(child, command)); + } + return builder.build(); + } + private BrigadierUtils() { throw new AssertionError(); } diff --git a/proxy/src/test/java/com/velocitypowered/proxy/command/CommandManagerTests.java b/proxy/src/test/java/com/velocitypowered/proxy/command/CommandManagerTests.java index 34d3f8336..729bb17ca 100644 --- a/proxy/src/test/java/com/velocitypowered/proxy/command/CommandManagerTests.java +++ b/proxy/src/test/java/com/velocitypowered/proxy/command/CommandManagerTests.java @@ -109,18 +109,6 @@ public class CommandManagerTests { assertTrue(manager.hasCommand("bar")); } - @Test - void testAlreadyRegisteredThrows() { - VelocityCommandManager manager = createManager(); - manager.register("BAR", new NoopSimpleCommand()); - assertThrows(IllegalArgumentException.class, () -> { - CommandMeta meta = manager.metaBuilder("baz") - .aliases("BAr") - .build(); - manager.register(meta, new NoopRawCommand()); - }); - } - @Test void testBrigadierExecute() { VelocityCommandManager manager = createManager(); @@ -166,9 +154,9 @@ public class CommandManagerTests { assertTrue(manager.executeImmediately(MockCommandSource.INSTANCE, "buy 14").join()); assertTrue(checkedRequires.compareAndSet(true, false)); assertTrue(executed.get()); - assertFalse(manager.execute(MockCommandSource.INSTANCE, "buy 9").join(), + assertTrue(manager.execute(MockCommandSource.INSTANCE, "buy 9").join(), "Invalid arg returns false"); - assertFalse(manager.executeImmediately(MockCommandSource.INSTANCE, "buy 12 bananas") + assertTrue(manager.executeImmediately(MockCommandSource.INSTANCE, "buy 12 bananas") .join()); assertTrue(checkedRequires.get()); } @@ -336,7 +324,7 @@ public class CommandManagerTests { .join().isEmpty()); assertEquals( ImmutableList.of("123"), - manager.offerSuggestions(MockCommandSource.INSTANCE, "simPle foo ").join()); + manager.offerSuggestions(MockCommandSource.INSTANCE, "simPle foo").join()); assertEquals( ImmutableList.of("baz", "foo"), manager.offerSuggestions(MockCommandSource.INSTANCE, "raw ").join()); diff --git a/proxy/src/test/java/com/velocitypowered/proxy/network/PacketRegistryTest.java b/proxy/src/test/java/com/velocitypowered/proxy/network/PacketRegistryTest.java deleted file mode 100644 index 862304c80..000000000 --- a/proxy/src/test/java/com/velocitypowered/proxy/network/PacketRegistryTest.java +++ /dev/null @@ -1,145 +0,0 @@ -package com.velocitypowered.proxy.network; - -import static com.google.common.collect.Iterables.getLast; -import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_11; -import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_12; -import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_12_1; -import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_12_2; -import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_13; -import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_14_2; -import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_8; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import com.velocitypowered.api.network.ProtocolVersion; -import com.velocitypowered.proxy.network.packet.Packet; -import com.velocitypowered.proxy.network.packet.PacketDirection; -import com.velocitypowered.proxy.network.packet.serverbound.ServerboundHandshakePacket; -import com.velocitypowered.proxy.network.packet.serverbound.ServerboundStatusPingPacket; - -import io.netty.buffer.Unpooled; -import org.junit.jupiter.api.Test; - -class PacketRegistryTest { - - private StateRegistry.PacketRegistry setupRegistry() { - StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( - PacketDirection.CLIENTBOUND); - registry.register(ServerboundHandshakePacket.class, ServerboundHandshakePacket.DECODER, - new StateRegistry.PacketMapping(0x01, MINECRAFT_1_8, false), - new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12, false)); - return registry; - } - - @Test - void packetRegistryWorks() { - StateRegistry.PacketRegistry registry = setupRegistry(); - Packet packet = registry.getProtocolRegistry(MINECRAFT_1_12).readPacket(0, Unpooled.EMPTY_BUFFER, PacketDirection.SERVERBOUND, MINECRAFT_1_12); - assertNotNull(packet, "Packet was not found in registry"); - assertEquals(ServerboundHandshakePacket.class, - packet.getClass(), "Registry returned wrong class"); - - assertEquals(0, registry.getProtocolRegistry(MINECRAFT_1_12).getPacketId(packet), - "Registry did not return the correct packet ID"); - } - - @Test - void packetRegistryLinkingWorks() { - StateRegistry.PacketRegistry registry = setupRegistry(); - Packet packet = registry.getProtocolRegistry(MINECRAFT_1_12_1).readPacket(0, Unpooled.EMPTY_BUFFER, PacketDirection.SERVERBOUND, MINECRAFT_1_12); - assertNotNull(packet, "Packet was not found in registry"); - assertEquals(ServerboundHandshakePacket.class, - packet.getClass(), "Registry returned wrong class"); - assertEquals(0, registry.getProtocolRegistry(MINECRAFT_1_12_1).getPacketId(packet), - "Registry did not return the correct packet ID"); - assertEquals(0, registry.getProtocolRegistry(MINECRAFT_1_14_2).getPacketId(packet), - "Registry did not return the correct packet ID"); - assertEquals(1, registry.getProtocolRegistry(MINECRAFT_1_11).getPacketId(packet), - "Registry did not return the correct packet ID"); - assertNull(registry.getProtocolRegistry(MINECRAFT_1_14_2).readPacket(0x01, Unpooled.EMPTY_BUFFER, PacketDirection.SERVERBOUND, MINECRAFT_1_12), - "Registry should return a null"); - } - - @Test - void failOnNoMappings() { - StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( - PacketDirection.CLIENTBOUND); - assertThrows(IllegalArgumentException.class, - () -> registry.register(ServerboundHandshakePacket.class, - ServerboundHandshakePacket.DECODER)); - assertThrows(IllegalArgumentException.class, - () -> registry.getProtocolRegistry(ProtocolVersion.UNKNOWN) - .getPacketId(new ServerboundHandshakePacket())); - } - - @Test - void failOnWrongOrder() { - StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( - PacketDirection.CLIENTBOUND); - assertThrows(IllegalArgumentException.class, - () -> registry.register(ServerboundHandshakePacket.class, - ServerboundHandshakePacket.DECODER, - new StateRegistry.PacketMapping(0x01, MINECRAFT_1_13, false), - new StateRegistry.PacketMapping(0x00, MINECRAFT_1_8, false))); - assertThrows(IllegalArgumentException.class, - () -> registry.register(ServerboundHandshakePacket.class, - ServerboundHandshakePacket.DECODER, - new StateRegistry.PacketMapping(0x01, MINECRAFT_1_13, false), - new StateRegistry.PacketMapping(0x01, MINECRAFT_1_13, false))); - } - - @Test - void failOnDuplicate() { - StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( - PacketDirection.CLIENTBOUND); - registry.register(ServerboundHandshakePacket.class, - ServerboundHandshakePacket.DECODER, - new StateRegistry.PacketMapping(0x00, MINECRAFT_1_8, false)); - assertThrows(IllegalArgumentException.class, - () -> registry.register(ServerboundHandshakePacket.class, - ServerboundHandshakePacket.DECODER, - new StateRegistry.PacketMapping(0x01, MINECRAFT_1_12, false))); - assertThrows(IllegalArgumentException.class, - () -> registry.register(ServerboundStatusPingPacket.class, ServerboundStatusPingPacket.DECODER, - new StateRegistry.PacketMapping(0x00, MINECRAFT_1_13, false))); - } - - @Test - void shouldNotFailWhenRegisterLatestProtocolVersion() { - StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( - PacketDirection.CLIENTBOUND); - assertDoesNotThrow(() -> registry.register(ServerboundHandshakePacket.class, - ServerboundHandshakePacket.DECODER, - new StateRegistry.PacketMapping(0x00, MINECRAFT_1_8, false), - new StateRegistry.PacketMapping(0x01, getLast(ProtocolVersion.SUPPORTED_VERSIONS), - false))); - } - - @Test - void registrySuppliesCorrectPacketsByProtocol() { - StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry( - PacketDirection.CLIENTBOUND); - registry.register(ServerboundHandshakePacket.class, ServerboundHandshakePacket.DECODER, - new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12, false), - new StateRegistry.PacketMapping(0x01, MINECRAFT_1_12_1, false), - new StateRegistry.PacketMapping(0x02, MINECRAFT_1_13, false)); - assertEquals(ServerboundHandshakePacket.class, - registry.getProtocolRegistry(MINECRAFT_1_12).readPacket(0x00, Unpooled.EMPTY_BUFFER, - PacketDirection.SERVERBOUND, MINECRAFT_1_12).getClass()); - assertEquals(ServerboundHandshakePacket.class, - registry.getProtocolRegistry(MINECRAFT_1_12_1).readPacket(0x01, Unpooled.EMPTY_BUFFER, - PacketDirection.SERVERBOUND, MINECRAFT_1_12).getClass()); - assertEquals(ServerboundHandshakePacket.class, - registry.getProtocolRegistry(MINECRAFT_1_12_2).readPacket(0x01, Unpooled.EMPTY_BUFFER, - PacketDirection.SERVERBOUND, MINECRAFT_1_12).getClass()); - assertEquals(ServerboundHandshakePacket.class, - registry.getProtocolRegistry(MINECRAFT_1_13).readPacket(0x02, Unpooled.EMPTY_BUFFER, - PacketDirection.SERVERBOUND, MINECRAFT_1_12).getClass()); - assertEquals(ServerboundHandshakePacket.class, - registry.getProtocolRegistry(MINECRAFT_1_14_2).readPacket(0x02, Unpooled.EMPTY_BUFFER, - PacketDirection.SERVERBOUND, MINECRAFT_1_12).getClass()); - } -} \ No newline at end of file