Skip to content

Commit

Permalink
feat(bungee): support basecomponent in packet fields
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotcorreia committed Oct 31, 2023
1 parent c04ef89 commit 52e1579
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class BungeeListener extends MessageToMessageEncoder<DefinedPacket> {
private final BungeeLanguagePlayer owner;
private final int protocolVersion;

private final HashMap<UUID, String> tabListCache = new HashMap<>();
private final HashMap<UUID, BaseComponent> tabListCache = new HashMap<>();

public BungeeListener(BungeeLanguagePlayer owner, int protocolVersion) {
this.owner = owner;
Expand All @@ -51,13 +51,13 @@ private void handlePlayerListItem(DefinedPacket packet) {
.getAction() == PlayerListItem.Action.ADD_PLAYER) {
if (i.getDisplayName() != null) {
try {
String original = TextComponent.toLegacyText(ComponentSerializer.parse(i.getDisplayName()));
String original = TextComponent.toLegacyText(i.getDisplayName());
String translated = translate(original,
Triton.get().getConf().getTabSyntax());
if (!original.equals(translated)) {
PlayerListItem.Item item = clonePlayerListItem(i);
tabListCache.put(item.getUuid(), item.getDisplayName());
item.setDisplayName(ComponentSerializer.toString(TextComponent.fromLegacyText(translated)));
tabListCache.put(item.getUuid(), item.getDisplayName().duplicate());
item.setDisplayName(TextComponent.fromArray(TextComponent.fromLegacyText(translated)));
items.add(item);
continue;
} else tabListCache.remove(i.getUuid());
Expand All @@ -83,13 +83,13 @@ private void handlePlayerListItemUpdate(DefinedPacket packet) {
for (PlayerListItem.Item i : p.getItems()) {
if (i.getDisplayName() != null) {
try {
String original = TextComponent.toLegacyText(ComponentSerializer.parse(i.getDisplayName()));
String original = TextComponent.toLegacyText(i.getDisplayName());
String translated = translate(original,
Triton.get().getConf().getTabSyntax());
if (!original.equals(translated)) {
PlayerListItem.Item item = clonePlayerListItem(i);
tabListCache.put(item.getUuid(), item.getDisplayName());
item.setDisplayName(ComponentSerializer.toString(TextComponent.fromLegacyText(translated)));
item.setDisplayName(TextComponent.fromArray(TextComponent.fromLegacyText(translated)));
items.add(item);
continue;
} else tabListCache.remove(i.getUuid());
Expand Down Expand Up @@ -144,34 +144,33 @@ private boolean handleSystemChat(DefinedPacket packet) {
int type = p.getPosition();
if ((type == 2 && !Triton.get().getConf().isActionbars()) || (type != 2 && !Triton.get().getConf().isChat()))
return true;
BaseComponent[] text = ComponentSerializer.parse(p.getMessage());
text = Triton.get().getLanguageParser().parseComponent(owner, type != 2 ?
Triton.get().getConf().getChatSyntax() : Triton.get().getConf().getActionbarSyntax(), text);
BaseComponent[] text = Triton.get().getLanguageParser().parseComponent(owner, type != 2 ?
Triton.get().getConf().getChatSyntax() : Triton.get().getConf().getActionbarSyntax(), p.getMessage());
if (text == null)
return false;
p.setMessage(ComponentSerializer.toString(text));
p.setMessage(TextComponent.fromArray(text));
return true;
}

private boolean handleTitle(DefinedPacket packet) {
Title p = (Title) packet;
if (p.getText() == null) return true;
BaseComponent[] result = Triton.get().getLanguageParser().parseComponent(owner,
Triton.get().getConf().getTitleSyntax(), ComponentSerializer.parse(p.getText()));
Triton.get().getConf().getTitleSyntax(), p.getText());
if (result == null)
return false;
p.setText(ComponentSerializer.toString(result));
p.setText(TextComponent.fromArray(result));
return true;
}

private boolean handleSubtitle(DefinedPacket packet) {
Subtitle p = (Subtitle) packet;
if (p.getText() == null) return true;
BaseComponent[] result = Triton.get().getLanguageParser().parseComponent(owner,
Triton.get().getConf().getTitleSyntax(), ComponentSerializer.parse(p.getText()));
Triton.get().getConf().getTitleSyntax(), p.getText());
if (result == null)
return false;
p.setText(ComponentSerializer.toString(result));
p.setText(TextComponent.fromArray(result));
return true;
}

Expand All @@ -184,24 +183,24 @@ private void handleBossbar(DefinedPacket packet) {
}
if (p.getAction() != 0 && p.getAction() != 3) return;
owner.setBossbar(uuid, p.getTitle());
p.setTitle(serializeComponent(Triton.get().getLanguageParser().parseComponent(owner,
Triton.get().getConf().getBossbarSyntax(), ComponentSerializer.parse(p.getTitle()))));
p.setTitle(nullOrTranslatable(Triton.get().getLanguageParser().parseComponent(owner,
Triton.get().getConf().getBossbarSyntax(), p.getTitle())));
}

private void handlePlayerListHeaderFooter(DefinedPacket packet) {
PlayerListHeaderFooter p = (PlayerListHeaderFooter) packet;
owner.setLastTabHeader(p.getHeader());
owner.setLastTabFooter(p.getFooter());
p.setHeader(serializeComponent(Triton.get().getLanguageParser().parseComponent(owner,
Triton.get().getConf().getTabSyntax(), ComponentSerializer.parse(p.getHeader()))));
p.setFooter(serializeComponent(Triton.get().getLanguageParser().parseComponent(owner,
Triton.get().getConf().getTabSyntax(), ComponentSerializer.parse(p.getFooter()))));
p.setHeader(nullOrTranslatable(Triton.get().getLanguageParser().parseComponent(owner,
Triton.get().getConf().getTabSyntax(), p.getHeader())));
p.setFooter(nullOrTranslatable(Triton.get().getLanguageParser().parseComponent(owner,
Triton.get().getConf().getTabSyntax(), p.getFooter())));
}

private void handleKick(DefinedPacket packet) {
Kick p = (Kick) packet;
p.setMessage(serializeComponent(Triton.get().getLanguageParser().parseComponent(owner,
Triton.get().getConf().getKickSyntax(), ComponentSerializer.parse(p.getMessage()))));
p.setMessage(nullOrTranslatable(Triton.get().getLanguageParser().parseComponent(owner,
Triton.get().getConf().getKickSyntax(), p.getMessage())));
}

@Override
Expand Down Expand Up @@ -249,13 +248,13 @@ protected void encode(ChannelHandlerContext ctx, DefinedPacket packet,

private void send(DefinedPacket packet) {
if (owner.getCurrentConnection() instanceof UserConnection) {
((UserConnection)owner.getCurrentConnection()).sendPacketQueued(packet);
((UserConnection) owner.getCurrentConnection()).sendPacketQueued(packet);
}
}

public void refreshTab() {
List<PlayerListItem.Item> items = new ArrayList<>();
for (Map.Entry<UUID, String> item : tabListCache.entrySet()) {
for (Map.Entry<UUID, BaseComponent> item : tabListCache.entrySet()) {
PlayerListItem.Item i = new PlayerListItem.Item();
i.setUuid(item.getKey());
i.setDisplayName(item.getValue());
Expand All @@ -274,14 +273,14 @@ public void refreshTab() {
}
}

public void refreshBossbar(UUID uuid, String json) {
public void refreshBossbar(UUID uuid, BaseComponent json) {
if (owner.getParent().getPendingConnection().getVersion() < 107) return;
BossBar p = new BossBar(uuid, 3);
p.setTitle(json);
send(p);
}

public void refreshTabHeaderFooter(String header, String footer) {
public void refreshTabHeaderFooter(BaseComponent header, BaseComponent footer) {
send(new PlayerListHeaderFooter(header, footer));
}

Expand Down Expand Up @@ -312,9 +311,11 @@ private String translate(String s, MainConfig.FeatureSyntax syntax) {
return result;
}

private String serializeComponent(BaseComponent... bc) {
if (bc == null) return ComponentSerializer.toString(new TranslatableComponent(""));
return ComponentSerializer.toString(bc);
private BaseComponent nullOrTranslatable(BaseComponent... bc) {
if (bc == null) {
return new TranslatableComponent("");
}
return TextComponent.fromArray(bc);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import io.netty.handler.codec.MessageToMessageEncoder;
import lombok.RequiredArgsConstructor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.TranslatableComponent;
import net.md_5.bungee.chat.ComponentSerializer;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.packet.Kick;

Expand All @@ -28,18 +28,20 @@ protected void encode(ChannelHandlerContext ctx, DefinedPacket packet,
if (this.lang == null)
this.lang = Triton.get().getStorage().getLanguageFromIp(ip).getName();

kick.setMessage(serializeComponent(Triton.get().getLanguageParser().parseComponent(this.lang,
Triton.get().getConf().getKickSyntax(), ComponentSerializer.parse(kick.getMessage()))));
kick.setMessage(nullOrTranslatable(Triton.get().getLanguageParser().parseComponent(this.lang,
Triton.get().getConf().getKickSyntax(), kick.getMessage())));
}
} catch (Exception | Error e) {
e.printStackTrace();
}
out.add(packet);
}

private String serializeComponent(BaseComponent... bc) {
if (bc == null) return ComponentSerializer.toString(new TranslatableComponent(""));
return ComponentSerializer.toString(bc);
private BaseComponent nullOrTranslatable(BaseComponent... bc) {
if (bc == null) {
return new TranslatableComponent("");
}
return TextComponent.fromArray(bc);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.val;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.connection.Connection;
import net.md_5.bungee.api.connection.ProxiedPlayer;
Expand All @@ -28,9 +29,9 @@ public class BungeeLanguagePlayer implements LanguagePlayer {
private Language language;
private BungeeListener listener;

private String lastTabHeader;
private String lastTabFooter;
private HashMap<UUID, String> bossBars = new HashMap<>();
private BaseComponent lastTabHeader;
private BaseComponent lastTabFooter;
private HashMap<UUID, BaseComponent> bossBars = new HashMap<>();
private boolean waitingForClientLocale = false;

public BungeeLanguagePlayer(UUID parent) {
Expand All @@ -46,20 +47,20 @@ public BungeeLanguagePlayer(UUID uuid, Connection connection) {
load();
}

public void setBossbar(UUID uuid, String lastBossBar) {
bossBars.put(uuid, lastBossBar);
public void setBossbar(UUID uuid, BaseComponent lastBossBar) {
bossBars.put(uuid, lastBossBar.duplicate());
}

public void removeBossbar(UUID uuid) {
bossBars.remove(uuid);
}

public void setLastTabHeader(String lastTabHeader) {
this.lastTabHeader = lastTabHeader;
public void setLastTabHeader(BaseComponent lastTabHeader) {
this.lastTabHeader = lastTabHeader.duplicate();
}

public void setLastTabFooter(String lastTabFooter) {
this.lastTabFooter = lastTabFooter;
public void setLastTabFooter(BaseComponent lastTabFooter) {
this.lastTabFooter = lastTabFooter.duplicate();
}

@Override
Expand Down Expand Up @@ -106,7 +107,7 @@ public void refreshAll() {
if (Triton.get().getConf().isTab() && lastTabHeader != null && lastTabFooter != null)
listener.refreshTabHeaderFooter(lastTabHeader, lastTabFooter);
if (Triton.get().getConf().isBossbars())
for (Map.Entry<UUID, String> entry : bossBars.entrySet())
for (Map.Entry<UUID, BaseComponent> entry : bossBars.entrySet())
listener.refreshBossbar(entry.getKey(), entry.getValue());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,8 @@

import com.rexcantor64.triton.api.language.Language;

import java.util.UUID;

public interface LanguagePlayer extends com.rexcantor64.triton.api.players.LanguagePlayer {

void setBossbar(UUID uuid, String lastBossBar);

void removeBossbar(UUID uuid);

void setLastTabHeader(String lastTabHeader);

void setLastTabFooter(String lastTabFooter);

boolean isWaitingForClientLocale();

void waitForClientLocale();
Expand Down

0 comments on commit 52e1579

Please sign in to comment.