From 7e87c90b8d08e66b273471bd752de4879f39e436 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Tue, 18 Sep 2018 17:02:19 -0400 Subject: [PATCH] Add method to create wrapped block data from the spigot api Fixes #525 --- .../protocol/wrappers/WrappedBlockData.java | 29 ++++++++++++++++- .../wrappers/WrappedBlockDataTest.java | 31 +++++++++++++++---- 2 files changed, 53 insertions(+), 7 deletions(-) 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 3f07ff93..2a584df3 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 @@ -26,6 +26,7 @@ import com.comphenix.protocol.utility.MinecraftReflection; import com.comphenix.protocol.utility.MinecraftVersion; import org.bukkit.Material; +import org.bukkit.block.data.BlockData; /** * Represents a wrapper around IBlockData. @@ -45,6 +46,7 @@ public abstract class WrappedBlockData extends AbstractWrapper implements Clonab private static MethodAccessor BLOCK_FROM_MATERIAL; private static MethodAccessor GET_BLOCK_DATA; private static MethodAccessor FROM_LEGACY_DATA; + private static MethodAccessor GET_HANDLE; static { if (MinecraftVersion.atOrAbove(MinecraftVersion.AQUATIC_UPDATE)) { @@ -98,6 +100,15 @@ public abstract class WrappedBlockData extends AbstractWrapper implements Clonab .returnTypeExact(IBLOCK_DATA) .build(); GET_BLOCK_DATA = Accessors.getMethodAccessor(fuzzy.getMethod(contract)); + + fuzzy = FuzzyReflection.fromClass(MinecraftReflection.getCraftBukkitClass("block.data.CraftBlockData")); + contract = FuzzyMethodContract + .newBuilder() + .banModifier(Modifier.STATIC) + .parameterCount(0) + .returnTypeExact(IBLOCK_DATA) + .build(); + GET_HANDLE = Accessors.getMethodAccessor(fuzzy.getMethod(contract)); } } @@ -145,6 +156,10 @@ public abstract class WrappedBlockData extends AbstractWrapper implements Clonab private static WrappedBlockData createNewData(Material material, int data) { return new NewBlockData(FROM_LEGACY_DATA.invoke(null, material, (byte) data)); } + + private static WrappedBlockData createNewData(BlockData data) { + return new NewBlockData(GET_HANDLE.invoke(data)); + } } private static class OldBlockData extends WrappedBlockData { @@ -304,6 +319,15 @@ public abstract class WrappedBlockData extends AbstractWrapper implements Clonab : new OldBlockData(handle); } + /** + * Creates a new Wrapped Block Data instance from a given Spigot Block Data + * @param data Spigot block data + * @return The new Wrapped Block Data + */ + public static WrappedBlockData createData(BlockData data) { + return NewBlockData.createNewData(data); + } + @Override public String toString() { return "WrappedBlockData[handle=" + handle + "]"; @@ -320,9 +344,12 @@ public abstract class WrappedBlockData extends AbstractWrapper implements Clonab @Override public boolean equals(Object o) { + if (o == this) return true; + if (o instanceof WrappedBlockData) { WrappedBlockData that = (WrappedBlockData) o; - return this.getType() == that.getType() && getData() == that.getData(); + return this.handle.equals(that.handle) + || (this.getType() == that.getType() && this.getData() == that.getData()); } return false; 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 79b882b7..cb893a10 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 @@ -16,15 +16,20 @@ */ package com.comphenix.protocol.wrappers; -import static org.junit.Assert.assertEquals; +import com.comphenix.protocol.BukkitInitialization; + +import net.minecraft.server.v1_13_R2.IBlockData; -import org.bukkit.DyeColor; 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.junit.BeforeClass; import org.junit.Test; -import com.comphenix.protocol.BukkitInitialization; -import com.comphenix.protocol.utility.MinecraftReflection; +import static org.junit.Assert.assertEquals; /** * @author dmulloy2 @@ -33,12 +38,12 @@ import com.comphenix.protocol.utility.MinecraftReflection; public class WrappedBlockDataTest { @BeforeClass - public static void initializeBukkit() throws IllegalAccessException { + public static void initializeBukkit() { BukkitInitialization.initializeItemMeta(); } @Test - public void test() { + public void testMaterialCreation() { Material type = Material.BLUE_WOOL; WrappedBlockData wrapper = WrappedBlockData.createData(type); @@ -52,4 +57,18 @@ public class WrappedBlockDataTest { assertEquals(wrapper.getType(), back.getType()); assertEquals(wrapper.getData(), back.getData()); } + + @Test + public void testDataCreation() { + IBlockData nmsData = CraftMagicNumbers.getBlock(Material.CYAN_STAINED_GLASS_PANE).getBlockData(); + GlassPane data = (GlassPane) CraftBlockData.fromData(nmsData); + data.setFace(BlockFace.EAST, true); + + WrappedBlockData wrapper = WrappedBlockData.createData(data); + assertEquals(wrapper.getType(), Material.CYAN_STAINED_GLASS_PANE); + + GlassPane back = new CraftGlassPane((IBlockData) wrapper.getHandle()); + assertEquals(back.hasFace(BlockFace.EAST), data.hasFace(BlockFace.EAST)); + assertEquals(back.hasFace(BlockFace.SOUTH), data.hasFace(BlockFace.SOUTH)); + } }