Added option to broadcast advancement completion to online players.

This commit is contained in:
tastybento 2021-03-21 09:33:54 -07:00
parent fc7cef52c9
commit 1e65b76ccf
4 changed files with 39 additions and 4 deletions

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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

View File

@ -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!'