Skip to content

Commit

Permalink
Add sieges.yml and add better error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
HumorousFool committed Nov 17, 2023
1 parent 6687b8c commit 0628a4d
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
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;
import org.bukkit.entity.Player;
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> stage;
@Nullable private LocalDateTime startTime;
@Nullable private OfflinePlayer player;

public Siege(@NotNull Pair<String, SiegeConfig> 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,47 +13,54 @@ public class SiegeConfig {
private final int scheduleStart, scheduleEnd, delayBeforeStart, duration, dailyIncome, cost;
private final boolean doubleCostPerOwnedSiegeRegion;

public static Pair<String, SiegeConfig> load(Map.Entry<String, Map<String, ?>> config) {
Map<String,Object> siegeMap = (Map<String, Object>) config.getValue();
return new Pair<>(config.getKey(), new SiegeConfig(
(String) siegeMap.get("SiegeRegion"),
(String) siegeMap.get("RegionToControl"),
(List<Integer>) siegeMap.get("DaysOfTheWeek"),
(List<String>) siegeMap.get("CraftsToWin"),
(List<String>) siegeMap.get("CommandsOnStart"),
(List<String>) siegeMap.get("CommandsOnWin"),
(List<String>) 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<Integer> daysOfWeek,
@NotNull List<String> craftsToWin, @NotNull List<String> commandsOnStart,
@NotNull List<String> commandsOnWin, @NotNull List<String> 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
Expand All @@ -69,6 +73,7 @@ public String getAttackRegion() {
return attackRegion;
}

@NotNull
public List<Integer> getDaysOfWeek() {
return daysOfWeek;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Siege> sieges = new HashSet<>();
Expand All @@ -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<String, Map<String, ?>> siegesMap = (Map<String, Map<String, ?>>) data.get("sieges");
for (Map.Entry<String, Map<String, ?>> 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();
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/main/resources/sieges.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0628a4d

Please sign in to comment.