From 4dd79391c99a13052ee2d59ec9b13fffbe6da1df Mon Sep 17 00:00:00 2001 From: Zeanon Date: Wed, 5 May 2021 14:30:48 +0200 Subject: [PATCH] Damage flag HOOZAH --- .../features/region/DamageCommand.java | 89 +++++++++++++++++++ .../features/region/DamageListener.java | 40 +++++++++ .../steamwar/bausystem/region/flags/Flag.java | 1 + .../region/flags/flagvalues/DamageMode.java | 60 +++++++++++++ 4 files changed, 190 insertions(+) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/region/DamageCommand.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/region/DamageListener.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/region/flags/flagvalues/DamageMode.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/DamageCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/DamageCommand.java new file mode 100644 index 00000000..6a029c33 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/DamageCommand.java @@ -0,0 +1,89 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.region; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.Permission; +import de.steamwar.bausystem.SWUtils; +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.DamageMode; +import de.steamwar.command.SWCommand; +import org.bukkit.entity.Player; + + +@Linked(LinkageType.COMMAND) +public class DamageCommand extends SWCommand { + + protected DamageCommand() { + super("damage", "dmg"); + } + + @Register(help = true) + public void genericHelp(Player p, String... args) { + p.sendMessage("§8/§edamage §8- §7Toggle Spielerschaden"); + } + + @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 Spielerschaden (de-)aktivieren"; + } + + private String getEnableMessage() { + return "§cRegions Spielerschaden deaktiviert"; + } + + private String getDisableMessage() { + return "§aRegions Spielerschaden aktiviert"; + } + + private boolean toggle(Region region) { + switch (region.getPlain(Flag.DAMAGE, DamageMode.class)) { + case ALLOW: + region.set(Flag.DAMAGE, DamageMode.DENY); + return true; + default: + case DENY: + region.set(Flag.DAMAGE, DamageMode.ALLOW); + return false; + } + } + + private boolean permissionCheck(Player player) { + if (!Permission.hasPermission(player, Permission.WORLD)) { + player.sendMessage(BauSystem.PREFIX + getNoPermMessage()); + return false; + } + return true; + } +} \ No newline at end of file diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/DamageListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/DamageListener.java new file mode 100644 index 00000000..0441d310 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/DamageListener.java @@ -0,0 +1,40 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +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.DamageMode; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityDamageEvent; + + +@Linked(LinkageType.LISTENER) +public class DamageListener implements Listener { + + @EventHandler + public void onPlayerDamage(EntityDamageEvent e) { + if (e.getEntity() instanceof Player && Region.getRegion(e.getEntity().getLocation()).get(Flag.DAMAGE) == DamageMode.DENY) e.setCancelled(true); + } +} \ No newline at end of file diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/flags/Flag.java b/BauSystem_Main/src/de/steamwar/bausystem/region/flags/Flag.java index dbfb9b39..96f9ed5a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/flags/Flag.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/flags/Flag.java @@ -32,6 +32,7 @@ public enum Flag implements EnumDisplay { COLOR("FLAG_COLOR", ColorMode.class, ColorMode.YELLOW), TNT("FLAG_TNT", TNTMode.class, TNTMode.ONLY_TB), FIRE("FLAG_FIRE", FireMode.class, FireMode.ALLOW), + DAMAGE("FLAG_DAMAGE", DamageMode.class, DamageMode.ALLOW), FREEZE("FLAG_FREEZE", FreezeMode.class, FreezeMode.INACTIVE), PROTECT("FLAG_PROTECT", ProtectMode.class, ProtectMode.INACTIVE); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/flags/flagvalues/DamageMode.java b/BauSystem_Main/src/de/steamwar/bausystem/region/flags/flagvalues/DamageMode.java new file mode 100644 index 00000000..735afb4d --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/flags/flagvalues/DamageMode.java @@ -0,0 +1,60 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.region.flags.flagvalues; + +import de.steamwar.bausystem.region.flags.Flag; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public enum DamageMode implements Flag.Value { + + ALLOW("FLAG_DAMAGE_ALLOW"), + DENY("FLAG_DAMAGE_DENY"); + + private static DamageMode[] values; + private final String chatValue; + + @Override + public DamageMode[] getValues() { + if (DamageMode.values == null) { + DamageMode.values = DamageMode.values(); //NOSONAR + } + return DamageMode.values; + } + + @Override + public DamageMode getValue() { + return this; + } + + @Override + public DamageMode getValueOf(final String name) { + try { + return DamageMode.valueOf(name); + } catch (IllegalArgumentException e) { + if (name.equalsIgnoreCase("false")) { + return DamageMode.DENY; + } + return DamageMode.ALLOW; + } + } +} \ No newline at end of file