From ad87ad74954c3c35657a6638c2fc386df2536473 Mon Sep 17 00:00:00 2001 From: YoyoNow Date: Thu, 31 Jul 2025 13:53:10 +0200 Subject: [PATCH] Add RegionConfig --- .../features/autostart/AutostartListener.java | 12 +- .../design/endstone/DesignEndStone.java | 23 ++-- .../endstone/DesignEndStoneCommand.java | 2 +- .../region/RegionScoreboardElement.java | 7 +- .../features/techhider/TechHiderCommand.java | 14 +- .../bausystem/features/xray/XrayCommand.java | 21 +-- .../de/steamwar/bausystem/region/Region.java | 3 +- .../bausystem/region/RegionConfig.java | 124 ++++++++++++++++++ 8 files changed, 146 insertions(+), 60 deletions(-) create mode 100644 BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionConfig.java diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/autostart/AutostartListener.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/autostart/AutostartListener.java index 59e4fd3a..fbc6430f 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/autostart/AutostartListener.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/autostart/AutostartListener.java @@ -31,8 +31,6 @@ import lombok.Getter; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.block.data.type.Chest; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -41,7 +39,6 @@ import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; -import java.io.File; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -132,17 +129,10 @@ public class AutostartListener implements Listener { if (!regionStartTime.containsKey(region)) return; if (!region.getTestblockArea().inRegion(block.getLocation(), true)) return; long tickDiff = TPSUtils.currentRealTick.get() - regionStartTime.remove(region); - long preFightDurationInSeconds = getPreFightDurationInSeconds(region); + long preFightDurationInSeconds = region.getGameModeConfig().getTimes().getPreFightDuration(); RegionUtils.message(region, "AUTOSTART_MESSAGE_RESULT1", tickDiff); RegionUtils.message(region, "AUTOSTART_MESSAGE_RESULT2", preFightDurationInSeconds, ((preFightDurationInSeconds * 20) - tickDiff)); RegionUtils.message(region, "AUTOSTART_MESSAGE_RESULT3"); }); } - - private int getPreFightDurationInSeconds(Region region) { - File file = region.getGameModeConfig().orElse(null); - if (file == null) return 30; - FileConfiguration config = YamlConfiguration.loadConfiguration(file); - return config.getInt("Times.PreFightDuration", 30); - } } diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/design/endstone/DesignEndStone.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/design/endstone/DesignEndStone.java index 35437be8..129ebe71 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/design/endstone/DesignEndStone.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/design/endstone/DesignEndStone.java @@ -33,6 +33,7 @@ import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import java.util.*; +import java.util.stream.Collectors; public class DesignEndStone { @@ -42,7 +43,7 @@ public class DesignEndStone { private REntityServer entityServer = new REntityServer(); private List entities = new ArrayList<>(); private Set locations = new HashSet<>(); - private Set limited = new HashSet<>(); + private Set limited; private boolean calculateFromBottom; public DesignEndStone(Region region) { @@ -53,17 +54,15 @@ public class DesignEndStone { this.maxY = region.getBuildArea().getMaxPoint(false).getY(); this.maxZ = region.getBuildArea().getMaxPoint(false).getZ(); - YamlConfiguration config = YamlConfiguration.loadConfiguration(region.getGameModeConfig().get()); - for(Map entry : config.getMapList("Schematic.Limited")) { - int amount = (Integer) entry.get("Amount"); - Set materials = new HashSet<>((List) entry.get("Materials")); - if(amount == 0) { - materials.forEach(s -> { - limited.add(Material.getMaterial(s)); - }); - } - } - calculateFromBottom = config.getBoolean("Arena.NoFloor", false); + limited = region.getGameModeConfig() + .getSchematic() + .getLimited() + .entrySet() + .stream() + .filter(entry -> entry.getValue() == 0) + .flatMap(entry -> entry.getKey().stream()) + .collect(Collectors.toSet()); + calculateFromBottom = region.getGameModeConfig().getArena().isNoFloor(); entityServer.setCallback((player, rEntity, entityAction) -> { if (entityAction != REntityServer.EntityAction.ATTACK) return; diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/design/endstone/DesignEndStoneCommand.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/design/endstone/DesignEndStoneCommand.java index 61fa0278..6d10d479 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/design/endstone/DesignEndStoneCommand.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/design/endstone/DesignEndStoneCommand.java @@ -52,7 +52,7 @@ public class DesignEndStoneCommand extends SWCommand implements Listener { BauSystem.MESSAGE.send("DESIGN_ENDSTONE_REGION_ERROR", player); return; } - if (region.getGameModeConfig().isEmpty()) { + if (!region.getGameModeConfig().isLoaded()) { BauSystem.MESSAGE.send("DESIGN_ENDSTONE_REGION_ERROR", player); return; } diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/region/RegionScoreboardElement.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/region/RegionScoreboardElement.java index bfc3a6e4..b810bb1e 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/region/RegionScoreboardElement.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/region/RegionScoreboardElement.java @@ -46,11 +46,8 @@ public class RegionScoreboardElement implements ScoreboardElement { @Override public String get(Region region, Player p) { if (region.getType().isGlobal()) return null; - // TODO: Improve this! - Optional gameModeConfig = region.getGameModeConfig(); - if (gameModeConfig.isEmpty()) return null; - YamlConfiguration config = YamlConfiguration.loadConfiguration(region.getGameModeConfig().get()); - List names = config.getStringList("Server.ChatNames"); + if (!region.getGameModeConfig().isLoaded()) return null; + List names = region.getGameModeConfig().getServer().getChatNames(); if (names.isEmpty()) return null; return "§e" + BauSystem.MESSAGE.parse("SCOREBOARD_REGION", p) + "§8: §7" + names.get(0); } diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/techhider/TechHiderCommand.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/techhider/TechHiderCommand.java index e3510306..5f205e79 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/techhider/TechHiderCommand.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/techhider/TechHiderCommand.java @@ -69,26 +69,16 @@ public class TechHiderCommand extends SWCommand implements Listener, ScoreboardE } Optional techHider = techHiders.computeIfAbsent(region, rg -> { - File file = rg.getGameModeConfig().orElse(null); - if (file == null) { - return Optional.empty(); - } - - FileConfiguration config = YamlConfiguration.loadConfiguration(file); - if (!config.getBoolean("Techhider.Active", false)) { + if (!region.getGameModeConfig().getTechhider().isActive()) { return Optional.empty(); } hidden.put(rg, new HashSet<>()); - String obfuscateWith = config.getString("Techhider.ObfuscateWith", "end_stone"); - Set hiddenBlocks = Collections.unmodifiableSet(new HashSet<>(config.getStringList("Techhider.HiddenBlocks"))); - Set hiddenBlockEntities = Collections.unmodifiableSet(new HashSet<>(config.getStringList("Techhider.HiddenBlockEntities"))); - TechHider current = new TechHider((p, cX, cY) -> { if (rg.getBuildArea().isChunkOutside(cX, cY)) return true; return !hidden.get(rg).contains(p); - }, Material.valueOf(obfuscateWith.toUpperCase()), hiddenBlocks.stream().map(String::toUpperCase).map(Material::valueOf).collect(Collectors.toSet()), hiddenBlockEntities); + }, region.getGameModeConfig().getTechhider().getObfuscateWith(), region.getGameModeConfig().getTechhider().getHiddenBlocks(), region.getGameModeConfig().getTechhider().getHiddenBlockEntities()); current.enable(); return Optional.of(current); diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/xray/XrayCommand.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/xray/XrayCommand.java index 547e31d9..19f0a390 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/xray/XrayCommand.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/xray/XrayCommand.java @@ -19,8 +19,8 @@ package de.steamwar.bausystem.features.xray; -import de.steamwar.Reflection; import com.comphenix.tinyprotocol.TinyProtocol; +import de.steamwar.Reflection; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.features.techhider.TechHiderCommand; import de.steamwar.bausystem.region.Region; @@ -34,8 +34,6 @@ import de.steamwar.techhider.TechHider; import net.md_5.bungee.api.ChatMessageType; import org.bukkit.Bukkit; import org.bukkit.Material; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -43,7 +41,6 @@ import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerQuitEvent; -import java.io.File; import java.util.*; import java.util.function.BiFunction; @@ -70,24 +67,14 @@ public class XrayCommand extends SWCommand implements Listener, ScoreboardElemen } Optional techHider = techHiders.computeIfAbsent(region, rg -> { - File file = rg.getGameModeConfig().orElse(null); - if (file == null) { + if (!region.getGameModeConfig().getTechhider().isActive()) { return Optional.empty(); } - FileConfiguration config = YamlConfiguration.loadConfiguration(file); - if (!config.getBoolean("Techhider.Active", false)) { - Set blocks = new HashSet<>(Arrays.asList(Material.END_STONE, Material.IRON_BLOCK)); - xrayedBlocks.put(region, blocks); - return Optional.of(createXray(rg, blocks)); - } - hidden.put(rg, new HashSet<>()); - String obfuscateWith = config.getString("Techhider.ObfuscateWith", "end_stone"); - - Set blocks = new HashSet<>(Arrays.asList(Material.getMaterial(obfuscateWith.toUpperCase()))); - if (obfuscateWith.equals("end_stone")) blocks.add(Material.END_STONE_BRICKS); + Set blocks = new HashSet<>(Arrays.asList(region.getGameModeConfig().getTechhider().getObfuscateWith())); + if (blocks.contains(Material.END_STONE)) blocks.add(Material.END_STONE_BRICKS); xrayedBlocks.put(region, blocks); return Optional.of(createXray(rg, blocks)); }); diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java index 59b20632..fb6d0f55 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java @@ -24,7 +24,6 @@ import de.steamwar.bausystem.utils.PasteBuilder; import org.bukkit.Location; import java.io.File; -import java.util.Optional; import java.util.UUID; import java.util.function.BiConsumer; import java.util.stream.Stream; @@ -55,7 +54,7 @@ public interface Region { Area getTestblockArea(); - Optional getGameModeConfig(); + RegionConfig getGameModeConfig(); RegionHistory getHistory(); diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionConfig.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionConfig.java new file mode 100644 index 00000000..5c7da406 --- /dev/null +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionConfig.java @@ -0,0 +1,124 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 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; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.util.*; +import java.util.stream.Collectors; + +@Getter +public class RegionConfig { + + private final boolean loaded; + + private Times Times = new Times(); + private Schematic Schematic = new Schematic(); + private Arena Arena = new Arena(); + private Server Server = new Server(); + private Techhider Techhider = new Techhider(); + + public RegionConfig(File file) { + if (file == null || !file.exists()) { + loaded = false; + return; + } + loaded = true; + + FileConfiguration config = YamlConfiguration.loadConfiguration(file); + + Times.load(config.getConfigurationSection("Times")); + Schematic.load(config.getConfigurationSection("Schematic")); + Arena.load(config.getConfigurationSection("Arena")); + Server.load(config.getConfigurationSection("Server")); + Techhider.load(config.getConfigurationSection("Techhider")); + } + + @NoArgsConstructor(access = AccessLevel.PRIVATE) + @Getter + public static class Times { + private int PreFightDuration = 30; + + private void load(ConfigurationSection section) { + PreFightDuration = section.getInt("PreFightDuration", 30); + } + } + + @NoArgsConstructor(access = AccessLevel.PRIVATE) + @Getter + public static class Schematic { + private Map, Integer> Limited = new HashMap<>(); + + private void load(ConfigurationSection section) { + for(Map entry : section.getMapList("Limited")) { + int amount = (Integer) entry.get("Amount"); + Set materials = new HashSet<>((List) entry.get("Materials")); + if (amount == 0) { + materials.forEach(material -> Limited.put(Collections.singleton(Material.getMaterial(material)), 0)); + } else { + Limited.put(materials.stream().map(Material::getMaterial).collect(Collectors.toSet()), amount); + } + } + } + } + + @NoArgsConstructor(access = AccessLevel.PRIVATE) + @Getter + public static class Arena { + private boolean NoFloor = false; + + private void load(ConfigurationSection section) { + NoFloor = section.getBoolean("NoFloor", false); + } + } + + @NoArgsConstructor(access = AccessLevel.PRIVATE) + @Getter + public static class Server { + private List ChatNames; + + private void load(ConfigurationSection section) { + ChatNames = section.getStringList("ChatNames"); + } + } + + @NoArgsConstructor(access = AccessLevel.PRIVATE) + @Getter + public static class Techhider { + private boolean Active = false; + private Material ObfuscateWith = Material.END_STONE; + private Set HiddenBlocks = new HashSet<>(); + private Set HiddenBlockEntities = new HashSet<>(); + + private void load(ConfigurationSection section) { + Active = section.getBoolean("Active", false); + ObfuscateWith = Material.getMaterial(section.getString("ObfuscateWith", "END_STONE")); + HiddenBlocks = section.getStringList("HiddenBlocks").stream().map(Material::getMaterial).collect(Collectors.toUnmodifiableSet()); + HiddenBlockEntities = Collections.unmodifiableSet(new HashSet<>(section.getStringList("HiddenBlockEntities"))); + } + } +}