From 19c57b0e5e99d732ce30944e5a340b8432d5970a Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Sun, 20 Mar 2016 13:35:59 -0400 Subject: [PATCH] Restore disabled tests, fix block data --- .../protocol/reflect/FuzzyReflection.java | 37 +++++++++++++++++-- .../protocol/wrappers/WrappedBlockData.java | 2 +- .../protocol/events/PacketContainerTest.java | 19 +++++++++- .../utility/MinecraftReflectionTest.java | 10 ++--- .../utility/StreamSerializerTest.java | 2 +- .../wrappers/WrappedBlockDataTest.java | 10 +++-- 6 files changed, 64 insertions(+), 16 deletions(-) diff --git a/modules/API/src/main/java/com/comphenix/protocol/reflect/FuzzyReflection.java b/modules/API/src/main/java/com/comphenix/protocol/reflect/FuzzyReflection.java index 1fd1c791..c539dc8b 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/reflect/FuzzyReflection.java +++ b/modules/API/src/main/java/com/comphenix/protocol/reflect/FuzzyReflection.java @@ -168,13 +168,42 @@ public class FuzzyReflection { */ public Method getMethod(AbstractFuzzyMatcher matcher) { List result = getMethodList(matcher); - - if (result.size() > 0) + + if (result.size() > 0) { return result.get(0); - else + } else { throw new IllegalArgumentException("Unable to find a method that matches " + matcher); + } } - + + /** + * Retrieve a method that matches. If there are multiple methods that match, the first one with the preferred + * name is selected. + *

+ * ForceAccess must be TRUE in order for this method to access private, protected and package level method. + * @param matcher - the matcher to use. + * @param preferred - the preferred name. + * @return The first method that satisfies the given matcher. + * @throws IllegalArgumentException If the method cannot be found. + */ + public Method getMethod(AbstractFuzzyMatcher matcher, String preferred) { + List result = getMethodList(matcher); + + if (result.size() > 1) { + for (Method method : result) { + if (method.getName().equals(preferred)) { + return method; + } + } + } + + if (result.size() > 0) { + return result.get(0); + } else { + throw new IllegalArgumentException("Unable to find a method that matches " + matcher); + } + } + /** * Retrieve a list of every method that matches the given matcher. *

diff --git a/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java b/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java index 63387bca..87e8a5a1 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java +++ b/modules/API/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java @@ -56,7 +56,7 @@ public class WrappedBlockData extends AbstractWrapper { .parameterExactArray(IBLOCK_DATA) .returnTypeExact(int.class) .build(); - TO_LEGACY_DATA = Accessors.getMethodAccessor(fuzzy.getMethod(contract)); + TO_LEGACY_DATA = Accessors.getMethodAccessor(fuzzy.getMethod(contract, "toLegacyData")); fuzzy = FuzzyReflection.fromClass(MAGIC_NUMBERS); GET_NMS_BLOCK = Accessors.getMethodAccessor(fuzzy.getMethodByParameters("getBlock", BLOCK, diff --git a/modules/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java b/modules/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java index 8cc53ed0..bfe9225e 100644 --- a/modules/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java +++ b/modules/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java @@ -62,6 +62,7 @@ import com.comphenix.protocol.utility.MinecraftReflection; import com.comphenix.protocol.utility.Util; import com.comphenix.protocol.wrappers.BlockPosition; import com.comphenix.protocol.wrappers.BukkitConverters; +import com.comphenix.protocol.wrappers.EnumWrappers.SoundCategory; import com.comphenix.protocol.wrappers.WrappedBlockData; import com.comphenix.protocol.wrappers.WrappedChatComponent; import com.comphenix.protocol.wrappers.WrappedDataWatcher; @@ -475,6 +476,22 @@ public class PacketContainerTest { assertEquals(e, (byte) packet.getBytes().read(2)); } + @Test + public void testMobEffectList() { + PacketContainer container = new PacketContainer(PacketType.Play.Server.REMOVE_ENTITY_EFFECT); + container.getEffectTypes().write(0, PotionEffectType.GLOWING); + + assertEquals(container.getEffectTypes().read(0), PotionEffectType.GLOWING); + } + + @Test + public void testSoundCategory() { + PacketContainer container = new PacketContainer(PacketType.Play.Server.NAMED_SOUND_EFFECT); + container.getSoundCategories().write(0, SoundCategory.PLAYERS); + + assertEquals(container.getSoundCategories().read(0), SoundCategory.PLAYERS); + } + private static final List BLACKLISTED = Util.asList( PacketType.Play.Client.CUSTOM_PAYLOAD, PacketType.Play.Server.CUSTOM_PAYLOAD, PacketType.Play.Server.SET_COOLDOWN @@ -528,7 +545,7 @@ public class PacketContainerTest { testEquality(firstMod.read(i), secondMod.read(i)); } } - } catch (IllegalArgumentException e) { + } catch (IllegalArgumentException e) { if (!registered) { // Let the test pass System.err.println("The packet ID " + type + " is not registered."); diff --git a/modules/ProtocolLib/src/test/java/com/comphenix/protocol/utility/MinecraftReflectionTest.java b/modules/ProtocolLib/src/test/java/com/comphenix/protocol/utility/MinecraftReflectionTest.java index 81be33b3..bb35f0d0 100644 --- a/modules/ProtocolLib/src/test/java/com/comphenix/protocol/utility/MinecraftReflectionTest.java +++ b/modules/ProtocolLib/src/test/java/com/comphenix/protocol/utility/MinecraftReflectionTest.java @@ -6,6 +6,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import net.minecraft.server.v1_9_R1.ChatComponentText; import net.minecraft.server.v1_9_R1.ChunkCoordIntPair; +import net.minecraft.server.v1_9_R1.DataWatcher; import net.minecraft.server.v1_9_R1.IBlockData; import net.minecraft.server.v1_9_R1.IChatBaseComponent; import net.minecraft.server.v1_9_R1.IChatBaseComponent.ChatSerializer; @@ -124,11 +125,10 @@ public class MinecraftReflectionTest { assertEquals(NBTCompressedStreamTools.class, MinecraftReflection.getNbtCompressedStreamToolsClass()); } - // TODO Fix this - /*@Test - public void testWatchableObject() { - assertEquals(WatchableObject.class, MinecraftReflection.getWatchableObjectClass()); - }*/ + @Test + public void testDataWatcherItem() { + assertEquals(DataWatcher.Item.class, MinecraftReflection.getDataWatcherItemClass()); + } @Test public void testItemStacks() { diff --git a/modules/ProtocolLib/src/test/java/com/comphenix/protocol/utility/StreamSerializerTest.java b/modules/ProtocolLib/src/test/java/com/comphenix/protocol/utility/StreamSerializerTest.java index 35fe65b2..4b60d64b 100644 --- a/modules/ProtocolLib/src/test/java/com/comphenix/protocol/utility/StreamSerializerTest.java +++ b/modules/ProtocolLib/src/test/java/com/comphenix/protocol/utility/StreamSerializerTest.java @@ -42,7 +42,7 @@ public class StreamSerializerTest { @Test public void testStrings() throws IOException { StreamSerializer serializer = new StreamSerializer(); - String initial = "Hello - this is a ��� test."; + String initial = "Hello - this is a test."; // Buffer ByteArrayOutputStream buffer = new ByteArrayOutputStream(); diff --git a/modules/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedBlockDataTest.java b/modules/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedBlockDataTest.java index fb4a1474..df54aaee 100644 --- a/modules/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedBlockDataTest.java +++ b/modules/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedBlockDataTest.java @@ -20,6 +20,8 @@ import static org.junit.Assert.assertEquals; import org.bukkit.DyeColor; import org.bukkit.Material; +import org.junit.BeforeClass; +import org.junit.Test; import com.comphenix.protocol.BukkitInitialization; import com.comphenix.protocol.utility.MinecraftReflection; @@ -30,12 +32,12 @@ import com.comphenix.protocol.utility.MinecraftReflection; public class WrappedBlockDataTest { - //@BeforeClass - public static void initializeBukkit() { - BukkitInitialization.initializePackage(); + @BeforeClass + public static void initializeBukkit() throws IllegalAccessException { + BukkitInitialization.initializeItemMeta(); } - //@Test + @Test public void test() { Material type = Material.WOOL; int data = DyeColor.BLUE.getWoolData();