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 d9258c54..cbf2693f 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedBlockData.java @@ -38,6 +38,7 @@ public class WrappedBlockData extends AbstractWrapper { private static final Class BLOCK = MinecraftReflection.getBlockClass(); private static MethodAccessor FROM_LEGACY_DATA = null; + private static MethodAccessor TO_LEGACY_DATA = null; private static MethodAccessor GET_NMS_BLOCK = null; private static MethodAccessor GET_BLOCK = null; @@ -50,6 +51,13 @@ public class WrappedBlockData extends AbstractWrapper { .build(); FROM_LEGACY_DATA = Accessors.getMethodAccessor(fuzzy.getMethod(contract)); + contract = FuzzyMethodContract.newBuilder() + .banModifier(Modifier.STATIC) + .parameterExactArray(IBLOCK_DATA) + .returnTypeExact(int.class) + .build(); + TO_LEGACY_DATA = Accessors.getMethodAccessor(fuzzy.getMethod(contract)); + fuzzy = FuzzyReflection.fromClass(MAGIC_NUMBERS); GET_NMS_BLOCK = Accessors.getMethodAccessor(fuzzy.getMethodByParameters("getBlock", BLOCK, new Class[] { Material.class })); @@ -73,6 +81,15 @@ public class WrappedBlockData extends AbstractWrapper { return BukkitConverters.getBlockConverter().getSpecific(block); } + /** + * Retrieves the data of this BlockData. + * @return The data of this BlockData. + */ + public int getData() { + Object block = GET_BLOCK.invoke(handle); + return (Integer) TO_LEGACY_DATA.invoke(block, handle); + } + /** * Sets the type of this BlockData. * @param type New type diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedBlockDataTest.java b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedBlockDataTest.java index 79148a00..e2269d6c 100644 --- a/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedBlockDataTest.java +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedBlockDataTest.java @@ -18,6 +18,7 @@ package com.comphenix.protocol.wrappers; import static org.junit.Assert.assertEquals; +import org.bukkit.DyeColor; import org.bukkit.Material; import org.junit.BeforeClass; import org.junit.Test; @@ -38,11 +39,18 @@ public class WrappedBlockDataTest { @Test public void test() { - Material type = Material.STONE; - WrappedBlockData data = WrappedBlockData.createData(type); - Object generic = BukkitConverters.getWrappedBlockDataConverter().getGeneric(MinecraftReflection.getIBlockDataClass(), data); + Material type = Material.WOOL; + int data = DyeColor.BLUE.getWoolData(); + + WrappedBlockData wrapper = WrappedBlockData.createData(type, data); + + assertEquals(wrapper.getType(), type); + assertEquals(wrapper.getData(), data); + + Object generic = BukkitConverters.getWrappedBlockDataConverter().getGeneric(MinecraftReflection.getIBlockDataClass(), wrapper); WrappedBlockData back = BukkitConverters.getWrappedBlockDataConverter().getSpecific(generic); - assertEquals(data.getType(), back.getType()); + assertEquals(wrapper.getType(), back.getType()); + assertEquals(wrapper.getData(), back.getData()); } } \ No newline at end of file