From 565f169e9473a8db565fba443a0c2430dc09ef25 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Fri, 3 May 2019 23:22:44 -0400 Subject: [PATCH] Update to Minecraft 1.14 --- build.gradle | 6 +- .../com/comphenix/protocol/PacketLogging.java | 2 +- .../com/comphenix/protocol/PacketType.java | 247 ++++++++++-------- .../comphenix/protocol/ProtocolLibrary.java | 4 +- .../protocol/events/PacketMetadata.java | 9 +- .../injector/netty/ChannelInjector.java | 19 +- .../injector/netty/NettyProtocolRegistry.java | 6 +- .../comphenix/protocol/metrics/Metrics.java | 12 +- .../protocol/metrics/Statistics.java | 10 +- .../reflect/cloning/ImmutableDetector.java | 4 + .../comphenix/protocol/utility/Constants.java | 2 +- .../utility/MinecraftProtocolVersion.java | 2 + .../protocol/utility/MinecraftVersion.java | 5 + .../protocol/wrappers/EnumWrappers.java | 23 +- .../comphenix/protocol/wrappers/Vector3F.java | 5 +- .../wrappers/WrappedAttributeModifier.java | 88 ++++++- .../protocol/wrappers/WrappedIntHashMap.java | 2 + .../protocol/BukkitInitialization.java | 15 +- .../comphenix/protocol/PacketTypeTest.java | 122 ++++++++- .../protocol/events/PacketContainerTest.java | 25 +- .../reflect/cloning/AggregateClonerTest.java | 4 +- .../utility/MinecraftReflectionTest.java | 26 +- .../utility/StreamSerializerTest.java | 10 +- .../wrappers/ChunkCoordIntPairTest.java | 6 +- .../protocol/wrappers/EnumWrappersTest.java | 12 +- .../wrappers/WrappedAttributeTest.java | 9 +- .../wrappers/WrappedBlockDataTest.java | 10 +- .../wrappers/WrappedDataWatcherTest.java | 10 +- .../wrappers/WrappedIntHashMapTest.java | 4 +- .../wrappers/WrappedParticleTest.java | 4 + .../protocol/wrappers/nbt/NbtFactoryTest.java | 4 +- 31 files changed, 454 insertions(+), 253 deletions(-) diff --git a/build.gradle b/build.gradle index 8121763e..a0fcffed 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,4 @@ -import shadow.org.apache.tools.ant.filters.ReplaceTokens +import org.apache.tools.ant.filters.ReplaceTokens plugins { id 'com.github.johnrengelman.shadow' version '2.0.4' @@ -21,8 +21,8 @@ repositories { dependencies { compile group: 'com.github.jengelman.gradle.plugins', name: 'shadow', version: '2.0.4' compile group: 'io.netty', name: 'netty-all', version: '4.0.23.Final' - compile group: 'org.spigotmc', name: 'spigot-api', version: '1.13.1-R0.1-SNAPSHOT' - compile group: 'org.spigotmc', name: 'spigot', version: '1.13.1-R0.1-SNAPSHOT' + compile group: 'org.spigotmc', name: 'spigot-api', version: '1.14-R0.1-SNAPSHOT' + compile group: 'org.spigotmc', name: 'spigot', version: '1.14-R0.1-SNAPSHOT' compile group: 'cglib', name: 'cglib-nodep', version: '3.2.5' compile group: 'com.comphenix.executors', name: 'BukkitExecutors', version: '1.1-SNAPSHOT' testCompile group: 'junit', name: 'junit', version: '4.12' diff --git a/src/main/java/com/comphenix/protocol/PacketLogging.java b/src/main/java/com/comphenix/protocol/PacketLogging.java index bf279c9d..1958616f 100644 --- a/src/main/java/com/comphenix/protocol/PacketLogging.java +++ b/src/main/java/com/comphenix/protocol/PacketLogging.java @@ -33,11 +33,11 @@ import com.comphenix.protocol.events.PacketListener; import com.comphenix.protocol.injector.netty.WirePacket; import com.google.common.base.Charsets; -import org.apache.commons.io.HexDump; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.craftbukkit.libs.org.apache.commons.io.HexDump; import org.bukkit.plugin.Plugin; /** diff --git a/src/main/java/com/comphenix/protocol/PacketType.java b/src/main/java/com/comphenix/protocol/PacketType.java index 156166fb..363bf0c2 100644 --- a/src/main/java/com/comphenix/protocol/PacketType.java +++ b/src/main/java/com/comphenix/protocol/PacketType.java @@ -123,78 +123,84 @@ public class PacketType implements Serializable, Cloneable, Comparable(protocol.getClass().getSuperclass(), false); + } + StructureModifier>>> maps = modifier.withTarget(protocol).withType(Map.class); for (Entry>> entry : maps.read(0).entrySet()) { String direction = entry.getKey().toString(); @@ -92,6 +95,7 @@ public class NettyProtocolRegistry extends ProtocolRegistry { protected void associatePackets(Register register, Map> lookup, Protocol protocol, Sender sender) { for (Entry> entry : lookup.entrySet()) { PacketType type = PacketType.fromCurrent(protocol, sender, entry.getKey(), entry.getValue()); + // System.out.println(Arrays.toString(type.getClassNames()) + " -> " + entry.getValue()); try { register.typeToClass.put(type, entry.getValue()); diff --git a/src/main/java/com/comphenix/protocol/metrics/Metrics.java b/src/main/java/com/comphenix/protocol/metrics/Metrics.java index 99819944..bb8b0c83 100644 --- a/src/main/java/com/comphenix/protocol/metrics/Metrics.java +++ b/src/main/java/com/comphenix/protocol/metrics/Metrics.java @@ -1,13 +1,12 @@ package com.comphenix.protocol.metrics; -import org.apache.commons.lang3.tuple.Pair; import org.bukkit.Bukkit; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.craftbukkit.libs.org.apache.commons.lang3.tuple.Pair; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.ServicePriority; -import org.bukkit.plugin.java.JavaPlugin; import org.json.simple.JSONArray; import org.json.simple.JSONObject; @@ -16,16 +15,9 @@ import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.Timer; -import java.util.TimerTask; -import java.util.UUID; +import java.util.*; import java.util.concurrent.Callable; import java.util.logging.Level; import java.util.zip.GZIPOutputStream; diff --git a/src/main/java/com/comphenix/protocol/metrics/Statistics.java b/src/main/java/com/comphenix/protocol/metrics/Statistics.java index 67b74e32..846d6ef7 100644 --- a/src/main/java/com/comphenix/protocol/metrics/Statistics.java +++ b/src/main/java/com/comphenix/protocol/metrics/Statistics.java @@ -17,17 +17,15 @@ package com.comphenix.protocol.metrics; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - import com.comphenix.protocol.ProtocolLib; import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketListener; +import org.bukkit.craftbukkit.libs.org.apache.commons.lang3.tuple.Pair; -import org.apache.commons.lang3.tuple.Pair; -import org.bukkit.plugin.Plugin; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; public class Statistics { diff --git a/src/main/java/com/comphenix/protocol/reflect/cloning/ImmutableDetector.java b/src/main/java/com/comphenix/protocol/reflect/cloning/ImmutableDetector.java index b129d2e2..288b9a5d 100644 --- a/src/main/java/com/comphenix/protocol/reflect/cloning/ImmutableDetector.java +++ b/src/main/java/com/comphenix/protocol/reflect/cloning/ImmutableDetector.java @@ -69,6 +69,10 @@ public class ImmutableDetector implements Cloner { add(MinecraftReflection::getFluidTypeClass); add(MinecraftReflection::getParticleTypeClass); } + + if (MinecraftVersion.atOrAbove(MinecraftVersion.VILLAGE_UPDATE)) { + add(() -> MinecraftReflection.getMinecraftClass("EntityTypes")); + } } private static void add(Supplier> getClass) { diff --git a/src/main/java/com/comphenix/protocol/utility/Constants.java b/src/main/java/com/comphenix/protocol/utility/Constants.java index d44b4775..e04c5dac 100644 --- a/src/main/java/com/comphenix/protocol/utility/Constants.java +++ b/src/main/java/com/comphenix/protocol/utility/Constants.java @@ -21,7 +21,7 @@ package com.comphenix.protocol.utility; */ public final class Constants { - public static final String PACKAGE_VERSION = "v1_13_R2"; + public static final String PACKAGE_VERSION = "v1_14_R1"; public static final String NMS = "net.minecraft.server." + PACKAGE_VERSION; public static final String OBC = "org.bukkit.craftbukkit." + PACKAGE_VERSION; } diff --git a/src/main/java/com/comphenix/protocol/utility/MinecraftProtocolVersion.java b/src/main/java/com/comphenix/protocol/utility/MinecraftProtocolVersion.java index 00ea7c04..ac392752 100644 --- a/src/main/java/com/comphenix/protocol/utility/MinecraftProtocolVersion.java +++ b/src/main/java/com/comphenix/protocol/utility/MinecraftProtocolVersion.java @@ -49,6 +49,8 @@ public class MinecraftProtocolVersion { map.put(new MinecraftVersion(1, 12, 2), 340); map.put(new MinecraftVersion(1, 13, 0), 393); map.put(new MinecraftVersion(1, 13, 1), 401); + map.put(new MinecraftVersion(1, 13, 2), 404); + map.put(new MinecraftVersion(1, 14, 0), 477); return map; } diff --git a/src/main/java/com/comphenix/protocol/utility/MinecraftVersion.java b/src/main/java/com/comphenix/protocol/utility/MinecraftVersion.java index 008dd67b..66205567 100644 --- a/src/main/java/com/comphenix/protocol/utility/MinecraftVersion.java +++ b/src/main/java/com/comphenix/protocol/utility/MinecraftVersion.java @@ -44,6 +44,11 @@ public class MinecraftVersion implements Comparable, Serializa */ private static final Pattern VERSION_PATTERN = Pattern.compile(".*\\(.*MC.\\s*([a-zA-z0-9\\-\\.]+)\\s*\\)"); + /** + * Version 1.14 - village and pillage update. + */ + public static final MinecraftVersion VILLAGE_UPDATE = new MinecraftVersion("1.14"); + /** * Version 1.13 - update aquatic. */ diff --git a/src/main/java/com/comphenix/protocol/wrappers/EnumWrappers.java b/src/main/java/com/comphenix/protocol/wrappers/EnumWrappers.java index 853545b2..c5573b1c 100644 --- a/src/main/java/com/comphenix/protocol/wrappers/EnumWrappers.java +++ b/src/main/java/com/comphenix/protocol/wrappers/EnumWrappers.java @@ -1,9 +1,5 @@ package com.comphenix.protocol.wrappers; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; - import com.comphenix.protocol.PacketType; import com.comphenix.protocol.PacketType.Protocol; import com.comphenix.protocol.ProtocolLogger; @@ -12,9 +8,12 @@ import com.comphenix.protocol.reflect.FuzzyReflection; import com.comphenix.protocol.utility.MinecraftReflection; import com.google.common.collect.Maps; -import org.bukkit.EntityEffect; import org.bukkit.GameMode; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + /** * Represents a generic enum converter. * @author Kristian @@ -387,7 +386,13 @@ public abstract class EnumWrappers { PROTOCOL_CLASS = getEnum(PacketType.Handshake.Client.SET_PROTOCOL.getPacketClass(), 0); CLIENT_COMMAND_CLASS = getEnum(PacketType.Play.Client.CLIENT_COMMAND.getPacketClass(), 0); CHAT_VISIBILITY_CLASS = getEnum(PacketType.Play.Client.SETTINGS.getPacketClass(), 0); - DIFFICULTY_CLASS = getEnum(PacketType.Play.Server.LOGIN.getPacketClass(), 1); + + try { + DIFFICULTY_CLASS = getEnum(PacketType.Play.Server.SERVER_DIFFICULTY.getPacketClass(), 0); + } catch (Exception ex) { + DIFFICULTY_CLASS = getEnum(PacketType.Play.Server.LOGIN.getPacketClass(), 1); + } + ENTITY_USE_ACTION_CLASS = getEnum(PacketType.Play.Client.USE_ENTITY.getPacketClass(), 0); GAMEMODE_CLASS = getEnum(PacketType.Play.Server.LOGIN.getPacketClass(), 0); RESOURCE_PACK_STATUS_CLASS = getEnum(PacketType.Play.Client.RESOURCE_PACK_STATUS.getPacketClass(), 0); @@ -447,9 +452,9 @@ public abstract class EnumWrappers { try { return FuzzyReflection.fromClass(clazz, true).getFieldListByType(Enum.class).get(index).getType(); } catch (Throwable ex) { - /* if (ProtocolLogger.isDebugEnabled()) { - ex.printStackTrace(); - } */ + if (ProtocolLogger.isDebugEnabled()) { + // ex.printStackTrace(); + } return null; } diff --git a/src/main/java/com/comphenix/protocol/wrappers/Vector3F.java b/src/main/java/com/comphenix/protocol/wrappers/Vector3F.java index 36b88ba0..3102713c 100644 --- a/src/main/java/com/comphenix/protocol/wrappers/Vector3F.java +++ b/src/main/java/com/comphenix/protocol/wrappers/Vector3F.java @@ -3,12 +3,11 @@ */ package com.comphenix.protocol.wrappers; -import java.lang.reflect.Constructor; - import com.comphenix.protocol.reflect.EquivalentConverter; import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.utility.MinecraftReflection; -import com.comphenix.protocol.wrappers.BukkitConverters.IgnoreNullConverter; + +import java.lang.reflect.Constructor; /** * @author dmulloy2 diff --git a/src/main/java/com/comphenix/protocol/wrappers/WrappedAttributeModifier.java b/src/main/java/com/comphenix/protocol/wrappers/WrappedAttributeModifier.java index 18aa3868..d26a5ed4 100644 --- a/src/main/java/com/comphenix/protocol/wrappers/WrappedAttributeModifier.java +++ b/src/main/java/com/comphenix/protocol/wrappers/WrappedAttributeModifier.java @@ -5,10 +5,12 @@ import java.util.UUID; import java.util.function.Supplier; import javax.annotation.Nonnull; +import com.comphenix.protocol.reflect.EquivalentConverter; import com.comphenix.protocol.reflect.FuzzyReflection; import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.reflect.fuzzy.FuzzyMethodContract; import com.comphenix.protocol.utility.MinecraftReflection; +import com.comphenix.protocol.utility.MinecraftVersion; import com.google.common.base.Objects; import com.google.common.base.Preconditions; @@ -20,6 +22,49 @@ import com.google.common.base.Preconditions; * @author Kristian */ public class WrappedAttributeModifier extends AbstractWrapper { + private static final Class OPERATION_CLASS = MinecraftReflection.getMinecraftClass("AttributeModifier$Operation"); + private static final boolean OPERATION_ENUM = MinecraftVersion.atOrAbove(MinecraftVersion.VILLAGE_UPDATE); + private static final EquivalentConverter OPERATION_CONVERTER = new IndexedEnumConverter<>(Operation.class, OPERATION_CLASS); + + private static class IndexedEnumConverter> implements EquivalentConverter { + private Class specificClass; + private Class genericClass; + + private IndexedEnumConverter(Class specificClass, Class genericClass) { + this.specificClass = specificClass; + this.genericClass = genericClass; + } + + @Override + public Object getGeneric(T specific) { + int ordinal = specific.ordinal(); + for (Object elem : genericClass.getEnumConstants()) { + if (((Enum) elem).ordinal() == ordinal) { + return elem; + } + } + + return null; + } + + @Override + public T getSpecific(Object generic) { + int ordinal = ((Enum) generic).ordinal(); + for (T elem : specificClass.getEnumConstants()) { + if (elem.ordinal() == ordinal) { + return elem; + } + } + + return null; + } + + @Override + public Class getSpecificType() { + return specificClass; + } + } + /** * Represents the different modifier operations. *

@@ -137,7 +182,12 @@ public class WrappedAttributeModifier extends AbstractWrapper { } this.amount = (Double) modifier.withType(double.class).read(0); - this.operation = Operation.fromId((Integer) modifier.withType(int.class).read(0)); + + if (OPERATION_ENUM) { + this.operation = modifier.withType(OPERATION_CLASS, OPERATION_CONVERTER).readSafely(0); + } else { + this.operation = Operation.fromId((Integer) modifier.withType(int.class).readSafely(0)); + } } /** @@ -374,16 +424,7 @@ public class WrappedAttributeModifier extends AbstractWrapper { // Retrieve the correct constructor if (ATTRIBUTE_MODIFIER_CONSTRUCTOR == null) { - ATTRIBUTE_MODIFIER_CONSTRUCTOR = FuzzyReflection.fromClass( - MinecraftReflection.getAttributeModifierClass(), true).getConstructor( - FuzzyMethodContract.newBuilder().parameterCount(4). - parameterDerivedOf(UUID.class, 0). - parameterExactType(String.class, 1). - parameterExactType(double.class, 2). - parameterExactType(int.class, 3).build()); - - // Just in case - ATTRIBUTE_MODIFIER_CONSTRUCTOR.setAccessible(true); + ATTRIBUTE_MODIFIER_CONSTRUCTOR = getConstructor(); } // Construct it @@ -391,7 +432,7 @@ public class WrappedAttributeModifier extends AbstractWrapper { // No need to read these values with a modifier return new WrappedAttributeModifier( ATTRIBUTE_MODIFIER_CONSTRUCTOR.newInstance( - uuid, name, amount, operation.getId()), + uuid, name, amount, getOperationParam(operation)), uuid, name, amount, operation ); } catch (Exception e) { @@ -399,4 +440,27 @@ public class WrappedAttributeModifier extends AbstractWrapper { } } } + + private static Object getOperationParam(Operation operation) { + return OPERATION_ENUM ? OPERATION_CONVERTER.getGeneric(operation) : operation.getId(); + } + + private static Constructor getConstructor() { + FuzzyMethodContract.Builder builder = FuzzyMethodContract + .newBuilder() + .parameterCount(4) + .parameterDerivedOf(UUID.class, 0) + .parameterExactType(String.class, 1) + .parameterExactType(double.class, 2); + if (OPERATION_ENUM) { + builder = builder.parameterExactType(OPERATION_CLASS, 3); + } else { + builder = builder.parameterExactType(int.class, 3); + } + + Constructor ret = FuzzyReflection.fromClass(MinecraftReflection.getAttributeModifierClass(), true) + .getConstructor(builder.build()); + ret.setAccessible(true); + return ret; + } } diff --git a/src/main/java/com/comphenix/protocol/wrappers/WrappedIntHashMap.java b/src/main/java/com/comphenix/protocol/wrappers/WrappedIntHashMap.java index 0b16d168..08cd8935 100644 --- a/src/main/java/com/comphenix/protocol/wrappers/WrappedIntHashMap.java +++ b/src/main/java/com/comphenix/protocol/wrappers/WrappedIntHashMap.java @@ -14,7 +14,9 @@ import com.google.common.base.Preconditions; /** * Represents a wrapper for the internal IntHashMap in Minecraft. * @author Kristian + * @deprecated IntHashMap no longer exists */ +@Deprecated public class WrappedIntHashMap extends AbstractWrapper { private static Method PUT_METHOD; private static Method GET_METHOD; diff --git a/src/test/java/com/comphenix/protocol/BukkitInitialization.java b/src/test/java/com/comphenix/protocol/BukkitInitialization.java index 21e52a87..c8aa3ce4 100644 --- a/src/test/java/com/comphenix/protocol/BukkitInitialization.java +++ b/src/test/java/com/comphenix/protocol/BukkitInitialization.java @@ -4,16 +4,17 @@ import com.comphenix.protocol.utility.Constants; import com.comphenix.protocol.utility.MinecraftReflection; import com.comphenix.protocol.utility.MinecraftVersion; -import net.minecraft.server.v1_13_R2.DispenserRegistry; +import net.minecraft.server.v1_14_R1.DispenserRegistry; import org.apache.logging.log4j.LogManager; import org.bukkit.Bukkit; import org.bukkit.Server; -import org.bukkit.craftbukkit.v1_13_R2.CraftServer; -import org.bukkit.craftbukkit.v1_13_R2.inventory.CraftItemFactory; -import org.bukkit.craftbukkit.v1_13_R2.util.Versioning; +import org.bukkit.craftbukkit.v1_14_R1.CraftServer; +import org.bukkit.craftbukkit.v1_14_R1.inventory.CraftItemFactory; +import org.bukkit.craftbukkit.v1_14_R1.util.Versioning; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * Used to ensure that ProtocolLib and Bukkit is prepared to be tested. @@ -55,7 +56,7 @@ public class BukkitInitialization { ex.printStackTrace(); } - DispenserRegistry.c(); // Basically registers everything + DispenserRegistry.init(); // Mock the server object Server mockedServer = mock(Server.class); @@ -88,7 +89,7 @@ public class BukkitInitialization { } MinecraftReflection.setMinecraftPackage(Constants.NMS, Constants.OBC); - MinecraftVersion.setCurrentVersion(MinecraftVersion.AQUATIC_UPDATE); + MinecraftVersion.setCurrentVersion(MinecraftVersion.VILLAGE_UPDATE); } } } \ No newline at end of file diff --git a/src/test/java/com/comphenix/protocol/PacketTypeTest.java b/src/test/java/com/comphenix/protocol/PacketTypeTest.java index 58473f8e..d0c4fd1c 100644 --- a/src/test/java/com/comphenix/protocol/PacketTypeTest.java +++ b/src/test/java/com/comphenix/protocol/PacketTypeTest.java @@ -17,6 +17,8 @@ package com.comphenix.protocol; import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; @@ -24,11 +26,14 @@ import java.util.TreeMap; import com.comphenix.protocol.PacketType.Protocol; import com.comphenix.protocol.PacketType.Sender; import com.comphenix.protocol.injector.packet.PacketRegistry; +import com.comphenix.protocol.utility.Constants; +import com.comphenix.protocol.utility.MinecraftReflection; -import net.minecraft.server.v1_13_R2.EnumProtocol; -import net.minecraft.server.v1_13_R2.EnumProtocolDirection; -import net.minecraft.server.v1_13_R2.PacketLoginInStart; +import net.minecraft.server.v1_14_R1.EnumProtocol; +import net.minecraft.server.v1_14_R1.EnumProtocolDirection; +import net.minecraft.server.v1_14_R1.PacketLoginInStart; +import org.apache.commons.lang.WordUtils; import org.junit.BeforeClass; import org.junit.Test; @@ -39,6 +44,117 @@ import static org.junit.Assert.*; */ public class PacketTypeTest { + @SuppressWarnings("unchecked") + public static void main(String[] args) throws Exception { + MinecraftReflection.setMinecraftPackage(Constants.NMS, Constants.OBC); + EnumProtocol[] protocols = EnumProtocol.values(); + for (EnumProtocol protocol : protocols) { + System.out.println(WordUtils.capitalize(protocol.name().toLowerCase())); + + Field field = EnumProtocol.class.getDeclaredField("h"); + field.setAccessible(true); + + Map>> map = (Map>>) field.get(protocol); + for (Entry>> entry : map.entrySet()) { + Map> treeMap = new TreeMap>(entry.getValue()); + System.out.println(" " + entry.getKey()); + for (Entry> entry1 : treeMap.entrySet()) { + System.out.println(generateNewType(entry1.getKey(), entry1.getValue())); + } + } + } + } + + private static String formatHex(int dec) { + if (dec < 0) { + return "-1"; + } + + String hex = Integer.toHexString(dec).toUpperCase(); + return "0x" + (hex.length() < 2 ? "0" : "") + hex; + } + + private static List splitOnCaps(String string) { + List list = new ArrayList<>(); + StringBuilder builder = new StringBuilder(); + char[] chars = string.toCharArray(); + for (int i = 0; i < chars.length; i++) { + char c = chars[i]; + if (i != 0 && Character.isUpperCase(c)) { + list.add(builder.toString()); + builder = new StringBuilder(); + } + + builder.append(c); + } + + list.add(builder.toString()); + return list; + } + + private static String generateNewType(int packetId, Class clazz) { + StringBuilder builder = new StringBuilder(); + builder.append("\t\t\t"); + builder.append("public static final PacketType "); + + StringBuilder nameBuilder = new StringBuilder(); + List split = splitOnCaps(clazz.getSimpleName()); + + // We're not interested in the first 3 + for (int i = 3; i < split.size(); i++) { + nameBuilder.append(split.get(i)); + } + + String className = nameBuilder.toString(); + + // Format it like SET_PROTOCOL + StringBuilder fieldName = new StringBuilder(); + char[] chars = className.toCharArray(); + for (int i = 0; i < chars.length; i++) { + char c = chars[i]; + if (i != 0 && Character.isUpperCase(c)) { + fieldName.append("_"); + } + fieldName.append(Character.toUpperCase(c)); + } + + builder.append(fieldName.toString().replace("N_B_T", "NBT")); + builder.append(" = "); + + // Add spacing + if (builder.length() > 65) { + builder.append("\n"); + } else { + while (builder.length() < 65) { + builder.append(" "); + } + } + builder.append("new "); + builder.append("PacketType(PROTOCOL, SENDER, "); + + int legacy = -1; + + try { + PacketType type = PacketType.fromClass(clazz); + if (type != null) { + legacy = type.getCurrentId(); + } + } catch (Throwable ex) { + // ex.printStackTrace(); + } + + builder.append(formatHex(packetId)); + builder.append(", "); + builder.append(formatHex(legacy)); + builder.append(", "); + if (legacy == -1) { + builder.append(" "); + } + builder.append("\"").append(className).append("\""); + builder.append(");"); + return builder.toString(); + } + @BeforeClass public static void initializeReflection() { BukkitInitialization.initializePackage(); diff --git a/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java b/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java index 00bd2db9..51b324fc 100644 --- a/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java +++ b/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java @@ -16,10 +16,6 @@ */ package com.comphenix.protocol.events; -import java.lang.reflect.Array; -import java.lang.reflect.Field; -import java.util.*; - import com.comphenix.protocol.BukkitInitialization; import com.comphenix.protocol.PacketType; import com.comphenix.protocol.injector.PacketConstructor; @@ -36,11 +32,11 @@ import com.comphenix.protocol.wrappers.nbt.NbtCompound; import com.comphenix.protocol.wrappers.nbt.NbtFactory; import com.google.common.collect.Lists; -import net.minecraft.server.v1_13_R2.*; -import net.minecraft.server.v1_13_R2.PacketPlayOutUpdateAttributes.AttributeSnapshot; +import net.minecraft.server.v1_14_R1.*; +import net.minecraft.server.v1_14_R1.PacketPlayOutUpdateAttributes.AttributeSnapshot; import org.apache.commons.lang.SerializationUtils; -import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang.builder.EqualsBuilder; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.Sound; @@ -54,6 +50,10 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PowerMockIgnore; +import java.lang.reflect.Array; +import java.lang.reflect.Field; +import java.util.*; + import static com.comphenix.protocol.utility.TestUtils.*; import static org.junit.Assert.*; @@ -359,7 +359,7 @@ public class PacketContainerTest { // Initialize some test data List modifiers = Lists.newArrayList( - new AttributeModifier(UUID.randomUUID(), "Unknown synced attribute modifier", 10, 0)); + new AttributeModifier(UUID.randomUUID(), "Unknown synced attribute modifier", 10, AttributeModifier.Operation.ADDITION)); // Obtain an AttributeSnapshot instance. This is complicated by the fact that AttributeSnapshots // are inner classes (which is ultimately pointless because AttributeSnapshots don't access any @@ -508,20 +508,13 @@ public class PacketContainerTest { public void testDeepClone() { // Try constructing all the packets for (PacketType type : PacketType.values()) { - if (BLACKLISTED.contains(type) || type.isDeprecated() || type.name().contains("CUSTOM_PAYLOAD")) { + if (BLACKLISTED.contains(type) || type.isDeprecated() || type.name().contains("CUSTOM_PAYLOAD") || !type.isSupported()) { continue; } - // Whether or not this packet has been registered - boolean registered = type.isSupported(); - try { PacketContainer constructed = new PacketContainer(type); - if (!registered) { - fail("Expected IllegalArgumentException(Packet " + type + " not registered)"); - } - // Initialize default values constructed.getModifier().writeDefaults(); diff --git a/src/test/java/com/comphenix/protocol/reflect/cloning/AggregateClonerTest.java b/src/test/java/com/comphenix/protocol/reflect/cloning/AggregateClonerTest.java index 2a6e4738..0ec30c7c 100644 --- a/src/test/java/com/comphenix/protocol/reflect/cloning/AggregateClonerTest.java +++ b/src/test/java/com/comphenix/protocol/reflect/cloning/AggregateClonerTest.java @@ -6,8 +6,8 @@ import static org.junit.Assert.assertEquals; import java.util.Arrays; import java.util.List; -import net.minecraft.server.v1_13_R2.ItemStack; -import net.minecraft.server.v1_13_R2.NonNullList; +import net.minecraft.server.v1_14_R1.ItemStack; +import net.minecraft.server.v1_14_R1.NonNullList; import org.junit.BeforeClass; import org.junit.Test; diff --git a/src/test/java/com/comphenix/protocol/utility/MinecraftReflectionTest.java b/src/test/java/com/comphenix/protocol/utility/MinecraftReflectionTest.java index 9f836548..8a3576e1 100644 --- a/src/test/java/com/comphenix/protocol/utility/MinecraftReflectionTest.java +++ b/src/test/java/com/comphenix/protocol/utility/MinecraftReflectionTest.java @@ -6,22 +6,22 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import net.minecraft.server.v1_13_R2.ChatComponentText; -import net.minecraft.server.v1_13_R2.ChunkCoordIntPair; -import net.minecraft.server.v1_13_R2.DataWatcher; -import net.minecraft.server.v1_13_R2.IBlockData; -import net.minecraft.server.v1_13_R2.IChatBaseComponent; -import net.minecraft.server.v1_13_R2.IChatBaseComponent.ChatSerializer; -import net.minecraft.server.v1_13_R2.NBTCompressedStreamTools; -import net.minecraft.server.v1_13_R2.PacketPlayOutUpdateAttributes.AttributeSnapshot; -import net.minecraft.server.v1_13_R2.PlayerConnection; -import net.minecraft.server.v1_13_R2.ServerPing; -import net.minecraft.server.v1_13_R2.ServerPing.ServerData; -import net.minecraft.server.v1_13_R2.ServerPing.ServerPingPlayerSample; +import net.minecraft.server.v1_14_R1.ChatComponentText; +import net.minecraft.server.v1_14_R1.ChunkCoordIntPair; +import net.minecraft.server.v1_14_R1.DataWatcher; +import net.minecraft.server.v1_14_R1.IBlockData; +import net.minecraft.server.v1_14_R1.IChatBaseComponent; +import net.minecraft.server.v1_14_R1.IChatBaseComponent.ChatSerializer; +import net.minecraft.server.v1_14_R1.NBTCompressedStreamTools; +import net.minecraft.server.v1_14_R1.PacketPlayOutUpdateAttributes.AttributeSnapshot; +import net.minecraft.server.v1_14_R1.PlayerConnection; +import net.minecraft.server.v1_14_R1.ServerPing; +import net.minecraft.server.v1_14_R1.ServerPing.ServerData; +import net.minecraft.server.v1_14_R1.ServerPing.ServerPingPlayerSample; import org.bukkit.Material; import org.bukkit.block.Block; -import org.bukkit.craftbukkit.v1_13_R2.inventory.CraftItemStack; +import org.bukkit.craftbukkit.v1_14_R1.inventory.CraftItemStack; import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; import org.junit.AfterClass; diff --git a/src/test/java/com/comphenix/protocol/utility/StreamSerializerTest.java b/src/test/java/com/comphenix/protocol/utility/StreamSerializerTest.java index ef3c2423..fc1ad6a4 100644 --- a/src/test/java/com/comphenix/protocol/utility/StreamSerializerTest.java +++ b/src/test/java/com/comphenix/protocol/utility/StreamSerializerTest.java @@ -1,13 +1,9 @@ package com.comphenix.protocol.utility; -import java.io.*; - import com.comphenix.protocol.BukkitInitialization; import com.comphenix.protocol.wrappers.nbt.NbtCompound; import com.comphenix.protocol.wrappers.nbt.NbtFactory; -import net.minecraft.server.v1_13_R2.IntHashMap; - import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; @@ -17,6 +13,8 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PowerMockIgnore; +import java.io.*; + import static com.comphenix.protocol.utility.TestUtils.assertItemsEqual; import static org.junit.Assert.assertEquals; @@ -30,10 +28,10 @@ public class StreamSerializerTest { BukkitInitialization.initializeItemMeta(); } - @Test + /*@Test public void testMinecraftReflection() { assertEquals(IntHashMap.class, MinecraftReflection.getIntHashMapClass()); - } + }*/ @Test public void testStrings() throws IOException { diff --git a/src/test/java/com/comphenix/protocol/wrappers/ChunkCoordIntPairTest.java b/src/test/java/com/comphenix/protocol/wrappers/ChunkCoordIntPairTest.java index 5736c539..6a723c0f 100644 --- a/src/test/java/com/comphenix/protocol/wrappers/ChunkCoordIntPairTest.java +++ b/src/test/java/com/comphenix/protocol/wrappers/ChunkCoordIntPairTest.java @@ -16,14 +16,14 @@ public class ChunkCoordIntPairTest { @Test public void test() { - net.minecraft.server.v1_13_R2.ChunkCoordIntPair pair = new net.minecraft.server.v1_13_R2.ChunkCoordIntPair(1, 2); + net.minecraft.server.v1_14_R1.ChunkCoordIntPair pair = new net.minecraft.server.v1_14_R1.ChunkCoordIntPair(1, 2); ChunkCoordIntPair specific = ChunkCoordIntPair.getConverter().getSpecific(pair); assertEquals(1, specific.getChunkX()); assertEquals(2, specific.getChunkZ()); - net.minecraft.server.v1_13_R2.ChunkCoordIntPair roundtrip = - (net.minecraft.server.v1_13_R2.ChunkCoordIntPair) ChunkCoordIntPair.getConverter(). + net.minecraft.server.v1_14_R1.ChunkCoordIntPair roundtrip = + (net.minecraft.server.v1_14_R1.ChunkCoordIntPair) ChunkCoordIntPair.getConverter(). getGeneric(specific); assertEquals(1, roundtrip.x); diff --git a/src/test/java/com/comphenix/protocol/wrappers/EnumWrappersTest.java b/src/test/java/com/comphenix/protocol/wrappers/EnumWrappersTest.java index 4d9752e8..8ddc3e8f 100644 --- a/src/test/java/com/comphenix/protocol/wrappers/EnumWrappersTest.java +++ b/src/test/java/com/comphenix/protocol/wrappers/EnumWrappersTest.java @@ -2,12 +2,12 @@ package com.comphenix.protocol.wrappers; import static org.junit.Assert.assertEquals; -import net.minecraft.server.v1_13_R2.EntityHuman.EnumChatVisibility; -import net.minecraft.server.v1_13_R2.EnumDifficulty; -import net.minecraft.server.v1_13_R2.EnumGamemode; -import net.minecraft.server.v1_13_R2.EnumProtocol; -import net.minecraft.server.v1_13_R2.PacketPlayInClientCommand.EnumClientCommand; -import net.minecraft.server.v1_13_R2.PacketPlayInUseEntity.EnumEntityUseAction; +import net.minecraft.server.v1_14_R1.EnumChatVisibility; +import net.minecraft.server.v1_14_R1.EnumDifficulty; +import net.minecraft.server.v1_14_R1.EnumGamemode; +import net.minecraft.server.v1_14_R1.EnumProtocol; +import net.minecraft.server.v1_14_R1.PacketPlayInClientCommand.EnumClientCommand; +import net.minecraft.server.v1_14_R1.PacketPlayInUseEntity.EnumEntityUseAction; import org.junit.BeforeClass; import org.junit.Test; diff --git a/src/test/java/com/comphenix/protocol/wrappers/WrappedAttributeTest.java b/src/test/java/com/comphenix/protocol/wrappers/WrappedAttributeTest.java index 6c141ea0..eb68c145 100644 --- a/src/test/java/com/comphenix/protocol/wrappers/WrappedAttributeTest.java +++ b/src/test/java/com/comphenix/protocol/wrappers/WrappedAttributeTest.java @@ -6,9 +6,9 @@ import static org.junit.Assert.assertTrue; import java.util.List; -import net.minecraft.server.v1_13_R2.AttributeModifier; -import net.minecraft.server.v1_13_R2.PacketPlayOutUpdateAttributes; -import net.minecraft.server.v1_13_R2.PacketPlayOutUpdateAttributes.AttributeSnapshot; +import net.minecraft.server.v1_14_R1.AttributeModifier; +import net.minecraft.server.v1_14_R1.PacketPlayOutUpdateAttributes; +import net.minecraft.server.v1_14_R1.PacketPlayOutUpdateAttributes.AttributeSnapshot; import org.junit.Before; import org.junit.BeforeClass; @@ -90,6 +90,7 @@ public class WrappedAttributeTest { } private AttributeModifier getModifierCopy(WrappedAttributeModifier modifier) { - return new AttributeModifier(modifier.getUUID(), modifier.getName(), modifier.getAmount(), modifier.getOperation().getId()); + AttributeModifier.Operation operation = AttributeModifier.Operation.values()[modifier.getOperation().getId()]; + return new AttributeModifier(modifier.getUUID(), modifier.getName(), modifier.getAmount(), operation); } } diff --git a/src/test/java/com/comphenix/protocol/wrappers/WrappedBlockDataTest.java b/src/test/java/com/comphenix/protocol/wrappers/WrappedBlockDataTest.java index cb893a10..4dbcfc44 100644 --- a/src/test/java/com/comphenix/protocol/wrappers/WrappedBlockDataTest.java +++ b/src/test/java/com/comphenix/protocol/wrappers/WrappedBlockDataTest.java @@ -18,14 +18,14 @@ package com.comphenix.protocol.wrappers; import com.comphenix.protocol.BukkitInitialization; -import net.minecraft.server.v1_13_R2.IBlockData; +import net.minecraft.server.v1_14_R1.IBlockData; import org.bukkit.Material; import org.bukkit.block.BlockFace; import org.bukkit.block.data.type.GlassPane; -import org.bukkit.craftbukkit.v1_13_R2.block.data.CraftBlockData; -import org.bukkit.craftbukkit.v1_13_R2.block.impl.CraftGlassPane; -import org.bukkit.craftbukkit.v1_13_R2.util.CraftMagicNumbers; +import org.bukkit.craftbukkit.v1_14_R1.block.data.CraftBlockData; +import org.bukkit.craftbukkit.v1_14_R1.block.impl.CraftStainedGlassPane; +import org.bukkit.craftbukkit.v1_14_R1.util.CraftMagicNumbers; import org.junit.BeforeClass; import org.junit.Test; @@ -67,7 +67,7 @@ public class WrappedBlockDataTest { WrappedBlockData wrapper = WrappedBlockData.createData(data); assertEquals(wrapper.getType(), Material.CYAN_STAINED_GLASS_PANE); - GlassPane back = new CraftGlassPane((IBlockData) wrapper.getHandle()); + GlassPane back = new CraftStainedGlassPane((IBlockData) wrapper.getHandle()); assertEquals(back.hasFace(BlockFace.EAST), data.hasFace(BlockFace.EAST)); assertEquals(back.hasFace(BlockFace.SOUTH), data.hasFace(BlockFace.SOUTH)); } diff --git a/src/test/java/com/comphenix/protocol/wrappers/WrappedDataWatcherTest.java b/src/test/java/com/comphenix/protocol/wrappers/WrappedDataWatcherTest.java index 25697a04..37009d27 100644 --- a/src/test/java/com/comphenix/protocol/wrappers/WrappedDataWatcherTest.java +++ b/src/test/java/com/comphenix/protocol/wrappers/WrappedDataWatcherTest.java @@ -23,10 +23,10 @@ import com.comphenix.protocol.wrappers.WrappedDataWatcher.Registry; import com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer; import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject; -import net.minecraft.server.v1_13_R2.EntityEgg; +import net.minecraft.server.v1_14_R1.EntityEgg; -import org.bukkit.craftbukkit.v1_13_R2.entity.CraftEgg; -import org.bukkit.craftbukkit.v1_13_R2.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_14_R1.entity.CraftEgg; +import org.bukkit.craftbukkit.v1_14_R1.entity.CraftEntity; import org.junit.BeforeClass; import org.junit.Test; @@ -87,8 +87,8 @@ public class WrappedDataWatcherTest { @Test public void testSerializers() { - Serializer blockPos = Registry.get(net.minecraft.server.v1_13_R2.BlockPosition.class, false); - Serializer optionalBlockPos = Registry.get(net.minecraft.server.v1_13_R2.BlockPosition.class, true); + Serializer blockPos = Registry.get(net.minecraft.server.v1_14_R1.BlockPosition.class, false); + Serializer optionalBlockPos = Registry.get(net.minecraft.server.v1_14_R1.BlockPosition.class, true); assertNotSame(blockPos, optionalBlockPos); // assertNull(Registry.get(ItemStack.class, false)); diff --git a/src/test/java/com/comphenix/protocol/wrappers/WrappedIntHashMapTest.java b/src/test/java/com/comphenix/protocol/wrappers/WrappedIntHashMapTest.java index ea2d5ef1..3719144d 100644 --- a/src/test/java/com/comphenix/protocol/wrappers/WrappedIntHashMapTest.java +++ b/src/test/java/com/comphenix/protocol/wrappers/WrappedIntHashMapTest.java @@ -10,12 +10,12 @@ import com.comphenix.protocol.BukkitInitialization; public class WrappedIntHashMapTest { - @BeforeClass + //@BeforeClass public static void initializeBukkit() { BukkitInitialization.initializePackage(); } - @Test + //@Test public void testIntMap() { WrappedIntHashMap test = WrappedIntHashMap.newMap(); test.put(1, "hello"); diff --git a/src/test/java/com/comphenix/protocol/wrappers/WrappedParticleTest.java b/src/test/java/com/comphenix/protocol/wrappers/WrappedParticleTest.java index 0cfa979e..a6a66401 100644 --- a/src/test/java/com/comphenix/protocol/wrappers/WrappedParticleTest.java +++ b/src/test/java/com/comphenix/protocol/wrappers/WrappedParticleTest.java @@ -4,6 +4,8 @@ import com.comphenix.protocol.BukkitInitialization; import com.comphenix.protocol.PacketType; import com.comphenix.protocol.events.PacketContainer; +import net.minecraft.server.v1_14_R1.PacketPlayOutWorldParticles; + import org.bukkit.Color; import org.bukkit.Material; import org.bukkit.Particle; @@ -24,6 +26,8 @@ public class WrappedParticleTest { @Test public void testBlockData() { PacketContainer packet = new PacketContainer(PacketType.Play.Server.WORLD_PARTICLES); + System.out.println(packet.getType().getPacketClass()); + System.out.println(PacketType.fromClass(PacketPlayOutWorldParticles.class)); WrappedParticle before = WrappedParticle.create(Particle.BLOCK_CRACK, WrappedBlockData.createData(Material.LAPIS_BLOCK)); packet.getNewParticles().write(0, before); diff --git a/src/test/java/com/comphenix/protocol/wrappers/nbt/NbtFactoryTest.java b/src/test/java/com/comphenix/protocol/wrappers/nbt/NbtFactoryTest.java index 14cc3c39..6f553ed9 100644 --- a/src/test/java/com/comphenix/protocol/wrappers/nbt/NbtFactoryTest.java +++ b/src/test/java/com/comphenix/protocol/wrappers/nbt/NbtFactoryTest.java @@ -26,8 +26,8 @@ import java.io.DataInputStream; import java.io.DataOutput; import java.io.DataOutputStream; -import net.minecraft.server.v1_13_R2.ItemStack; -import net.minecraft.server.v1_13_R2.Items; +import net.minecraft.server.v1_14_R1.ItemStack; +import net.minecraft.server.v1_14_R1.Items; import org.junit.BeforeClass; import org.junit.Test;