From 24d5b1739a3caa64aae3909b64b00bbbb393ad2a Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Mon, 15 Jun 2015 00:58:16 -0400 Subject: [PATCH] Fix the MultiBlockChangeInfo wrapper Also add a few tests to make sure it keeps working --- .../protocol/utility/MinecraftReflection.java | 2 +- .../wrappers/MultiBlockChangeInfo.java | 32 ++++++++-- .../protocol/wrappers/WrappedBlockData.java | 10 ++++ .../utility/MinecraftReflectionTest.java | 6 ++ .../wrappers/MultiBlockChangeTest.java | 59 +++++++++++++++++++ .../wrappers/WrappedAttributeTest.java | 13 +--- .../wrappers/WrappedBlockDataTest.java | 48 +++++++++++++++ 7 files changed, 153 insertions(+), 17 deletions(-) create mode 100644 ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/MultiBlockChangeTest.java create mode 100644 ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedBlockDataTest.java diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java index ba016e42..e8ea294a 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java @@ -1740,7 +1740,7 @@ public class MinecraftReflection { * @return The MultiBlockChangeInfo class */ public static Class getMultiBlockChangeInfoClass() { - return getMinecraftClass("PacketPlayOutMultiBlockChange$MultiBlockChangeInfo"); + return getMinecraftClass("MultiBlockChangeInfo", "PacketPlayOutMultiBlockChange$MultiBlockChangeInfo"); } /** diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/MultiBlockChangeInfo.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/MultiBlockChangeInfo.java index aad75998..f8724318 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/MultiBlockChangeInfo.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/MultiBlockChangeInfo.java @@ -1,5 +1,18 @@ /** - * (c) 2015 dmulloy2 + * ProtocolLib - Bukkit server library that allows access to the Minecraft protocol. + * Copyright (C) 2015 dmulloy2 + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program; + * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + * 02111-1307 USA */ package com.comphenix.protocol.wrappers; @@ -9,6 +22,7 @@ import java.lang.reflect.Constructor; import org.bukkit.Location; import org.bukkit.World; +import com.comphenix.protocol.PacketType; import com.comphenix.protocol.reflect.EquivalentConverter; import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.utility.MinecraftReflection; @@ -20,6 +34,8 @@ import com.comphenix.protocol.utility.MinecraftReflection; */ public class MultiBlockChangeInfo { + private static Constructor constructor; + private short location; private WrappedBlockData data; private ChunkCoordIntPair chunk; @@ -164,15 +180,19 @@ public class MultiBlockChangeInfo { @Override public Object getGeneric(Class genericType, MultiBlockChangeInfo specific) { try { - Constructor constructor = MinecraftReflection.getMultiBlockChangeInfoClass().getConstructor( - short.class, - MinecraftReflection.getIBlockDataClass() - ); + if (constructor == null) { + constructor = MinecraftReflection.getMultiBlockChangeInfoClass().getConstructor( + PacketType.Play.Server.MULTI_BLOCK_CHANGE.getPacketClass(), + short.class, + MinecraftReflection.getIBlockDataClass() + ); + } return constructor.newInstance( + null, specific.location, BukkitConverters.getWrappedBlockDataConverter().getGeneric(MinecraftReflection.getIBlockDataClass(), specific.data) - ); + ); } catch (Throwable ex) { throw new RuntimeException("Failed to construct MultiBlockChangeInfo instance.", ex); } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java index 5a1c525a..d9258c54 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java @@ -117,4 +117,14 @@ public class WrappedBlockData extends AbstractWrapper { public String toString() { return "WrappedBlockData[handle=" + handle + "]"; } + + @Override + public boolean equals(Object o) { + if (o instanceof WrappedBlockData) { + WrappedBlockData that = (WrappedBlockData) o; + return this.getType() == that.getType(); + } + + return false; + } } \ No newline at end of file diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/utility/MinecraftReflectionTest.java b/ProtocolLib/src/test/java/com/comphenix/protocol/utility/MinecraftReflectionTest.java index 1b6f08e7..3bc77fcb 100644 --- a/ProtocolLib/src/test/java/com/comphenix/protocol/utility/MinecraftReflectionTest.java +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/utility/MinecraftReflectionTest.java @@ -7,6 +7,7 @@ import static org.mockito.Mockito.verify; import net.minecraft.server.v1_8_R3.ChatComponentText; import net.minecraft.server.v1_8_R3.ChunkCoordIntPair; import net.minecraft.server.v1_8_R3.DataWatcher.WatchableObject; +import net.minecraft.server.v1_8_R3.IBlockData; import net.minecraft.server.v1_8_R3.IChatBaseComponent; import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer; import net.minecraft.server.v1_8_R3.NBTCompressedStreamTools; @@ -95,6 +96,11 @@ public class MinecraftReflectionTest { assertEquals(ChunkCoordIntPair.class, MinecraftReflection.getChunkCoordIntPair()); } + @Test + public void testIBlockData() { + assertEquals(IBlockData.class, MinecraftReflection.getIBlockDataClass()); + } + @Test public void testPlayerConnection() { assertEquals(PlayerConnection.class, MinecraftReflection.getPlayerConnectionClass()); diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/MultiBlockChangeTest.java b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/MultiBlockChangeTest.java new file mode 100644 index 00000000..3b259f3e --- /dev/null +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/MultiBlockChangeTest.java @@ -0,0 +1,59 @@ +/** + * ProtocolLib - Bukkit server library that allows access to the Minecraft protocol. + * Copyright (C) 2015 dmulloy2 + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program; + * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + * 02111-1307 USA + */ +package com.comphenix.protocol.wrappers; + +import static org.junit.Assert.assertEquals; + +import org.bukkit.Material; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.comphenix.protocol.BukkitInitialization; +import com.comphenix.protocol.utility.MinecraftReflection; + +/** + * @author dmulloy2 + */ + +public class MultiBlockChangeTest { + + @BeforeClass + public static void initializeBukkit() throws IllegalAccessException { + BukkitInitialization.initializePackage(); + } + + @Test + public void test() { + int x = 10; + int y = 128; + int z = 7; + + short location = (short) (x << 12 | z << 8 | y); + + ChunkCoordIntPair chunk = new ChunkCoordIntPair(1, 2); + WrappedBlockData blockData = WrappedBlockData.createData(Material.STONE); + MultiBlockChangeInfo info = new MultiBlockChangeInfo(location, blockData, chunk); + + Object generic = MultiBlockChangeInfo.getConverter(chunk).getGeneric(MinecraftReflection.getMultiBlockChangeInfoClass(), info); + MultiBlockChangeInfo back = MultiBlockChangeInfo.getConverter(chunk).getSpecific(generic); + + assertEquals(info.getX(), back.getX()); + assertEquals(info.getY(), back.getY()); + assertEquals(info.getZ(), back.getZ()); + assertEquals(info.getData(), back.getData()); + } +} \ No newline at end of file diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedAttributeTest.java b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedAttributeTest.java index 181bbbf2..6e5e96d2 100644 --- a/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedAttributeTest.java +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedAttributeTest.java @@ -4,7 +4,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertTrue; -import java.util.Collection; import java.util.List; import net.minecraft.server.v1_8_R3.AttributeModifier; @@ -86,16 +85,10 @@ public class WrappedAttributeTest { modifiers.add((AttributeModifier) wrapper.getHandle()); } - class PacketAccessor extends PacketPlayOutUpdateAttributes { - - public AttributeSnapshot attributeSnapshot(String string, double d, Collection coll) { - return new AttributeSnapshot(string, d, coll); - } - } - - return new PacketAccessor().attributeSnapshot(attribute.getAttributeKey(), attribute.getBaseValue(), modifiers); + PacketPlayOutUpdateAttributes accessor = new PacketPlayOutUpdateAttributes(); + return accessor.new AttributeSnapshot(attribute.getAttributeKey(), attribute.getBaseValue(), modifiers); } - + private AttributeModifier getModifierCopy(WrappedAttributeModifier modifier) { return new AttributeModifier(modifier.getUUID(), modifier.getName(), modifier.getAmount(), modifier.getOperation().getId()); } diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedBlockDataTest.java b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedBlockDataTest.java new file mode 100644 index 00000000..2b5e8320 --- /dev/null +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedBlockDataTest.java @@ -0,0 +1,48 @@ +/** + * ProtocolLib - Bukkit server library that allows access to the Minecraft protocol. + * Copyright (C) 2015 dmulloy2 + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program; + * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + * 02111-1307 USA + */ +package com.comphenix.protocol.wrappers; + +import static org.junit.Assert.assertEquals; + +import org.bukkit.Material; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.comphenix.protocol.BukkitInitialization; +import com.comphenix.protocol.utility.MinecraftReflection; + +/** + * @author dmulloy2 + */ + +public class WrappedBlockDataTest { + + @BeforeClass + public static void initializeBukkit() throws IllegalAccessException { + BukkitInitialization.initializePackage(); + } + + @Test + public void test() { + Material type = Material.STONE; + WrappedBlockData data = WrappedBlockData.createData(type); + Object generic = BukkitConverters.getWrappedBlockDataConverter().getGeneric(MinecraftReflection.getIBlockDataClass(), data); + WrappedBlockData back = BukkitConverters.getWrappedBlockDataConverter().getSpecific(generic); + + assertEquals(data.getType(), back.getType()); + } +} \ No newline at end of file