From d2824625472383d08a678768e3e8f181ffbc2e3f Mon Sep 17 00:00:00 2001 From: md678685 Date: Wed, 24 Apr 2019 14:28:15 +0100 Subject: [PATCH] Use MaterialUtil to check signs --- .../essentials/signs/EssentialsSign.java | 6 ++--- .../essentials/signs/SignBlockListener.java | 17 ++++++------- .../essentials/signs/SignEntityListener.java | 6 ++--- .../essentials/signs/SignProtection.java | 2 +- .../essentials/utils/MaterialUtil.java | 25 ++++++++++++++++--- 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java b/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java index 46dcb4e0c..59e4eba01 100644 --- a/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java +++ b/Essentials/src/com/earth2me/essentials/signs/EssentialsSign.java @@ -2,6 +2,7 @@ package com.earth2me.essentials.signs; import com.earth2me.essentials.*; import com.earth2me.essentials.utils.EnumUtil; +import com.earth2me.essentials.utils.MaterialUtil; import com.earth2me.essentials.utils.NumberUtil; import net.ess3.api.IEssentials; import net.ess3.api.MaxMoneyException; @@ -26,7 +27,6 @@ import static com.earth2me.essentials.I18n.tl; public class EssentialsSign { - private static final Material SIGN_POST = EnumUtil.getMaterial("SIGN", "SIGN_POST"); private static final Set EMPTY_SET = new HashSet(); protected static final BigDecimal MINTRANSACTION = new BigDecimal("0.01"); protected transient final String signName; @@ -206,13 +206,13 @@ public class EssentialsSign { protected static boolean checkIfBlockBreaksSigns(final Block block) { final Block sign = block.getRelative(BlockFace.UP); - if (sign.getType() == SIGN_POST && isValidSign(new BlockSign(sign))) { + if (MaterialUtil.isSignPost(sign.getType()) && isValidSign(new BlockSign(sign))) { return true; } final BlockFace[] directions = new BlockFace[]{BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST}; for (BlockFace blockFace : directions) { final Block signblock = block.getRelative(blockFace); - if (signblock.getType() == Material.WALL_SIGN) { + if (MaterialUtil.isWallSign(signblock.getType())) { try { final org.bukkit.material.Sign signMat = (org.bukkit.material.Sign) signblock.getState().getData(); if (signMat != null && signMat.getFacing() == blockFace && isValidSign(new BlockSign(signblock))) { diff --git a/Essentials/src/com/earth2me/essentials/signs/SignBlockListener.java b/Essentials/src/com/earth2me/essentials/signs/SignBlockListener.java index 89b027a1b..c1fdbfa9e 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignBlockListener.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignBlockListener.java @@ -4,6 +4,7 @@ import com.earth2me.essentials.I18n; import com.earth2me.essentials.User; import com.earth2me.essentials.utils.EnumUtil; import com.earth2me.essentials.utils.FormatUtil; +import com.earth2me.essentials.utils.MaterialUtil; import net.ess3.api.IEssentials; import net.ess3.api.MaxMoneyException; import org.bukkit.ChatColor; @@ -22,8 +23,6 @@ import java.util.logging.Logger; public class SignBlockListener implements Listener { private static final Logger LOGGER = Logger.getLogger("Essentials"); - private static final Material WALL_SIGN = Material.WALL_SIGN; - private static final Material SIGN_POST = EnumUtil.getMaterial("SIGN", "SIGN_POST"); private final transient IEssentials ess; public SignBlockListener(IEssentials ess) { @@ -55,7 +54,7 @@ public class SignBlockListener implements Listener { } final Material mat = block.getType(); - if (mat == SIGN_POST || mat == WALL_SIGN) { + if (MaterialUtil.isSign(mat)) { final Sign csign = (Sign) block.getState(); for (EssentialsSign sign : ess.getSettings().enabledSigns()) { @@ -143,12 +142,12 @@ public class SignBlockListener implements Listener { } final Block against = event.getBlockAgainst(); - if ((against.getType() == WALL_SIGN || against.getType() == SIGN_POST) && EssentialsSign.isValidSign(ess, new EssentialsSign.BlockSign(against))) { + if (MaterialUtil.isSign(against.getType()) && EssentialsSign.isValidSign(ess, new EssentialsSign.BlockSign(against))) { event.setCancelled(true); return; } final Block block = event.getBlock(); - if (block.getType() == WALL_SIGN || block.getType() == SIGN_POST) { + if (MaterialUtil.isSign(block.getType())) { return; } for (EssentialsSign sign : ess.getSettings().enabledSigns()) { @@ -167,7 +166,7 @@ public class SignBlockListener implements Listener { } final Block block = event.getBlock(); - if (((block.getType() == WALL_SIGN || block.getType() == SIGN_POST) && EssentialsSign.isValidSign(ess, new EssentialsSign.BlockSign(block))) || EssentialsSign.checkIfBlockBreaksSigns(block)) { + if ((MaterialUtil.isSign(block.getType()) && EssentialsSign.isValidSign(ess, new EssentialsSign.BlockSign(block))) || EssentialsSign.checkIfBlockBreaksSigns(block)) { event.setCancelled(true); return; } @@ -187,7 +186,7 @@ public class SignBlockListener implements Listener { } final Block block = event.getBlock(); - if (((block.getType() == WALL_SIGN || block.getType() == SIGN_POST) && EssentialsSign.isValidSign(ess, new EssentialsSign.BlockSign(block))) || EssentialsSign.checkIfBlockBreaksSigns(block)) { + if ((MaterialUtil.isSign(block.getType()) && EssentialsSign.isValidSign(ess, new EssentialsSign.BlockSign(block))) || EssentialsSign.checkIfBlockBreaksSigns(block)) { event.setCancelled(true); return; } @@ -207,7 +206,7 @@ public class SignBlockListener implements Listener { } for (Block block : event.getBlocks()) { - if (((block.getType() == WALL_SIGN || block.getType() == SIGN_POST) && EssentialsSign.isValidSign(ess, new EssentialsSign.BlockSign(block))) || EssentialsSign.checkIfBlockBreaksSigns(block)) { + if ((MaterialUtil.isSign(block.getType()) && EssentialsSign.isValidSign(ess, new EssentialsSign.BlockSign(block))) || EssentialsSign.checkIfBlockBreaksSigns(block)) { event.setCancelled(true); return; } @@ -232,7 +231,7 @@ public class SignBlockListener implements Listener { final Block[] affectedBlocks = new Block[]{pistonBaseBlock, pistonBaseBlock.getRelative(event.getDirection()), event.getRetractLocation().getBlock()}; for (Block block : affectedBlocks) { - if (((block.getType() == WALL_SIGN || block.getType() == SIGN_POST) && EssentialsSign.isValidSign(ess, new EssentialsSign.BlockSign(block))) || EssentialsSign.checkIfBlockBreaksSigns(block)) { + if ((MaterialUtil.isSign(block.getType()) && EssentialsSign.isValidSign(ess, new EssentialsSign.BlockSign(block))) || EssentialsSign.checkIfBlockBreaksSigns(block)) { event.setCancelled(true); return; } diff --git a/Essentials/src/com/earth2me/essentials/signs/SignEntityListener.java b/Essentials/src/com/earth2me/essentials/signs/SignEntityListener.java index 7b8670200..08544ba99 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignEntityListener.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignEntityListener.java @@ -1,6 +1,7 @@ package com.earth2me.essentials.signs; import com.earth2me.essentials.utils.EnumUtil; +import com.earth2me.essentials.utils.MaterialUtil; import net.ess3.api.IEssentials; import org.bukkit.Material; import org.bukkit.block.Block; @@ -13,7 +14,6 @@ import org.bukkit.event.entity.EntityExplodeEvent; public class SignEntityListener implements Listener { - private static final Material SIGN_POST = EnumUtil.getMaterial("SIGN", "SIGN_POST"); private final transient IEssentials ess; public SignEntityListener(final IEssentials ess) { @@ -28,7 +28,7 @@ public class SignEntityListener implements Listener { } for (Block block : event.blockList()) { - if (((block.getType() == Material.WALL_SIGN || block.getType() == SIGN_POST) && EssentialsSign.isValidSign(ess, new EssentialsSign.BlockSign(block))) || EssentialsSign.checkIfBlockBreaksSigns(block)) { + if ((MaterialUtil.isSign(block.getType()) && EssentialsSign.isValidSign(ess, new EssentialsSign.BlockSign(block))) || EssentialsSign.checkIfBlockBreaksSigns(block)) { event.setCancelled(true); return; } @@ -49,7 +49,7 @@ public class SignEntityListener implements Listener { } final Block block = event.getBlock(); - if (((block.getType() == Material.WALL_SIGN || block.getType() == SIGN_POST) && EssentialsSign.isValidSign(ess, new EssentialsSign.BlockSign(block))) || EssentialsSign.checkIfBlockBreaksSigns(block)) { + if ((MaterialUtil.isSign(block.getType()) && EssentialsSign.isValidSign(ess, new EssentialsSign.BlockSign(block))) || EssentialsSign.checkIfBlockBreaksSigns(block)) { event.setCancelled(true); return; } diff --git a/Essentials/src/com/earth2me/essentials/signs/SignProtection.java b/Essentials/src/com/earth2me/essentials/signs/SignProtection.java index 9368ae60e..d36f199d1 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignProtection.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignProtection.java @@ -75,7 +75,7 @@ public class SignProtection extends EssentialsSign { final Block sign = entry.getKey().getBlock(); if (!hasAdjacentBlock(sign, block)) { block.setType(Material.AIR); - final Trade trade = new Trade(new ItemStack(Material.SIGN, 1), ess); + final Trade trade = new Trade(new ItemStack(sign.getType(), 1), ess); trade.pay(player, OverflowType.DROP); } } diff --git a/Essentials/src/com/earth2me/essentials/utils/MaterialUtil.java b/Essentials/src/com/earth2me/essentials/utils/MaterialUtil.java index 02b44d54a..7504605f1 100644 --- a/Essentials/src/com/earth2me/essentials/utils/MaterialUtil.java +++ b/Essentials/src/com/earth2me/essentials/utils/MaterialUtil.java @@ -3,6 +3,7 @@ package com.earth2me.essentials.utils; import org.bukkit.Bukkit; import org.bukkit.DyeColor; import org.bukkit.Material; +import org.bukkit.Registry; import org.bukkit.material.MaterialData; import java.util.EnumSet; @@ -21,7 +22,8 @@ public class MaterialUtil { // includes TIPPED_ARROW which also has potion effects private static final Set PLAYER_HEADS; private static final Set POTIONS; - private static final Set SIGNS; + private static final Set SIGN_POSTS; + private static final Set WALL_SIGNS; public static final Material SPAWNER = EnumUtil.getMaterial("MOB_SPAWNER", "SPAWNER"); @@ -55,7 +57,14 @@ public class MaterialUtil { POTIONS = EnumUtil.getAllMatching(Material.class, "POTION", "SPLASH_POTION", "LINGERING_POTION", "TIPPED_ARROW"); - SIGNS = EnumUtil.getAllMatching(Material.class, "SIGN", "SIGN_POST", "WALL_SIGN"); + SIGN_POSTS = EnumUtil.getAllMatching(Material.class, "SIGN", "SIGN_POST", + "ACACIA_SIGN", "BIRCH_SIGN", + "DARK_OAK_SIGN", "JUNGLE_SIGN", + "OAK_SIGN", "SPRUCE_SIGN"); + + WALL_SIGNS = EnumUtil.getAllMatching(Material.class, "WALL_SIGN", + "ACACIA_WALL_SIGN", "BIRCH_WALL_SIGN", "DARK_OAK_WALL_SIGN", "JUNGLE_WALL_SIGN", + "OAK_WALL_SIGN", "SPRUCE_WALL_SIGN"); } public static boolean isBed(Material material) { @@ -94,8 +103,16 @@ public class MaterialUtil { return POTIONS.contains(material); } + public static boolean isSignPost(Material material) { + return SIGN_POSTS.contains(material); + } + + public static boolean isWallSign(Material material) { + return WALL_SIGNS.contains(material); + } + public static boolean isSign(Material material) { - return SIGNS.contains(material); + return isSignPost(material) || isWallSign(material); } public static boolean isSkull(Material material) { @@ -108,7 +125,7 @@ public class MaterialUtil { try { return Bukkit.getUnsafe().fromLegacy(new MaterialData(material, damage)); } catch (NoSuchMethodError error) { - return material; + return null; } } }