diff --git a/src/main/java/ru/redguy/extendedpistons/ExtendedPistons.java b/src/main/java/ru/redguy/extendedpistons/ExtendedPistons.java index 48a81e4..4e5c30a 100644 --- a/src/main/java/ru/redguy/extendedpistons/ExtendedPistons.java +++ b/src/main/java/ru/redguy/extendedpistons/ExtendedPistons.java @@ -46,6 +46,8 @@ public final class ExtendedPistons extends JavaPlugin { conf.set("enabled-times.Sat",new ArrayList() {{add("14:00-18:00");}}); if(!conf.isSet("enabled-times.Sun")) conf.set("enabled-times.Sun",new ArrayList() {{add("14:00-18:00");}}); + if(!conf.isSet("sticky-piston-pulls-chest")) + conf.set("sticky-piston-pulls-chest",true); saveConfig(); @@ -88,6 +90,10 @@ public final class ExtendedPistons extends JavaPlugin { return false; } + public boolean isStickyPistonPulls() { + return conf.getBoolean("sticky-piston-pulls-chest"); + } + @Override public void onDisable() { diff --git a/src/main/java/ru/redguy/extendedpistons/WorldListener.java b/src/main/java/ru/redguy/extendedpistons/WorldListener.java index 7920fac..1d599f9 100644 --- a/src/main/java/ru/redguy/extendedpistons/WorldListener.java +++ b/src/main/java/ru/redguy/extendedpistons/WorldListener.java @@ -5,7 +5,6 @@ import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.*; -import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -19,16 +18,15 @@ import java.util.Comparator; import java.util.*; public class WorldListener implements Listener { - @EventHandler(priority = EventPriority.LOW,ignoreCancelled = true) + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onPistonChanged(BlockPistonExtendEvent e) { - if(e instanceof BlockPistonExtendedExtendEvent) return; + if (e instanceof BlockPistonExtendedExtendEvent) return; Block block = e.getBlock(); BlockState state = block.getState(); PistonBaseMaterial piston = (PistonBaseMaterial) state.getData(); int maxBlocks = findNearbyPlayerMaxBlocks(block.getLocation()); ArrayList blocks = new ArrayList<>(); - Block toBreak = null; boolean isMovable = true; Block firstBlock = block.getRelative(piston.getFacing()); if (firstBlock.isLiquid() || firstBlock.isEmpty()) { @@ -38,12 +36,12 @@ public class WorldListener implements Listener { if (b.isLiquid() || b.isEmpty()) break; if (b.getPistonMoveReaction().equals(PistonMoveReaction.IGNORE) || b.getPistonMoveReaction().equals(PistonMoveReaction.BLOCK)) { if (!(b.getType() == Material.CHEST || b.getType() == Material.FURNACE || b.getType() == Material.BURNING_FURNACE)) { - isMovable = false; + if (ExtendedPistons.INSTANCE.isAllowedTime()) + isMovable = false; } else { blocks.add(b); } - } else if(b.getPistonMoveReaction().equals(PistonMoveReaction.BREAK)) { - toBreak = b; + } else if (b.getPistonMoveReaction().equals(PistonMoveReaction.BREAK)) { break; } else { blocks.add(b); @@ -61,40 +59,41 @@ public class WorldListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onRedstone(BlockRedstoneEvent e) { - HashMap states = new HashMap<>(); + HashMap states = new HashMap<>(); for (BlockFace face : new BlockFace[]{BlockFace.SELF, BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) { - states.put(face,e.getBlock().getRelative(face).getState()); + states.put(face, e.getBlock().getRelative(face).getState()); } - Bukkit.getScheduler().runTask(ExtendedPistons.INSTANCE,() -> { + Bukkit.getScheduler().runTask(ExtendedPistons.INSTANCE, () -> { if (e.getOldCurrent() != 0 && e.getNewCurrent() == 0) { - ArrayList checkedBlockFaces = new ArrayList() {{ - add(BlockFace.SELF); - add(BlockFace.UP); - add(BlockFace.DOWN); - }}; - if (e.getBlock().getType().equals(Material.REDSTONE_WIRE)) { - for (BlockFace blockFace : new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) { - Block block = e.getBlock().getRelative(blockFace); - if (block.getBlockPower() == 0) checkedBlockFaces.add(blockFace); + if (ExtendedPistons.INSTANCE.isStickyPistonPulls()) { + ArrayList checkedBlockFaces = new ArrayList() {{ + add(BlockFace.SELF); + add(BlockFace.UP); + add(BlockFace.DOWN); + }}; + if (e.getBlock().getType().equals(Material.REDSTONE_WIRE)) { + for (BlockFace blockFace : new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}) { + Block block = e.getBlock().getRelative(blockFace); + if (block.getBlockPower() == 0) checkedBlockFaces.add(blockFace); + } + } else { + checkedBlockFaces.add(BlockFace.NORTH); + checkedBlockFaces.add(BlockFace.SOUTH); + checkedBlockFaces.add(BlockFace.EAST); + checkedBlockFaces.add(BlockFace.WEST); } - } else { - checkedBlockFaces.add(BlockFace.NORTH); - checkedBlockFaces.add(BlockFace.SOUTH); - checkedBlockFaces.add(BlockFace.EAST); - checkedBlockFaces.add(BlockFace.WEST); - } - for (BlockFace face : checkedBlockFaces) { - Block block = e.getBlock().getRelative(face); - BlockState state = states.get(face); - if (state.getType().equals(Material.PISTON_STICKY_BASE)) { - PistonBaseMaterial piston = (PistonBaseMaterial) state.getData(); + for (BlockFace face : checkedBlockFaces) { + Block block = e.getBlock().getRelative(face); + BlockState state = states.get(face); + if (state.getType().equals(Material.PISTON_STICKY_BASE)) { + PistonBaseMaterial piston = (PistonBaseMaterial) state.getData(); - Block b = block.getRelative(piston.getFacing(),2); - if(b.getType().equals(Material.CHEST)||b.getType().equals(Material.FURNACE)||b.getType().equals(Material.BURNING_FURNACE)) { - Block b2 = block.getRelative(piston.getFacing()); - BlockPistonRetractEvent ev = new BlockPistonRetractEvent(block, Lists.newArrayList(b2),piston.getFacing()); - Bukkit.getPluginManager().callEvent(ev); - if(ev.isCancelled()) continue; + Block b = block.getRelative(piston.getFacing(), 2); + if (ExtendedPistons.INSTANCE.isAllowedTime() && (b.getType().equals(Material.CHEST) || b.getType().equals(Material.FURNACE) || b.getType().equals(Material.BURNING_FURNACE))) { + Block b2 = block.getRelative(piston.getFacing()); + BlockPistonRetractEvent ev = new BlockPistonRetractEvent(block, Lists.newArrayList(b2), piston.getFacing()); + Bukkit.getPluginManager().callEvent(ev); + if (ev.isCancelled()) continue; b2.setType(b.getType()); b2.setData(b.getData()); if (b.getType().equals(Material.CHEST)) { @@ -109,6 +108,7 @@ public class WorldListener implements Listener { oldFurnace.getInventory().clear(); } b.setType(Material.AIR); + } } } } @@ -147,7 +147,8 @@ public class WorldListener implements Listener { if (b.isLiquid() || b.isEmpty()) break; if (b.getPistonMoveReaction().equals(PistonMoveReaction.IGNORE) || b.getPistonMoveReaction().equals(PistonMoveReaction.BLOCK)) { if (!(b.getType() == Material.CHEST || b.getType() == Material.FURNACE || b.getType() == Material.BURNING_FURNACE)) { - isMovable = false; + if (ExtendedPistons.INSTANCE.isAllowedTime()) + isMovable = false; } else { blocks.add(b); } @@ -196,11 +197,11 @@ public class WorldListener implements Listener { public int findNearbyPlayerMaxBlocks(Location location) { Optional op = Bukkit.getOnlinePlayers().stream().min(Comparator.comparingDouble(p -> p.getLocation().distance(location))); - if(op.isPresent()) { + 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); + 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); } } return 12;