From 76631908f0898b370510e45f7e718769d036b666 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 9 May 2023 19:45:27 +0200 Subject: [PATCH] Add Flag.ITEMS for 1.19 server and up Signed-off-by: yoyosource --- BauSystem_Main/src/BauSystem.properties | 9 ++ BauSystem_Main/src/BauSystem_de.properties | 9 ++ .../features/region/ItemsCommand.java | 87 +++++++++++++++++++ .../features/region/ItemsListener.java | 68 +++++++++++++++ .../bausystem/region/FlagStorage.java | 2 +- .../steamwar/bausystem/region/flags/Flag.java | 4 +- .../region/flags/flagvalues/ItemMode.java | 60 +++++++++++++ 7 files changed, 237 insertions(+), 2 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/region/ItemsCommand.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/region/ItemsListener.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/region/flags/flagvalues/ItemMode.java diff --git a/BauSystem_Main/src/BauSystem.properties b/BauSystem_Main/src/BauSystem.properties index 9f4b2edb..fcc1f0e5 100644 --- a/BauSystem_Main/src/BauSystem.properties +++ b/BauSystem_Main/src/BauSystem.properties @@ -46,6 +46,7 @@ FLAG_TNT = TNT FLAG_FIRE = Fire FLAG_FREEZE = Freeze FLAG_PROTECT = Protect +FLAG_ITEMS = Items FLAG_FIRE_ALLOW = §con FLAG_FIRE_DENY = §aoff @@ -60,6 +61,9 @@ FLAG_TNT_ALLOW = §aon FLAG_TNT_DENY = §coff FLAG_TNT_ONLY_TB = §7no §ebuild area +FLAG_ITEMS_ACTIVE = §aon +FLAG_ITEMS_INACTIVE = §coff + FLAG_COLOR_WHITE = §fWhite FLAG_COLOR_ORANGE = §6Orange FLAG_COLOR_MAGENTA = §dMagenta @@ -1052,6 +1056,11 @@ REGION_FREEZE_HELP=§8/§efreeze §8- §7Toggle Freeze REGION_FREEZE_NO_PERMS=§cYou are not allowed to freeze this world REGION_FREEZE_ENABLED=§cRegion frozen REGION_FREEZE_DISABLED=§aRegion thawed +REGION_ITEMS_HELP=§8/§eitems §8- §7Toggle Items +REGION_ITEMS_NO_PERMS=§cYou are not allowed to toggle items in this world +REGION_ITEMS_ENABLED=§cItems disabled in this region +REGION_ITEMS_ENABLED_GLOBAL=§cItems disabled in this world +REGION_ITEMS_DISABLED=§aItems enabled in this region REGION_PROTECT_HELP=§8/§eprotect §8- §7Protect the region REGION_PROTECT_DISABLE=§cProtection disabled REGION_PROTECT_ENABLE=§aProtection enabled diff --git a/BauSystem_Main/src/BauSystem_de.properties b/BauSystem_Main/src/BauSystem_de.properties index 97ba1f97..b9e86e58 100644 --- a/BauSystem_Main/src/BauSystem_de.properties +++ b/BauSystem_Main/src/BauSystem_de.properties @@ -46,6 +46,7 @@ FLAG_TNT = TNT FLAG_FIRE = Fire FLAG_FREEZE = Freeze FLAG_PROTECT = Protect +FLAG_ITEMS = Items FLAG_FIRE_ALLOW = §can FLAG_FIRE_DENY = §aaus @@ -60,6 +61,9 @@ FLAG_TNT_ALLOW = §aan FLAG_TNT_DENY = §caus FLAG_TNT_ONLY_TB = §7Kein §eBaurahmen +FLAG_ITEMS_ACTIVE = §aan +FLAG_ITEMS_INACTIVE = §caus + FLAG_COLOR_WHITE = §fWeiß FLAG_COLOR_ORANGE = §6Orange FLAG_COLOR_MAGENTA = §dMagenta @@ -1021,6 +1025,11 @@ REGION_FREEZE_HELP=§8/§efreeze §8- §7Toggle Freeze REGION_FREEZE_NO_PERMS=§cDu darfst diese Welt nicht einfrieren REGION_FREEZE_ENABLED=§cRegion eingefroren REGION_FREEZE_DISABLED=§aRegion aufgetaut +REGION_ITEMS_HELP=§8/§eitems §8- §7Toggle Items +REGION_ITEMS_NO_PERMS=§cDu darfst hier nicht Items (de-)aktivieren +REGION_ITEMS_ENABLED=§cItems deaktiviert in dieser Region +REGION_ITEMS_ENABLED_GLOBAL=§cItems sind auf dem Server deaktiviert. +REGION_ITEMS_DISABLED=§aItems aktiviert in dieser Region REGION_PROTECT_HELP=§8/§eprotect §8- §7Schütze die Region REGION_PROTECT_DISABLE=§cBoden Schutz aufgehoben REGION_PROTECT_ENABLE=§aBoden geschützt diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/ItemsCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/ItemsCommand.java new file mode 100644 index 00000000..8eb42c64 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/ItemsCommand.java @@ -0,0 +1,87 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 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.Permission; +import de.steamwar.bausystem.region.GlobalRegion; +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.ItemMode; +import de.steamwar.command.SWCommand; +import de.steamwar.command.TypeValidator; +import de.steamwar.linkage.Linked; +import de.steamwar.linkage.MinVersion; +import org.bukkit.entity.Player; + +@Linked +@MinVersion(19) +public class ItemsCommand extends SWCommand { + + public ItemsCommand() { + super("items"); + } + + @Register(description = "REGION_ITEMS_HELP") + public void toggleCommand(@Validator Player p) { + Region region = Region.getRegion(p.getLocation()); + if (region != GlobalRegion.getInstance() && GlobalRegion.getInstance().getPlain(Flag.ITEMS, ItemMode.class) == ItemMode.INACTIVE) { + RegionUtils.actionBar(region, "REGION_ITEMS_ENABLED_GLOBAL"); + return; + } + + if (toggle(region)) { + RegionUtils.actionBar(region, getEnableMessage()); + } else { + RegionUtils.actionBar(region, getDisableMessage()); + } + } + + private String getNoPermMessage() { + return "REGION_ITEMS_NO_PERMS"; + } + + private String getEnableMessage(){ + return "REGION_ITEMS_ENABLED"; + } + + private String getDisableMessage(){ + return "REGION_ITEMS_DISABLED"; + } + + private boolean toggle(Region region) { + switch (region.getPlain(Flag.ITEMS, ItemMode.class)) { + case ACTIVE: + region.set(Flag.ITEMS, ItemMode.INACTIVE); + return false; + default: + case INACTIVE: + region.set(Flag.ITEMS, ItemMode.ACTIVE); + return true; + } + } + + @ClassValidator(value = Player.class, local = true) + public TypeValidator validator() { + return (commandSender, player, messageSender) -> { + return !messageSender.send(!Permission.hasPermission(player, Permission.WORLD), getNoPermMessage()); + }; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/ItemsListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/ItemsListener.java new file mode 100644 index 00000000..1becf08b --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/ItemsListener.java @@ -0,0 +1,68 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 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.region.GlobalRegion; +import de.steamwar.bausystem.region.Region; +import de.steamwar.bausystem.region.flags.Flag; +import de.steamwar.bausystem.region.flags.flagvalues.ItemMode; +import de.steamwar.bausystem.utils.ScoreboardElement; +import de.steamwar.linkage.Linked; +import de.steamwar.linkage.MinVersion; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.ItemSpawnEvent; + +@Linked +@MinVersion(19) +public class ItemsListener implements Listener, ScoreboardElement { + + private static ItemMode getMode(Region region) { + ItemMode itemMode = GlobalRegion.getInstance().getPlain(Flag.ITEMS, ItemMode.class); + if (itemMode == ItemMode.INACTIVE) return ItemMode.INACTIVE; + return region.getPlain(Flag.ITEMS, ItemMode.class); + } + + @EventHandler + public void onItemSpawn(ItemSpawnEvent event) { + if (getMode(Region.getRegion(event.getLocation())) == ItemMode.INACTIVE) { + event.setCancelled(true); + } + } + + @Override + public ScoreboardGroup getGroup() { + return ScoreboardGroup.REGION; + } + + @Override + public int order() { + return 3; + } + + @Override + public String get(Region region, Player p) { + ItemMode itemMode = getMode(region); + if (itemMode == ItemMode.ACTIVE) return null; + return "§e" + BauSystem.MESSAGE.parse(Flag.ITEMS.getChatValue(), p) + "§8: " + BauSystem.MESSAGE.parse(itemMode.getChatValue(), p); + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/FlagStorage.java b/BauSystem_Main/src/de/steamwar/bausystem/region/FlagStorage.java index b89f3df5..e0ee43a5 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/FlagStorage.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/FlagStorage.java @@ -84,7 +84,7 @@ public class FlagStorage { } public Flag.Value get(final Flag flagType) { - return flags.get(flagType); + return flags.getOrDefault(flagType, flagType.getDefaultValue()); } public boolean set(final Tag tag) { 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 0c45d1ce..86888666 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/flags/Flag.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/flags/Flag.java @@ -33,7 +33,9 @@ public enum Flag implements EnumDisplay { TNT("FLAG_TNT", TNTMode.class, TNTMode.ONLY_TB), FIRE("FLAG_FIRE", FireMode.class, FireMode.ALLOW), FREEZE("FLAG_FREEZE", FreezeMode.class, FreezeMode.INACTIVE), - PROTECT("FLAG_PROTECT", ProtectMode.class, ProtectMode.ACTIVE); + PROTECT("FLAG_PROTECT", ProtectMode.class, ProtectMode.ACTIVE), + ITEMS("FLAG_ITEMS", ItemMode.class, ItemMode.ACTIVE), + ; @Getter private static final Set flags; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/flags/flagvalues/ItemMode.java b/BauSystem_Main/src/de/steamwar/bausystem/region/flags/flagvalues/ItemMode.java new file mode 100644 index 00000000..c0cefa38 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/flags/flagvalues/ItemMode.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 ItemMode implements Flag.Value { + + ACTIVE("FLAG_ITEMS_ACTIVE"), + INACTIVE("FLAG_ITEMS_INACTIVE"); + + private static ItemMode[] values; + private final String chatValue; + + @Override + public ItemMode[] getValues() { + if (ItemMode.values == null) { + ItemMode.values = ItemMode.values(); //NOSONAR + } + return ItemMode.values; + } + + @Override + public ItemMode getValue() { + return this; + } + + @Override + public ItemMode getValueOf(final String name) { + try { + return ItemMode.valueOf(name.toUpperCase()); + } catch (IllegalArgumentException e) { + if (name.equalsIgnoreCase("false")) { + return ItemMode.INACTIVE; + } + return ItemMode.ACTIVE; + } + } +}