From 3506b416f62cb5aaa0d49a593751aa05a3a4b530 Mon Sep 17 00:00:00 2001 From: Tom Hicks Date: Mon, 14 Jul 2025 09:11:55 -0700 Subject: [PATCH] Fixes serialization/deserialization of the score table. --- TODO.md | 6 +- build.gradle | 1 + .../plugins/mobscores/MobScoresPlugin.java | 128 +++++++++++------- src/main/resources/config.yml | 23 ++++ 4 files changed, 103 insertions(+), 55 deletions(-) create mode 100644 src/main/resources/config.yml diff --git a/TODO.md b/TODO.md index b9131ab..0b5a115 100644 --- a/TODO.md +++ b/TODO.md @@ -10,10 +10,10 @@ - [x] Refactor all logger usage to use getLogger() from JavaPlugin. - [x] Refactor all event listeners to use the modern event system (Listener interface, @EventHandler, registerEvents). - [x] Replace use of org.bukkit.util.config.Configuration with the modern configuration API (getConfig(), saveConfig(), etc.). -- [ ] Update score table to use Bukkit entity types or enums instead of CraftBukkit class names. +- [x] Update score table to use Bukkit entity types or enums instead of CraftBukkit class names. - [ ] If storing player scores, refactor to use UUID as the key instead of Player or String. -- [ ] Build the plugin with Gradle (./gradlew build) and test on a modern Paper server using the provided bash scripts. _(Depends on: all refactors and plugin.yml update)_ -- [ ] Address any bugs or incompatibilities found during testing on a modern server. _(Depends on: build and test)_ +- [x] Build the plugin with Gradle (./gradlew build) and test on a modern Paper server using the provided bash scripts. +- [x] Address any bugs or incompatibilities found during testing on a modern server. - [ ] Update README.md and CONTRIBUTING.md with new build, usage, and development instructions. _(Depends on: bugfixes)_ - [ ] (Optional) Add new features, quality-of-life improvements, automated tests, or CI configuration. _(Depends on: docs update)_ - [x] Update MobScores to depend on the latest version of the ScoreKeeper plugin (update dependency in build.gradle and plugin.yml as needed). diff --git a/build.gradle b/build.gradle index 31a184d..4ad4f96 100644 --- a/build.gradle +++ b/build.gradle @@ -51,6 +51,7 @@ tasks.withType(Javadoc) { } processResources { + inputs.property "version", project.version filesMatching('plugin.yml') { expand(project.properties) } diff --git a/src/main/java/com/majinnaibu/minecraft/plugins/mobscores/MobScoresPlugin.java b/src/main/java/com/majinnaibu/minecraft/plugins/mobscores/MobScoresPlugin.java index 1ecde28..28dd3cf 100644 --- a/src/main/java/com/majinnaibu/minecraft/plugins/mobscores/MobScoresPlugin.java +++ b/src/main/java/com/majinnaibu/minecraft/plugins/mobscores/MobScoresPlugin.java @@ -20,12 +20,13 @@ package com.majinnaibu.minecraft.plugins.mobscores; import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import java.util.Set; +import java.util.logging.Level; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.entity.Zombie; import org.bukkit.plugin.PluginManager; @@ -37,7 +38,7 @@ import com.majinnaibu.minecraft.plugins.scorekeeper.ScoreKeeperPlugin; public class MobScoresPlugin extends JavaPlugin { private Map _claimedMobs = new HashMap(); - private Map _scoreTable = new HashMap(); + private Map _scoreTable = new HashMap(); private ScoreKeeperPlugin _scoreKeeper = null; private final String _logPrefix = "[MobScores] "; @@ -47,27 +48,24 @@ public class MobScoresPlugin extends JavaPlugin { @Override public void onDisable() { - getConfig().set("ScoreTable", _scoreTable); + getConfig().set("ScoreTable", serializeScoreTable(_scoreTable)); saveConfig(); } - @SuppressWarnings("unchecked") @Override public void onEnable() { // Create the default config if it doesn't exist. saveDefaultConfig(); + getConfig().set("ScoreTable", serializeScoreTable(getDefaultScoreTable())); + saveConfig(); + // Load our score table from config or set defaults. - if (getConfig().contains("ScoreTable")) { - Object rawValue = getConfig().get("ScoreTable"); - if (rawValue instanceof Map) { - _scoreTable = new HashMap<>((Map) rawValue); - } else { - _scoreTable = getDefaultScoreTable(); - } + if (getConfig().isConfigurationSection("ScoreTable")) { + deserializeScoreTable(getConfig().getConfigurationSection("ScoreTable").getValues(false)); } else { _scoreTable = getDefaultScoreTable(); - getConfig().set("ScoreTable", _scoreTable); + getConfig().set("ScoreTable", serializeScoreTable(_scoreTable)); saveConfig(); } @@ -75,6 +73,7 @@ public class MobScoresPlugin extends JavaPlugin { _scoreKeeper = (ScoreKeeperPlugin)pm.getPlugin("ScoreKeeper"); if(_scoreKeeper == null){ + logWarning("Unable to find ScoreKeeper plugin."); pm.disablePlugin(this); } @@ -84,25 +83,30 @@ public class MobScoresPlugin extends JavaPlugin { logInfo(getPluginMeta().getName() + " version " + getPluginMeta().getVersion() + " is enabled!"); } - private HashMap getDefaultScoreTable() { - HashMap scores = new HashMap(); + private HashMap getDefaultScoreTable() { + HashMap scores = new HashMap(); - scores.put("org.bukkit.craftbukkit.entity.CraftZombie", 50); - scores.put("org.bukkit.craftbukkit.entity.CraftChicken", 0); - scores.put("org.bukkit.craftbukkit.entity.CraftCow", 0); - scores.put("org.bukkit.craftbukkit.entity.CraftCreeper", 50); - scores.put("org.bukkit.craftbukkit.entity.CraftGhast", 100); - scores.put("org.bukkit.craftbukkit.entity.CraftGiant", 250); - scores.put("org.bukkit.craftbukkit.entity.CraftPig", 0); - scores.put("org.bukkit.craftbukkit.entity.CraftPigZombie", 25); - scores.put("org.bukkit.craftbukkit.entity.CraftPlayer", 0); - scores.put("org.bukkit.craftbukkit.entity.CraftSheep", 0); - scores.put("org.bukkit.craftbukkit.entity.CraftSkeleton", 50); - scores.put("org.bukkit.craftbukkit.entity.CraftSlime", 100); - scores.put("org.bukkit.craftbukkit.entity.CraftSpider", 50); - scores.put("org.bukkit.craftbukkit.entity.CraftSquid", 0); - scores.put("org.bukkit.craftbukkit.entity.CraftWolf", 50); - scores.put("org.bukkit.craftbukkit.entity.CraftZombie", 50); + scores.put(EntityType.BLAZE, 100); + scores.put(EntityType.CAVE_SPIDER, 100); + scores.put(EntityType.CREEPER, 50); + scores.put(EntityType.DROWNED, 25); + scores.put(EntityType.ELDER_GUARDIAN, 250); + scores.put(EntityType.ENDERMAN, 100); + scores.put(EntityType.ENDERMITE, 1000); + scores.put(EntityType.END_CRYSTAL, 250); + scores.put(EntityType.GHAST, 250); + scores.put(EntityType.GIANT, 250); + scores.put(EntityType.GUARDIAN, 250); + scores.put(EntityType.HUSK, 25); + scores.put(EntityType.PILLAGER, 25); + scores.put(EntityType.RAVAGER, 50); + scores.put(EntityType.SHULKER, 50); + scores.put(EntityType.SILVERFISH, 0); + scores.put(EntityType.SKELETON, 50); + scores.put(EntityType.WITHER_SKELETON, 100); + scores.put(EntityType.ZOMBIE, 25); + scores.put(EntityType.SLIME, 100); + scores.put(EntityType.SPIDER, 50); return scores; } @@ -110,50 +114,70 @@ public class MobScoresPlugin extends JavaPlugin { public void claimMob(Entity entity, Player damager) { if(entity instanceof Zombie){ _claimedMobs.put((Zombie)entity, damager); - } - + } } public void awardScore(Entity entity) { if(_claimedMobs.containsKey(entity)){ - Class scoreClass = entity.getClass(); - String className = scoreClass.getName(); - if(_scoreTable.containsKey(className)){ + EntityType type = entity.getType(); + if (_scoreTable.containsKey(type)) { Player player = _claimedMobs.get(entity); - int score = _scoreTable.get(className); - + int score = _scoreTable.get(type); _scoreKeeper.addScore(player, score); - }else{ - logWarning("Unable to award score for {" + className + "}"); - Set keys = _scoreTable.keySet(); - String str = null; - for(Iterator i = keys.iterator(); i.hasNext(); str = i.next()){ - logInfo("{" + str + "}"); - } + } else { + logWarning("Unable to award score for {" + type.toString() + "}"); } } } public void sendPlayerScoreTable(Player player) { - Iterator> i=_scoreTable.entrySet().iterator(); - Map.Entry pair = null; + Iterator> i = _scoreTable.entrySet().iterator(); + Map.Entry pair = null; for(pair = i.next(); i.hasNext(); pair = i.next()){ if(pair.getValue() != 0){ - String key = pair.getKey(); - if(key.startsWith("org.bukkit.craftbukkit.entity.Craft")){ - sendPlayerMessage(player, key.substring(35) + " = " + pair.getValue().toString()); - }else{ - sendPlayerMessage(player, key + " = " + pair.getValue().toString()); - } + EntityType type = pair.getKey(); + Component entityName = Component.translatable(type.translationKey()); + sendPlayerMessage(player, entityName + " = " + pair.getValue().toString()); } } } + private Map deserializeScoreTable(Map rawValue) { + HashMap scoreTable = new HashMap<>(); + if (rawValue instanceof Map) { + scoreTable = new HashMap<>(); + for (Map.Entry entry : rawValue.entrySet()) { + try { + EntityType type = EntityType.valueOf(entry.getKey()); + Integer value = ((Number) entry.getValue()).intValue(); + scoreTable.put(type, value); + } catch (Exception ex) { + logError(ex); + } + } + return scoreTable; + } else { + return getDefaultScoreTable(); + } + } + + private Map serializeScoreTable(Map scoreTable) { + Map serializable = new HashMap<>(); + for (Map.Entry entry : scoreTable.entrySet()) { + serializable.put(entry.getKey().name(), entry.getValue()); + } + return serializable; + } + public void sendPlayerMessage(Player player, String message) { player.sendMessage(_messagePrefix.append(Component.text(message))); } + public void logError(Exception ex) { + getLogger().log(Level.SEVERE, _logPrefix + ex.toString()); + } + public void logInfo(String messag) { getLogger().info(_logPrefix + messag); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..3e4990d --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,23 @@ +# MobScores default configuration +ScoreTable: + DROWNED: 25 + ENDERMAN: 100 + GUARDIAN: 250 + SILVERFISH: 0 + ELDER_GUARDIAN: 250 + HUSK: 25 + BLAZE: 100 + GHAST: 250 + ENDERMITE: 1000 + SPIDER: 50 + PILLAGER: 25 + GIANT: 250 + END_CRYSTAL: 250 + SKELETON: 50 + WITHER_SKELETON: 100 + CREEPER: 50 + ZOMBIE: 25 + CAVE_SPIDER: 100 + SLIME: 100 + RAVAGER: 50 + SHULKER: 50 \ No newline at end of file