Properly deal with custom chat type registries

This commit is contained in:
Nassim Jahnke 2022-06-09 20:07:58 +02:00
parent f08223d1e9
commit 6a700d228d
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
3 changed files with 66 additions and 30 deletions

View File

@ -311,14 +311,32 @@ public final class Protocol1_18_2To1_19 extends BackwardsProtocol<ClientboundPac
private void handleChatType(final PacketWrapper wrapper, final JsonElement senderName, final JsonElement teamName, final JsonElement text) throws Exception {
translatableRewriter.processText(text);
final byte type = wrapper.get(Type.BYTE, 0);
if (type > 2) {
wrapper.set(Type.BYTE, 0, (byte) 0); // Chat type
byte chatTypeId = wrapper.get(Type.BYTE, 0);
final DimensionRegistryStorage dimensionRegistryStorage = wrapper.user().get(DimensionRegistryStorage.class);
final String chatTypeKey = dimensionRegistryStorage.chatTypeKey(chatTypeId);
switch (chatTypeKey) {
default:
case "minecraft:chat":
chatTypeId = 0;
break;
case "minecraft:system":
chatTypeId = 1;
break;
case "minecraft:game_info":
chatTypeId = 2;
break;
}
final String key = CHAT_KEYS[type];
final String key = CHAT_KEYS[chatTypeId];
if (key != null) {
Component component = Component.text(ViaBackwards.getConfig().chatTypeFormat(key));
final String chatFormat = ViaBackwards.getConfig().chatTypeFormat(key);
if (chatFormat == null) {
wrapper.cancel();
ViaBackwards.getPlatform().getLogger().severe("Chat type format " + key + " is not defined under chat-types in the ViaBackwards config.");
return;
}
Component component = Component.text(chatFormat);
if (key.equals("chat.type.team.text")) {
Preconditions.checkNotNull(teamName, "Team name is null");
component = component.replaceText(replace(teamName));

View File

@ -35,14 +35,12 @@ import com.viaversion.viaversion.api.type.types.version.Types1_18;
import com.viaversion.viaversion.api.type.types.version.Types1_19;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag;
import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.ClientboundPackets1_18;
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.ClientboundPackets1_19;
import java.util.HashMap;
import java.util.Map;
public final class EntityPackets1_19 extends EntityRewriter<Protocol1_18_2To1_19> {
public EntityPackets1_19(final Protocol1_18_2To1_19 protocol) {
@ -127,27 +125,45 @@ public final class EntityPackets1_19 extends EntityRewriter<Protocol1_18_2To1_19
map(Type.STRING_ARRAY); // Worlds
map(Type.NBT); // Dimension registry
handler(wrapper -> {
final DimensionRegistryStorage dimensionRegistryStorage = wrapper.user().get(DimensionRegistryStorage.class);
// Cache dimensions and find current dimension
final String dimensionKey = wrapper.read(Type.STRING);
final ListTag dimensions = ((CompoundTag) wrapper.get(Type.NBT, 0).get("minecraft:dimension_type")).get("value");
final Map<String, CompoundTag> dimensionsMap = new HashMap<>(dimensions.size());
final CompoundTag registry = wrapper.get(Type.NBT, 0);
final ListTag dimensions = ((CompoundTag) registry.get("minecraft:dimension_type")).get("value");
boolean found = false;
for (final Tag dimension : dimensions) {
final CompoundTag dimensionCompound = (CompoundTag) dimension;
final StringTag nameTag = dimensionCompound.get("name");
final CompoundTag dimensionData = dimensionCompound.get("element");
dimensionsMap.put(nameTag.getValue(), dimensionData.clone());
dimensionRegistryStorage.addDimension(nameTag.getValue(), dimensionData.clone());
if (!found && nameTag.getValue().equals(dimensionKey)) {
wrapper.write(Type.NBT, dimensionData);
found = true;
}
}
if (!found) {
throw new IllegalStateException("Could not find dimension " + dimensionKey + " in dimension registry");
}
wrapper.user().get(DimensionRegistryStorage.class).setDimensions(dimensionsMap);
// Add biome category and track biomes
final CompoundTag biomeRegistry = registry.get("minecraft:worldgen/biome");
final ListTag biomes = biomeRegistry.get("value");
for (final Tag biome : biomes.getValue()) {
final CompoundTag biomeCompound = ((CompoundTag) biome).get("element");
biomeCompound.put("category", new StringTag("none"));
}
tracker(wrapper.user()).setBiomesSent(biomes.size());
// Cache and remove chat types
final ListTag chatTypes = ((CompoundTag) registry.remove("minecraft:chat_type")).get("value");
for (final Tag chatType : chatTypes) {
final CompoundTag chatTypeCompound = (CompoundTag) chatType;
final NumberTag idTag = chatTypeCompound.get("id");
final StringTag nameTag = chatTypeCompound.get("name");
dimensionRegistryStorage.addChatType(idTag.asInt(), nameTag.getValue());
}
});
map(Type.STRING); // World
map(Type.LONG); // Seed
@ -161,20 +177,6 @@ public final class EntityPackets1_19 extends EntityRewriter<Protocol1_18_2To1_19
read(Type.OPTIONAL_GLOBAL_POSITION); // Read last death location
handler(worldDataTrackerHandler(1));
handler(playerTrackerHandler());
handler(wrapper -> {
final CompoundTag registry = wrapper.get(Type.NBT, 0);
final CompoundTag biomeRegistry = registry.get("minecraft:worldgen/biome");
final ListTag biomes = biomeRegistry.get("value");
for (final Tag biome : biomes.getValue()) {
final CompoundTag biomeCompound = ((CompoundTag) biome).get("element");
biomeCompound.put("category", new StringTag("none"));
}
registry.remove("minecraft:chat_type");
// Track amount of biomes sent
tracker(wrapper.user()).setBiomesSent(biomes.size());
});
}
});

View File

@ -18,6 +18,8 @@
package com.viaversion.viabackwards.protocol.protocol1_18_2to1_19.storage;
import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectMap;
import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import org.checkerframework.checker.nullness.qual.Nullable;
@ -26,15 +28,29 @@ import java.util.Map;
public final class DimensionRegistryStorage implements StorableObject {
private Map<String, CompoundTag> dimensions = new HashMap<>();
private final Map<String, CompoundTag> dimensions = new HashMap<>();
private final Int2ObjectMap<String> chatTypes = new Int2ObjectOpenHashMap<>();
public @Nullable CompoundTag dimension(final String dimensionKey) {
final CompoundTag compoundTag = dimensions.get(dimensionKey);
return compoundTag != null ? compoundTag.clone() : null;
}
public void setDimensions(final Map<String, CompoundTag> dimensions) {
this.dimensions = dimensions;
public void addDimension(final String dimensionKey, final CompoundTag dimension) {
dimensions.put(dimensionKey, dimension);
}
public @Nullable String chatTypeKey(final int id) {
return chatTypes.get(id);
}
public void addChatType(final int id, final String chatTypeKey) {
chatTypes.put(id, chatTypeKey);
}
public void clear() {
dimensions.clear();
chatTypes.clear();
}
@Override