From a7b0506245f986f41b0c48d095be914e640527ef Mon Sep 17 00:00:00 2001 From: Christopher White Date: Mon, 27 May 2024 00:35:07 -0700 Subject: [PATCH] Switch parts of config parsing to PGMs TextParser.parseComponent (#61) Signed-off-by: Christopher White <18whitechristop@gmail.com> --- .../community/broadcast/BroadcastConfig.java | 20 ++++++++++++------ .../community/broadcast/BroadcastFeature.java | 9 ++++---- .../pgm/community/info/InfoCommandData.java | 21 +++++++++++-------- .../dev/pgm/community/motd/MotdConfig.java | 12 ++++++++--- .../dev/pgm/community/motd/MotdFeature.java | 7 +++---- 5 files changed, 42 insertions(+), 27 deletions(-) diff --git a/src/main/java/dev/pgm/community/broadcast/BroadcastConfig.java b/src/main/java/dev/pgm/community/broadcast/BroadcastConfig.java index 22391a37..497eac60 100644 --- a/src/main/java/dev/pgm/community/broadcast/BroadcastConfig.java +++ b/src/main/java/dev/pgm/community/broadcast/BroadcastConfig.java @@ -1,8 +1,13 @@ package dev.pgm.community.broadcast; +import static tc.oc.pgm.util.text.TextParser.parseComponent; + import dev.pgm.community.feature.config.FeatureConfigImpl; import java.util.List; +import java.util.stream.Collectors; +import net.kyori.adventure.text.Component; import org.bukkit.configuration.Configuration; +import tc.oc.pgm.util.text.TextParser; public class BroadcastConfig extends FeatureConfigImpl { @@ -15,8 +20,8 @@ public class BroadcastConfig extends FeatureConfigImpl { private boolean announceEnabled; private int announceDelay; - private String announcePrefix; - private List announceMessages; + private Component announcePrefix; + private List announceMessages; public BroadcastConfig(Configuration config) { super(KEY, config); @@ -42,11 +47,11 @@ public int getAnnounceDelay() { return announceDelay; } - public String getAnnouncePrefix() { + public Component getAnnouncePrefix() { return announcePrefix; } - public List getAnnounceMessages() { + public List getAnnounceMessages() { return announceMessages; } @@ -63,7 +68,10 @@ public void reload(Configuration config) { this.announceEnabled = config.getBoolean(getAnnounceKey() + ".enabled"); this.announceDelay = config.getInt(getAnnounceKey() + ".delay-seconds"); - this.announcePrefix = config.getString(getAnnounceKey() + ".prefix"); - this.announceMessages = config.getStringList(getAnnounceKey() + ".messages"); + this.announcePrefix = parseComponent(config.getString(getAnnounceKey() + ".prefix")); + this.announceMessages = + config.getStringList(getAnnounceKey() + ".messages").stream() + .map(TextParser::parseComponent) + .collect(Collectors.toList()); } } diff --git a/src/main/java/dev/pgm/community/broadcast/BroadcastFeature.java b/src/main/java/dev/pgm/community/broadcast/BroadcastFeature.java index 246c6fbe..08587ad2 100644 --- a/src/main/java/dev/pgm/community/broadcast/BroadcastFeature.java +++ b/src/main/java/dev/pgm/community/broadcast/BroadcastFeature.java @@ -55,14 +55,13 @@ private void announce() { if (!getBroadcastConfig().isAnnounceEnabled()) return; if (getBroadcastConfig().getAnnounceMessages().isEmpty()) return; Duration timeSince = Duration.between(lastAnnounce, Instant.now()); - List messages = getBroadcastConfig().getAnnounceMessages(); + List messages = getBroadcastConfig().getAnnounceMessages(); if (timeSince.getSeconds() > getBroadcastConfig().getAnnounceDelay()) { if (lastAnnounceIndex >= messages.size()) lastAnnounceIndex = 0; - String msg = messages.get(lastAnnounceIndex); - Component prefix = text(colorize(getBroadcastConfig().getAnnouncePrefix())); - Component message = text(colorize(msg)); - BroadcastUtils.sendGlobalMessage(text().append(prefix).append(message).build()); + Component prefix = getBroadcastConfig().getAnnouncePrefix(); + BroadcastUtils.sendGlobalMessage( + text().append(prefix).append(messages.get(lastAnnounceIndex)).build()); lastAnnounceIndex++; lastAnnounce = Instant.now(); } diff --git a/src/main/java/dev/pgm/community/info/InfoCommandData.java b/src/main/java/dev/pgm/community/info/InfoCommandData.java index 0808b30a..f38152e8 100644 --- a/src/main/java/dev/pgm/community/info/InfoCommandData.java +++ b/src/main/java/dev/pgm/community/info/InfoCommandData.java @@ -3,10 +3,12 @@ import static net.kyori.adventure.text.Component.text; import java.util.List; +import java.util.stream.Collectors; +import net.kyori.adventure.text.Component; import org.bukkit.command.CommandSender; import org.bukkit.configuration.ConfigurationSection; import tc.oc.pgm.util.Audience; -import tc.oc.pgm.util.bukkit.BukkitUtils; +import tc.oc.pgm.util.text.TextParser; public class InfoCommandData { @@ -14,10 +16,10 @@ public class InfoCommandData { private static final String PERMISSION_KEY = "permission"; private String name; - private List lines; + private List lines; private String permission; - public InfoCommandData(String name, List lines, String permission) { + public InfoCommandData(String name, List lines, String permission) { this.name = name; this.lines = lines; this.permission = permission; @@ -25,14 +27,18 @@ public InfoCommandData(String name, List lines, String permission) { public static InfoCommandData of(ConfigurationSection section) { return new InfoCommandData( - section.getName(), section.getStringList(LINES_KEY), section.getString(PERMISSION_KEY)); + section.getName(), + section.getStringList(LINES_KEY).stream() + .map(TextParser::parseComponent) + .collect(Collectors.toList()), + section.getString(PERMISSION_KEY)); } public String getName() { return name; } - public List getLines() { + public List getLines() { return lines; } @@ -50,9 +56,6 @@ public void sendCommand(CommandSender sender) { } } - getLines().stream() - .map(BukkitUtils::colorize) - .map(msg -> text(msg)) - .forEach(viewer::sendMessage); + getLines().forEach(viewer::sendMessage); } } diff --git a/src/main/java/dev/pgm/community/motd/MotdConfig.java b/src/main/java/dev/pgm/community/motd/MotdConfig.java index da78f11b..bef14102 100644 --- a/src/main/java/dev/pgm/community/motd/MotdConfig.java +++ b/src/main/java/dev/pgm/community/motd/MotdConfig.java @@ -2,25 +2,31 @@ import dev.pgm.community.feature.config.FeatureConfigImpl; import java.util.List; +import java.util.stream.Collectors; +import net.kyori.adventure.text.Component; import org.bukkit.configuration.Configuration; +import tc.oc.pgm.util.text.TextParser; public class MotdConfig extends FeatureConfigImpl { private static final String KEY = "motd"; - private List lines; + private List lines; public MotdConfig(Configuration config) { super(KEY, config); } - public List getLines() { + public List getLines() { return lines; } @Override public void reload(Configuration config) { super.reload(config); - this.lines = config.getStringList(getKey() + ".lines"); + this.lines = + config.getStringList(getKey() + ".lines").stream() + .map(TextParser::parseComponent) + .collect(Collectors.toList()); } } diff --git a/src/main/java/dev/pgm/community/motd/MotdFeature.java b/src/main/java/dev/pgm/community/motd/MotdFeature.java index a8c621e9..16f529b5 100644 --- a/src/main/java/dev/pgm/community/motd/MotdFeature.java +++ b/src/main/java/dev/pgm/community/motd/MotdFeature.java @@ -6,7 +6,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.PlayerJoinEvent; -import tc.oc.pgm.util.bukkit.BukkitUtils; +import tc.oc.pgm.util.Audience; /** MotdFeature - Displays a configurable message at login * */ public class MotdFeature extends FeatureBase { @@ -25,8 +25,7 @@ public MotdConfig getMotdConfig() { @EventHandler(priority = EventPriority.LOW) public void onPlayerJoin(PlayerJoinEvent event) { if (getMotdConfig().getLines().isEmpty()) return; - for (String line : getMotdConfig().getLines()) { - event.getPlayer().sendMessage(BukkitUtils.colorize(line)); - } + Audience audience = Audience.get(event.getPlayer()); + getMotdConfig().getLines().forEach(audience::sendMessage); } }