Make 1.19 chat decoration method public

This commit is contained in:
Nassim Jahnke 2022-08-04 16:23:21 +02:00
parent b032a868bb
commit 2b735172bd
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
2 changed files with 54 additions and 15 deletions

View File

@ -0,0 +1,39 @@
/*
* 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.viaversion.protocols.protocol1_19_1to1_19;
import com.google.gson.JsonElement;
public final class ChatDecorationResult {
private final JsonElement content;
private final boolean overlay;
public ChatDecorationResult(final JsonElement content, final boolean overlay) {
this.content = content;
this.overlay = overlay;
}
public JsonElement content() {
return content;
}
public boolean overlay() {
return overlay;
}
}

View File

@ -116,14 +116,20 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
// Back to system chat
final JsonElement signedContent = wrapper.read(Type.COMPONENT);
final JsonElement unsignedContent = wrapper.read(Type.OPTIONAL_COMPONENT);
final int chatType = wrapper.read(Type.VAR_INT);
final int chatTypeId = wrapper.read(Type.VAR_INT);
wrapper.read(Type.UUID); // Sender UUID
final JsonElement senderName = wrapper.read(Type.COMPONENT);
final JsonElement teamName = wrapper.read(Type.OPTIONAL_COMPONENT);
if (!decorateChatMessage(wrapper, chatType, senderName, teamName, unsignedContent != null ? unsignedContent : signedContent)) {
final CompoundTag chatType = wrapper.user().get(ChatTypeStorage.class).chatType(chatTypeId);
final ChatDecorationResult decorationResult = decorateChatMessage(chatType, chatTypeId, senderName, teamName, unsignedContent != null ? unsignedContent : signedContent);
if (decorationResult == null) {
wrapper.cancel();
return;
}
wrapper.write(Type.COMPONENT, decorationResult.content());
wrapper.write(Type.BOOLEAN, decorationResult.overlay());
});
read(Type.LONG); // Timestamp
read(Type.LONG); // Salt
@ -265,7 +271,7 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
if (data.length == 1 && data[0] > 1) {
data[0] = 1;
} else if (data.length == 0) { // Or the version is omitted (default version would be used)
data = new byte[] { 1 };
data = new byte[]{1};
wrapper.set(Type.REMAINING_BYTES, 0, data);
} else {
Via.getPlatform().getLogger().warning("Received unexpected data in velocity:player_info (length=" + data.length + ")");
@ -281,11 +287,10 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
connection.put(new ChatTypeStorage());
}
private boolean decorateChatMessage(final PacketWrapper wrapper, final int chatTypeId, final JsonElement senderName, @Nullable final JsonElement teamName, final JsonElement message) {
final CompoundTag chatType = wrapper.user().get(ChatTypeStorage.class).chatType(chatTypeId);
public static @Nullable ChatDecorationResult decorateChatMessage(final CompoundTag chatType, final int chatTypeId, final JsonElement senderName, @Nullable final JsonElement teamName, final JsonElement message) {
if (chatType == null) {
Via.getPlatform().getLogger().warning("Chat message has unknown chat type id " + chatTypeId + ". Message: " + message);
return false;
return null;
}
CompoundTag chatData = chatType.<CompoundTag>get("element").get("chat");
@ -294,7 +299,7 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
chatData = chatType.<CompoundTag>get("element").get("overlay");
if (chatData == null) {
// Either narration or something we don't know
return false;
return null;
}
overlay = true;
@ -302,9 +307,7 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
final CompoundTag decoaration = chatData.get("decoration");
if (decoaration == null) {
wrapper.write(Type.COMPONENT, message);
wrapper.write(Type.BOOLEAN, overlay);
return true;
return new ChatDecorationResult(message, overlay);
}
final String translationKey = (String) decoaration.get("translation_key").getValue();
@ -356,9 +359,6 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
}
componentBuilder.args(arguments);
}
wrapper.write(Type.COMPONENT, GsonComponentSerializer.gson().serializeToTree(componentBuilder.build()));
wrapper.write(Type.BOOLEAN, overlay);
return true;
return new ChatDecorationResult(GsonComponentSerializer.gson().serializeToTree(componentBuilder.build()), overlay);
}
}