diff --git a/src/main/java/net/knarcraft/minigames/arena/dropper/DropperArena.java b/src/main/java/net/knarcraft/minigames/arena/dropper/DropperArena.java index e4200fb..88e5d68 100644 --- a/src/main/java/net/knarcraft/minigames/arena/dropper/DropperArena.java +++ b/src/main/java/net/knarcraft/minigames/arena/dropper/DropperArena.java @@ -21,6 +21,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.UUID; +import java.util.logging.Level; import static net.knarcraft.minigames.util.InputValidationHelper.isInvalid; @@ -167,13 +168,13 @@ public class DropperArena implements Arena { public void addReward(@NotNull RewardCondition rewardCondition, @NotNull Reward reward) { this.rewards.computeIfAbsent(rewardCondition, k -> new HashSet<>()); this.rewards.get(rewardCondition).add(reward); - this.dropperArenaHandler.saveArenas(); + this.saveArena(); } @Override public void clearRewards(@NotNull RewardCondition rewardCondition) { this.rewards.remove(rewardCondition); - this.dropperArenaHandler.saveArenas(); + this.saveArena(); } @Override @@ -267,7 +268,7 @@ public class DropperArena implements Arena { return false; } else { this.spawnLocation = newLocation; - this.dropperArenaHandler.saveArenas(); + this.saveArena(); return true; } } @@ -283,7 +284,7 @@ public class DropperArena implements Arena { return false; } else { this.exitLocation = newLocation; - this.dropperArenaHandler.saveArenas(); + this.saveArena(); return true; } } @@ -300,7 +301,7 @@ public class DropperArena implements Arena { this.arenaName = arenaName; // Update the arena lookup map to make sure the new name can be used immediately this.dropperArenaHandler.updateLookupName(oldName, this.getArenaNameSanitized()); - this.dropperArenaHandler.saveArenas(); + this.saveArena(); return true; } else { return false; @@ -320,7 +321,7 @@ public class DropperArena implements Arena { return false; } else { this.winBlockType = material; - this.dropperArenaHandler.saveArenas(); + this.saveArena(); return true; } } @@ -338,7 +339,7 @@ public class DropperArena implements Arena { return false; } else { this.playerHorizontalVelocity = horizontalVelocity; - this.dropperArenaHandler.saveArenas(); + this.saveArena(); return true; } } @@ -354,11 +355,24 @@ public class DropperArena implements Arena { return false; } else { this.playerVerticalVelocity = verticalVelocity; - this.dropperArenaHandler.saveArenas(); + this.saveArena(); return true; } } + /** + * Saves this arena to disk + */ + public void saveArena() { + try { + DropperArenaStorageHelper.saveSingleDropperArena(this); + } catch (IOException exception) { + MiniGames.log(Level.SEVERE, "Unable to save arena! " + + "Data loss can occur!"); + MiniGames.log(Level.SEVERE, exception.getMessage()); + } + } + @Override public boolean equals(Object other) { if (!(other instanceof DropperArena otherArena)) { diff --git a/src/main/java/net/knarcraft/minigames/arena/parkour/ParkourArena.java b/src/main/java/net/knarcraft/minigames/arena/parkour/ParkourArena.java index 798e3ab..bbe15a5 100644 --- a/src/main/java/net/knarcraft/minigames/arena/parkour/ParkourArena.java +++ b/src/main/java/net/knarcraft/minigames/arena/parkour/ParkourArena.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; +import java.util.logging.Level; import static net.knarcraft.minigames.util.InputValidationHelper.isInvalid; @@ -180,13 +181,13 @@ public class ParkourArena implements Arena { public void addReward(@NotNull RewardCondition rewardCondition, @NotNull Reward reward) { this.rewards.computeIfAbsent(rewardCondition, k -> new HashSet<>()); this.rewards.get(rewardCondition).add(reward); - this.parkourArenaHandler.saveArenas(); + this.saveArena(); } @Override public void clearRewards(@NotNull RewardCondition rewardCondition) { this.rewards.remove(rewardCondition); - this.parkourArenaHandler.saveArenas(); + this.saveArena(); } @Override @@ -314,7 +315,7 @@ public class ParkourArena implements Arena { return false; } else { this.spawnLocation = newLocation; - this.parkourArenaHandler.saveArenas(); + this.saveArena(); return true; } } @@ -330,7 +331,7 @@ public class ParkourArena implements Arena { return false; } else { this.exitLocation = newLocation; - this.parkourArenaHandler.saveArenas(); + this.saveArena(); return true; } } @@ -347,7 +348,7 @@ public class ParkourArena implements Arena { this.arenaName = arenaName; // Update the arena lookup map to make sure the new name can be used immediately this.parkourArenaHandler.updateLookupName(oldName, this.getArenaNameSanitized()); - this.parkourArenaHandler.saveArenas(); + this.saveArena(); return true; } else { return false; @@ -367,7 +368,7 @@ public class ParkourArena implements Arena { return false; } else { this.winBlockType = material; - this.parkourArenaHandler.saveArenas(); + this.saveArena(); return true; } } @@ -383,7 +384,7 @@ public class ParkourArena implements Arena { return false; } else { this.winLocation = newLocation.clone(); - this.parkourArenaHandler.saveArenas(); + this.saveArena(); return true; } } @@ -406,7 +407,7 @@ public class ParkourArena implements Arena { this.killPlaneBlockNames = killPlaneBlockNames; this.killPlaneBlocks = parsed; } - this.parkourArenaHandler.saveArenas(); + this.saveArena(); return true; } @@ -422,7 +423,7 @@ public class ParkourArena implements Arena { } this.checkpoints.add(checkpoint.clone()); - this.parkourArenaHandler.saveArenas(); + this.saveArena(); return true; } @@ -437,10 +438,23 @@ public class ParkourArena implements Arena { } this.checkpoints.clear(); - this.parkourArenaHandler.saveArenas(); + this.saveArena(); return true; } + /** + * Saves this arena to disk + */ + public void saveArena() { + try { + ParkourArenaStorageHelper.saveSingleParkourArena(this); + } catch (IOException exception) { + MiniGames.log(Level.SEVERE, "Unable to save arena! " + + "Data loss can occur!"); + MiniGames.log(Level.SEVERE, exception.getMessage()); + } + } + @Override public boolean equals(Object other) { if (!(other instanceof ParkourArena otherArena)) { diff --git a/src/main/java/net/knarcraft/minigames/util/DropperArenaStorageHelper.java b/src/main/java/net/knarcraft/minigames/util/DropperArenaStorageHelper.java index 7e22f31..a32bc6f 100644 --- a/src/main/java/net/knarcraft/minigames/util/DropperArenaStorageHelper.java +++ b/src/main/java/net/knarcraft/minigames/util/DropperArenaStorageHelper.java @@ -97,22 +97,46 @@ public final class DropperArenaStorageHelper { YamlConfiguration configuration = new YamlConfiguration(); ConfigurationSection arenaSection = configuration.createSection(dropperArenasConfigurationSection); for (DropperArena arena : arenas.values()) { - //Note: While the arena name is used as the key, as the key has to be sanitized, the un-sanitized arena name - // must be stored as well - @NotNull ConfigurationSection configSection = arenaSection.createSection(arena.getArenaId().toString()); - configSection.set(DropperArenaStorageKey.ID.getKey(), new SerializableUUID(arena.getArenaId())); - configSection.set(DropperArenaStorageKey.NAME.getKey(), arena.getArenaName()); - configSection.set(DropperArenaStorageKey.SPAWN_LOCATION.getKey(), arena.getSpawnLocation()); - configSection.set(DropperArenaStorageKey.EXIT_LOCATION.getKey(), arena.getExitLocation()); - configSection.set(DropperArenaStorageKey.PLAYER_VERTICAL_VELOCITY.getKey(), arena.getPlayerVerticalVelocity()); - configSection.set(DropperArenaStorageKey.PLAYER_HORIZONTAL_VELOCITY.getKey(), arena.getPlayerHorizontalVelocity()); - configSection.set(DropperArenaStorageKey.WIN_BLOCK_TYPE.getKey(), new SerializableMaterial(arena.getWinBlockType())); - RewardStorageHelper.saveRewards(arena, configSection, DropperArenaStorageKey.REWARDS.getKey()); - saveDropperArenaData(arena.getData()); + saveDropperArena(arenaSection, arena); } configuration.save(dropperArenaFile); } + /** + * Saves a single arena + * + * @param arena

The arena to save

+ * @throws IOException

If unable to write to the file

+ */ + public static void saveSingleDropperArena(DropperArena arena) throws IOException { + YamlConfiguration configuration = new YamlConfiguration(); + ConfigurationSection arenaSection = configuration.createSection(dropperArenasConfigurationSection); + saveDropperArena(arenaSection, arena); + configuration.save(dropperArenaFile); + } + + /** + * Updates the given configuration section with the arena's data, and stores arena data for the arena + * + * @param arenaSection

The configuration section to update

+ * @param arena

The arena to save

+ * @throws IOException

If unable to save the arena data

+ */ + public static void saveDropperArena(ConfigurationSection arenaSection, DropperArena arena) throws IOException { + //Note: While the arena name is used as the key, as the key has to be sanitized, the un-sanitized arena name + // must be stored as well + @NotNull ConfigurationSection configSection = arenaSection.createSection(arena.getArenaId().toString()); + configSection.set(DropperArenaStorageKey.ID.getKey(), new SerializableUUID(arena.getArenaId())); + configSection.set(DropperArenaStorageKey.NAME.getKey(), arena.getArenaName()); + configSection.set(DropperArenaStorageKey.SPAWN_LOCATION.getKey(), arena.getSpawnLocation()); + configSection.set(DropperArenaStorageKey.EXIT_LOCATION.getKey(), arena.getExitLocation()); + configSection.set(DropperArenaStorageKey.PLAYER_VERTICAL_VELOCITY.getKey(), arena.getPlayerVerticalVelocity()); + configSection.set(DropperArenaStorageKey.PLAYER_HORIZONTAL_VELOCITY.getKey(), arena.getPlayerHorizontalVelocity()); + configSection.set(DropperArenaStorageKey.WIN_BLOCK_TYPE.getKey(), new SerializableMaterial(arena.getWinBlockType())); + RewardStorageHelper.saveRewards(arena, configSection, DropperArenaStorageKey.REWARDS.getKey()); + saveDropperArenaData(arena.getData()); + } + /** * Loads all arenas * diff --git a/src/main/java/net/knarcraft/minigames/util/ParkourArenaStorageHelper.java b/src/main/java/net/knarcraft/minigames/util/ParkourArenaStorageHelper.java index 876f823..c54bc5b 100644 --- a/src/main/java/net/knarcraft/minigames/util/ParkourArenaStorageHelper.java +++ b/src/main/java/net/knarcraft/minigames/util/ParkourArenaStorageHelper.java @@ -99,23 +99,61 @@ public final class ParkourArenaStorageHelper { YamlConfiguration configuration = new YamlConfiguration(); ConfigurationSection arenaSection = configuration.createSection(parkourArenasConfigurationSection); for (ParkourArena arena : arenas.values()) { - //Note: While the arena name is used as the key, as the key has to be sanitized, the un-sanitized arena name - // must be stored as well - @NotNull ConfigurationSection configSection = arenaSection.createSection(arena.getArenaId().toString()); - configSection.set(ParkourArenaStorageKey.ID.getKey(), new SerializableUUID(arena.getArenaId())); - configSection.set(ParkourArenaStorageKey.NAME.getKey(), arena.getArenaName()); - configSection.set(ParkourArenaStorageKey.SPAWN_LOCATION.getKey(), arena.getSpawnLocation()); - configSection.set(ParkourArenaStorageKey.EXIT_LOCATION.getKey(), arena.getExitLocation()); - configSection.set(ParkourArenaStorageKey.WIN_BLOCK_TYPE.getKey(), new SerializableMaterial(arena.getWinBlockType())); - configSection.set(ParkourArenaStorageKey.WIN_LOCATION.getKey(), arena.getWinLocation()); - configSection.set(ParkourArenaStorageKey.KILL_PLANE_BLOCKS.getKey(), arena.getKillPlaneBlockNames()); - configSection.set(ParkourArenaStorageKey.CHECKPOINTS.getKey(), arena.getCheckpoints()); - RewardStorageHelper.saveRewards(arena, configSection, ParkourArenaStorageKey.REWARDS.getKey()); - saveParkourArenaData(arena.getData()); + saveParkourArena(arenaSection, arena); } configuration.save(parkourArenaFile); } + /** + * Saves a single arena + * + * @param arena

The arena to save

+ * @throws IOException

If unable to write to the file

+ */ + public static void saveSingleParkourArena(ParkourArena arena) throws IOException { + YamlConfiguration configuration = new YamlConfiguration(); + ConfigurationSection arenaSection = configuration.createSection(parkourArenasConfigurationSection); + saveParkourArena(arenaSection, arena); + configuration.save(parkourArenaFile); + } + + /** + * Updates the given configuration section with the arena's data, and stores arena data for the arena + * + * @param arenaSection

The configuration section to update

+ * @param arena

The arena to save

+ * @throws IOException

If unable to save the arena data

+ */ + public static void saveParkourArena(ConfigurationSection arenaSection, ParkourArena arena) throws IOException { + //Note: While the arena name is used as the key, as the key has to be sanitized, the un-sanitized arena name + // must be stored as well + @NotNull ConfigurationSection configSection = arenaSection.createSection(arena.getArenaId().toString()); + configSection.set(ParkourArenaStorageKey.ID.getKey(), new SerializableUUID(arena.getArenaId())); + configSection.set(ParkourArenaStorageKey.NAME.getKey(), arena.getArenaName()); + configSection.set(ParkourArenaStorageKey.SPAWN_LOCATION.getKey(), arena.getSpawnLocation()); + configSection.set(ParkourArenaStorageKey.EXIT_LOCATION.getKey(), arena.getExitLocation()); + configSection.set(ParkourArenaStorageKey.WIN_BLOCK_TYPE.getKey(), new SerializableMaterial(arena.getWinBlockType())); + configSection.set(ParkourArenaStorageKey.WIN_LOCATION.getKey(), arena.getWinLocation()); + configSection.set(ParkourArenaStorageKey.KILL_PLANE_BLOCKS.getKey(), getKillPlaneBlocks(arena)); + configSection.set(ParkourArenaStorageKey.CHECKPOINTS.getKey(), arena.getCheckpoints()); + RewardStorageHelper.saveRewards(arena, configSection, ParkourArenaStorageKey.REWARDS.getKey()); + saveParkourArenaData(arena.getData()); + } + + /** + * Gets a list of the kill plane blocks for the given arena + * + * @param arena

The arena to get kill plane blocks for

+ * @return

The kill plane blocks

+ */ + private static List getKillPlaneBlocks(ParkourArena arena) { + if (arena.getKillPlaneBlockNames() == null) { + return new ArrayList<>(); + } else { + return new ArrayList<>(arena.getKillPlaneBlockNames()); + } + } + /** * Loads all arenas * @@ -163,7 +201,13 @@ public final class ParkourArenaStorageHelper { Location winLocation = (Location) configurationSection.get(ParkourArenaStorageKey.WIN_LOCATION.getKey()); SerializableMaterial winBlockType = (SerializableMaterial) configurationSection.get( ParkourArenaStorageKey.WIN_BLOCK_TYPE.getKey()); - List killPlaneBlockNames = configurationSection.getList(ParkourArenaStorageKey.KILL_PLANE_BLOCKS.getKey()); + List killPlaneBlockNamesList = configurationSection.getList(ParkourArenaStorageKey.KILL_PLANE_BLOCKS.getKey()); + Set killPlaneBlockNames; + if (killPlaneBlockNamesList == null) { + killPlaneBlockNames = new HashSet<>(); + } else { + killPlaneBlockNames = new HashSet<>((List) killPlaneBlockNamesList); + } List checkpoints = (List) configurationSection.get(ParkourArenaStorageKey.CHECKPOINTS.getKey()); Map> rewards = RewardStorageHelper.loadRewards(configurationSection, @@ -195,8 +239,7 @@ public final class ParkourArenaStorageHelper { } return new ParkourArena(arenaId, arenaName, spawnLocation, exitLocation, winBlockType.getRawValue(), winLocation, - (Set) killPlaneBlockNames, checkpoints, rewards, arenaData, - MiniGames.getInstance().getParkourArenaHandler()); + killPlaneBlockNames, checkpoints, rewards, arenaData, MiniGames.getInstance().getParkourArenaHandler()); } /**