From e3987386b11433e7309926606c9443633803687f Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 19 Apr 2021 17:22:15 +0200 Subject: [PATCH] Add RegionUtils Add FireCommand Add FireListener Add FreezeCommand Add FreezeListener Add TNTCommand Add TNTListener --- .../bausystem/features/region/.gitkeep | 0 .../features/region/FireCommand.java | 69 ++++++++++ .../features/region/FireListener.java | 26 ++++ .../features/region/FreezeCommand.java | 69 ++++++++++ .../features/region/FreezeListener.java | 85 +++++++++++++ .../bausystem/features/region/TNTCommand.java | 119 ++++++++++++++++++ .../features/region/TNTListener.java | 38 ++++++ .../de/steamwar/bausystem/region/Region.java | 8 ++ .../bausystem/region/RegionUtils.java | 17 +++ 9 files changed, 431 insertions(+) delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/region/.gitkeep create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/region/FireCommand.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/region/FireListener.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/region/FreezeCommand.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/region/FreezeListener.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTCommand.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/region/RegionUtils.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/.gitkeep b/BauSystem_Main/src/de/steamwar/bausystem/features/region/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/FireCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/FireCommand.java new file mode 100644 index 00000000..c576639e --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/FireCommand.java @@ -0,0 +1,69 @@ +package de.steamwar.bausystem.features.region; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.Permission; +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.bausystem.region.Region; +import de.steamwar.bausystem.region.RegionUtils; +import de.steamwar.bausystem.region.flags.Flag; +import de.steamwar.bausystem.region.flags.flagvalues.FireMode; +import de.steamwar.command.SWCommand; +import org.bukkit.entity.Player; + +@Linked(LinkageType.COMMAND) +public class FireCommand extends SWCommand { + + public FireCommand() { + super("fire"); + } + + @Register(help = true) + public void genericHelp(Player p, String... args) { + p.sendMessage("§8/§efire §8- §7Toggle Feuerschaden"); + } + + @Register + public void toggleCommand(Player p) { + if (!permissionCheck(p)) return; + Region region = Region.getRegion(p.getLocation()); + if (toggle(region)) { + RegionUtils.actionBar(region, getEnableMessage()); + } else { + RegionUtils.actionBar(region, getDisableMessage()); + } + } + + private String getNoPermMessage() { + return "§cDu darfst hier nicht Feuerschaden (de-)aktivieren"; + } + + private String getEnableMessage() { + return "§cRegions Feuerschaden deaktiviert"; + } + + private String getDisableMessage() { + return "§aRegions Feuerschaden aktiviert"; + } + + private boolean toggle(Region region) { + switch (region.getPlain(Flag.FIRE, FireMode.class)) { + case ALLOW: + region.set(Flag.FIRE, FireMode.DENY); + return true; + default: + case DENY: + region.set(Flag.FIRE, FireMode.ALLOW); + return false; + } + } + + private boolean permissionCheck(Player player) { + if (!Permission.hasPermission(player, Permission.WORLD)) { + player.sendMessage(BauSystem.PREFIX + getNoPermMessage()); + return false; + } + return true; + } + +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/FireListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/FireListener.java new file mode 100644 index 00000000..9a303881 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/FireListener.java @@ -0,0 +1,26 @@ +package de.steamwar.bausystem.features.region; + +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.bausystem.region.Region; +import de.steamwar.bausystem.region.flags.Flag; +import de.steamwar.bausystem.region.flags.flagvalues.FireMode; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBurnEvent; +import org.bukkit.event.block.BlockSpreadEvent; + +@Linked(LinkageType.LISTENER) +public class FireListener implements Listener { + + @EventHandler + public void onFireDamage(BlockBurnEvent e) { + if (Region.getRegion(e.getBlock().getLocation()).getPlain(Flag.FIRE, FireMode.class) == FireMode.DENY) e.setCancelled(true); + } + + @EventHandler + public void onFireSpread(BlockSpreadEvent e) { + if (Region.getRegion(e.getBlock().getLocation()).getPlain(Flag.FIRE, FireMode.class) == FireMode.DENY) e.setCancelled(true); + } + +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/FreezeCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/FreezeCommand.java new file mode 100644 index 00000000..13db4d25 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/FreezeCommand.java @@ -0,0 +1,69 @@ +package de.steamwar.bausystem.features.region; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.Permission; +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.bausystem.region.Region; +import de.steamwar.bausystem.region.RegionUtils; +import de.steamwar.bausystem.region.flags.Flag; +import de.steamwar.bausystem.region.flags.flagvalues.FreezeMode; +import de.steamwar.command.SWCommand; +import org.bukkit.entity.Player; + +@Linked(LinkageType.COMMAND) +public class FreezeCommand extends SWCommand { + + public FreezeCommand() { + super("freeze", "stoplag"); + } + + @Register(help = true) + public void genericHelp(Player p, String... args) { + p.sendMessage("§8/§efreeze §8- §7Toggle Freeze"); + } + + @Register + public void toggleCommand(Player p) { + if (!permissionCheck(p)) return; + Region region = Region.getRegion(p.getLocation()); + if (toggle(region)) { + RegionUtils.actionBar(region, getEnableMessage()); + } else { + RegionUtils.actionBar(region, getDisableMessage()); + } + } + + private String getNoPermMessage() { + return "§cDu darfst diese Welt nicht einfrieren"; + } + + private String getEnableMessage(){ + return "§cRegion eingefroren"; + } + + private String getDisableMessage(){ + return "§aRegion aufgetaut"; + } + + private boolean toggle(Region region) { + switch (region.getPlain(Flag.FREEZE, FreezeMode.class)) { + case ACTIVE: + region.set(Flag.FREEZE, FreezeMode.INACTIVE); + return false; + default: + case INACTIVE: + region.set(Flag.FREEZE, FreezeMode.ACTIVE); + return true; + } + } + + private boolean permissionCheck(Player player) { + if (!Permission.hasPermission(player, Permission.WORLD)) { + player.sendMessage(BauSystem.PREFIX + getNoPermMessage()); + return false; + } + return true; + } + +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/FreezeListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/FreezeListener.java new file mode 100644 index 00000000..82f5e2d1 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/FreezeListener.java @@ -0,0 +1,85 @@ +package de.steamwar.bausystem.features.region; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.bausystem.region.Region; +import de.steamwar.bausystem.region.flags.Flag; +import de.steamwar.bausystem.region.flags.flagvalues.FreezeMode; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.entity.EntityType; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.*; +import org.bukkit.event.entity.EntityChangeBlockEvent; +import org.bukkit.event.entity.EntitySpawnEvent; +import org.bukkit.event.inventory.InventoryMoveItemEvent; + +@Linked(LinkageType.LISTENER) +public class FreezeListener implements Listener { + + @EventHandler + public void onEntitySpawn(EntitySpawnEvent e) { + if (Region.getRegion(e.getLocation()).getPlain(Flag.FREEZE, FreezeMode.class) == FreezeMode.INACTIVE) { + return; + } + e.setCancelled(true); + if (e.getEntityType() == EntityType.PRIMED_TNT) { + Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> { + e.getLocation().getBlock().setType(Material.TNT, false); + }, 1L); + } + } + + @EventHandler + public void onBlockCanBuild(BlockCanBuildEvent e) { + if (!e.isBuildable()) return; + if (Region.getRegion(e.getBlock().getLocation()).getPlain(Flag.FREEZE, FreezeMode.class) == FreezeMode.INACTIVE) return; + if (e.getMaterial() == Material.TNT) { + e.setBuildable(false); + e.getBlock().setType(Material.TNT, false); + } + } + + @EventHandler + public void onEntityChangeBlock(EntityChangeBlockEvent e) { + if (Region.getRegion(e.getBlock().getLocation()).getPlain(Flag.FREEZE, FreezeMode.class) == FreezeMode.ACTIVE) e.setCancelled(true); + } + + @EventHandler + public void onPhysicsEvent(BlockPhysicsEvent e){ + if (Region.getRegion(e.getBlock().getLocation()).getPlain(Flag.FREEZE, FreezeMode.class) == FreezeMode.ACTIVE) e.setCancelled(true); + } + + @EventHandler + public void onPistonExtend(BlockPistonExtendEvent e){ + if (Region.getRegion(e.getBlock().getLocation()).getPlain(Flag.FREEZE, FreezeMode.class) == FreezeMode.ACTIVE) e.setCancelled(true); + } + + @EventHandler + public void onPistonRetract(BlockPistonRetractEvent e){ + if (Region.getRegion(e.getBlock().getLocation()).getPlain(Flag.FREEZE, FreezeMode.class) == FreezeMode.ACTIVE) e.setCancelled(true); + } + + @EventHandler + public void onBlockGrow(BlockGrowEvent e){ + if (Region.getRegion(e.getBlock().getLocation()).getPlain(Flag.FREEZE, FreezeMode.class) == FreezeMode.ACTIVE) e.setCancelled(true); + } + + @EventHandler + public void onRedstoneEvent(BlockRedstoneEvent e) { + if (Region.getRegion(e.getBlock().getLocation()).getPlain(Flag.FREEZE, FreezeMode.class) == FreezeMode.ACTIVE) e.setNewCurrent(e.getOldCurrent()); + } + + @EventHandler + public void onBlockDispense(BlockDispenseEvent e) { + if (Region.getRegion(e.getBlock().getLocation()).getPlain(Flag.FREEZE, FreezeMode.class) == FreezeMode.ACTIVE) e.setCancelled(true); + } + + @EventHandler + public void onInventoryMoveEvent(InventoryMoveItemEvent e){ + if (Region.getRegion(e.getDestination().getLocation()).getPlain(Flag.FREEZE, FreezeMode.class) == FreezeMode.ACTIVE) e.setCancelled(true); + } + +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTCommand.java new file mode 100644 index 00000000..2cb547c4 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTCommand.java @@ -0,0 +1,119 @@ +package de.steamwar.bausystem.features.region; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.Permission; +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.bausystem.region.Region; +import de.steamwar.bausystem.region.RegionUtils; +import de.steamwar.bausystem.region.flags.Flag; +import de.steamwar.bausystem.region.flags.flagvalues.TNTMode; +import de.steamwar.bausystem.region.utils.RegionType; +import de.steamwar.command.SWCommand; +import de.steamwar.command.SWCommandUtils; +import de.steamwar.command.TypeMapper; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Linked(LinkageType.COMMAND) +public class TNTCommand extends SWCommand { + + public TNTCommand() { + super("tnt"); + } + + @Register(help = true) + public void genericHelp(Player p, String... args) { + p.sendMessage("§8/§etnt §8- §7Ändere das TNT verhalten"); + p.sendMessage("§8/§etnt §8[§7Mode§8] §8- §7Setzte das TNT verhalten auf einen Modus"); + } + + @Register + public void toggleCommand(Player p) { + if (!permissionCheck(p)) return; + Region region = Region.getRegion(p.getLocation()); + tntToggle(region, null, null); + } + + @Register + public void setCommand(Player p, TNTMode tntMode) { + if (!permissionCheck(p)) return; + Region region = Region.getRegion(p.getLocation()); + + String requestedMessage = null; + switch (tntMode) { + case ALLOW: + requestedMessage = getEnableMessage(); + break; + case DENY: + requestedMessage = getDisableMessage(); + break; + case ONLY_TB: + requestedMessage = getTestblockEnableMessage(); + break; + } + tntToggle(region, tntMode, requestedMessage); + } + + @ClassMapper(value = TNTMode.class, local = true) + public TypeMapper tntModeTypeMapper() { + Map tntModeMap = new HashMap<>(); + tntModeMap.put("an", TNTMode.ALLOW); + tntModeMap.put("on", TNTMode.ALLOW); + tntModeMap.put("aus", TNTMode.DENY); + tntModeMap.put("off", TNTMode.DENY); + tntModeMap.put("testblock", TNTMode.ONLY_TB); + tntModeMap.put("tb", TNTMode.ONLY_TB); + List tabCompletes = new ArrayList<>(tntModeMap.keySet()); + return SWCommandUtils.createMapper(s -> tntModeMap.getOrDefault(s, null), s -> tabCompletes); + } + + private String getEnableMessage() { + return "§aTNT-Schaden aktiviert"; + } + + private String getDisableMessage() { + return "§cTNT-Schaden deaktiviert"; + } + + private String getTestblockEnableMessage() { + return "§aTNT-Schaden außerhalb Baurahmen aktiviert"; + } + + private void tntToggle(Region region, TNTMode requestedMode, String requestedMessage) { + if (requestedMode != null && region.hasType(RegionType.TESTBLOCK)) { + region.set(Flag.TNT, requestedMode); + RegionUtils.actionBar(region, requestedMessage); + return; + } + switch (region.getPlain(Flag.TNT, TNTMode.class)) { + case ALLOW: + case ONLY_TB: + region.set(Flag.TNT, TNTMode.DENY); + RegionUtils.actionBar(region, getDisableMessage()); + break; + case DENY: + if (region.hasType(RegionType.TESTBLOCK)) { + region.set(Flag.TNT, TNTMode.ONLY_TB); + RegionUtils.actionBar(region, getTestblockEnableMessage()); + } else { + region.set(Flag.TNT, TNTMode.ALLOW); + RegionUtils.actionBar(region, getEnableMessage()); + } + break; + } + } + + private boolean permissionCheck(Player p) { + if (!Permission.hasPermission(p, Permission.WORLD)) { + p.sendMessage(BauSystem.PREFIX + "§cDu darfst hier nicht TNT-Schaden (de-)aktivieren"); + return false; + } + return true; + } + +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java new file mode 100644 index 00000000..053e9893 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java @@ -0,0 +1,38 @@ +package de.steamwar.bausystem.features.region; + +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.bausystem.region.Region; +import de.steamwar.bausystem.region.RegionUtils; +import de.steamwar.bausystem.region.flags.Flag; +import de.steamwar.bausystem.region.flags.flagvalues.TNTMode; +import de.steamwar.bausystem.region.utils.RegionExtensionType; +import de.steamwar.bausystem.region.utils.RegionType; +import org.bukkit.block.data.type.TNT; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityExplodeEvent; + +@Linked(LinkageType.LISTENER) +public class TNTListener implements Listener { + + @EventHandler + public void onExplode(EntityExplodeEvent event) { + event.blockList().removeIf(block -> { + Region region = Region.getRegion(block.getLocation()); + TNTMode value = region.getPlain(Flag.TNT); + if (value == TNTMode.ALLOW) { + return false; + } + if (region.inRegion(block.getLocation(), RegionType.BUILD, RegionExtensionType.NORMAL)) { + RegionUtils.actionBar(region, "§cEine Explosion hätte Blöcke im Baubereich zerstört"); + return true; + } + if (region.inRegion(block.getLocation(), RegionType.BUILD, RegionExtensionType.EXTENSION)) { + RegionUtils.actionBar(region, "§cEine Explosion hätte Blöcke im Ausfahrbereich zerstört"); + return true; + } + return value == TNTMode.DENY; + }); + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java b/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java index b6737917..81439d6e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java @@ -219,6 +219,14 @@ public class Region { return flagStorage.get(flagType); } + public & Flag.Value> T getPlain(Flag flagType) { + return (T) flagStorage.get(flagType).getValue(); + } + + public & Flag.Value> T getPlain(Flag flagType, Class type) { + return (T) flagStorage.get(flagType).getValue(); + } + public void reset(RegionType regionType) { if (!hasType(regionType)) { return; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/RegionUtils.java b/BauSystem_Main/src/de/steamwar/bausystem/region/RegionUtils.java new file mode 100644 index 00000000..6219c831 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/RegionUtils.java @@ -0,0 +1,17 @@ +package de.steamwar.bausystem.region; + +import de.steamwar.bausystem.region.utils.RegionExtensionType; +import de.steamwar.bausystem.region.utils.RegionType; +import lombok.experimental.UtilityClass; +import net.md_5.bungee.api.ChatMessageType; +import net.md_5.bungee.api.chat.TextComponent; +import org.bukkit.Bukkit; + +@UtilityClass +public class RegionUtils { + + public void actionBar(Region region, String s) { + Bukkit.getOnlinePlayers().stream().filter(player -> region.inRegion(player.getLocation(), RegionType.NORMAL, RegionExtensionType.NORMAL)).forEach(player -> player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(s))); + } + +}