Handle chat types

This commit is contained in:
Nassim Jahnke 2022-10-22 11:25:45 +02:00
parent 100901029e
commit 9d3ee288df
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
7 changed files with 215 additions and 72 deletions

View File

@ -25,7 +25,9 @@ import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter;
import com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.packets.BlockItemPackets1_19_3;
import com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.packets.EntityPackets1_19_3;
import com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.storage.ChatSessionStorage;
import com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.storage.ChatTypeStorage1_19_3;
import com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.storage.NonceStorage;
import com.viaversion.viabackwards.protocol.protocol1_19to1_19_1.Protocol1_19To1_19_1;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.ProfileKey;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_19Types;
@ -59,7 +61,6 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol<ClientboundP
public static final BackwardsMappings MAPPINGS = new BackwardsMappings("1.19.3", "1.19", Protocol1_19_3To1_19_1.class, true);
private static final ByteArrayType.OptionalByteArrayType OPTIONAL_SIGNATURE_BYTES_TYPE = new ByteArrayType.OptionalByteArrayType(256);
private static final ByteArrayType SIGNATURE_BYTES_TYPE = new ByteArrayType(256);
private static final byte[] EMPTY_BYTES = new byte[0];
private final EntityPackets1_19_3 entityRewriter = new EntityPackets1_19_3(this);
private final BlockItemPackets1_19_3 itemRewriter = new BlockItemPackets1_19_3(this);
private final TranslatableRewriter translatableRewriter = new TranslatableRewriter(this);
@ -70,12 +71,12 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol<ClientboundP
@Override
protected void registerPackets() {
// Make sure this protocol's mappingdata is loaded after ViaVersion has loaded theirs, since VB depends on it instead of duplicating all the data
executeAsyncAfterLoaded(Protocol1_19_3To1_19_1.class, () -> {
MAPPINGS.load();
entityRewriter.onMappingDataLoaded();
});
translatableRewriter.registerComponentPacket(ClientboundPackets1_19_3.SYSTEM_CHAT);
translatableRewriter.registerComponentPacket(ClientboundPackets1_19_3.ACTIONBAR);
translatableRewriter.registerComponentPacket(ClientboundPackets1_19_3.TITLE_TEXT);
translatableRewriter.registerComponentPacket(ClientboundPackets1_19_3.TITLE_SUBTITLE);
@ -209,7 +210,7 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol<ClientboundP
read(Type.PLAYER_MESSAGE_SIGNATURE_ARRAY); // Last seen messages
read(Type.OPTIONAL_PLAYER_MESSAGE_SIGNATURE); // Last received message
handler(wrapper -> {
//TODO is this fine (probably not)?
//TODO is this fine (probably not)? same for chat_command
final int offset = 0;
final BitSet acknowledged = new BitSet(20);
wrapper.write(Type.VAR_INT, offset);
@ -232,7 +233,6 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol<ClientboundP
}
wrapper.read(Type.BOOLEAN); // Signed preview
//TODO is this fine (probably not)?
final int offset = 0;
final BitSet acknowledged = new BitSet(20);
wrapper.write(Type.VAR_INT, offset);
@ -262,18 +262,23 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol<ClientboundP
final JsonElement unsignedContent = wrapper.read(Type.OPTIONAL_COMPONENT);
final JsonElement content = unsignedContent != null ? unsignedContent : GsonComponentSerializer.gson().serializeToTree(Component.text(plainContent));
wrapper.write(Type.COMPONENT, content);
wrapper.write(Type.BOOLEAN, false);
translatableRewriter.processText(content);
final int filterMaskType = wrapper.read(Type.VAR_INT);
if (filterMaskType == 2) {
wrapper.read(Type.LONG_ARRAY_PRIMITIVE); // Mask
}
//TODO chat type handling
wrapper.read(Type.VAR_INT);
wrapper.read(Type.COMPONENT);
wrapper.read(Type.OPTIONAL_COMPONENT);
final int chatTypeId = wrapper.read(Type.VAR_INT);
final JsonElement senderName = wrapper.read(Type.COMPONENT);
final JsonElement targetName = wrapper.read(Type.OPTIONAL_COMPONENT);
final JsonElement result = Protocol1_19To1_19_1.decorateChatMessage(wrapper.user().get(ChatTypeStorage1_19_3.class), chatTypeId, senderName, targetName, content);
if (result == null) {
wrapper.cancel();
return;
}
wrapper.write(Type.COMPONENT, result);
wrapper.write(Type.BOOLEAN, false);
});
}
});
@ -297,6 +302,7 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol<ClientboundP
@Override
public void init(final UserConnection user) {
user.put(new ChatSessionStorage());
user.put(new ChatTypeStorage1_19_3());
addEntityTracker(user, new EntityTrackerBase(user, Entity1_19Types.PLAYER, true));
}

View File

@ -19,6 +19,8 @@ package com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.packets;
import com.viaversion.viabackwards.api.rewriters.EntityRewriter;
import com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.Protocol1_19_1To1_19_3;
import com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.storage.ChatTypeStorage1_19_3;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.ProfileKey;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_19_3Types;
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
@ -29,8 +31,13 @@ import com.viaversion.viaversion.api.type.types.BitSetType;
import com.viaversion.viaversion.api.type.types.version.Types1_19;
import com.viaversion.viaversion.api.type.types.version.Types1_19_3;
import com.viaversion.viaversion.libs.gson.JsonElement;
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.Tag;
import com.viaversion.viaversion.protocols.protocol1_19_1to1_19.ClientboundPackets1_19_1;
import com.viaversion.viaversion.protocols.protocol1_19_3to1_19_1.ClientboundPackets1_19_3;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.BitSet;
import java.util.UUID;
@ -38,7 +45,13 @@ import java.util.UUID;
public final class EntityPackets1_19_3 extends EntityRewriter<Protocol1_19_1To1_19_3> {
private static final BitSetType PROFILE_ACTIONS_ENUM_TYPE = new BitSetType(6);
private static final int[] PROFILE_ACTIONS = {0, 2, 4, 5}; // Ignore initialize chat and listed status
private static final int[] PROFILE_ACTIONS = {2, 4, 5}; // Ignore initialize chat and listed status; add player already handled before
private static final int ADD_PLAYER = 0;
private static final int INITIALIZE_CHAT = 1;
private static final int UPDATE_GAMEMODE = 2;
private static final int UPDATE_LISTED = 3;
private static final int UPDATE_LATENCY = 4;
private static final int UPDATE_DISPLAYNAME = 5;
public EntityPackets1_19_3(final Protocol1_19_1To1_19_3 protocol) {
super(protocol);
@ -64,6 +77,17 @@ public final class EntityPackets1_19_3 extends EntityRewriter<Protocol1_19_1To1_
handler(dimensionDataHandler());
handler(biomeSizeTracker());
handler(worldDataTrackerHandlerByKey());
handler(wrapper -> {
final ChatTypeStorage1_19_3 chatTypeStorage = wrapper.user().get(ChatTypeStorage1_19_3.class);
chatTypeStorage.clear();
final CompoundTag registry = wrapper.get(Type.NBT, 0);
final ListTag chatTypes = ((CompoundTag) registry.get("minecraft:chat_type")).get("value");
for (final Tag chatType : chatTypes) {
final CompoundTag chatTypeCompound = (CompoundTag) chatType;
final NumberTag idTag = chatTypeCompound.get("id");
chatTypeStorage.addChatType(idTag.asInt(), chatTypeCompound);
}
});
}
});
@ -83,65 +107,80 @@ public final class EntityPackets1_19_3 extends EntityRewriter<Protocol1_19_1To1_
wrapper.cancel();
final BitSet actions = wrapper.read(PROFILE_ACTIONS_ENUM_TYPE);
final int entries = wrapper.read(Type.VAR_INT);
for (final int action : PROFILE_ACTIONS) {
if (!actions.get(action)) {
continue;
}
if (actions.get(ADD_PLAYER)) {
// Special case, as we need to write everything into one action
final PacketWrapper playerInfoPacket = wrapper.create(ClientboundPackets1_19_1.PLAYER_INFO);
if (action == 0) {
playerInfoPacket.write(Type.VAR_INT, 0);
} else if (action == 2) {
playerInfoPacket.write(Type.VAR_INT, 1);
} else {
playerInfoPacket.write(Type.VAR_INT, action - 2);
}
playerInfoPacket.write(Type.VAR_INT, 0);
playerInfoPacket.write(Type.VAR_INT, entries);
playerInfoPacket.write(Type.UUID, wrapper.read(Type.UUID));
switch (action) {
case 0: // Add player
playerInfoPacket.write(Type.STRING, wrapper.read(Type.STRING)); // Player Name
for (int i = 0; i < entries; i++) {
playerInfoPacket.write(Type.UUID, wrapper.read(Type.UUID));
playerInfoPacket.write(Type.STRING, wrapper.read(Type.STRING)); // Player Name
final int properties = wrapper.read(Type.VAR_INT);
playerInfoPacket.write(Type.VAR_INT, properties);
for (int j = 0; j < properties; j++) {
playerInfoPacket.write(Type.STRING, wrapper.read(Type.STRING)); // Name
playerInfoPacket.write(Type.STRING, wrapper.read(Type.STRING)); // Value
playerInfoPacket.write(Type.OPTIONAL_STRING, wrapper.read(Type.OPTIONAL_STRING)); // Signature
}
final int properties = wrapper.read(Type.VAR_INT);
playerInfoPacket.write(Type.VAR_INT, properties);
for (int j = 0; j < properties; j++) {
playerInfoPacket.write(Type.STRING, wrapper.read(Type.STRING)); // Name
playerInfoPacket.write(Type.STRING, wrapper.read(Type.STRING)); // Value
playerInfoPacket.write(Type.OPTIONAL_STRING, wrapper.read(Type.OPTIONAL_STRING)); // Signature
}
// Now check for the other parts individually and add dummy values if not present
final ProfileKey profileKey;
if (actions.get(1)) {
wrapper.read(Type.UUID); // Session UUID
profileKey = wrapper.read(Type.OPTIONAL_PROFILE_KEY);
} else {
profileKey = null;
}
// Now check for the other parts individually and add dummy values if not present
final ProfileKey profileKey;
if (actions.get(INITIALIZE_CHAT)) {
wrapper.read(Type.UUID); // Session UUID
profileKey = wrapper.read(Type.OPTIONAL_PROFILE_KEY);
} else {
profileKey = null;
}
final int gamemode = actions.get(2) ? wrapper.read(Type.VAR_INT) : 0;
if (actions.get(3)) {
wrapper.read(Type.BOOLEAN); // Listed - throw away
}
final int gamemode = actions.get(UPDATE_GAMEMODE) ? wrapper.read(Type.VAR_INT) : 0;
if (actions.get(UPDATE_LISTED)) {
wrapper.read(Type.BOOLEAN); // Listed - throw away
}
final int latency = actions.get(4) ? wrapper.read(Type.VAR_INT) : 0;
final JsonElement displayName = actions.get(5) ? wrapper.read(Type.OPTIONAL_COMPONENT) : null;
playerInfoPacket.write(Type.VAR_INT, gamemode);
playerInfoPacket.write(Type.VAR_INT, latency);
playerInfoPacket.write(Type.OPTIONAL_COMPONENT, displayName);
playerInfoPacket.write(Type.OPTIONAL_PROFILE_KEY, profileKey);
playerInfoPacket.send(Protocol1_19_1To1_19_3.class);
return; // We're done
case 2: // Update gamemode
case 4: // Update latency
playerInfoPacket.write(Type.VAR_INT, wrapper.read(Type.VAR_INT));
break;
case 5: // Update display name
playerInfoPacket.write(Type.OPTIONAL_COMPONENT, wrapper.read(Type.OPTIONAL_COMPONENT));
break;
final int latency = actions.get(UPDATE_LATENCY) ? wrapper.read(Type.VAR_INT) : 0;
final JsonElement displayName = actions.get(UPDATE_DISPLAYNAME) ? wrapper.read(Type.OPTIONAL_COMPONENT) : null;
playerInfoPacket.write(Type.VAR_INT, gamemode);
playerInfoPacket.write(Type.VAR_INT, latency);
playerInfoPacket.write(Type.OPTIONAL_COMPONENT, displayName);
playerInfoPacket.write(Type.OPTIONAL_PROFILE_KEY, profileKey);
playerInfoPacket.send(Protocol1_19_1To1_19_3.class);
}
playerInfoPacket.send(Protocol1_19_1To1_19_3.class);
return;
}
final PlayerProfileUpdate[] updates = new PlayerProfileUpdate[entries];
for (int i = 0; i < entries; i++) {
final UUID uuid = wrapper.read(Type.UUID);
int gamemode = 0;
int latency = 0;
JsonElement displayName = null;
for (final int action : PROFILE_ACTIONS) {
if (!actions.get(action)) {
continue;
}
switch (action) {
case UPDATE_GAMEMODE:
gamemode = wrapper.read(Type.VAR_INT);
break;
case UPDATE_LATENCY:
latency = wrapper.read(Type.VAR_INT);
break;
case UPDATE_DISPLAYNAME:
displayName = wrapper.read(Type.OPTIONAL_COMPONENT);
break;
}
}
updates[i] = new PlayerProfileUpdate(uuid, gamemode, latency, displayName);
}
if (actions.get(UPDATE_GAMEMODE)) {
sendPlayerProfileUpdate(wrapper.user(), 1, updates);
} else if (actions.get(UPDATE_LATENCY)) {
sendPlayerProfileUpdate(wrapper.user(), 2, updates);
} else if (actions.get(UPDATE_DISPLAYNAME)) {
sendPlayerProfileUpdate(wrapper.user(), 3, updates);
}
});
}
@ -161,12 +200,34 @@ public final class EntityPackets1_19_3 extends EntityRewriter<Protocol1_19_1To1_
});
}
private void sendPlayerProfileUpdate(final UserConnection connection, final int action, final PlayerProfileUpdate[] updates) throws Exception {
final PacketWrapper playerInfoPacket = PacketWrapper.create(ClientboundPackets1_19_1.PLAYER_INFO, connection);
playerInfoPacket.write(Type.VAR_INT, action);
playerInfoPacket.write(Type.VAR_INT, updates.length);
for (final PlayerProfileUpdate update : updates) {
playerInfoPacket.write(Type.UUID, update.uuid());
if (action == 1) {
playerInfoPacket.write(Type.VAR_INT, update.gamemode());
} else if (action == 2) {
playerInfoPacket.write(Type.VAR_INT, update.latency());
} else if (action == 3) {
playerInfoPacket.write(Type.OPTIONAL_COMPONENT, update.displayName());
} else {
throw new IllegalArgumentException("Invalid action: " + action);
}
}
playerInfoPacket.send(Protocol1_19_1To1_19_3.class);
}
@Override
public void registerRewrites() {
//TODO metadata oopsies
filter().handler((event, meta) -> {
final int id = meta.metaType().typeId();
meta.setMetaType(Types1_19.META_TYPES.byId(id > 2 ? id - 1 : id)); // long added
if (id > 2) {
meta.setMetaType(Types1_19.META_TYPES.byId(id - 1)); // long added
} else if (id != 2) {
meta.setMetaType(Types1_19.META_TYPES.byId(id));
}
});
registerMetaTypeHandler(Types1_19.META_TYPES.itemType, Types1_19.META_TYPES.blockStateType, Types1_19.META_TYPES.particleType, Types1_19.META_TYPES.optionalComponentType);
@ -189,4 +250,34 @@ public final class EntityPackets1_19_3 extends EntityRewriter<Protocol1_19_1To1_
public EntityType typeFromId(final int typeId) {
return Entity1_19_3Types.getTypeFromId(typeId);
}
private static final class PlayerProfileUpdate {
private final UUID uuid;
private final int gamemode;
private final int latency;
private final JsonElement displayName;
private PlayerProfileUpdate(final UUID uuid, final int gamemode, final int latency, @Nullable final JsonElement displayName) {
this.uuid = uuid;
this.gamemode = gamemode;
this.latency = latency;
this.displayName = displayName;
}
public UUID uuid() {
return uuid;
}
public int gamemode() {
return gamemode;
}
public int latency() {
return latency;
}
public @Nullable JsonElement displayName() {
return displayName;
}
}
}

View File

@ -0,0 +1,23 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2022 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.storage;
import com.viaversion.viabackwards.protocol.protocol1_19to1_19_1.storage.ChatRegistryStorage;
public final class ChatTypeStorage1_19_3 extends ChatRegistryStorage {
}

View File

@ -23,6 +23,7 @@ import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter;
import com.viaversion.viabackwards.protocol.protocol1_19to1_19_1.packets.EntityPackets1_19_1;
import com.viaversion.viabackwards.protocol.protocol1_19to1_19_1.storage.ChatRegistryStorage;
import com.viaversion.viabackwards.protocol.protocol1_19to1_19_1.storage.ChatRegistryStorage1_19_1;
import com.viaversion.viabackwards.protocol.protocol1_19to1_19_1.storage.NonceStorage;
import com.viaversion.viabackwards.protocol.protocol1_19to1_19_1.storage.ReceivedMessagesStorage;
import com.viaversion.viaversion.api.connection.UserConnection;
@ -101,7 +102,7 @@ public final class Protocol1_19To1_19_1 extends BackwardsProtocol<ClientboundPac
map(Type.STRING); // Dimension key
map(Type.STRING); // World
handler(wrapper -> {
final ChatRegistryStorage chatTypeStorage = wrapper.user().get(ChatRegistryStorage.class);
final ChatRegistryStorage chatTypeStorage = wrapper.user().get(ChatRegistryStorage1_19_1.class);
chatTypeStorage.clear();
final CompoundTag registry = wrapper.get(Type.NBT, 0);
@ -172,7 +173,7 @@ public final class Protocol1_19To1_19_1 extends BackwardsProtocol<ClientboundPac
final int chatTypeId = wrapper.read(Type.VAR_INT);
final JsonElement senderName = wrapper.read(Type.COMPONENT);
final JsonElement targetName = wrapper.read(Type.OPTIONAL_COMPONENT);
decoratedMessage = decorateChatMessage(wrapper, chatTypeId, senderName, targetName, message);
decoratedMessage = decorateChatMessage(wrapper.user().get(ChatRegistryStorage1_19_1.class), chatTypeId, senderName, targetName, message);
if (decoratedMessage == null) {
wrapper.cancel();
return;
@ -348,7 +349,7 @@ public final class Protocol1_19To1_19_1 extends BackwardsProtocol<ClientboundPac
@Override
public void init(final UserConnection user) {
user.put(new ChatRegistryStorage());
user.put(new ChatRegistryStorage1_19_1());
user.put(new ReceivedMessagesStorage());
addEntityTracker(user, new EntityTrackerBase(user, Entity1_19Types.PLAYER, true));
}
@ -363,8 +364,8 @@ public final class Protocol1_19To1_19_1 extends BackwardsProtocol<ClientboundPac
return entityRewriter;
}
private @Nullable JsonElement decorateChatMessage(final PacketWrapper wrapper, final int chatTypeId, final JsonElement senderName, @Nullable final JsonElement targetName, final JsonElement message) {
CompoundTag chatType = wrapper.user().get(ChatRegistryStorage.class).chatType(chatTypeId);
public static @Nullable JsonElement decorateChatMessage(final ChatRegistryStorage chatRegistryStorage, final int chatTypeId, final JsonElement senderName, @Nullable final JsonElement targetName, final JsonElement message) {
CompoundTag chatType = chatRegistryStorage.chatType(chatTypeId);
if (chatType == null) {
ViaBackwards.getPlatform().getLogger().warning("Chat message has unknown chat type id " + chatTypeId + ". Message: " + message);
return null;

View File

@ -24,7 +24,7 @@ import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.Protocol1_19To1_18_2;
import org.checkerframework.checker.nullness.qual.Nullable;
public final class ChatRegistryStorage implements StorableObject {
public abstract class ChatRegistryStorage implements StorableObject {
private final Int2ObjectMap<CompoundTag> chatTypes = new Int2ObjectOpenHashMap<>();

View File

@ -0,0 +1,21 @@
/*
* This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards
* Copyright (C) 2016-2022 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viabackwards.protocol.protocol1_19to1_19_1.storage;
public final class ChatRegistryStorage1_19_1 extends ChatRegistryStorage {
}

View File

@ -662,6 +662,7 @@
"minecraft:bamboo_mosaic_stairs": "minecraft:birch_stairs[",
"minecraft:bamboo_slab": "minecraft:birch_slab[",
"minecraft:bamboo_mosaic_slab": "minecraft:birch_slab[",
"minecraft:bamboo_fence": "minecraft:birch_fence[",
"minecraft:bamboo_fence_gate": "minecraft:birch_fence_gate[",
"minecraft:bamboo_door": "minecraft:birch_door["
},