From 1e65b76ccf5c92ecc8b959430b43423551eb34af Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 21 Mar 2021 09:33:54 -0700 Subject: [PATCH] Added option to broadcast advancement completion to online players. --- .../java/world/bentobox/boxed/Settings.java | 20 +++++++++++++++++++ .../boxed/listeners/AdvancementListener.java | 19 ++++++++++++++---- src/main/resources/config.yml | 3 +++ src/main/resources/locales/en-US.yml | 1 + 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/main/java/world/bentobox/boxed/Settings.java b/src/main/java/world/bentobox/boxed/Settings.java index 9e8705c..9cb18f1 100644 --- a/src/main/java/world/bentobox/boxed/Settings.java +++ b/src/main/java/world/bentobox/boxed/Settings.java @@ -54,6 +54,12 @@ public class Settings implements WorldSettings { @ConfigEntry(path = "boxed.command.default-action") private String defaultPlayerAction = "go"; + /* Boxed */ + @ConfigComment("Announce advancements. We recommend you set the game rule `/gamerule announceAdvancements false`") + @ConfigComment("but that blocks all new advancement announcements. This setting tells Boxed to broadcast new advancements.") + @ConfigEntry(path = "boxed.broadcast-advancements") + private boolean broadcastAdvancements; + /* WORLD */ @ConfigComment("Friendly name for this world. Used in admin commands. Must be a single word") @ConfigEntry(path = "world.friendly-name") @@ -1788,4 +1794,18 @@ public class Settings implements WorldSettings { public void setMakeNetherPortals(boolean makeNetherPortals) { this.makeNetherPortals = makeNetherPortals; } + + /** + * @return the broadcastAdvancements + */ + public boolean isBroadcastAdvancements() { + return broadcastAdvancements; + } + + /** + * @param broadcastAdvancements the broadcastAdvancements to set + */ + public void setBroadcastAdvancements(boolean broadcastAdvancements) { + this.broadcastAdvancements = broadcastAdvancements; + } } diff --git a/src/main/java/world/bentobox/boxed/listeners/AdvancementListener.java b/src/main/java/world/bentobox/boxed/listeners/AdvancementListener.java index 393d8ce..a98a81d 100644 --- a/src/main/java/world/bentobox/boxed/listeners/AdvancementListener.java +++ b/src/main/java/world/bentobox/boxed/listeners/AdvancementListener.java @@ -10,6 +10,7 @@ import java.util.stream.StreamSupport; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.NamespacedKey; +import org.bukkit.Server; import org.bukkit.Sound; import org.bukkit.Statistic; import org.bukkit.advancement.Advancement; @@ -88,8 +89,14 @@ public class AdvancementListener implements Listener { // Sync grantAdv(u, addon.getAdvManager().getIsland(island).getAdvancements()); }); + // Broadcast + if (addon.getSettings().isBroadcastAdvancements()) { + Bukkit.getOnlinePlayers().stream().filter(p -> p.hasPermission(Server.BROADCAST_CHANNEL_USERS)) + .map(User::getInstance) + .forEach(u -> u.sendMessage("boxed.user-completed", TextVariables.NAME, user.getName(), TextVariables.DESCRIPTION, this.keyToString(u, key))); + } } - + /** * Synchronize the player's advancements to that of the island. * Player's advancements should be cleared before calling this othewise they will get add the island ones as well. @@ -104,13 +111,17 @@ public class AdvancementListener implements Listener { private void informPlayer(User user, NamespacedKey key, int score) { user.getPlayer().playSound(user.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1F, 2F); + user.sendMessage("boxed.completed", TextVariables.NAME, keyToString(user, key)); + user.sendMessage("boxed.size-changed", TextVariables.NUMBER, String.valueOf(score)); + + } + + private String keyToString(User user, NamespacedKey key) { String adv = user.getTranslationOrNothing("boxed.advancements." + key.toString()); if (adv.isEmpty()) { adv = Util.prettifyText(key.getKey().substring(key.getKey().lastIndexOf("/") + 1, key.getKey().length())); } - user.sendMessage("boxed.completed", TextVariables.NAME, adv); - user.sendMessage("boxed.size-changed", TextVariables.NUMBER, String.valueOf(score)); - + return adv; } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 5ed2397..1fb40bf 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -15,6 +15,9 @@ boxed: # Sub-command of main player command that will be run on each player command call. # By default it is sub-command 'go'. default-action: go + # Announce advancements. We recommend you set the game rule `/gamerule announceAdvancements false` + # but that blocks all new advancement announcements. This setting tells Boxed to broadcast new advancements. + broadcast-advancements: false world: # Friendly name for this world. Used in admin commands. Must be a single word friendly-name: Boxed diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index 6fa1158..af817ff 100755 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -4,6 +4,7 @@ boxed: completed: '&a [name] completed!' size-changed: '&a Box size increased by [number]!' + user-completed: '&a [name] completed [description]!' general: errors: no-island: '&c You do not have a box!'