From c538f4a3dc05e46f4f51dea76b6323d553602c74 Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Tue, 30 Apr 2019 21:31:24 +0100 Subject: [PATCH] Remove all usage of old MaterialData and other outdated code This should make it more future-proof and possibly even work with 1.14 already. --- .../com/Acrobot/Breeze/Utils/BlockUtil.java | 6 +- .../Listeners/Block/Break/SignBreak.java | 58 +------------------ .../com/Acrobot/ChestShop/Utils/uBlock.java | 26 ++++----- 3 files changed, 17 insertions(+), 73 deletions(-) diff --git a/src/main/java/com/Acrobot/Breeze/Utils/BlockUtil.java b/src/main/java/com/Acrobot/Breeze/Utils/BlockUtil.java index dbc396e..8b8b390 100644 --- a/src/main/java/com/Acrobot/Breeze/Utils/BlockUtil.java +++ b/src/main/java/com/Acrobot/Breeze/Utils/BlockUtil.java @@ -1,12 +1,10 @@ package com.Acrobot.Breeze.Utils; -import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.DoubleChest; import org.bukkit.block.data.BlockData; import org.bukkit.block.data.Directional; -import org.bukkit.block.data.Rotatable; import org.bukkit.block.data.type.Sign; import org.bukkit.block.data.type.WallSign; import org.bukkit.entity.Player; @@ -24,8 +22,8 @@ public class BlockUtil { * @return Is this block a sign? */ public static boolean isSign(Block block) { - return block.getType() == Material.SIGN - || block.getType() == Material.WALL_SIGN; + BlockData data = block.getBlockData(); + return data instanceof Sign || data instanceof WallSign; } /** diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Block/Break/SignBreak.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Block/Break/SignBreak.java index c2d051b..b0aeaa5 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Block/Break/SignBreak.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Block/Break/SignBreak.java @@ -4,7 +4,6 @@ import com.Acrobot.Breeze.Utils.BlockUtil; import com.Acrobot.ChestShop.ChestShop; import com.Acrobot.ChestShop.Configuration.Properties; import com.Acrobot.ChestShop.Events.ShopDestroyedEvent; -import com.Acrobot.ChestShop.Permission; import com.Acrobot.ChestShop.Signs.ChestShopSign; import com.Acrobot.ChestShop.UUIDs.NameManager; import com.Acrobot.ChestShop.Utils.uBlock; @@ -20,8 +19,6 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.*; import org.bukkit.event.entity.EntityExplodeEvent; -import org.bukkit.material.Directional; -import org.bukkit.material.PistonBaseMaterial; import org.bukkit.metadata.FixedMetadataValue; import java.util.ArrayList; @@ -78,7 +75,7 @@ public class SignBreak implements Listener { @EventHandler(ignoreCancelled = true) public static void onBlockPistonExtend(BlockPistonExtendEvent event) { - for (Block block : getExtendBlocks(event)) { + for (Block block : event.getBlocks()) { if (!canBlockBeBroken(block, null)) { event.setCancelled(true); return; @@ -88,7 +85,7 @@ public class SignBreak implements Listener { @EventHandler(ignoreCancelled = true) public static void onBlockPistonRetract(BlockPistonRetractEvent event) { - for (Block block : getRetractBlocks(event)) { + for (Block block : event.getBlocks()) { if (!canBlockBeBroken(block, null)) { event.setCancelled(true); return; @@ -189,55 +186,4 @@ public class SignBreak implements Listener { return attachedSigns; } } - - private static List getRetractBlocks(BlockPistonRetractEvent event) { - try { - return event.getBlocks(); - } catch (NoSuchMethodError outdated) { // backwards compatiblity - List blocks = new ArrayList<>(); - Block block = getRetractLocationBlock(event); - if (block != null && !BlockUtil.isSign(block)) { - blocks.add(block); - } - return blocks; - } - } - - //Those are fixes for a very old CraftBukkit's piston bug, where piston appears not to be a piston. - private static BlockFace getPistonDirection(Block block) { - return block.getState().getData() instanceof PistonBaseMaterial ? ((Directional) block.getState().getData()).getFacing() : null; - } - - private static Block getRetractLocationBlock(BlockPistonRetractEvent event) { - BlockFace pistonDirection = getPistonDirection(event.getBlock()); - return pistonDirection != null ? event.getBlock().getRelative((pistonDirection), 2).getLocation().getBlock() : null; - } - - private static List getExtendBlocks(BlockPistonExtendEvent event) { - try { - return event.getBlocks(); - } catch (NoSuchMethodError outdated) { // backwards compatiblity - BlockFace pistonDirection = getPistonDirection(event.getBlock()); - - if (pistonDirection == null) { - return new ArrayList<>(); - } - - Block piston = event.getBlock(); - List pushedBlocks = new ArrayList<>(); - - for (int currentBlock = 1; currentBlock < event.getLength() + 1; currentBlock++) { - Block block = piston.getRelative(pistonDirection, currentBlock); - Material blockType = block.getType(); - - if (blockType == Material.AIR) { - break; - } - - pushedBlocks.add(block); - } - - return pushedBlocks; - } - } } diff --git a/src/main/java/com/Acrobot/ChestShop/Utils/uBlock.java b/src/main/java/com/Acrobot/ChestShop/Utils/uBlock.java index 980792d..4b39efe 100644 --- a/src/main/java/com/Acrobot/ChestShop/Utils/uBlock.java +++ b/src/main/java/com/Acrobot/ChestShop/Utils/uBlock.java @@ -11,8 +11,8 @@ import org.bukkit.block.Container; import org.bukkit.block.Sign; import org.bukkit.block.data.BlockData; import org.bukkit.block.data.type.Chest; +import org.bukkit.block.data.type.WallSign; import org.bukkit.inventory.InventoryHolder; -import org.bukkit.material.Attachable; /** * @author Acrobot @@ -46,8 +46,9 @@ public class uBlock { @Deprecated public static org.bukkit.block.Chest findConnectedChest(Sign sign) { BlockFace signFace = null; - if (((org.bukkit.material.Sign) sign.getData()).isWallSign()) { - signFace = ((Attachable) sign.getData()).getAttachedFace(); + BlockData data = sign.getBlockData(); + if (data instanceof WallSign) { + signFace = ((WallSign) data).getFacing().getOppositeFace(); } return findConnectedChest(sign.getBlock(), signFace); } @@ -59,9 +60,9 @@ public class uBlock { public static org.bukkit.block.Chest findConnectedChest(Block block) { BlockFace signFace = null; if (BlockUtil.isSign(block)) { - Sign sign = (Sign) block.getState(); - if (((org.bukkit.material.Sign) sign.getData()).isWallSign()) { - signFace = ((Attachable) sign.getData()).getAttachedFace(); + BlockData data = block.getBlockData(); + if (data instanceof WallSign) { + signFace = ((WallSign) data).getFacing().getOppositeFace(); } } return findConnectedChest(block, signFace); @@ -92,19 +93,18 @@ public class uBlock { public static Container findConnectedContainer(Sign sign) { BlockFace signFace = null; - if (((org.bukkit.material.Sign) sign.getData()).isWallSign()) { - signFace = ((Attachable) sign.getData()).getAttachedFace(); + BlockData data = sign.getBlockData(); + if (data instanceof WallSign) { + signFace = ((WallSign) data).getFacing().getOppositeFace(); } return findConnectedContainer(sign.getLocation(), signFace); } public static Container findConnectedContainer(Block block) { BlockFace signFace = null; - if (BlockUtil.isSign(block)) { - Sign sign = (Sign) block.getState(); - if (((org.bukkit.material.Sign) sign.getData()).isWallSign()) { - signFace = ((Attachable) sign.getData()).getAttachedFace(); - } + BlockData data = block.getBlockData(); + if (data instanceof WallSign) { + signFace = ((WallSign) data).getFacing().getOppositeFace(); } return findConnectedContainer(block.getLocation(), signFace); }