Skip to content

Commit

Permalink
null safety around chat type registry and rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Nov 5, 2024
1 parent c152764 commit 54b3fe8
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 56 deletions.
4 changes: 0 additions & 4 deletions src/main/java/com/zenith/cache/data/chat/ChatSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
public class ChatSession {
private final UUID sessionId;
protected StepPlayerCertificates.PlayerCertificates playerCertificates;
protected LastSeenTrackedEntry[] lastSeenTrackedEntries;
protected int offset;
protected int tail;
protected LastSeenTrackedEntry lastTrackedMessage;
protected int chainIndex = 0;

public void sign(ServerboundChatPacket packet) {
Expand Down
25 changes: 16 additions & 9 deletions src/main/java/com/zenith/cache/data/chat/ChatType.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@
import java.util.ArrayList;
import java.util.List;

import static com.zenith.Shared.CLIENT_LOG;

public record ChatType(int id, String translationKey, List<String> parameters) {
public Component render(@Nullable Component sender, @Nullable Component content, @Nullable Component target) {
List<Component> args = new ArrayList<>(parameters.size());
for (var parameter : parameters) {
switch (parameter) {
case "sender" -> args.add(sender);
case "content" -> args.add(content);
case "target" -> args.add(target);
try {
List<Component> args = new ArrayList<>(parameters.size());
for (var parameter : parameters) {
switch (parameter) {
case "sender" -> args.add(sender);
case "content" -> args.add(content);
case "target" -> args.add(target);
}
}
return Component
.translatable(translationKey)
.arguments(args);
} catch (final Exception e) {
CLIENT_LOG.warn("Failed to render chat type: {}", translationKey, e);
return Component.translatable(translationKey);
}
return Component
.translatable(translationKey)
.arguments(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import lombok.Data;
import org.geysermc.mcprotocollib.protocol.data.game.RegistryEntry;
import org.jetbrains.annotations.Nullable;

import java.util.List;

Expand All @@ -34,7 +35,7 @@ public void initialize(List<RegistryEntry> registryEntries) {
}
}

public ChatType getChatType(int id) {
public @Nullable ChatType getChatType(int id) {
return chatTypes.get(id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,32 @@ public class DisguisedChatHandler implements PacketHandler<ClientboundDisguisedC
public ClientboundDisguisedChatPacket apply(final ClientboundDisguisedChatPacket packet, final ClientSession session) {
var senderPlayerEntry = CACHE.getTabListCache().getFromName(ComponentSerializer.serializePlain(packet.getName()));
ChatType chatType = CACHE.getChatCache().getChatTypeRegistry().getChatType(packet.getChatType().id());
Component chatComponent = chatType.render(
packet.getName(),
packet.getMessage(),
packet.getTargetName());
if (CONFIG.client.extra.logChatMessages) {
CLIENT_LOG.info("{}", ComponentSerializer.serializeJson(chatComponent));
if (chatType != null) {
Component chatComponent = chatType.render(
packet.getName(),
packet.getMessage(),
packet.getTargetName());
if (CONFIG.client.extra.logChatMessages) {
CLIENT_LOG.info("{}", ComponentSerializer.serializeJson(chatComponent));
}
Optional<PlayerListEntry> whisperTarget = Optional.empty();
if ("commands.message.display.incoming".equals(chatType.translationKey())) {
whisperTarget = CACHE.getTabListCache().get(CACHE.getProfileCache().getProfile().getId());
} else if ("commands.message.display.outgoing".equals(chatType.translationKey())) {
whisperTarget = CACHE.getTabListCache().getFromName( // ???
ComponentSerializer.serializePlain(packet.getTargetName())
);
}
EVENT_BUS.postAsync(new ServerChatReceivedEvent(
senderPlayerEntry,
chatComponent,
ComponentSerializer.serializePlain(chatComponent),
whisperTarget,
Optional.empty()
));
} else {
CLIENT_LOG.warn("Unknown chat type: {}", packet.getChatType().id());
}
Optional<PlayerListEntry> whisperTarget = Optional.empty();
if ("commands.message.display.incoming".equals(chatType.translationKey())) {
whisperTarget = CACHE.getTabListCache().get(CACHE.getProfileCache().getProfile().getId());
} else if ("commands.message.display.outgoing".equals(chatType.translationKey())) {
whisperTarget = CACHE.getTabListCache().getFromName( // ???
ComponentSerializer.serializePlain(packet.getTargetName())
);
}
EVENT_BUS.postAsync(new ServerChatReceivedEvent(
senderPlayerEntry,
chatComponent,
ComponentSerializer.serializePlain(chatComponent),
whisperTarget,
Optional.empty()
));
return packet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,32 @@ public ClientboundPlayerChatPacket apply(ClientboundPlayerChatPacket packet, Cli
// 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());
Component chatComponent = chatType.render(
packet.getName(),
Component.text(packet.getContent()),
packet.getTargetName());
if (CONFIG.client.extra.logChatMessages) {
CLIENT_LOG.info("{}", ComponentSerializer.serializeJson(chatComponent));
if (chatType != null) {
Component chatComponent = chatType.render(
packet.getName(),
Component.text(packet.getContent()),
packet.getTargetName());
if (CONFIG.client.extra.logChatMessages) {
CLIENT_LOG.info("{}", ComponentSerializer.serializeJson(chatComponent));
}
Optional<PlayerListEntry> whisperTarget = Optional.empty();
if ("commands.message.display.incoming".equals(chatType.translationKey())) {
whisperTarget = CACHE.getTabListCache().get(CACHE.getProfileCache().getProfile().getId());
} else if ("commands.message.display.outgoing".equals(chatType.translationKey())) {
whisperTarget = CACHE.getTabListCache().getFromName( // ???
ComponentSerializer.serializePlain(packet.getTargetName())
);
}
EVENT_BUS.postAsync(new ServerChatReceivedEvent(
senderPlayerEntry,
chatComponent,
ComponentSerializer.serializePlain(chatComponent),
whisperTarget,
Optional.empty()
));
} else {
CLIENT_LOG.warn("Unknown chat type: {}", packet.getChatType().id());
}
Optional<PlayerListEntry> whisperTarget = Optional.empty();
if ("commands.message.display.incoming".equals(chatType.translationKey())) {
whisperTarget = CACHE.getTabListCache().get(CACHE.getProfileCache().getProfile().getId());
} else if ("commands.message.display.outgoing".equals(chatType.translationKey())) {
whisperTarget = CACHE.getTabListCache().getFromName( // ???
ComponentSerializer.serializePlain(packet.getTargetName())
);
}
EVENT_BUS.postAsync(new ServerChatReceivedEvent(
senderPlayerEntry,
chatComponent,
ComponentSerializer.serializePlain(chatComponent),
whisperTarget,
Optional.empty()
));
return packet;
}
}

0 comments on commit 54b3fe8

Please sign in to comment.