hollow-cube/fix-entity-metadata-translation

(cherry picked from commit d47db72421)
This commit is contained in:
mworzala 2023-07-09 19:14:40 -04:00
parent f8518bdae8
commit 47a5f9dea4
No known key found for this signature in database
GPG Key ID: B148F922E64797C7
2 changed files with 14 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package net.minestom.demo;
import net.kyori.adventure.text.Component;
import net.minestom.server.MinecraftServer;
import net.minestom.server.adventure.MinestomAdventure;
import net.minestom.server.adventure.audience.Audiences;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.coordinate.Vec;
@ -160,6 +161,9 @@ public class PlayerInit {
var eventHandler = MinecraftServer.getGlobalEventHandler();
eventHandler.addChild(DEMO_NODE);
MinestomAdventure.AUTOMATIC_COMPONENT_TRANSLATION = true;
MinestomAdventure.COMPONENT_TRANSLATOR = (c, l) -> c;
eventHandler.addListener(ServerTickMonitorEvent.class, event -> LAST_TICK.set(event.getTickMonitor()));
BenchmarkManager benchmarkManager = MinecraftServer.getBenchmarkManager();

View File

@ -13,8 +13,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.function.UnaryOperator;
import static net.minestom.server.network.NetworkBuffer.BYTE;
import static net.minestom.server.network.NetworkBuffer.VAR_INT;
import static net.minestom.server.network.NetworkBuffer.*;
public record EntityMetaDataPacket(int entityId,
@NotNull Map<Integer, Metadata.Entry<?>> entries) implements ComponentHoldingServerPacket {
@ -69,9 +68,17 @@ public record EntityMetaDataPacket(int entityId,
final var entries = new HashMap<Integer, Metadata.Entry<?>>();
this.entries.forEach((key, value) -> {
final var t = value.type();
final var v = value.value();
entries.put(key, v instanceof Component c ? Metadata.OptChat(operator.apply(c)) : value);
if (v instanceof Component c) {
var translated = operator.apply(c);
entries.put(key, t == Metadata.TYPE_OPTCHAT ? Metadata.OptChat(translated) : Metadata.Chat(translated));
} else {
entries.put(key, value);
}
entries.put(key, v instanceof Component c ? Metadata.Chat(operator.apply(c)) : value);
});
return new EntityMetaDataPacket(this.entityId, entries);