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.
This commit is contained in:
Andreas Troelsen 2021-07-30 01:27:35 +02:00
parent b4cd509eff
commit 15698d3eee
1 changed files with 11 additions and 1 deletions

View File

@ -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;