From 7ad3a1f4681bdbeae1d53fa9e7177d9e3f60419a Mon Sep 17 00:00:00 2001 From: md_5 Date: Tue, 9 Jul 2019 10:47:07 +1000 Subject: [PATCH] SPIGOT-5146: BlockDataMeta does not work --- nms-patches/ItemBlock.patch | 42 +++++++++++++++++-- .../block/data/CraftBlockData.java | 13 +++++- .../craftbukkit/inventory/CraftMetaItem.java | 17 ++++---- 3 files changed, 58 insertions(+), 14 deletions(-) diff --git a/nms-patches/ItemBlock.patch b/nms-patches/ItemBlock.patch index e5a3fcf6f2..12c5ed7c7a 100644 --- a/nms-patches/ItemBlock.patch +++ b/nms-patches/ItemBlock.patch @@ -21,18 +21,52 @@ itemstack.subtract(1); return EnumInteractionResult.SUCCESS; } -@@ -120,8 +125,15 @@ +@@ -89,6 +94,21 @@ + + if (nbttagcompound != null) { + NBTTagCompound nbttagcompound1 = nbttagcompound.getCompound("BlockStateTag"); ++ // CraftBukkit start ++ iblockdata1 = getBlockState(iblockdata1, nbttagcompound1); ++ } ++ ++ if (iblockdata1 != iblockdata) { ++ world.setTypeAndData(blockposition, iblockdata1, 2); ++ } ++ ++ return iblockdata1; ++ } ++ ++ public static IBlockData getBlockState(IBlockData iblockdata, NBTTagCompound nbttagcompound1) { ++ IBlockData iblockdata1 = iblockdata; ++ { ++ // CraftBukkit end + BlockStateList blockstatelist = iblockdata.getBlock().getStates(); + Iterator iterator = nbttagcompound1.getKeys().iterator(); + +@@ -103,11 +123,6 @@ + } + } + } +- +- if (iblockdata1 != iblockdata) { +- world.setTypeAndData(blockposition, iblockdata1, 2); +- } +- + return iblockdata1; + } + +@@ -120,8 +135,15 @@ protected boolean b(BlockActionContext blockactioncontext, IBlockData iblockdata) { EntityHuman entityhuman = blockactioncontext.getEntity(); VoxelShapeCollision voxelshapecollision = entityhuman == null ? VoxelShapeCollision.a() : VoxelShapeCollision.a((Entity) entityhuman); + // CraftBukkit start - store default return + boolean defaultReturn = (!this.d() || iblockdata.canPlace(blockactioncontext.getWorld(), blockactioncontext.getClickPosition())) && blockactioncontext.getWorld().a(iblockdata, blockactioncontext.getClickPosition(), voxelshapecollision); + org.bukkit.entity.Player player = (blockactioncontext.getEntity() instanceof EntityPlayer) ? (org.bukkit.entity.Player) blockactioncontext.getEntity().getBukkitEntity() : null; - -- return (!this.d() || iblockdata.canPlace(blockactioncontext.getWorld(), blockactioncontext.getClickPosition())) && blockactioncontext.getWorld().a(iblockdata, blockactioncontext.getClickPosition(), voxelshapecollision); ++ + BlockCanBuildEvent event = new BlockCanBuildEvent(CraftBlock.at(blockactioncontext.getWorld(), blockactioncontext.getClickPosition()), player, CraftBlockData.fromData(iblockdata), defaultReturn); + blockactioncontext.getWorld().getServer().getPluginManager().callEvent(event); -+ + +- return (!this.d() || iblockdata.canPlace(blockactioncontext.getWorld(), blockactioncontext.getClickPosition())) && blockactioncontext.getWorld().a(iblockdata, blockactioncontext.getClickPosition(), voxelshapecollision); + return event.isBuildable(); + // CraftBukkit end } diff --git a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java index c1666a9baf..49c9975cf6 100644 --- a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java +++ b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java @@ -22,6 +22,7 @@ import net.minecraft.server.IBlockData; import net.minecraft.server.IBlockState; import net.minecraft.server.INamable; import net.minecraft.server.IRegistry; +import net.minecraft.server.NBTTagCompound; import org.bukkit.Material; import org.bukkit.block.BlockFace; import org.bukkit.block.data.BlockData; @@ -275,8 +276,16 @@ public class CraftBlockData implements BlockData { return stateString.toString(); } - public String toStates() { - return ((BlockDataAbstract) state).getStateMap().entrySet().stream().map(BlockDataAbstract.STATE_TO_VALUE).collect(Collectors.joining(",")).toString(); + public NBTTagCompound toStates() { + NBTTagCompound compound = new NBTTagCompound(); + + for (Map.Entry, Comparable> entry : state.getStateMap().entrySet()) { + IBlockState iblockstate = (IBlockState) entry.getKey(); + + compound.setString(iblockstate.a(), iblockstate.a(entry.getValue())); + } + + return compound; } @Override diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java index 2a513bd96f..d13a9f7293 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java @@ -41,6 +41,7 @@ import net.minecraft.server.EnumChatFormat; import net.minecraft.server.EnumItemSlot; import net.minecraft.server.GenericAttributes; import net.minecraft.server.IChatBaseComponent; +import net.minecraft.server.ItemBlock; import net.minecraft.server.NBTBase; import net.minecraft.server.NBTCompressedStreamTools; import net.minecraft.server.NBTTagCompound; @@ -263,7 +264,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta { private IChatBaseComponent locName; private List lore; private Integer customModelData; - private String blockData; + private NBTTagCompound blockData; private Map enchantments; private Multimap attributeModifiers; private int repairCost; @@ -356,8 +357,8 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta { if (tag.hasKeyOfType(CUSTOM_MODEL_DATA.NBT, CraftMagicNumbers.NBT.TAG_INT)) { customModelData = tag.getInt(CUSTOM_MODEL_DATA.NBT); } - if (tag.hasKeyOfType(BLOCK_DATA.NBT, CraftMagicNumbers.NBT.TAG_STRING)) { - blockData = tag.getString(BLOCK_DATA.NBT); + if (tag.hasKeyOfType(BLOCK_DATA.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND)) { + blockData = tag.getCompound(BLOCK_DATA.NBT); } this.enchantments = buildEnchantments(tag, ENCHANTMENTS); @@ -484,9 +485,9 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta { setCustomModelData(customModelData); } - String blockData = SerializableMeta.getObject(String.class, map, BLOCK_DATA.BUKKIT, true); + Map blockData = SerializableMeta.getObject(Map.class, map, BLOCK_DATA.BUKKIT, true); if (blockData != null) { - this.blockData = blockData; + this.blockData = (NBTTagCompound) CraftNBTTagConfigSerializer.deserialize(blockData); } enchantments = buildEnchantments(map, ENCHANTMENTS); @@ -627,7 +628,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta { } if (hasBlockData()) { - itemTag.setString(BLOCK_DATA.NBT, blockData); + itemTag.set(BLOCK_DATA.NBT, blockData); } if (hideFlag != 0) { @@ -919,7 +920,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta { @Override public BlockData getBlockData(Material material) { - return CraftBlockData.newData(material, '[' + blockData + ']'); + return CraftBlockData.fromData(ItemBlock.getBlockState(CraftMagicNumbers.getBlock(material).getBlockData(), blockData)); } @Override @@ -1225,7 +1226,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta { builder.put(CUSTOM_MODEL_DATA.BUKKIT, customModelData); } if (hasBlockData()) { - builder.put(BLOCK_DATA.BUKKIT, blockData); + builder.put(BLOCK_DATA.BUKKIT, CraftNBTTagConfigSerializer.serialize(blockData)); } serializeEnchantments(enchantments, builder, ENCHANTMENTS);