Persist scores between server launches

This commit is contained in:
2026-09-26 18:11:59 -07:00
parent 6100865cb0
commit 46c93685ad
2 changed files with 24 additions and 5 deletions

View File

@@ -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}." |

View File

@@ -17,6 +17,8 @@ along with ScoreKeeper. If not, see <https://www.gnu.org/licenses/agpl-3.0.txt>.
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.");