From 828fb025aa73b825d9d6e8175b0c9126a0c6efc6 Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 3 Feb 2021 21:00:22 +0100 Subject: [PATCH] Simplify RegionSystem --- .../bausystem/commands/CommandFire.java | 10 --- .../bausystem/commands/CommandFreeze.java | 32 -------- .../bausystem/commands/CommandInfo.java | 6 +- .../bausystem/commands/CommandTNT.java | 26 ------ .../commands/RegionToggleCommand.java | 19 ++--- .../bausystem/world/BauScoreboard.java | 12 +-- .../de/steamwar/bausystem/world/Region.java | 81 ++++++++++++++++--- 7 files changed, 80 insertions(+), 106 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandFire.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandFire.java index 9c341d0..8dcf312 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandFire.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandFire.java @@ -45,18 +45,10 @@ public class CommandFire extends RegionToggleCommand { return region.isFire(); } - @Override - boolean toggleGlobal() { - Region.NoRegion.fire = !Region.NoRegion.fire; - return Region.NoRegion.fire; - } - @EventHandler public void onFireDamage(BlockBurnEvent e) { Region.getRegion(e.getBlock().getLocation(), region -> { if (region.isFire()) e.setCancelled(true); - }, () -> { - if (Region.NoRegion.fire) e.setCancelled(true); }); } @@ -64,8 +56,6 @@ public class CommandFire extends RegionToggleCommand { public void onFireSpread(BlockSpreadEvent e){ Region.getRegion(e.getBlock().getLocation(), region -> { if (region.isFire()) e.setCancelled(true); - }, () -> { - if (Region.NoRegion.fire) e.setCancelled(true); }); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandFreeze.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandFreeze.java index 4873c01..37c7a60 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandFreeze.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandFreeze.java @@ -48,18 +48,10 @@ public class CommandFreeze extends RegionToggleCommand { return region.isFreeze(); } - @Override - boolean toggleGlobal() { - Region.NoRegion.freeze = !Region.NoRegion.freeze; - return Region.NoRegion.freeze; - } - @EventHandler public void onEntitySpawn(EntitySpawnEvent e) { Region.getRegion(e.getLocation(), region -> { if (region.isFreeze()) e.setCancelled(true); - }, () -> { - if (Region.NoRegion.freeze) e.setCancelled(true); }); } @@ -74,14 +66,6 @@ public class CommandFreeze extends RegionToggleCommand { e.setBuildable(false); e.getBlock().setType(Material.TNT, false); } - }, () -> { - if (!Region.NoRegion.freeze) { - return; - } - if (e.getMaterial() == Material.TNT) { - e.setBuildable(false); - e.getBlock().setType(Material.TNT, false); - } }); } @@ -89,8 +73,6 @@ public class CommandFreeze extends RegionToggleCommand { public void onEntityChangeBlock(EntityChangeBlockEvent e) { Region.getRegion(e.getBlock().getLocation(), region -> { if (region.isFreeze()) e.setCancelled(true); - }, () -> { - if (Region.NoRegion.freeze) e.setCancelled(true); }); } @@ -98,8 +80,6 @@ public class CommandFreeze extends RegionToggleCommand { public void onPhysicsEvent(BlockPhysicsEvent e){ Region.getRegion(e.getBlock().getLocation(), region -> { if (region.isFreeze()) e.setCancelled(true); - }, () -> { - if (Region.NoRegion.freeze) e.setCancelled(true); }); } @@ -107,8 +87,6 @@ public class CommandFreeze extends RegionToggleCommand { public void onPistonExtend(BlockPistonExtendEvent e){ Region.getRegion(e.getBlock().getLocation(), region -> { if (region.isFreeze()) e.setCancelled(true); - }, () -> { - if (Region.NoRegion.freeze) e.setCancelled(true); }); } @@ -116,8 +94,6 @@ public class CommandFreeze extends RegionToggleCommand { public void onPistonRetract(BlockPistonRetractEvent e){ Region.getRegion(e.getBlock().getLocation(), region -> { if (region.isFreeze()) e.setCancelled(true); - }, () -> { - if (Region.NoRegion.freeze) e.setCancelled(true); }); } @@ -125,8 +101,6 @@ public class CommandFreeze extends RegionToggleCommand { public void onBlockGrow(BlockGrowEvent e){ Region.getRegion(e.getBlock().getLocation(), region -> { if (region.isFreeze()) e.setCancelled(true); - }, () -> { - if (Region.NoRegion.freeze) e.setCancelled(true); }); } @@ -134,8 +108,6 @@ public class CommandFreeze extends RegionToggleCommand { public void onRedstoneEvent(BlockRedstoneEvent e){ Region.getRegion(e.getBlock().getLocation(), region -> { if (region.isFreeze()) e.setNewCurrent(e.getOldCurrent()); - }, () -> { - if (Region.NoRegion.freeze) e.setNewCurrent(e.getOldCurrent()); }); } @@ -143,8 +115,6 @@ public class CommandFreeze extends RegionToggleCommand { public void onBlockDispense(BlockDispenseEvent e){ Region.getRegion(e.getBlock().getLocation(), region -> { if (region.isFreeze()) e.setCancelled(true); - }, () -> { - if (Region.NoRegion.freeze) e.setCancelled(true); }); } @@ -152,8 +122,6 @@ public class CommandFreeze extends RegionToggleCommand { public void onInventoryMoveEvent(InventoryMoveItemEvent e){ Region.getRegion(e.getDestination().getLocation(), region -> { if (region.isFreeze()) e.setCancelled(true); - }, () -> { - if (Region.NoRegion.freeze) e.setCancelled(true); }); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandInfo.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandInfo.java index fd8fc56..980133b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandInfo.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandInfo.java @@ -42,11 +42,7 @@ public class CommandInfo implements CommandExecutor { sender.sendMessage(BauSystem.PREFIX + "Besitzer: §e" + SteamwarUser.get(BauSystem.getOwnerID()).getUserName()); Region region = Region.getRegion(player.getLocation()); - if (region == null) { - sender.sendMessage(BauSystem.PREFIX + "§eTNT§8: " + Region.NoRegion.tnt.getName() + " §eFire§8: " + (Region.NoRegion.fire ? "§aAUS" : "§cAN") + " §eFreeze§8: " + (Region.NoRegion.freeze ? "§aAN" : "§cAUS")); - } else { - sender.sendMessage(BauSystem.PREFIX + "§eTNT§8: " + region.getTntMode().getName() + " §eFire§8: " + (region.isFire() ? "§aAUS" : "§cAN") + " §eFreeze§8: " + (region.isFreeze() ? "§aAN" : "§cAUS")); - } + sender.sendMessage(BauSystem.PREFIX + "§eTNT§8: " + region.getTntMode().getName() + " §eFire§8: " + (region.isFire() ? "§aAUS" : "§cAN") + " §eFreeze§8: " + (region.isFreeze() ? "§aAN" : "§cAUS")); List members = BauweltMember.getMembers(BauSystem.getOwnerID()); StringBuilder membermessage = new StringBuilder().append(BauSystem.PREFIX).append("Mitglieder: "); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTNT.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTNT.java index c976f9a..84a9a5f 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTNT.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTNT.java @@ -111,33 +111,10 @@ public class CommandTNT implements CommandExecutor, Listener { } Region region = Region.getRegion(player.getLocation()); - if (region == null) { - tntGlobalToggle(requestedMode, requestedMessage); - return false; - } tntToggle(region, requestedMode, requestedMessage); return false; } - private void tntGlobalToggle(TNTMode requestedMode, String requestedMessage) { - if (requestedMode != null && requestedMode != TNTMode.ONLY_TB) { - Region.NoRegion.tnt = requestedMode; - RegionToggleCommand.actionBar(requestedMessage); - return; - } - switch (Region.NoRegion.tnt) { - case ON: - case ONLY_TB: - Region.NoRegion.tnt = TNTMode.OFF; - RegionToggleCommand.actionBar(getDisableMessage()); - break; - case OFF: - Region.NoRegion.tnt = TNTMode.ON; - RegionToggleCommand.actionBar(getEnableMessage()); - break; - } - } - private void tntToggle(Region region, TNTMode requestedMode, String requestedMessage) { if (requestedMode != null && region.hasTestblock()) { region.setTntMode(requestedMode); @@ -166,9 +143,6 @@ public class CommandTNT implements CommandExecutor, Listener { public void onExplode(EntityExplodeEvent event) { event.blockList().removeIf(block -> { Region region = Region.getRegion(block.getLocation()); - if (region == null) { - return Region.NoRegion.tnt == TNTMode.OFF; - } if (region.getTntMode() == TNTMode.OFF) return true; if (region.getTntMode() == TNTMode.ON) return false; if (region.hasBuildRegion() && region.inBuildRegion(block.getLocation())) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/RegionToggleCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/RegionToggleCommand.java index 6eba349..d07e013 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/RegionToggleCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/RegionToggleCommand.java @@ -52,14 +52,6 @@ public abstract class RegionToggleCommand implements CommandExecutor, Listener { } Region region = Region.getRegion(player.getLocation()); - if (region == null) { - if (toggleGlobal()) { - actionBar(getEnableMessage()); - } else { - actionBar(getDisableMessage()); - } - return false; - } if (toggle(region)) { actionBar(region, getEnableMessage()); } else { @@ -69,11 +61,11 @@ public abstract class RegionToggleCommand implements CommandExecutor, Listener { } public static void actionBar(Region region, String s) { - Bukkit.getOnlinePlayers().stream().filter(player -> region.inRegion(player.getLocation())).forEach(player -> player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(s))); - } - - public static void actionBar(String s) { - Bukkit.getOnlinePlayers().stream().filter(player -> Region.getRegion(player.getLocation()) == null).forEach(player -> player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(s))); + if (Region.GlobalRegion.isGlobalRegion(region)) { + Bukkit.getOnlinePlayers().stream().filter(player -> Region.getRegion(player.getLocation()) == null).forEach(player -> player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(s))); + } else { + Bukkit.getOnlinePlayers().stream().filter(player -> region.inRegion(player.getLocation())).forEach(player -> player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(s))); + } } abstract String getNoPermMessage(); @@ -84,6 +76,5 @@ public abstract class RegionToggleCommand implements CommandExecutor, Listener { * {@code true} for {@link #getEnableMessage()}, {@code false} for {@link #getDisableMessage()} */ abstract boolean toggle(Region region); - abstract boolean toggleGlobal(); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/BauScoreboard.java b/BauSystem_Main/src/de/steamwar/bausystem/world/BauScoreboard.java index 316cf00..17873b7 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/BauScoreboard.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/BauScoreboard.java @@ -60,15 +60,9 @@ public class BauScoreboard implements Listener { strings.add("§eUhrzeit§8: §7" + new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime())); strings.add("§2"); Region region = Region.getRegion(p.getLocation()); - if (region != null) { - strings.add("§eTNT§8: " + region.getTntMode().getName()); - strings.add("§eFreeze§8: " + (region.isFreeze() ? "§aan" : "§caus")); - strings.add("§eFire§8: " + (region.isFire() ? "§aaus" : "§can")); - } else { - strings.add("§eTNT§8: " + Region.NoRegion.tnt.getName()); - strings.add("§eFreeze§8: " + (Region.NoRegion.freeze ? "§aan" : "§caus")); - strings.add("§eFire§8: " + (Region.NoRegion.fire ? "§aaus" : "§can")); - } + strings.add("§eTNT§8: " + region.getTntMode().getName()); + strings.add("§eFreeze§8: " + (region.isFreeze() ? "§aan" : "§caus")); + strings.add("§eFire§8: " + (region.isFire() ? "§aaus" : "§can")); strings.add("§eTrace§8: " + RecordStateMachine.getRecordStatus().getName()); strings.add("§eLoader§8: " + (AutoLoader.hasLoader(p) ? "§aan" : "§caus")); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java b/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java index 781a0b7..ff9f130 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java @@ -41,13 +41,6 @@ public class Region { private static final List regions = new ArrayList<>(); private static boolean buildArea = false; - public static class NoRegion { - private NoRegion() {} - public static TNTMode tnt = TNTMode.OFF; - public static boolean fire = false; - public static boolean freeze = false; - } - static{ YamlConfiguration config = new YamlConfiguration(); try { @@ -77,10 +70,10 @@ public class Region { for (Region region : regions) { if (region.inRegion(location)) return region; } - return null; + return GlobalRegion.getInstance(); } - public static void getRegion(Location location, Consumer regionConsumer, Runnable noRegion) { + public static void getRegion(Location location, Consumer regionConsumer) { boolean b = true; for (Region region : regions) { if (region.inRegion(location)) { @@ -88,7 +81,7 @@ public class Region { b = false; } } - if (b) noRegion.run(); + if (b) regionConsumer.accept(GlobalRegion.getInstance()); } private final String name; @@ -114,6 +107,16 @@ public class Region { regions.add(this); } + public Region(String name) { + this.name = name; + this.minX = 0; + this.minY = 0; + this.minZ = 0; + this.prototype = null; + this.optionsLinkedWith = null; + tntMode = TNTMode.OFF; + } + private void setLinkedRegion(Consumer regionConsumer) { if (optionsLinkedWith == null) { return; @@ -194,6 +197,64 @@ public class Region { prototype.protect(this, schem); } + public static class GlobalRegion extends Region { + + private static final GlobalRegion GLOBAL_REGION = new GlobalRegion(); + + public static GlobalRegion getInstance() { + return GLOBAL_REGION; + } + + public static boolean isGlobalRegion(Region region) { + return region == GLOBAL_REGION; + } + + public GlobalRegion() { + super("Global"); + } + + @Override + public boolean inRegion(Location l) { + return true; + } + + @Override + public boolean hasBuildRegion() { + return false; + } + + @Override + public boolean inBuildRegion(Location l) { + return false; + } + + @Override + public void fastreset() { + + } + + @Override + public boolean hasTestblock() { + return false; + } + + @Override + public void resetTestblock(Schematic schem) throws IOException, NoClipboardException { + + } + + @Override + public boolean hasProtection() { + return false; + } + + @Override + public void protect(Schematic schem) throws IOException, NoClipboardException { + + } + + } + public static class Prototype{ private static final Map prototypes = new HashMap<>();