From 46c93685ade7b9a2ffd30d8993e9dd029d5d17c4 Mon Sep 17 00:00:00 2001 From: Tom Hicks Date: Sat, 26 Sep 2026 18:11:59 -0700 Subject: [PATCH] Persist scores between server launches --- TODO.md | 2 +- .../scorekeeper/ScoreKeeperPlugin.java | 27 ++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/TODO.md b/TODO.md index 593881f..0ee50cc 100644 --- a/TODO.md +++ b/TODO.md @@ -20,7 +20,7 @@ | ID | Status | Title | |:-----:|:--------:|:------| -| SK-01 | Ready | Save scores between server launches. | +| SK-01 | Done | Save scores between server launches. | | SK-02 | Ready | Allow tracking scores by bucket. An admin or other plugin should be able to create types of scores with named other than points. { id: "Bread", singular: "loaf", plural: "loaves", initialValue: 0 } Each player's score will be tracked in a bucket by id. Players may be all switched to a bucket or individually switched. |\ | SK-03 | Ready | Add optional score reporting to buckets with these four values. { id: "Bread", singular: "loaf", plural: "loaves", initialValue: 0, reporting:"admin|global|player|none" } admin means ops/admin only, global means everyone on the server, player means only tell the player, and none means don't report changes. | SK-04 | Ready | Tell a player when their bucket changes. Something like "You are now tracking ${id}. You have ${score} ${plural or singular depending in score}." | diff --git a/src/main/java/com/majinnaibu/minecraft/plugins/scorekeeper/ScoreKeeperPlugin.java b/src/main/java/com/majinnaibu/minecraft/plugins/scorekeeper/ScoreKeeperPlugin.java index 169f39b..f84aefc 100644 --- a/src/main/java/com/majinnaibu/minecraft/plugins/scorekeeper/ScoreKeeperPlugin.java +++ b/src/main/java/com/majinnaibu/minecraft/plugins/scorekeeper/ScoreKeeperPlugin.java @@ -17,6 +17,8 @@ along with ScoreKeeper. If not, see . package com.majinnaibu.minecraft.plugins.scorekeeper; +import java.io.File; +import java.io.IOException; import java.util.HashMap; import java.util.UUID; import java.util.logging.Level; @@ -31,6 +33,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -44,8 +47,15 @@ public class ScoreKeeperPlugin extends JavaPlugin { @Override public void onDisable() { - // TODO: save score data to file - logWarning("Unable to save scores to file. This feature is not implemented yet."); + YamlConfiguration scores = new YamlConfiguration(); + for (var entry : _playerScores.entrySet()) { + scores.set(entry.getKey().toString(), entry.getValue()); + } + try { + scores.save(new File(getDataFolder(), "scores.yml")); + } catch (IOException ex) { + logError(ex); + } } @Override @@ -56,8 +66,17 @@ public class ScoreKeeperPlugin extends JavaPlugin { getCommand("score-reset").setExecutor(new ScoreResetCommand(this)); getCommand("score-archive").setExecutor(new ScoreArchiveCommand(this)); - // TODO: load score data from file - logWarning("Unable to load scores from file. This feature is not " + "implemented yet."); + File scoreFile = new File(getDataFolder(), "scores.yml"); + if (scoreFile.isFile()) { + YamlConfiguration scores = YamlConfiguration.loadConfiguration(scoreFile); + for (String key : scores.getKeys(false)) { + try { + _playerScores.put(UUID.fromString(key), scores.getInt(key)); + } catch (IllegalArgumentException ex) { + logWarning("Ignoring score with invalid player UUID: " + key); + } + } + } logInfo( getPluginMeta().getName() + " version " + getPluginMeta().getVersion() + " is enabled.");