Skip to content

Commit

Permalink
use minimessage placeholders for tablist and ping response
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Nov 5, 2024
1 parent 54b3fe8 commit 4a558d7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static com.zenith.Shared.*;

public class DisguisedChatHandler implements PacketHandler<ClientboundDisguisedChatPacket, ClientSession> {

@Override
public ClientboundDisguisedChatPacket apply(final ClientboundDisguisedChatPacket packet, final ClientSession session) {
var senderPlayerEntry = CACHE.getTabListCache().getFromName(ComponentSerializer.serializePlain(packet.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ public class PlayerChatHandler implements PacketHandler<ClientboundPlayerChatPac

@Override
public ClientboundPlayerChatPacket apply(ClientboundPlayerChatPacket packet, ClientSession session) {
// we shouldn't receive any of these packets on 2b or any anarchy server due to no chat reports plugins
// and this does not pass these packets through to our normal chat handlers
// so no chat relay, chat events, and other stuff
var senderPlayerEntry = CACHE.getTabListCache().get(packet.getSender());
var chatType = CACHE.getChatCache().getChatTypeRegistry().getChatType(packet.getChatType().id());
if (chatType != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.zenith.feature.queue.Queue;
import com.zenith.util.ComponentSerializer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.geysermc.mcprotocollib.auth.GameProfile;
import org.geysermc.mcprotocollib.network.Session;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodec;
Expand Down Expand Up @@ -106,37 +107,37 @@ public GameProfile[] getOnlinePlayerProfiles() {
}
}

public Component getMotd() {
var sb = new StringBuilder(150);
sb.append("<white>[<aqua>").append(CONFIG.authentication.username).append("<white>] <reset>- ");
if (Proxy.getInstance().isConnected()) {
sb
.append(getMotdStatus())
.append("<reset>\n<aqua>Online for: <white>[<reset>").append(Proxy.getInstance().getOnlineTimeString()).append("<white>]");
} else sb.append("<red>Disconnected");
return ComponentSerializer.minimessage(sb.toString());
}
private static final String motdMM = """
<white>[<aqua><username><white>] <reset>- <motd_body>
""";
private static final String motdDisconnectedBody = "<red>Disconnected";
private static final String motdConnectedBody = """
<motd_status><reset>
<aqua>Online for: <white>[<reset><online_time><white>]
""";
private static final String motdStatusInGame = "<green>In Game";
private static final String motdStatusInQueue = """
<in_queue> <white>[<aqua><queue_pos><white>] <queue_eta>
""";
private static final String motdInPrioQueue = "<red>In Prio Queue";
private static final String motdInQueue = "<red>In Queue";
private static final String motdQueuePosGeneric = "Queueing";
private static final String motdQueueEta = "<reset>- <red>ETA <white>[<aqua><eta><white>]";

private String getMotdStatus() { // in minedown formatted string
var proxy = Proxy.getInstance();
if (proxy.isInQueue()) {
var sb = new StringBuilder(100);
var prio = proxy.isPrio();
if (prio) sb.append("<red>In Prio Queue");
else sb.append("<red>In Queue");
sb.append(" <white>[<aqua>");
var qPos = proxy.getQueuePosition();
var qUndefined = qPos == Integer.MAX_VALUE;
if (!qUndefined) {
sb.append(qPos).append(" / ");
sb.append(prio ? Queue.getQueueStatus().prio() : Queue.getQueueStatus().regular());
} else sb.append("Queueing");
sb.append("<white>]");
if (!qUndefined)
sb.append(" <reset>- <red>ETA <white>[<aqua>").append(Queue.getQueueEta(qPos)).append("<white>]");
return sb.toString();
} else {
return "<green>In Game";
}
public Component getMotd() {
var prio = Proxy.getInstance().isPrio();
var qPos = Proxy.getInstance().getQueuePosition();
var qUndefined = qPos == Integer.MAX_VALUE;
return ComponentSerializer.minimessage(
motdMM,
Placeholder.unparsed("username", CONFIG.authentication.username),
Placeholder.parsed("motd_body", Proxy.getInstance().isConnected() ? motdConnectedBody : motdDisconnectedBody),
Placeholder.parsed("motd_status", Proxy.getInstance().isInQueue() ? motdStatusInQueue : motdStatusInGame),
Placeholder.unparsed("online_time", Proxy.getInstance().getOnlineTimeString()),
Placeholder.parsed("in_queue", prio ? motdInPrioQueue : motdInQueue),
Placeholder.unparsed("queue_pos", qUndefined ? motdQueuePosGeneric : qPos + " / " + (prio ? Queue.getQueueStatus().prio() : Queue.getQueueStatus().regular())),
Placeholder.unparsed("queue_eta", qUndefined ? "" : motdQueueEta),
Placeholder.parsed("eta", Queue.getQueueEta(qPos))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@
import com.zenith.network.server.ServerSession;
import com.zenith.util.ComponentSerializer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundTabListPacket;

import static com.zenith.Shared.*;

public class ServerTablistDataOutgoingHandler implements PacketHandler<ClientboundTabListPacket, ServerSession> {
// todo: allow users to configure the contents of this
private static final String footerMinimessage = """
<aqua><bold>ZenithProxy
<session_profile_name> </bold><gray>[<dark_aqua><session_ping>ms<gray>] <gray>-> <aqua><bold><client_profile_name> </bold><gray>[<dark_aqua><client_ping>ms<gray>]
<blue>Online: <aqua><bold><online_time></bold> <gray>- <blue>TPS: <aqua><bold><tps>
""";

@Override
public ClientboundTabListPacket apply(ClientboundTabListPacket packet, ServerSession session) {
Expand All @@ -23,21 +31,16 @@ public Component insertProxyDataIntoFooter(final Component footer, final ServerS
var clientProfile = CACHE.getProfileCache().getProfile();
var sessionProfileName = sessionProfile == null ? "Unknown" : sessionProfile.getName();
var clientProfileName = clientProfile == null ? "Unknown" : clientProfile.getName();
return footer.append(Component.text().appendNewline().append(ComponentSerializer.minimessage("<aqua><bold>ZenithProxy")).build())
.append(Component.text()
.appendNewline()
.append(ComponentSerializer.minimessage(
"<aqua><bold> " + sessionProfileName
+ " </bold><gray>[<dark_aqua>" + session.getPing() + "ms<gray>]"
+ " <gray>-> <aqua><bold>" + clientProfileName
+ " </bold><gray>[<dark_aqua>" + Proxy.getInstance().getClient().getPing() + "ms<gray>]"
)).build())
.append(Component.text()
.appendNewline()
.append(ComponentSerializer.minimessage(
"<blue>Online: <aqua><bold>" + Proxy.getInstance().getOnlineTimeString()
+ "</bold> <gray>- <blue>TPS: <aqua><bold>" + TPS.getTPS()))
.build());
var injectedFooter = ComponentSerializer.minimessage(
footerMinimessage,
Placeholder.unparsed("session_profile_name", sessionProfileName),
Placeholder.unparsed("session_ping", String.valueOf(session.getPing())),
Placeholder.unparsed("client_profile_name", clientProfileName),
Placeholder.unparsed("client_ping", String.valueOf(Proxy.getInstance().getClient().getPing())),
Placeholder.unparsed("online_time", Proxy.getInstance().getOnlineTimeString()),
Placeholder.unparsed("tps", TPS.getTPS())
);
return footer.append(injectedFooter);
} catch (final Exception e) {
SERVER_LOG.warn("Failed injecting proxy info to tablist footer", e);
return footer;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/zenith/util/ComponentSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.flattener.ComponentFlattener;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.serializer.ansi.ANSIComponentSerializer;
import net.kyori.adventure.translation.GlobalTranslator;
import net.kyori.adventure.translation.TranslationRegistry;
Expand Down Expand Up @@ -77,6 +78,10 @@ public static Component minimessage(String message) {
return MiniMessage.miniMessage().deserialize(message);
}

public static Component minimessage(String message, TagResolver... resolvers) {
return MiniMessage.miniMessage().deserialize(message, resolvers);
}

public static Component deserializeEmbed(final Embed embed) {
return EmbedSerializer.serialize(embed);
}
Expand Down

0 comments on commit 4a558d7

Please sign in to comment.