Skip to content

Commit

Permalink
Switch parts of config parsing to PGMs TextParser.parseComponent (#61)
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher White <18whitechristop@gmail.com>
  • Loading branch information
cswhite2000 authored May 27, 2024
1 parent 690ae2b commit a7b0506
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 27 deletions.
20 changes: 14 additions & 6 deletions src/main/java/dev/pgm/community/broadcast/BroadcastConfig.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -15,8 +20,8 @@ public class BroadcastConfig extends FeatureConfigImpl {

private boolean announceEnabled;
private int announceDelay;
private String announcePrefix;
private List<String> announceMessages;
private Component announcePrefix;
private List<Component> announceMessages;

public BroadcastConfig(Configuration config) {
super(KEY, config);
Expand All @@ -42,11 +47,11 @@ public int getAnnounceDelay() {
return announceDelay;
}

public String getAnnouncePrefix() {
public Component getAnnouncePrefix() {
return announcePrefix;
}

public List<String> getAnnounceMessages() {
public List<Component> getAnnounceMessages() {
return announceMessages;
}

Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,13 @@ private void announce() {
if (!getBroadcastConfig().isAnnounceEnabled()) return;
if (getBroadcastConfig().getAnnounceMessages().isEmpty()) return;
Duration timeSince = Duration.between(lastAnnounce, Instant.now());
List<String> messages = getBroadcastConfig().getAnnounceMessages();
List<Component> 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();
}
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/dev/pgm/community/info/InfoCommandData.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,42 @@
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 {

private static final String LINES_KEY = "lines";
private static final String PERMISSION_KEY = "permission";

private String name;
private List<String> lines;
private List<Component> lines;
private String permission;

public InfoCommandData(String name, List<String> lines, String permission) {
public InfoCommandData(String name, List<Component> lines, String permission) {
this.name = name;
this.lines = lines;
this.permission = 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<String> getLines() {
public List<Component> getLines() {
return lines;
}

Expand All @@ -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);
}
}
12 changes: 9 additions & 3 deletions src/main/java/dev/pgm/community/motd/MotdConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> lines;
private List<Component> lines;

public MotdConfig(Configuration config) {
super(KEY, config);
}

public List<String> getLines() {
public List<Component> 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());
}
}
7 changes: 3 additions & 4 deletions src/main/java/dev/pgm/community/motd/MotdFeature.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
}

0 comments on commit a7b0506

Please sign in to comment.