From 160a9074c495f45c5ecb7f670d282bf2c36be41e Mon Sep 17 00:00:00 2001 From: Ilya Date: Mon, 14 Feb 2022 19:16:22 +0300 Subject: [PATCH] Fix 3-blocks chests, limits and hide class cast expressions --- .../extendedpistons/ExtendedPistons.java | 2 +- .../redguy/extendedpistons/WorldListener.java | 154 ++++++++++-------- 2 files changed, 90 insertions(+), 66 deletions(-) diff --git a/src/main/java/ru/redguy/extendedpistons/ExtendedPistons.java b/src/main/java/ru/redguy/extendedpistons/ExtendedPistons.java index c8a45b1..4e5c30a 100644 --- a/src/main/java/ru/redguy/extendedpistons/ExtendedPistons.java +++ b/src/main/java/ru/redguy/extendedpistons/ExtendedPistons.java @@ -1,4 +1,4 @@ -package ru.redguy.extendedpistons; //TODO: slime sides chests move, refactor +package ru.redguy.extendedpistons; import net.milkbowl.vault.permission.Permission; import org.bukkit.Bukkit; diff --git a/src/main/java/ru/redguy/extendedpistons/WorldListener.java b/src/main/java/ru/redguy/extendedpistons/WorldListener.java index e0534f3..9eb1ee2 100644 --- a/src/main/java/ru/redguy/extendedpistons/WorldListener.java +++ b/src/main/java/ru/redguy/extendedpistons/WorldListener.java @@ -63,7 +63,12 @@ public class WorldListener implements Listener { Block block = e.getBlock().getRelative(face); BlockState state = states.get(face); if (state.getType().equals(Material.PISTON_STICKY_BASE)) { - PistonBaseMaterial piston = (PistonBaseMaterial) state.getData(); + PistonBaseMaterial piston; + try { + piston = (PistonBaseMaterial) state.getData(); + } catch (ClassCastException ex) { + continue; + } int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation()); PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(), block.getLocation(), piston.getFacing(), false, maxBlocks); @@ -81,7 +86,7 @@ public class WorldListener implements Listener { for (Block b : blocks) { Block b2 = b.getRelative(piston.getFacing().getOppositeFace()); b2.setType(b.getType()); - b2.setData(b.getData()); + b2.setData(b.getData()); //Deprecated but powerful if (b.getType().equals(Material.CHEST)) { Chest oldChest = (Chest) b.getState(); Chest newChest = (Chest) b2.getState(); @@ -133,7 +138,12 @@ public class WorldListener implements Listener { Block block = e.getBlock().getRelative(face); if (block.getType().equals(Material.PISTON_BASE) || block.getType().equals(Material.PISTON_STICKY_BASE)) { BlockState state = states.get(face); - PistonBaseMaterial piston = (PistonBaseMaterial) state.getData(); + PistonBaseMaterial piston; + try { + piston = (PistonBaseMaterial) state.getData(); + } catch (ClassCastException ex) { + continue; + } int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation()); PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(), block.getLocation(), piston.getFacing(), true, maxBlocks); @@ -143,35 +153,37 @@ public class WorldListener implements Listener { upper: for (Block b : blocks) { - Block b2 = b.getRelative(piston.getFacing()); - ArrayList faces = new ArrayList() {{ - add(BlockFace.NORTH); - add(BlockFace.SOUTH); - add(BlockFace.WEST); - add(BlockFace.EAST); - }}; - faces.remove(piston.getFacing().getOppositeFace()); - int chests = 0; - for (BlockFace blockFace : faces) { - Block b3 = b2.getRelative(blockFace); - if (b3.getType() == Material.CHEST) { - chests++; - Chest chest = (Chest) b3.getState(); - if (chest.getInventory() instanceof DoubleChestInventory) { - isMovable = false; - break upper; + if(b.getType().equals(Material.CHEST)) { + Block b2 = b.getRelative(piston.getFacing()); + ArrayList faces = new ArrayList() {{ + add(BlockFace.NORTH); + add(BlockFace.SOUTH); + add(BlockFace.WEST); + add(BlockFace.EAST); + }}; + faces.remove(piston.getFacing().getOppositeFace()); + int chests = 0; + for (BlockFace blockFace : faces) { + Block b3 = b2.getRelative(blockFace); + if (b3.getType() == Material.CHEST) { + chests++; + Chest chest = (Chest) b3.getState(); + if (chest.getInventory() instanceof DoubleChestInventory) { + isMovable = false; + break upper; + } } } - } - if (chests > 1) { - isMovable = false; - break; - } else if (chests == 1) { - if (b.getType() == Material.CHEST) { - Chest chest = (Chest) b.getState(); - if (chest.getInventory() instanceof DoubleChestInventory) { - isMovable = false; - break; + if (chests > 1) { + isMovable = false; + break; + } else if (chests == 1) { + if (b.getType() == Material.CHEST) { + Chest chest = (Chest) b.getState(); + if (chest.getInventory() instanceof DoubleChestInventory) { + isMovable = false; + break; + } } } } @@ -188,7 +200,7 @@ public class WorldListener implements Listener { for (Block b : blocks) { Block b2 = b.getRelative(piston.getFacing()); b2.setType(b.getType()); - b2.setData(b.getData()); + b2.setData(b.getData()); //Deprecated but powerful if (b.getType().equals(Material.CHEST)) { Chest oldChest = (Chest) b.getState(); Chest newChest = (Chest) b2.getState(); @@ -237,7 +249,12 @@ public class WorldListener implements Listener { Block block = e.getBlock().getRelative(face); BlockState state = states.get(face); if (state.getType().equals(Material.PISTON_STICKY_BASE)) { - PistonBaseMaterial piston = (PistonBaseMaterial) state.getData(); + PistonBaseMaterial piston; + try { + piston = (PistonBaseMaterial) state.getData(); + } catch (ClassCastException ex) { + continue; + } int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation()); PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(), block.getLocation(), piston.getFacing(), false, maxBlocks); @@ -255,7 +272,7 @@ public class WorldListener implements Listener { for (Block b : blocks) { Block b2 = b.getRelative(piston.getFacing().getOppositeFace()); b2.setType(b.getType()); - b2.setData(b.getData()); + b2.setData(b.getData()); //Deprecated but powerful if (b.getType().equals(Material.CHEST)) { Chest oldChest = (Chest) b.getState(); Chest newChest = (Chest) b2.getState(); @@ -294,7 +311,12 @@ public class WorldListener implements Listener { Block block = e.getBlock().getRelative(face); if (block.getType().equals(Material.PISTON_BASE) || block.getType().equals(Material.PISTON_STICKY_BASE)) { BlockState state = states.get(face); - PistonBaseMaterial piston = (PistonBaseMaterial) state.getData(); + PistonBaseMaterial piston; + try { + piston = (PistonBaseMaterial) state.getData(); + } catch (ClassCastException ex) { + continue; + } int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation()); PistonExtendsChecker checker = new PistonExtendsChecker(e.getBlock().getWorld(), block.getLocation(), piston.getFacing(), true, maxBlocks); @@ -304,35 +326,37 @@ public class WorldListener implements Listener { upper: for (Block b : blocks) { - Block b2 = b.getRelative(piston.getFacing()); - ArrayList faces = new ArrayList() {{ - add(BlockFace.NORTH); - add(BlockFace.SOUTH); - add(BlockFace.WEST); - add(BlockFace.EAST); - }}; - faces.remove(piston.getFacing().getOppositeFace()); - int chests = 0; - for (BlockFace blockFace : faces) { - Block b3 = b2.getRelative(blockFace); - if (b3.getType() == Material.CHEST) { - chests++; - Chest chest = (Chest) b3.getState(); - if (chest.getInventory() instanceof DoubleChestInventory) { - isMovable = false; - break upper; + if(b.getType().equals(Material.CHEST)) { + Block b2 = b.getRelative(piston.getFacing()); + ArrayList faces = new ArrayList() {{ + add(BlockFace.NORTH); + add(BlockFace.SOUTH); + add(BlockFace.WEST); + add(BlockFace.EAST); + }}; + faces.remove(piston.getFacing().getOppositeFace()); + int chests = 0; + for (BlockFace blockFace : faces) { + Block b3 = b2.getRelative(blockFace); + if (b3.getType() == Material.CHEST) { + chests++; + Chest chest = (Chest) b3.getState(); + if (chest.getInventory() instanceof DoubleChestInventory) { + isMovable = false; + break upper; + } } } - } - if (chests > 1) { - isMovable = false; - break; - } else if (chests == 1) { - if (b.getType() == Material.CHEST) { - Chest chest = (Chest) b.getState(); - if (chest.getInventory() instanceof DoubleChestInventory) { - isMovable = false; - break; + if (chests > 1) { + isMovable = false; + break; + } else if (chests == 1) { + if (b.getType() == Material.CHEST) { + Chest chest = (Chest) b.getState(); + if (chest.getInventory() instanceof DoubleChestInventory) { + isMovable = false; + break; + } } } } @@ -349,7 +373,7 @@ public class WorldListener implements Listener { for (Block b : blocks) { Block b2 = b.getRelative(piston.getFacing()); b2.setType(b.getType()); - b2.setData(b.getData()); + b2.setData(b.getData()); //Deprecated but powerful if (b.getType().equals(Material.CHEST)) { Chest oldChest = (Chest) b.getState(); Chest newChest = (Chest) b2.getState(); @@ -374,9 +398,9 @@ public class WorldListener implements Listener { Optional op = Bukkit.getOnlinePlayers().stream().min(Comparator.comparingDouble(p -> p.getLocation().distance(location))); if (op.isPresent()) { String[] groups = ExtendedPistons.INSTANCE.permsService.getPlayerGroups(op.get()); - for (String group : groups) { - if (ExtendedPistons.INSTANCE.conf.isSet("piston-move-limitations." + group) && ExtendedPistons.INSTANCE.conf.isInt("piston-move-limitations." + group)) - return ExtendedPistons.INSTANCE.conf.getInt("piston-move-limitations." + group); + Optional intOp = Arrays.stream(groups).map(group -> (ExtendedPistons.INSTANCE.conf.isSet("piston-move-limitations." + group) && ExtendedPistons.INSTANCE.conf.isInt("piston-move-limitations." + group))?ExtendedPistons.INSTANCE.conf.getInt("piston-move-limitations." + group):0).max(Comparator.comparingInt(i -> i)); + if(intOp.isPresent()) { + return intOp.get()==0?12:intOp.get(); } } return 12;