Skip to content

Commit

Permalink
Don't keep converting player count and count hover messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kennytv committed Jan 1, 2024
1 parent fa225ad commit 8941b55
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void proxyPing(final ProxyPingEvent event) {

final ServerPing ping = event.getResponse();
if (settings.hasCustomPlayerCountMessage()) {
ping.setVersion(new ServerPing.Protocol(settings.getPlayerCountMessage(), 1));
ping.setVersion(new ServerPing.Protocol(settings.getLegacyParsedPlayerCountMessage(), 1));
}

if (settings.isEnablePingMessages()) {
Expand All @@ -56,7 +56,7 @@ public void proxyPing(final ProxyPingEvent event) {
ping.setPlayers(players);
}

final String[] lines = settings.getPlayerCountHoverLines();
final String[] lines = settings.getLegacyParsedPlayerCountHoverLines();
final ServerPing.PlayerInfo[] samplePlayers = new ServerPing.PlayerInfo[lines.length];
for (int i = 0; i < lines.length; i++) {
samplePlayers[i] = new ServerPing.PlayerInfo(lines[i], "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
public abstract class MaintenancePlugin implements Maintenance {
public static final Gson GSON = new GsonBuilder().create();
public static final String HANGAR_URL = "https://hangar.papermc.io/kennytv/Maintenance";
protected final eu.kennytv.maintenance.core.event.EventManager eventManager;
protected final EventManager eventManager;
protected final Version version;
protected Settings settings;
protected ServerListPlusHook serverListPlusHook;
Expand Down
48 changes: 30 additions & 18 deletions core/src/main/java/eu/kennytv/maintenance/core/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@
import eu.kennytv.maintenance.lib.kyori.adventure.text.TextComponent;
import eu.kennytv.maintenance.lib.kyori.adventure.text.minimessage.MiniMessage;
import eu.kennytv.maintenance.lib.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import org.jetbrains.annotations.Nullable;

public class Settings implements eu.kennytv.maintenance.api.Settings {
public static final String NEW_LINE_REPLACEMENT = "<br>";
Expand All @@ -47,8 +53,8 @@ public class Settings implements eu.kennytv.maintenance.api.Settings {
private List<String> timerSpecificPingMessages;
private List<String> commandsOnMaintenanceEnable;
private List<String> commandsOnMaintenanceDisable;
private String playerCountMessage;
private List<String> playerCountHoverLines;
private String legacyParsedPlayerCountMessage;
private List<String> legacyParsedPlayerCountHoverLines;
private String prefixString;
private String languageName;
private boolean enablePingMessages;
Expand Down Expand Up @@ -168,6 +174,7 @@ private void loadSettings() {
updateConfig();

enablePingMessages = config.getBoolean("enable-pingmessages", true);
// Can't store these two already parsed as components because gradients will break replacements
pingMessages = loadPingMessages("pingmessages");
if (config.getBoolean("enable-timerspecific-messages")) {
timerSpecificPingMessages = loadPingMessages("timerspecific-pingmessages");
Expand All @@ -180,13 +187,16 @@ private void loadSettings() {
customMaintenanceIcon = config.getBoolean("custom-maintenance-icon");
joinNotifications = config.getBoolean("send-join-notification");
broadcastIntervals = new HashSet<>(config.getIntList("timer-broadcast-for-seconds"));

// Player count and player count hover messages are only accepted and rendered as legacy chat
if (plugin.getServerType() != ServerType.SPONGE) {
playerCountMessage = getConfigMessage("playercountmessage");
legacyParsedPlayerCountMessage = toLegacy(parse(getConfigMessage("playercountmessage")));
}

playerCountHoverLines = new ArrayList<>();
final String playerHoverMessage = config.getString("playercounthovermessage");
Collections.addAll(playerCountHoverLines, playerHoverMessage.split("<br>"));
legacyParsedPlayerCountHoverLines = new ArrayList<>();
for (final String line : config.getString("playercounthovermessage").split("<br>")) {
legacyParsedPlayerCountHoverLines.add(toLegacy(parse(line)));
}

languageName = config.getString("language").toLowerCase();
kickOnlinePlayers = config.getBoolean("kick-online-players", true);
Expand Down Expand Up @@ -350,6 +360,7 @@ public Component getMessage(final String path, final String... replacements) {
return parse(getLanguageString(path, replacements));
}

// TODO Cache platform components if there are no replacements
public Component getRandomPingMessage() {
if (plugin.isTaskRunning() && !plugin.getRunnable().shouldEnable()
&& hasTimerSpecificPingMessages() && !timerSpecificPingMessages.isEmpty()) {
Expand Down Expand Up @@ -505,17 +516,14 @@ public Set<Integer> getBroadcastIntervals() {
return broadcastIntervals;
}

// Yikeseroo
public String getPlayerCountMessage() {
return LegacyComponentSerializer.legacySection().serialize(parse(plugin.replacePingVariables(playerCountMessage)));
public String getLegacyParsedPlayerCountMessage() {
return plugin.replacePingVariables(legacyParsedPlayerCountMessage);
}

// Yikeseroo x2
public String[] getPlayerCountHoverLines() {
final String[] lines = new String[playerCountHoverLines.size()];
for (int i = 0; i < playerCountHoverLines.size(); i++) {
final String component = plugin.replacePingVariables(playerCountHoverLines.get(i));
lines[i] = LegacyComponentSerializer.legacySection().serialize(parse(component));
public String[] getLegacyParsedPlayerCountHoverLines() {
final String[] lines = new String[legacyParsedPlayerCountHoverLines.size()];
for (int i = 0; i < legacyParsedPlayerCountHoverLines.size(); i++) {
lines[i] = plugin.replacePingVariables(legacyParsedPlayerCountHoverLines.get(i));
}
return lines;
}
Expand All @@ -540,6 +548,10 @@ protected Component parse(final String s) {
return MiniMessage.miniMessage().deserialize(s);
}

protected String toLegacy(final Component component) {
return LegacyComponentSerializer.legacySection().serialize(component);
}

/*
* Note on why this even exists: Yaml will force save all strings containing line breaks '\n' in this rather chunky format:
* key: |-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ public void serverListPing(final PaperServerListPingEvent event) {

if (settings.hasCustomPlayerCountMessage()) {
event.setProtocolVersion(-1);
event.setVersion(settings.getPlayerCountMessage());
event.setVersion(settings.getLegacyParsedPlayerCountMessage());
}

if (settings.hasCustomPlayerCountHoverMessage()) {
final List<PlayerProfile> sample = event.getPlayerSample();
sample.clear();
for (final String string : settings.getPlayerCountHoverLines()) {
for (final String string : settings.getLegacyParsedPlayerCountHoverLines()) {
sample.add(new DummyProfile(string));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.bukkit.event.server.ServerListPingEvent;

public final class ServerInfoPacketListener extends PacketAdapter implements Listener {
private final UUID uuid = new UUID(0, 0);
private static final UUID ZERO_UUID = new UUID(0, 0);
private final MaintenanceSpigotPlugin plugin;
private final Settings settings;

Expand All @@ -61,13 +61,13 @@ public void onPacketSending(final PacketEvent event) {

if (settings.hasCustomPlayerCountMessage()) {
ping.setVersionProtocol(1);
ping.setVersionName(settings.getPlayerCountMessage());
ping.setVersionName(settings.getLegacyParsedPlayerCountMessage());
}

if (settings.hasCustomPlayerCountHoverMessage()) {
final List<WrappedGameProfile> players = new ArrayList<>();
for (final String string : settings.getPlayerCountHoverLines()) {
players.add(new WrappedGameProfile(uuid, string));
for (final String string : settings.getLegacyParsedPlayerCountHoverLines()) {
players.add(new WrappedGameProfile(ZERO_UUID, string));
}
ping.setPlayers(players);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void proxyPing(final ClientPingServerEvent event) {
response.players().ifPresent(players -> {
final List<GameProfile> profiles = players.profiles();
profiles.clear();
for (final String string : settings.getPlayerCountHoverLines()) {
for (final String string : settings.getLegacyParsedPlayerCountHoverLines()) {
profiles.add(GameProfile.of(UUID.randomUUID(), string));
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import java.util.UUID;

public final class ProxyPingListener implements EventHandler<ProxyPingEvent> {
private static final UUID ZERO_UUID = new UUID(0, 0);
private final MaintenanceVelocityPlugin plugin;
private final SettingsProxy settings;
private final UUID uuid = new UUID(0, 0);

public ProxyPingListener(final MaintenanceVelocityPlugin plugin, final SettingsProxy settings) {
this.plugin = plugin;
Expand All @@ -44,14 +44,14 @@ public void execute(final ProxyPingEvent event) {
final ServerPing ping = event.getPing();
final ServerPing.Builder builder = ping.asBuilder();
if (settings.hasCustomPlayerCountMessage()) {
builder.version(new ServerPing.Version(1, settings.getPlayerCountMessage()));
builder.version(new ServerPing.Version(1, settings.getLegacyParsedPlayerCountMessage()));
}

if (settings.hasCustomPlayerCountHoverMessage()) {
final String[] lines = settings.getPlayerCountHoverLines();
final String[] lines = settings.getLegacyParsedPlayerCountHoverLines();
final ServerPing.SamplePlayer[] samplePlayers = new ServerPing.SamplePlayer[lines.length];
for (int i = 0; i < lines.length; i++) {
samplePlayers[i] = new ServerPing.SamplePlayer(lines[i], uuid);
samplePlayers[i] = new ServerPing.SamplePlayer(lines[i], ZERO_UUID);
}
builder.samplePlayers(samplePlayers);
}
Expand Down

0 comments on commit 8941b55

Please sign in to comment.