diff --git a/src/main/java/net/countercraft/movecraft/warfare/features/siege/Siege.java b/src/main/java/net/countercraft/movecraft/warfare/features/siege/Siege.java index 8054158..d31905c 100644 --- a/src/main/java/net/countercraft/movecraft/warfare/features/siege/Siege.java +++ b/src/main/java/net/countercraft/movecraft/warfare/features/siege/Siege.java @@ -1,8 +1,10 @@ package net.countercraft.movecraft.warfare.features.siege; -import java.time.LocalDateTime; -import java.util.concurrent.atomic.AtomicReference; - +import net.countercraft.movecraft.repair.MovecraftRepair; +import net.countercraft.movecraft.warfare.MovecraftWarfare; +import net.countercraft.movecraft.warfare.features.Warfare; +import net.countercraft.movecraft.warfare.features.siege.events.SiegeBroadcastEvent; +import net.countercraft.movecraft.warfare.localisation.I18nSupport; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.Sound; @@ -10,27 +12,23 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import net.countercraft.movecraft.warfare.localisation.I18nSupport; -import net.countercraft.movecraft.repair.MovecraftRepair; -import net.countercraft.movecraft.util.Pair; -import net.countercraft.movecraft.warfare.MovecraftWarfare; -import net.countercraft.movecraft.warfare.features.Warfare; -import net.countercraft.movecraft.warfare.features.siege.events.SiegeBroadcastEvent; +import java.time.LocalDateTime; +import java.util.concurrent.atomic.AtomicReference; public class Siege extends Warfare { public enum Stage { IN_PROGRESS, PREPARATION, INACTIVE } - @NotNull private String name; + @NotNull private final String name; @NotNull private final SiegeConfig config; @NotNull private final AtomicReference stage; @Nullable private LocalDateTime startTime; @Nullable private OfflinePlayer player; - public Siege(@NotNull Pair nameAndConfig) { - name = nameAndConfig.getLeft(); - config = nameAndConfig.getRight(); + public Siege(@NotNull String siegeName, @NotNull SiegeConfig siegeConfig) { + name = siegeName; + config = siegeConfig; stage = new AtomicReference<>(); stage.set(Stage.INACTIVE); startTime = null; diff --git a/src/main/java/net/countercraft/movecraft/warfare/features/siege/SiegeConfig.java b/src/main/java/net/countercraft/movecraft/warfare/features/siege/SiegeConfig.java index 2026666..7ba93ef 100644 --- a/src/main/java/net/countercraft/movecraft/warfare/features/siege/SiegeConfig.java +++ b/src/main/java/net/countercraft/movecraft/warfare/features/siege/SiegeConfig.java @@ -1,12 +1,9 @@ package net.countercraft.movecraft.warfare.features.siege; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - +import org.bukkit.configuration.ConfigurationSection; import org.jetbrains.annotations.NotNull; -import net.countercraft.movecraft.util.Pair; +import java.util.List; public class SiegeConfig { @NotNull private final String attackRegion; @@ -16,47 +13,54 @@ public class SiegeConfig { private final int scheduleStart, scheduleEnd, delayBeforeStart, duration, dailyIncome, cost; private final boolean doubleCostPerOwnedSiegeRegion; - public static Pair load(Map.Entry> config) { - Map siegeMap = (Map) config.getValue(); - return new Pair<>(config.getKey(), new SiegeConfig( - (String) siegeMap.get("SiegeRegion"), - (String) siegeMap.get("RegionToControl"), - (List) siegeMap.get("DaysOfTheWeek"), - (List) siegeMap.get("CraftsToWin"), - (List) siegeMap.get("CommandsOnStart"), - (List) siegeMap.get("CommandsOnWin"), - (List) siegeMap.get("CommandsOnLose"), - (int) siegeMap.get("ScheduleStart"), - (int) siegeMap.get("ScheduleEnd"), - (int) siegeMap.get("DelayBeforeStart"), - (int) siegeMap.get("SiegeDuration"), - (int) siegeMap.get("DailyIncome"), - (int) siegeMap.get("CostToSiege"), - (boolean) siegeMap.get("DoubleCostPerOwnedSiegeRegion") - )); - } + public SiegeConfig(ConfigurationSection config) { + if(!config.isString("SiegeRegion")) + throw new IllegalArgumentException("Invalid SiegeRegion."); + attackRegion = config.getString("SiegeRegion"); + + if(!config.isString("RegionToControl")) + throw new IllegalArgumentException("Invalid RegionToControl."); + captureRegion = config.getString("RegionToControl"); + + if(!config.contains("DaysOfTheWeek")) + throw new IllegalArgumentException("Invalid DaysOfTheWeek."); + daysOfWeek = config.getIntegerList("DaysOfTheWeek"); + + if(!config.contains("CraftsToWin")) + throw new IllegalArgumentException("Invalid CraftsToWin."); + craftsToWin = config.getStringList("CraftsToWin"); + + commandsOnStart = config.getStringList("CommandsOnStart"); + commandsOnWin = config.getStringList("CommandsOnWin"); + commandsOnLose = config.getStringList("CommandsOnLose"); - private SiegeConfig( - @NotNull String attackRegion, @NotNull String captureRegion, - @NotNull List daysOfWeek, - @NotNull List craftsToWin, @NotNull List commandsOnStart, - @NotNull List commandsOnWin, @NotNull List commandsOnLose, - int scheduleStart, int scheduleEnd, int delayBeforeStart, int duration, int dailyIncome, int cost, - boolean doubleCostPerOwnedSiegeRegion) { - this.attackRegion = attackRegion; - this.captureRegion = captureRegion; - this.daysOfWeek = daysOfWeek; - this.craftsToWin = craftsToWin; - this.commandsOnStart = commandsOnStart == null ? new ArrayList<>() : commandsOnStart; - this.commandsOnWin = commandsOnWin == null ? new ArrayList<>() : commandsOnWin; - this.commandsOnLose = commandsOnLose == null ? new ArrayList<>() : commandsOnLose; - this.scheduleStart = scheduleStart; - this.scheduleEnd = scheduleEnd; - this.delayBeforeStart = delayBeforeStart; - this.duration = duration; - this.dailyIncome = dailyIncome; - this.cost = cost; - this.doubleCostPerOwnedSiegeRegion = doubleCostPerOwnedSiegeRegion; + if(!config.isInt("ScheduleStart")) + throw new IllegalArgumentException("Invalid ScheduleStart"); + scheduleStart = config.getInt("ScheduleStart"); + + if(!config.isInt("ScheduleEnd")) + throw new IllegalArgumentException("Invalid ScheduleEnd"); + scheduleEnd = config.getInt("ScheduleEnd"); + + if(!config.isInt("DelayBeforeStart")) + throw new IllegalArgumentException("Invalid DelayBeforeStart"); + delayBeforeStart = config.getInt("DelayBeforeStart"); + + if(!config.isInt("SiegeDuration")) + throw new IllegalArgumentException("Invalid SiegeDuration"); + duration = config.getInt("SiegeDuration"); + + if(!config.isInt("DailyIncome")) + throw new IllegalArgumentException("Invalid DailyIncome"); + dailyIncome = config.getInt("DailyIncome"); + + if(!config.isInt("CostToSiege")) + throw new IllegalArgumentException("Invalid CostToSiege"); + cost = config.getInt("CostToSiege"); + + if(!config.isBoolean("DoubleCostPerOwnedSiegeRegion")) + throw new IllegalArgumentException("Invalid DoubleCostPerOwnedSiegeRegion"); + doubleCostPerOwnedSiegeRegion = config.getBoolean("DoubleCostPerOwnedSiegeRegion"); } @NotNull @@ -69,6 +73,7 @@ public String getAttackRegion() { return attackRegion; } + @NotNull public List getDaysOfWeek() { return daysOfWeek; } diff --git a/src/main/java/net/countercraft/movecraft/warfare/features/siege/SiegeManager.java b/src/main/java/net/countercraft/movecraft/warfare/features/siege/SiegeManager.java index 1fc03d6..0df9d51 100644 --- a/src/main/java/net/countercraft/movecraft/warfare/features/siege/SiegeManager.java +++ b/src/main/java/net/countercraft/movecraft/warfare/features/siege/SiegeManager.java @@ -1,22 +1,18 @@ package net.countercraft.movecraft.warfare.features.siege; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.util.Calendar; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - +import net.countercraft.movecraft.warfare.features.siege.tasks.SiegePaymentTask; +import net.countercraft.movecraft.warfare.features.siege.tasks.SiegePreparationTask; +import net.countercraft.movecraft.warfare.features.siege.tasks.SiegeProgressTask; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.Plugin; import org.bukkit.scheduler.BukkitRunnable; import org.jetbrains.annotations.NotNull; -import org.yaml.snakeyaml.Yaml; -import net.countercraft.movecraft.warfare.features.siege.tasks.SiegePaymentTask; -import net.countercraft.movecraft.warfare.features.siege.tasks.SiegePreparationTask; -import net.countercraft.movecraft.warfare.features.siege.tasks.SiegeProgressTask; +import java.io.File; +import java.util.Calendar; +import java.util.HashSet; +import java.util.Set; public class SiegeManager extends BukkitRunnable { @NotNull private final Set sieges = new HashSet<>(); @@ -27,28 +23,35 @@ public SiegeManager(@NotNull Plugin warfare) { // load the sieges.yml file File siegesFile = new File(warfare.getDataFolder().getAbsolutePath() + "/sieges.yml"); - InputStream input; - try { - input = new FileInputStream(siegesFile); - } - catch (FileNotFoundException e) { - input = null; + if(!siegesFile.exists()) + { + warfare.saveResource("sieges.yml", false); } - if (input == null) { - warfare.getLogger().severe("Failed to load siege configuration. Please check the sieges.yml file."); + YamlConfiguration config = YamlConfiguration.loadConfiguration(siegesFile); + if(!config.contains("sieges") || !config.isConfigurationSection("sieges")) { + warfare.getLogger().severe("Failed to load siege configuration. All sieges should be contained within 'sieges' section."); + return; } - else { + + ConfigurationSection globalSection = config.getConfigurationSection("sieges"); + + for(String name : globalSection.getKeys(false)) { + if(!globalSection.isConfigurationSection(name)) { + warfare.getLogger().severe("Failed to load siege configuration for '" + name + "': Impropper formatting."); + continue; + } + ConfigurationSection siegeSection = globalSection.getConfigurationSection(name); + try { - Map data = new Yaml().loadAs(input, Map.class); - Map> siegesMap = (Map>) data.get("sieges"); - for (Map.Entry> entry : siegesMap.entrySet()) { - sieges.add(new Siege(SiegeConfig.load(entry))); - } - warfare.getLogger().info("Siege configuration loaded."); + sieges.add(new Siege(name, new SiegeConfig(siegeSection))); + } + catch (IllegalArgumentException e) { + warfare.getLogger().severe("Failed to load siege configuration for '" + name + "' " + e.getMessage()); } - catch (ClassCastException e) { - warfare.getLogger().severe("Failed to load siege configuration. Please check the sieges.yml file."); + catch (NullPointerException e) { + warfare.getLogger().severe("Failed to load siege configuration for '" + name + "'"); + e.printStackTrace(); } } } diff --git a/src/main/resources/sieges.yml b/src/main/resources/sieges.yml new file mode 100644 index 0000000..c6060e5 --- /dev/null +++ b/src/main/resources/sieges.yml @@ -0,0 +1,28 @@ +sieges: # This section must be here and must be the only one inside this file + exampleName: # Name of the siege, only used for organization and error checking + SiegeRegion: "ExampleSiegeBox" # The WorldGuard name of the siege region that people fight in + RegionToControl: "ExampleSiegeCity" # The region that is transferred by winning the siege + DaysOfTheWeek: # Days of the week that the siege can happen. 1 is Sunday + - 1 + - 2 + - 3 + - 4 + - 5 + - 6 + - 7 + CraftsToWin: # The craft types that can siege this region + - "Airship" + - "MediumAirship" + CommandsOnStart: # Commands the server runs on siege start (Optional) + - "say hi" + CommandsOnWin: # Commands the server runs on siege win (Optional) + - "say hi" + CommandsOnLose: # Commands the server runs on siege lose (Optional) + - "say hi" + ScheduleStart: 0 # Time of day where siege window is active written in 24 hour time (for example 2300) + ScheduleEnd: 2400 # Time of day where siege window is active written in 24 hour time (for example 2300) + DelayBeforeStart: 30 # Duration of the siege's preparationstage in seconds + SiegeDuration: 2400 # Duration of the siege's progress stage in seconds + DailyIncome: 0 # Amount of in-game money earned per day while after siege victory + CostToSiege: 0 # Cost to begin this siege + DoubleCostPerOwnedSiegeRegion: false # When enabled the siege cost is doubled for every siege region owned by the siege leader