From b165d6711857a2f4b5108133f401f26cdaa25fe7 Mon Sep 17 00:00:00 2001 From: Tom Hicks Date: Sat, 26 Sep 2026 18:20:54 -0700 Subject: [PATCH] Notify players when score buckets change --- README.md | 2 ++ TODO.md | 2 +- .../scorekeeper/ScoreKeeperPlugin.java | 25 ++++++++++++++++++- .../commands/ScoreBucketCommand.java | 6 +++-- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9a826ce..58d3350 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,8 @@ * `/score-bucket create [admin|global|player|none]` - Creates a bucket (operators). * `/score-bucket ` - Switches a player or the server default (operators). +When a player's active bucket changes, they are told the bucket name and their current score with its singular or plural unit. + NOTES: * All commands will use the executing player in place of [playerName] if it is omitted. * Permissions support is coming AFTER I get archive to work. diff --git a/TODO.md b/TODO.md index 9163710..36f5410 100644 --- a/TODO.md +++ b/TODO.md @@ -23,6 +23,6 @@ | SK-01 | Done | Save scores between server launches. | | SK-02 | Done | 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 | Done | 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}." | +| SK-04 | Done | Tell a player when their bucket changes. Something like "You are now tracking ${id}. You have ${score} ${plural or singular depending in score}." | | SK-05 | Ready | Enable high score runs for a bucket. Admins or pugins should be able to start a run on a certain kind of score. The top n (configurable) players will have their scores reported globally for this bucket and the individual player will have their score reported to them when it changes. Scores will be optionally reset to their initialValue with the old score for this bucket saved. High score runs will have a lifetime of a target high score, a time like 4 hours or 1 week, or until-disabled. When the high score run is finished all players will be messaged a table with the top n places and their scores along with the individual user's place and score. The scores will be reset to their previous values if they were reset to their initialValue at the beginning otherwise they will be left alone. | | SK-06 | Ready | Score archiving. Scores either all or within a given bucket should be able to be archived and reset to their initial value. The archived scores should be able to be listed via a command. | 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 42efe30..43fdd18 100644 --- a/src/main/java/com/majinnaibu/minecraft/plugins/scorekeeper/ScoreKeeperPlugin.java +++ b/src/main/java/com/majinnaibu/minecraft/plugins/scorekeeper/ScoreKeeperPlugin.java @@ -174,19 +174,33 @@ public class ScoreKeeperPlugin extends JavaPlugin { } public void switchPlayerBucket(Player player, String bucketId) { - requireBucket(bucketId); + ScoreBucket bucket = requireBucket(bucketId); UUID playerId = player.getUniqueId(); + String previousBucketId = getPlayerBucket(player); if (bucketId.equals(_defaultBucketId)) { _playerBuckets.remove(playerId); } else { _playerBuckets.put(playerId, bucketId); } + if (!previousBucketId.equals(bucketId)) { + sendBucketChangeMessage(player, bucket); + } } public void switchAllPlayers(String bucketId) { requireBucket(bucketId); + Map previousBuckets = new LinkedHashMap<>(); + for (Player player : getServer().getOnlinePlayers()) { + previousBuckets.put(player.getUniqueId(), getPlayerBucket(player)); + } _defaultBucketId = bucketId; _playerBuckets.clear(); + ScoreBucket bucket = requireBucket(bucketId); + for (Player player : getServer().getOnlinePlayers()) { + if (!bucketId.equals(previousBuckets.get(player.getUniqueId()))) { + sendBucketChangeMessage(player, bucket); + } + } } // endregion @@ -204,6 +218,15 @@ public class ScoreKeeperPlugin extends JavaPlugin { return bucket; } + private void sendBucketChangeMessage(Player player, ScoreBucket bucket) { + int score = getScore(player, bucket.getId()); + String unit = score == 1 ? bucket.getSingular() : bucket.getPlural(); + sendMessage( + player, + Component.text( + "You are now tracking " + bucket.getId() + ". You have " + score + " " + unit + ".")); + } + private void loadScores() { File scoreFile = new File(getDataFolder(), "scores.yml"); if (!scoreFile.isFile()) { diff --git a/src/main/java/com/majinnaibu/minecraft/plugins/scorekeeper/commands/ScoreBucketCommand.java b/src/main/java/com/majinnaibu/minecraft/plugins/scorekeeper/commands/ScoreBucketCommand.java index 66c09fc..a8b9455 100644 --- a/src/main/java/com/majinnaibu/minecraft/plugins/scorekeeper/commands/ScoreBucketCommand.java +++ b/src/main/java/com/majinnaibu/minecraft/plugins/scorekeeper/commands/ScoreBucketCommand.java @@ -103,8 +103,10 @@ public class ScoreBucketCommand implements CommandExecutor { private void switchPlayerBucket(CommandSender sender, Player target, String bucketId) { try { _plugin.switchPlayerBucket(target, bucketId); - _plugin.sendMessage( - sender, Component.text(target.getName() + " is now tracking " + bucketId + ".")); + if (sender != target) { + _plugin.sendMessage( + sender, Component.text(target.getName() + " is now tracking " + bucketId + ".")); + } } catch (IllegalArgumentException ex) { sendError(sender, ex.getMessage()); }