From 0ac68dc179b43648d55eb47ffbd85e7bb5d50faa Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Fri, 4 Aug 2017 14:01:06 -0400 Subject: [PATCH] Update to 1.12.1 Also contains changes for the packet wrapper update coming soonTM --- .../com/comphenix/protocol/PacketType.java | 223 +++++++++--------- .../protocol/events/PacketContainer.java | 12 + .../protocol/reflect/StructureModifier.java | 23 +- .../utility/MinecraftProtocolVersion.java | 1 + .../protocol/wrappers/AutoWrapper.java | 16 +- pom.xml | 2 +- 6 files changed, 159 insertions(+), 118 deletions(-) diff --git a/modules/API/src/main/java/com/comphenix/protocol/PacketType.java b/modules/API/src/main/java/com/comphenix/protocol/PacketType.java index d6fceaaf..46c6cb67 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/PacketType.java +++ b/modules/API/src/main/java/com/comphenix/protocol/PacketType.java @@ -109,85 +109,86 @@ public class PacketType implements Serializable, Cloneable, Comparable Element param + * @return A modifier for List fields + */ + public StructureModifier> getLists(EquivalentConverter converter) { + return structureModifier.withType( + List.class, + BukkitConverters.getListConverter(converter)); + } + /** * Retrieve a read/write structure for an enum. This allows for the use of * user-created enums that may not exist in ProtocolLib. The specific (user diff --git a/modules/API/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java b/modules/API/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java index bcf9cef8..1d7ef222 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java +++ b/modules/API/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java @@ -19,10 +19,7 @@ package com.comphenix.protocol.reflect; import java.lang.reflect.Field; import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -263,6 +260,24 @@ public class StructureModifier { return null; } } + + /** + * Reads the value of a field only if it exists. If the field does not exist, an empty {@link Optional} is returned. + *

+ * This method has the same functionality as {@link #readSafely(int)}, but enforces null checks by way of an Optional. + * It will eventually become the preferred method of reading fields. + * + * @param fieldIndex index of the field + * @return An optional that may contain the value of the field + * @see #readSafely(int) + */ + public Optional optionRead(int fieldIndex) { + try { + return Optional.ofNullable(read(fieldIndex)); + } catch (FieldAccessException ex) { + return Optional.empty(); + } + } /** * Determine whether or not a field is read-only (final). diff --git a/modules/API/src/main/java/com/comphenix/protocol/utility/MinecraftProtocolVersion.java b/modules/API/src/main/java/com/comphenix/protocol/utility/MinecraftProtocolVersion.java index 93f226f3..8c961ea7 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/utility/MinecraftProtocolVersion.java +++ b/modules/API/src/main/java/com/comphenix/protocol/utility/MinecraftProtocolVersion.java @@ -45,6 +45,7 @@ public class MinecraftProtocolVersion { map.put(new MinecraftVersion(1, 11, 0), 315); map.put(new MinecraftVersion(1, 11, 1), 316); map.put(new MinecraftVersion(1, 12, 0), 335); + map.put(new MinecraftVersion(1, 12, 1), 338); return map; } diff --git a/modules/API/src/main/java/com/comphenix/protocol/wrappers/AutoWrapper.java b/modules/API/src/main/java/com/comphenix/protocol/wrappers/AutoWrapper.java index c0f67d96..ae847be4 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/wrappers/AutoWrapper.java +++ b/modules/API/src/main/java/com/comphenix/protocol/wrappers/AutoWrapper.java @@ -18,11 +18,13 @@ package com.comphenix.protocol.wrappers; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.function.Function; import com.comphenix.protocol.reflect.EquivalentConverter; +import com.comphenix.protocol.utility.MinecraftReflection; /** * Automatically wraps an internal NMS class to a non-versioned, deofbuscated class. @@ -54,6 +56,10 @@ public class AutoWrapper implements EquivalentConverter { return new AutoWrapper<>(wrapperClass, nmsClass); } + public static AutoWrapper wrap(Class wrapperClass, String nmsClassName) { + return wrap(wrapperClass, MinecraftReflection.getMinecraftClass(nmsClassName)); + } + public AutoWrapper field(int index, Function wrapper, Function unwrapper) { wrappers.put(index, wrapper); unwrappers.put(index, unwrapper); @@ -74,7 +80,10 @@ public class AutoWrapper implements EquivalentConverter { } Field[] wrapperFields = wrapperClass.getDeclaredFields(); - Field[] nmsFields = nmsClass.getDeclaredFields(); + Field[] nmsFields = Arrays + .stream(nmsClass.getDeclaredFields()) + .filter(field -> !Modifier.isStatic(field.getModifiers())) + .toArray(Field[]::new); for (int i = 0; i < wrapperFields.length; i++) { try { @@ -107,7 +116,10 @@ public class AutoWrapper implements EquivalentConverter { } Field[] wrapperFields = wrapperClass.getDeclaredFields(); - Field[] nmsFields = nmsClass.getDeclaredFields(); + Field[] nmsFields = Arrays + .stream(nmsClass.getDeclaredFields()) + .filter(field -> !Modifier.isStatic(field.getModifiers())) + .toArray(Field[]::new); for (int i = 0; i < wrapperFields.length; i++) { try { diff --git a/pom.xml b/pom.xml index aff08c10..4377065f 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ UTF-8 4.4.0-SNAPSHOT - 1.12-R0.1-SNAPSHOT + 1.12.1-R0.1-SNAPSHOT