From 15698d3eee91cace92db681f6fd75f8585eac1ea Mon Sep 17 00:00:00 2001 From: Andreas Troelsen Date: Fri, 30 Jul 2021 01:27:35 +0200 Subject: [PATCH] Use block data to check if blocks are signs. Instead of relying on the evolving Material enum values, we can use the new BlockData API as advised by some of the folks "in the know". It is unclear how much of a performance impact the the "block data gathering" and `instanceof` checks incur, but this is a pretty secluded place in the code base, so probably nothing to worry about. An alternative solution could have been to check if the _name_ of the Material equals "SIGN" or "WALL_SIGN", or ends with "_SIGN". That should cover all cases in a sorta kinda safe manner, but it isn't as resilient as the BlockData/BlockState hierarchies. We could also employ the new Materials utility class and enumerate all sign types by name and just check for membership of the resulting set, but this creates another brittle crash point. --- .../com/garbagemule/MobArena/util/ClassChests.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/garbagemule/MobArena/util/ClassChests.java b/src/main/java/com/garbagemule/MobArena/util/ClassChests.java index 6561f0b..4d43c78 100644 --- a/src/main/java/com/garbagemule/MobArena/util/ClassChests.java +++ b/src/main/java/com/garbagemule/MobArena/util/ClassChests.java @@ -59,7 +59,7 @@ public class ClassChests { Block blockBehind = blockBelow.getRelative(backwards); // If the block below this sign is a class sign, swap the order - if (blockBelow.getType() == Material.OAK_WALL_SIGN || blockBelow.getType() == Material.OAK_SIGN) { + if (isSign(blockBelow)) { String className = ChatColor.stripColor(((Sign) blockBelow.getState()).getLine(0)).toLowerCase(); if (arena.getClasses().containsKey(className)) { blockSign = blockBehind; // Use blockSign as a temp while swapping @@ -85,6 +85,16 @@ public class ClassChests { return true; } + private static boolean isSign(Block block) { + // Because we're using org.bukkit.block.Sign for the block state stuff + // already, we can't import the block data type, so instead of having + // an awkard if-statement, we tuck the expression away in this sort of + // elegant helper method. + BlockData data = block.getBlockData(); + return data instanceof org.bukkit.block.data.type.Sign + || data instanceof org.bukkit.block.data.type.WallSign; + } + private static Block findChestBelow(Block b, int left) { if (left < 0) { return null;