From 5e708e9136741cacc7bb339be2c814164895a2d7 Mon Sep 17 00:00:00 2001 From: kashike Date: Wed, 25 Jul 2018 22:58:56 -0700 Subject: [PATCH] various things --- build.gradle | 11 +++-- settings.gradle | 1 - .../com/velocitypowered/proxy/Velocity.java | 5 +-- .../proxy/protocol/StateRegistry.java | 4 +- .../netty/MinecraftPipelineUtils.java | 40 ++++++++++++------- .../velocitypowered/proxy/util/UuidUtils.java | 16 ++++---- .../proxy/util/EncryptionUtilsTest.java | 15 +++---- .../proxy/util/UuidUtilsTest.java | 21 +++++----- 8 files changed, 61 insertions(+), 52 deletions(-) diff --git a/build.gradle b/build.gradle index 24d9c5827..e02567778 100644 --- a/build.gradle +++ b/build.gradle @@ -7,17 +7,20 @@ group 'com.velocitypowered' version '1.0-SNAPSHOT' sourceCompatibility = 1.8 +targetCompatibility = 1.8 repositories { + mavenLocal() mavenCentral() } dependencies { - compile 'io.netty:netty-handler:4.1.27.Final' + compile 'com.google.code.gson:gson:2.8.5' + compile 'com.google.guava:guava:25.1-jre' compile 'io.netty:netty-codec:4.1.27.Final' compile 'io.netty:netty-codec-http:4.1.27.Final' - compile 'com.google.guava:guava:25.1-jre' - compile 'com.google.code.gson:gson:2.8.5' + compile 'io.netty:netty-handler:4.1.27.Final' compile 'net.kyori:text:1.12-1.5.0' - testCompile group: 'junit', name: 'junit', version: '4.12' + testCompile 'org.junit.jupiter:junit-jupiter-api:5.3.0-M1' + testCompile 'org.junit.jupiter:junit-jupiter-engine:5.3.0-M1' } diff --git a/settings.gradle b/settings.gradle index 716a2c248..b3d35706e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1 @@ rootProject.name = 'velocity' - diff --git a/src/main/java/com/velocitypowered/proxy/Velocity.java b/src/main/java/com/velocitypowered/proxy/Velocity.java index 8f7396d26..908995bbc 100644 --- a/src/main/java/com/velocitypowered/proxy/Velocity.java +++ b/src/main/java/com/velocitypowered/proxy/Velocity.java @@ -5,9 +5,6 @@ public class Velocity { VelocityServer server = new VelocityServer(); server.initialize(); - while (true) { - // temporary until jline is added. - Thread.sleep(999999); - } + Thread.currentThread().join(); } } diff --git a/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java b/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java index ff2d598cf..f25e578fb 100644 --- a/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java +++ b/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java @@ -1,6 +1,8 @@ package com.velocitypowered.proxy.protocol; import com.velocitypowered.proxy.protocol.packets.*; +import io.netty.util.collection.IntObjectHashMap; +import io.netty.util.collection.IntObjectMap; import java.util.HashMap; import java.util.Map; @@ -49,7 +51,7 @@ public enum StateRegistry { public static class ProtocolMappings { private final ProtocolConstants.Direction direction; private final StateRegistry state; - private final Map> idsToSuppliers = new HashMap<>(); + private final IntObjectMap> idsToSuppliers = new IntObjectHashMap<>(); private final Map, Integer> packetClassesToIds = new HashMap<>(); public ProtocolMappings(ProtocolConstants.Direction direction, StateRegistry state) { diff --git a/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftPipelineUtils.java b/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftPipelineUtils.java index a1e0a0bcd..73c14e990 100644 --- a/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftPipelineUtils.java +++ b/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftPipelineUtils.java @@ -6,22 +6,32 @@ import io.netty.handler.timeout.ReadTimeoutHandler; import java.util.concurrent.TimeUnit; -public class MinecraftPipelineUtils { - public static void strapPipelineForProxy(Channel ch) { - ch.pipeline().addLast("read-timeout", new ReadTimeoutHandler(30, TimeUnit.SECONDS)); - ch.pipeline().addLast("legacy-ping-decode", new LegacyPingDecoder()); - ch.pipeline().addLast("frame-decoder", new MinecraftVarintFrameDecoder()); - ch.pipeline().addLast("legacy-ping-encode", LegacyPingEncoder.INSTANCE); - ch.pipeline().addLast("frame-encoder", MinecraftVarintLengthEncoder.INSTANCE); - ch.pipeline().addLast("minecraft-decoder", new MinecraftDecoder(ProtocolConstants.Direction.TO_SERVER)); - ch.pipeline().addLast("minecraft-encoder", new MinecraftEncoder(ProtocolConstants.Direction.TO_CLIENT)); +public interface MinecraftPipelineUtils { + String FRAME_DECODER = "frame-decoder"; + String FRAME_ENCODER = "frame-encoder"; + String LEGACY_PING_DECODER = "legacy-ping-decoder"; + String LEGACY_PING_ENCODER = "legacy-ping-encoder"; + String MINECRAFT_DECODER = "minecraft-decoder"; + String MINECRAFT_ENCODER = "minecraft-encoder"; + String READ_TIMEOUT = "read-timeout"; + + static void strapPipelineForProxy(Channel ch) { + ch.pipeline() + .addLast(READ_TIMEOUT, new ReadTimeoutHandler(30, TimeUnit.SECONDS)) + .addLast(LEGACY_PING_DECODER, new LegacyPingDecoder()) + .addLast(FRAME_DECODER, new MinecraftVarintFrameDecoder()) + .addLast(LEGACY_PING_ENCODER, LegacyPingEncoder.INSTANCE) + .addLast(FRAME_ENCODER, MinecraftVarintLengthEncoder.INSTANCE) + .addLast(MINECRAFT_DECODER, new MinecraftDecoder(ProtocolConstants.Direction.TO_SERVER)) + .addLast(MINECRAFT_ENCODER, new MinecraftEncoder(ProtocolConstants.Direction.TO_CLIENT)); } - public static void strapPipelineForBackend(Channel ch) { - ch.pipeline().addLast("read-timeout", new ReadTimeoutHandler(30, TimeUnit.SECONDS)); - ch.pipeline().addLast("frame-decoder", new MinecraftVarintFrameDecoder()); - ch.pipeline().addLast("frame-encoder", MinecraftVarintLengthEncoder.INSTANCE); - ch.pipeline().addLast("minecraft-decoder", new MinecraftDecoder(ProtocolConstants.Direction.TO_CLIENT)); - ch.pipeline().addLast("minecraft-encoder", new MinecraftEncoder(ProtocolConstants.Direction.TO_SERVER)); + static void strapPipelineForBackend(Channel ch) { + ch.pipeline() + .addLast(READ_TIMEOUT, new ReadTimeoutHandler(30, TimeUnit.SECONDS)) + .addLast(FRAME_DECODER, new MinecraftVarintFrameDecoder()) + .addLast(FRAME_ENCODER, MinecraftVarintLengthEncoder.INSTANCE) + .addLast(MINECRAFT_DECODER, new MinecraftDecoder(ProtocolConstants.Direction.TO_CLIENT)) + .addLast(MINECRAFT_ENCODER, new MinecraftEncoder(ProtocolConstants.Direction.TO_SERVER)); } } diff --git a/src/main/java/com/velocitypowered/proxy/util/UuidUtils.java b/src/main/java/com/velocitypowered/proxy/util/UuidUtils.java index 23553631e..963a57f21 100644 --- a/src/main/java/com/velocitypowered/proxy/util/UuidUtils.java +++ b/src/main/java/com/velocitypowered/proxy/util/UuidUtils.java @@ -3,20 +3,18 @@ package com.velocitypowered.proxy.util; import com.google.common.base.Preconditions; import java.nio.charset.StandardCharsets; +import java.util.Objects; import java.util.UUID; public enum UuidUtils { ; - public static UUID fromMojang(String id) { - Preconditions.checkNotNull(id, "id"); - Preconditions.checkArgument(id.length() == 32, "Length is incorrect"); - return UUID.fromString( - id.substring(0, 8) + "-" + - id.substring(8, 12) + "-" + - id.substring(12, 16) + "-" + - id.substring(16, 20) + "-" + - id.substring(20, 32) + public static UUID fromUndashed(final String string) { + Objects.requireNonNull(string, "string"); + Preconditions.checkArgument(string.length() == 32, "Length is incorrect"); + return new UUID( + Long.parseUnsignedLong(string.substring(0, 16), 16), + Long.parseUnsignedLong(string.substring(16), 16) ); } diff --git a/src/test/java/com/velocitypowered/proxy/util/EncryptionUtilsTest.java b/src/test/java/com/velocitypowered/proxy/util/EncryptionUtilsTest.java index 203f6606e..7f2e93933 100644 --- a/src/test/java/com/velocitypowered/proxy/util/EncryptionUtilsTest.java +++ b/src/test/java/com/velocitypowered/proxy/util/EncryptionUtilsTest.java @@ -1,19 +1,20 @@ package com.velocitypowered.proxy.util; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; -public class EncryptionUtilsTest { +import static org.junit.jupiter.api.Assertions.assertEquals; + +class EncryptionUtilsTest { @Test - public void twoComplementsSha1Digest() throws Exception { + void twoComplementsSha1Digest() throws Exception { String notchHash = hexDigest("Notch"); - Assert.assertEquals("4ed1f46bbe04bc756bcb17c0c7ce3e4632f06a48", notchHash); + assertEquals("4ed1f46bbe04bc756bcb17c0c7ce3e4632f06a48", notchHash); String jebHash = hexDigest("jeb_"); - Assert.assertEquals("-7c9d5b0044c130109a5d7b5fb5c317c02b4e28c1", jebHash); + assertEquals("-7c9d5b0044c130109a5d7b5fb5c317c02b4e28c1", jebHash); } private String hexDigest(String str) throws Exception { @@ -22,4 +23,4 @@ public class EncryptionUtilsTest { byte[] digested = digest.digest(); return EncryptionUtils.twoComplementsSha1Digest(digested); } -} \ No newline at end of file +} diff --git a/src/test/java/com/velocitypowered/proxy/util/UuidUtilsTest.java b/src/test/java/com/velocitypowered/proxy/util/UuidUtilsTest.java index cc62f37e4..2afed9251 100644 --- a/src/test/java/com/velocitypowered/proxy/util/UuidUtilsTest.java +++ b/src/test/java/com/velocitypowered/proxy/util/UuidUtilsTest.java @@ -1,26 +1,25 @@ package com.velocitypowered.proxy.util; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.UUID; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; -public class UuidUtilsTest { - private static final UUID VALID_UUID = UUID.fromString("6b501978-d3be-4f33-bcf6-6e7808f37a0d"); - private static final String VALID_MOJANG_UUID = "6b501978d3be4f33bcf66e7808f37a0d"; +class UuidUtilsTest { + private static final UUID EXPECTED_DASHED_UUID = UUID.fromString("6b501978-d3be-4f33-bcf6-6e7808f37a0d"); + private static final String ACTUAL_UNDASHED_UUID = EXPECTED_DASHED_UUID.toString().replace("-", ""); private static final UUID TEST_OFFLINE_PLAYER_UUID = UUID.fromString("708f6260-183d-3912-bbde-5e279a5e739a"); private static final String TEST_OFFLINE_PLAYER = "tuxed"; @Test - public void fromMojang() { - Assert.assertEquals("UUIDs do not match", VALID_UUID, UuidUtils.fromMojang(VALID_MOJANG_UUID)); + void testFromUndashed() { + assertEquals(EXPECTED_DASHED_UUID, UuidUtils.fromUndashed(ACTUAL_UNDASHED_UUID), "UUIDs do not match"); } @Test - public void generateOfflinePlayerUuid() { - Assert.assertEquals("UUIDs do not match", TEST_OFFLINE_PLAYER_UUID, UuidUtils.generateOfflinePlayerUuid(TEST_OFFLINE_PLAYER)); + void generateOfflinePlayerUuid() { + assertEquals(TEST_OFFLINE_PLAYER_UUID, UuidUtils.generateOfflinePlayerUuid(TEST_OFFLINE_PLAYER), "UUIDs do not match"); } -} \ No newline at end of file +}