3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-04 23:30:24 +01:00

Merge branch 'master' into dev

Dieser Commit ist enthalten in:
Nassim Jahnke 2023-12-27 19:44:46 +01:00
Commit 3904ed4fd7
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: EF6771C01F6EF02F
31 geänderte Dateien mit 392 neuen und 560 gelöschten Zeilen

Datei anzeigen

@ -1,6 +1,6 @@
<component name="CopyrightManager">
<copyright>
<option name="notice" value="This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion&#10;Copyright (C) &amp;#36;originalComment.match(&quot;Copyright \(C\) (\d+)&quot;, 1, &quot;-&quot;)&amp;#36;today.year ViaVersion and contributors&#10;&#10;This program is free software: you can redistribute it and/or modify&#10;it under the terms of the GNU General Public License as published by&#10;the Free Software Foundation, either version 3 of the License, or&#10;(at your option) any later version.&#10;&#10;This program is distributed in the hope that it will be useful,&#10;but WITHOUT ANY WARRANTY; without even the implied warranty of&#10;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the&#10;GNU General Public License for more details.&#10;&#10;You should have received a copy of the GNU General Public License&#10;along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;." />
<option name="notice" value="This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion&#10;Copyright (C) 2016-&amp;#36;today.year ViaVersion and contributors&#10;&#10;This program is free software: you can redistribute it and/or modify&#10;it under the terms of the GNU General Public License as published by&#10;the Free Software Foundation, either version 3 of the License, or&#10;(at your option) any later version.&#10;&#10;This program is distributed in the hope that it will be useful,&#10;but WITHOUT ANY WARRANTY; without even the implied warranty of&#10;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the&#10;GNU General Public License for more details.&#10;&#10;You should have received a copy of the GNU General Public License&#10;along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;." />
<option name="myName" value="viaversion-gpl" />
</copyright>
</component>

Datei anzeigen

@ -1,6 +1,6 @@
<component name="CopyrightManager">
<copyright>
<option name="notice" value="This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion&#10;Copyright (C) &amp;#36;originalComment.match(&quot;Copyright \(C\) (\d+)&quot;, 1, &quot;-&quot;)&amp;#36;today.year ViaVersion and contributors&#10;&#10;Permission is hereby granted, free of charge, to any person obtaining a copy&#10;of this software and associated documentation files (the &quot;Software&quot;), to deal&#10;in the Software without restriction, including without limitation the rights&#10;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell&#10;copies of the Software, and to permit persons to whom the Software is&#10;furnished to do so, subject to the following conditions:&#10;&#10;The above copyright notice and this permission notice shall be included in all&#10;copies or substantial portions of the Software.&#10;&#10;THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR&#10;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,&#10;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE&#10;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER&#10;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,&#10;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE&#10;SOFTWARE." />
<option name="notice" value="This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion&#10;Copyright (C) 2016-&amp;#36;today.year ViaVersion and contributors&#10;&#10;Permission is hereby granted, free of charge, to any person obtaining a copy&#10;of this software and associated documentation files (the &quot;Software&quot;), to deal&#10;in the Software without restriction, including without limitation the rights&#10;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell&#10;copies of the Software, and to permit persons to whom the Software is&#10;furnished to do so, subject to the following conditions:&#10;&#10;The above copyright notice and this permission notice shall be included in all&#10;copies or substantial portions of the Software.&#10;&#10;THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR&#10;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,&#10;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE&#10;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER&#10;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,&#10;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE&#10;SOFTWARE." />
<option name="myName" value="viaversion-mit" />
</copyright>
</component>

Datei anzeigen

@ -1,25 +0,0 @@
plugins {
id("com.github.johnrengelman.shadow")
}
// Shade and relocate adventure in an extra module, so that common/the rest can directly depend on a
// relocated adventure without breaking native platform's adventure usage with project wide relocation
tasks {
shadowJar {
relocate("net.kyori", "com.viaversion.viaversion.libs.kyori")
}
build {
dependsOn(shadowJar)
}
}
dependencies {
api(libs.bundles.adventure) {
exclude("org.checkerframework")
exclude("net.kyori", "adventure-api")
exclude("net.kyori", "adventure-bom")
exclude("com.google.code.gson", "gson")
}
}
publishShadowJar()

Datei anzeigen

@ -15,14 +15,14 @@ sourceSets {
}
dependencies {
api(projects.adventure) {
targetConfiguration = "shadow"
}
api(libs.fastutil)
api(libs.flare)
api(libs.flareFastutil)
api(libs.vianbt)
api(libs.gson)
implementation(rootProject.libs.text) {
exclude("com.google.code.gson", "gson")
}
compileOnlyApi(libs.snakeYaml)
compileOnlyApi(libs.netty)

Datei anzeigen

@ -25,7 +25,7 @@ package com.viaversion.viaversion.api.minecraft.signature.model.chain.v1_19_1;
import com.viaversion.viaversion.api.minecraft.PlayerMessageSignature;
import com.viaversion.viaversion.api.minecraft.signature.model.DecoratableMessage;
import com.viaversion.viaversion.api.minecraft.signature.util.DataConsumer;
import com.viaversion.viaversion.util.GsonUtil;
import net.lenni0451.mcstructs.text.utils.JsonUtils;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
@ -62,7 +62,7 @@ public class MessageBody {
dataOutputStream.write(this.content.plain().getBytes(StandardCharsets.UTF_8));
dataOutputStream.write(HASH_SEPARATOR_BYTE);
if (this.content.isDecorated()) {
dataOutputStream.write(GsonUtil.toSortedString(this.content.decorated(), null).getBytes(StandardCharsets.UTF_8));
dataOutputStream.write(JsonUtils.toSortedString(this.content.decorated(), null).getBytes(StandardCharsets.UTF_8));
}
for (PlayerMessageSignature lastSeenMessage : this.lastSeenMessages) {

Datei anzeigen

@ -25,7 +25,7 @@ package com.viaversion.viaversion.api.minecraft.signature.storage;
import com.viaversion.viaversion.api.minecraft.ProfileKey;
import com.viaversion.viaversion.api.minecraft.signature.model.DecoratableMessage;
import com.viaversion.viaversion.api.minecraft.signature.model.MessageMetadata;
import com.viaversion.viaversion.util.GsonUtil;
import net.lenni0451.mcstructs.text.utils.JsonUtils;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@ -48,7 +48,7 @@ public class ChatSession1_19_0 extends ChatSession {
buffer.putLong(metadata.sender().getMostSignificantBits()).putLong(metadata.sender().getLeastSignificantBits());
buffer.putLong(metadata.timestamp().getEpochSecond());
signer.accept(data);
signer.accept(GsonUtil.toSortedString(content.decorated(), null).getBytes(StandardCharsets.UTF_8));
signer.accept(JsonUtils.toSortedString(content.decorated(), null).getBytes(StandardCharsets.UTF_8));
});
}

Datei anzeigen

@ -46,7 +46,7 @@ public class ChunkSectionType1_8 extends Type<ChunkSection> {
ByteBuf littleEndianView = buffer.order(ByteOrder.LITTLE_ENDIAN);
for (int idx = 0; idx < ChunkSection.SIZE; idx++) {
blocks.setIdAt(idx, littleEndianView.readShort());
blocks.setIdAt(idx, littleEndianView.readUnsignedShort());
}
return chunkSection;

Datei anzeigen

@ -22,12 +22,8 @@
*/
package com.viaversion.viaversion.util;
import com.google.gson.*;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public final class GsonUtil {
private static final Gson GSON = new GsonBuilder().create();
@ -41,52 +37,4 @@ public final class GsonUtil {
return GSON;
}
/**
* Convert a json element to a sorted string.<br>
* If the {@code comparator} is null, {@link Comparator#naturalOrder()} will be used.
*
* @param element The element to convert
* @param comparator The comparator to use
* @return The sorted string
*/
public static String toSortedString(@Nullable final JsonElement element, @Nullable final Comparator<String> comparator) {
if (element == null) {
return null;
} else if (comparator != null) {
return sort(element, comparator).toString();
} else {
return sort(element, Comparator.naturalOrder()).toString();
}
}
/**
* Sort a json element.
*
* @param element The element to sort
* @param comparator The comparator to use
* @return The sorted element
*/
public static JsonElement sort(@Nullable final JsonElement element, final Comparator<String> comparator) {
if (element == null) {
return null;
} else if (element.isJsonArray()) {
final JsonArray array = element.getAsJsonArray();
for (int i = 0; i < array.size(); i++) {
array.set(i, sort(array.get(i), comparator));
}
return array;
} else if (element.isJsonObject()) {
final JsonObject object = element.getAsJsonObject();
final JsonObject sorted = new JsonObject();
final List<String> keys = new ArrayList<>(object.keySet());
keys.sort(comparator);
for (String key : keys) {
sorted.add(key, sort(object.get(key), comparator));
}
return sorted;
} else {
return element;
}
}
}

Datei anzeigen

@ -26,6 +26,7 @@ fun ShadowJar.configureRelocations() {
relocate("com.github.steveice10.opennbt", "com.viaversion.viaversion.libs.opennbt")
relocate("it.unimi.dsi.fastutil", "com.viaversion.viaversion.libs.fastutil")
relocate("space.vectrix.flare", "com.viaversion.viaversion.libs.flare")
relocate("net.lenni0451.mcstructs", "com.viaversion.viaversion.libs.mcstructs")
}
fun ShadowJar.configureExcludes() {

Datei anzeigen

@ -20,14 +20,12 @@ val main = setOf(
projects.viaversionVelocity
).map { it.dependencyProject }
val special = setOf(
projects.adventure
).map { it.dependencyProject }
// val special = setOf().map { it.dependencyProject }
subprojects {
when (this) {
in main -> plugins.apply("via.shadow-conventions")
in special -> plugins.apply("via.base-conventions")
// in special -> plugins.apply("via.base-conventions")
else -> plugins.apply("via.standard-conventions")
}
}

Datei anzeigen

@ -1,6 +1,9 @@
dependencies {
api(projects.viaversionApi)
api(projects.viaversionApiLegacy)
api(rootProject.libs.text) {
exclude("com.google.code.gson", "gson")
}
implementation(projects.compat.snakeyaml2Compat)
implementation(projects.compat.snakeyaml1Compat)

Datei anzeigen

@ -1,75 +0,0 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2023 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_13to1_12_2;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.libs.kyori.adventure.text.Component;
import com.viaversion.viaversion.libs.kyori.adventure.text.format.TextDecoration;
import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.legacyimpl.NBTLegacyHoverEventSerializer;
import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import java.util.logging.Level;
public final class ChatRewriter {
public static final GsonComponentSerializer HOVER_GSON_SERIALIZER = GsonComponentSerializer.builder().emitLegacyHoverEvent().legacyHoverEventSerializer(NBTLegacyHoverEventSerializer.get()).build();
public static JsonObject emptyComponent() {
final JsonObject object = new JsonObject();
object.addProperty("text", "");
return object;
}
public static String emptyComponentString() {
return "{\"text\":\"\"}";
}
public static String legacyTextToJsonString(String message, boolean itemData) {
// Not used for chat messages, so no need for url extraction
Component component = LegacyComponentSerializer.legacySection().deserialize(message);
if (itemData) {
component = Component.text().decoration(TextDecoration.ITALIC, false).append(component).build();
}
return GsonComponentSerializer.gson().serialize(component);
}
public static String legacyTextToJsonString(String legacyText) {
return legacyTextToJsonString(legacyText, false);
}
public static JsonElement legacyTextToJson(String legacyText) {
return JsonParser.parseString(legacyTextToJsonString(legacyText, false));
}
public static String jsonToLegacyText(String value) {
try {
Component component = HOVER_GSON_SERIALIZER.deserialize(value);
return LegacyComponentSerializer.legacySection().serialize(component);
} catch (Exception e) {
Via.getPlatform().getLogger().log(Level.WARNING, "Error converting json text to legacy: " + value, e);
return "";
}
}
@Deprecated/*(forRemoval = true)*/
public static void processTranslate(JsonElement value) {
Via.getManager().getProtocolManager().getProtocol(Protocol1_13To1_12_2.class).getComponentRewriter().processText(value);
}
}

Datei anzeigen

@ -62,6 +62,7 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockSto
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.TabCompleteTracker;
import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.util.ChatColorUtil;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.GsonUtil;
import java.util.ArrayList;
import java.util.HashMap;
@ -435,7 +436,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
// On create or update
if (mode == 0 || mode == 2) {
String value = wrapper.read(Type.STRING); // Value
wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(value));
wrapper.write(Type.COMPONENT, ComponentUtil.legacyToJson(value));
String type = wrapper.read(Type.STRING);
// integer or hearts
@ -456,7 +457,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
if (action == 0 || action == 2) {
String displayName = wrapper.read(Type.STRING); // Display Name
wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(displayName));
wrapper.write(Type.COMPONENT, ComponentUtil.legacyToJson(displayName));
String prefix = wrapper.read(Type.STRING); // Prefix moved
String suffix = wrapper.read(Type.STRING); // Suffix moved
@ -480,8 +481,8 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
wrapper.write(Type.VAR_INT, colour);
wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(prefix)); // Prefix
wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(suffix)); // Suffix
wrapper.write(Type.COMPONENT, ComponentUtil.legacyToJson(prefix)); // Prefix
wrapper.write(Type.COMPONENT, ComponentUtil.legacyToJson(suffix)); // Suffix
}
if (action == 0 || action == 3 || action == 4) {

Datei anzeigen

@ -18,19 +18,19 @@
package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.metadata;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_13;
import com.viaversion.viaversion.api.minecraft.Particle;
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_13;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.api.minecraft.Particle;
import com.viaversion.viaversion.api.type.types.version.Types1_13;
import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.EntityTypeRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.ParticleRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.packets.WorldPackets;
import com.viaversion.viaversion.rewriter.EntityRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import java.util.List;
public class MetadataRewriter1_13To1_12_2 extends EntityRewriter<ClientboundPackets1_12_1, Protocol1_13To1_12_2> {
@ -51,7 +51,7 @@ public class MetadataRewriter1_13To1_12_2 extends EntityRewriter<ClientboundPack
// Handle String -> Chat DisplayName
if (metadata.id() == 2) {
if (metadata.getValue() != null && !((String) metadata.getValue()).isEmpty()) {
metadata.setTypeAndValue(Types1_13.META_TYPES.optionalComponentType, ChatRewriter.legacyTextToJson((String) metadata.getValue()));
metadata.setTypeAndValue(Types1_13.META_TYPES.optionalComponentType, ComponentUtil.legacyToJson((String) metadata.getValue()));
} else {
metadata.setTypeAndValue(Types1_13.META_TYPES.optionalComponentType, null);
}

Datei anzeigen

@ -31,7 +31,6 @@ import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13;
@ -40,6 +39,7 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.MappingData
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.SoundSource;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.SpawnEggRewriter;
import com.viaversion.viaversion.rewriter.ItemRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.Key;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
@ -315,7 +315,7 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_12_1, Ser
if (display.get("Name") instanceof StringTag) {
StringTag name = display.get("Name");
display.put(NBT_TAG_NAME + "|Name", new StringTag(name.getValue()));
name.setValue(ChatRewriter.legacyTextToJsonString(name.getValue(), true));
name.setValue(ComponentUtil.legacyToJsonString(name.getValue(), true));
}
}
// ench is now Enchantments and now uses identifiers
@ -573,7 +573,7 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_12_1, Ser
if (display.get("Name") instanceof StringTag) {
StringTag name = display.get("Name");
StringTag via = display.remove(NBT_TAG_NAME + "|Name");
name.setValue(via != null ? via.getValue() : ChatRewriter.jsonToLegacyText(name.getValue()));
name.setValue(via != null ? via.getValue() : ComponentUtil.jsonToLegacy(name.getValue()));
}
}

Datei anzeigen

@ -26,9 +26,9 @@ import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.providers.BlockEntityProvider;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage;
import com.viaversion.viaversion.util.ComponentUtil;
public class BannerHandler implements BlockEntityProvider.BlockEntityHandler {
private static final int WALL_BANNER_START = 7110; // 4 each
@ -78,7 +78,7 @@ public class BannerHandler implements BlockEntityProvider.BlockEntityHandler {
Tag name = tag.get("CustomName");
if (name instanceof StringTag) {
((StringTag) name).setValue(ChatRewriter.legacyTextToJsonString(((StringTag) name).getValue()));
((StringTag) name).setValue(ComponentUtil.legacyToJsonString(((StringTag) name).getValue()));
}
return blockId;

Datei anzeigen

@ -24,9 +24,9 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.providers.BlockEntityProvider;
import com.viaversion.viaversion.util.ComponentUtil;
public class CommandBlockHandler implements BlockEntityProvider.BlockEntityHandler {
@ -36,7 +36,7 @@ public class CommandBlockHandler implements BlockEntityProvider.BlockEntityHandl
public int transform(UserConnection user, CompoundTag tag) {
Tag name = tag.get("CustomName");
if (name instanceof StringTag) {
((StringTag) name).setValue(ChatRewriter.legacyTextToJsonString(((StringTag) name).getValue()));
((StringTag) name).setValue(ComponentUtil.legacyToJsonString(((StringTag) name).getValue()));
}
Tag out = tag.get("LastOutput");
if (out instanceof StringTag) {

Datei anzeigen

@ -17,7 +17,11 @@
*/
package com.viaversion.viaversion.protocols.protocol1_14to1_13_2.packets;
import com.github.steveice10.opennbt.tag.builtin.*;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.DoubleTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.common.collect.Sets;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
@ -27,7 +31,6 @@ import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14;
@ -37,8 +40,8 @@ import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTr
import com.viaversion.viaversion.rewriter.ComponentRewriter;
import com.viaversion.viaversion.rewriter.ItemRewriter;
import com.viaversion.viaversion.rewriter.RecipeRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.Key;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
@ -248,7 +251,7 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_13, Serve
display.put(NBT_TAG_NAME + "|Lore", new ListTag(lore.clone().getValue())); // Save old lore
for (Tag loreEntry : lore) {
if (loreEntry instanceof StringTag) {
String jsonText = ChatRewriter.legacyTextToJsonString(((StringTag) loreEntry).getValue(), true);
String jsonText = ComponentUtil.legacyToJsonString(((StringTag) loreEntry).getValue(), true);
((StringTag) loreEntry).setValue(jsonText);
}
}
@ -277,7 +280,7 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_13, Serve
} else {
for (Tag loreEntry : lore) {
if (loreEntry instanceof StringTag) {
((StringTag) loreEntry).setValue(ChatRewriter.jsonToLegacyText(((StringTag) loreEntry).getValue()));
((StringTag) loreEntry).setValue(ComponentUtil.jsonToLegacy(((StringTag) loreEntry).getValue()));
}
}
}

Datei anzeigen

@ -18,7 +18,12 @@
package com.viaversion.viaversion.protocols.protocol1_19_1to1_19;
import com.github.steveice10.opennbt.stringified.SNBT;
import com.github.steveice10.opennbt.tag.builtin.*;
import com.github.steveice10.opennbt.tag.builtin.ByteTag;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.NumberTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.common.base.Preconditions;
import com.google.gson.JsonElement;
import com.viaversion.viaversion.api.Via;
@ -32,12 +37,6 @@ import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.libs.kyori.adventure.text.Component;
import com.viaversion.viaversion.libs.kyori.adventure.text.TranslatableComponent;
import com.viaversion.viaversion.libs.kyori.adventure.text.format.NamedTextColor;
import com.viaversion.viaversion.libs.kyori.adventure.text.format.Style;
import com.viaversion.viaversion.libs.kyori.adventure.text.format.TextDecoration;
import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets;
import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets;
import com.viaversion.viaversion.protocols.protocol1_19_1to1_19.storage.ChatTypeStorage;
@ -46,11 +45,16 @@ import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.ClientboundPacke
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.ServerboundPackets1_19;
import com.viaversion.viaversion.util.CipherUtil;
import com.viaversion.viaversion.util.Pair;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import net.lenni0451.mcstructs.core.TextFormatting;
import net.lenni0451.mcstructs.text.ATextComponent;
import net.lenni0451.mcstructs.text.Style;
import net.lenni0451.mcstructs.text.components.TranslationComponent;
import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
import org.checkerframework.checker.nullness.qual.Nullable;
public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPackets1_19, ClientboundPackets1_19_1, ServerboundPackets1_19, ServerboundPackets1_19_1> {
@ -326,7 +330,13 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
connection.put(new ChatTypeStorage());
}
public static @Nullable ChatDecorationResult decorateChatMessage(final CompoundTag chatType, final int chatTypeId, final JsonElement senderName, @Nullable final JsonElement teamName, final JsonElement message) {
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 null;
@ -344,38 +354,60 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
overlay = true;
}
final CompoundTag decoaration = chatData.get("decoration");
if (decoaration == null) {
final CompoundTag decoration = chatData.get("decoration");
if (decoration == null) {
return new ChatDecorationResult(message, overlay);
}
final String translationKey = (String) decoaration.get("translation_key").getValue();
final TranslatableComponent.Builder componentBuilder = Component.translatable().key(translationKey);
return new ChatDecorationResult(translatabaleComponentFromTag(decoration, senderName, teamName, message), overlay);
}
public static JsonElement translatabaleComponentFromTag(
final CompoundTag tag,
final JsonElement senderName,
@Nullable final JsonElement targetName,
final JsonElement message
) {
final String translationKey = (String) tag.get("translation_key").getValue();
final Style style = new Style();
// Add the style
final CompoundTag style = decoaration.get("style");
if (style != null) {
final Style.Builder styleBuilder = Style.style();
final StringTag color = style.get("color");
final CompoundTag styleTag = tag.get("style");
if (styleTag != null) {
final StringTag color = styleTag.get("color");
if (color != null) {
final NamedTextColor textColor = NamedTextColor.NAMES.value(color.getValue());
final TextFormatting textColor = TextFormatting.getByName(color.getValue());
if (textColor != null) {
styleBuilder.color(NamedTextColor.NAMES.value(color.getValue()));
style.setFormatting(textColor);
}
}
for (final String key : TextDecoration.NAMES.keys()) {
if (style.contains(key)) {
styleBuilder.decoration(TextDecoration.NAMES.value(key), style.<ByteTag>get(key).asByte() == 1);
for (final Map.Entry<String, TextFormatting> entry : TextFormatting.FORMATTINGS.entrySet()) {
final Tag formattingTag = styleTag.get(entry.getKey());
if (!(formattingTag instanceof ByteTag)) {
continue;
}
final boolean value = ((NumberTag) formattingTag).asBoolean();
final TextFormatting formatting = entry.getValue();
if (formatting == TextFormatting.OBFUSCATED) {
style.setObfuscated(value);
} else if (formatting == TextFormatting.BOLD) {
style.setBold(value);
} else if (formatting == TextFormatting.STRIKETHROUGH) {
style.setStrikethrough(value);
} else if (formatting == TextFormatting.UNDERLINE) {
style.setUnderlined(value);
} else if (formatting == TextFormatting.ITALIC) {
style.setItalic(value);
}
}
componentBuilder.style(styleBuilder.build());
}
// Add the replacements
final ListTag parameters = decoaration.get("parameters");
final ListTag parameters = tag.get("parameters");
final List<ATextComponent> arguments = new ArrayList<>();
if (parameters != null) {
final List<Component> arguments = new ArrayList<>();
for (final Tag element : parameters) {
JsonElement argument = null;
switch ((String) element.getValue()) {
@ -386,18 +418,19 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
argument = message;
break;
case "team_name":
Preconditions.checkNotNull(teamName, "Team name is null");
argument = teamName;
case "target": // So that this method can also be used in VB
Preconditions.checkNotNull(targetName, "Team name is null");
argument = targetName;
break;
default:
Via.getPlatform().getLogger().warning("Unknown parameter for chat decoration: " + element.getValue());
}
if (argument != null) {
arguments.add(GsonComponentSerializer.gson().deserializeFromTree(argument));
arguments.add(TextComponentSerializer.V1_18.deserialize(argument));
}
}
componentBuilder.args(arguments);
}
return new ChatDecorationResult(GsonComponentSerializer.gson().serializeToTree(componentBuilder.build()), overlay);
return TextComponentSerializer.V1_18.serializeJson(new TranslationComponent(translationKey, arguments));
}
}

Datei anzeigen

@ -39,8 +39,6 @@ import com.viaversion.viaversion.api.type.types.BitSetType;
import com.viaversion.viaversion.api.type.types.misc.ParticleType;
import com.viaversion.viaversion.api.type.types.version.Types1_19_3;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.libs.kyori.adventure.text.Component;
import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets;
import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets;
import com.viaversion.viaversion.protocols.protocol1_19_1to1_19.ClientboundPackets1_19_1;
@ -53,8 +51,8 @@ import com.viaversion.viaversion.rewriter.CommandRewriter;
import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.rewriter.StatisticsRewriter;
import com.viaversion.viaversion.rewriter.TagRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.Pair;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
@ -200,7 +198,7 @@ public final class Protocol1_19_3To1_19_1 extends AbstractProtocol<ClientboundPa
decoratedMessage = unsignedMessage;
}
if (decoratedMessage == null) {
decoratedMessage = GsonComponentSerializer.gson().serializeToTree(Component.text(plainMessage));
decoratedMessage = ComponentUtil.plainToJson(plainMessage);
}
final int filterMaskType = wrapper.read(Type.VAR_INT);

Datei anzeigen

@ -26,7 +26,6 @@ import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.misc.ParticleType;
import com.viaversion.viaversion.api.type.types.version.Types1_19_4;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter;
import com.viaversion.viaversion.protocols.protocol1_19_3to1_19_1.ClientboundPackets1_19_3;
import com.viaversion.viaversion.protocols.protocol1_19_3to1_19_1.ServerboundPackets1_19_3;
import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.data.MappingData;
@ -37,6 +36,7 @@ import com.viaversion.viaversion.rewriter.CommandRewriter;
import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.rewriter.StatisticsRewriter;
import com.viaversion.viaversion.rewriter.TagRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
@ -78,7 +78,7 @@ public final class Protocol1_19_4To1_19_3 extends AbstractProtocol<ClientboundPa
if (element != null) {
wrapper.write(Type.COMPONENT, element);
} else {
wrapper.write(Type.COMPONENT, ChatRewriter.emptyComponent());
wrapper.write(Type.COMPONENT, ComponentUtil.emptyJsonComponent());
}
final String iconBase64 = wrapper.read(Type.OPTIONAL_STRING);

Datei anzeigen

@ -32,7 +32,6 @@ import com.viaversion.viaversion.api.type.types.version.Types1_19;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets;
import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter;
import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17;
import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.ClientboundPackets1_18;
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.data.MappingData;
@ -48,6 +47,7 @@ import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.rewriter.StatisticsRewriter;
import com.viaversion.viaversion.rewriter.TagRewriter;
import com.viaversion.viaversion.util.CipherUtil;
import com.viaversion.viaversion.util.ComponentUtil;
import java.util.concurrent.ThreadLocalRandom;
public final class Protocol1_19To1_18_2 extends AbstractProtocol<ClientboundPackets1_18, ClientboundPackets1_19, ServerboundPackets1_17, ServerboundPackets1_19> {
@ -68,7 +68,7 @@ public final class Protocol1_19To1_18_2 extends AbstractProtocol<ClientboundPack
if (!isTextComponentNull(component)) {
return component;
} else {
return ChatRewriter.emptyComponent();
return ComponentUtil.emptyJsonComponent();
}
}

Datei anzeigen

@ -42,10 +42,10 @@ import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.Clientb
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ServerboundPackets1_20_3;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.rewriter.BlockItemPacketRewriter1_20_3;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.rewriter.EntityPacketRewriter1_20_3;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.util.ComponentConverter;
import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.rewriter.StatisticsRewriter;
import com.viaversion.viaversion.rewriter.TagRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import java.nio.charset.StandardCharsets;
import java.util.BitSet;
import java.util.UUID;
@ -336,11 +336,11 @@ public final class Protocol1_20_3To1_20_2 extends AbstractProtocol<ClientboundPa
}
private void convertComponent(final PacketWrapper wrapper) throws Exception {
wrapper.write(Type.TAG, ComponentConverter.jsonComponentToTag(wrapper.read(Type.COMPONENT)));
wrapper.write(Type.TAG, ComponentUtil.jsonToTag(wrapper.read(Type.COMPONENT)));
}
private void convertOptionalComponent(final PacketWrapper wrapper) throws Exception {
wrapper.write(Type.OPTIONAL_TAG, ComponentConverter.jsonComponentToTag(wrapper.read(Type.OPTIONAL_COMPONENT)));
wrapper.write(Type.OPTIONAL_TAG, ComponentUtil.jsonToTag(wrapper.read(Type.OPTIONAL_COMPONENT)));
}
@Override

Datei anzeigen

@ -32,8 +32,8 @@ import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.Clientbou
import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundPackets1_20_2;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.Protocol1_20_3To1_20_2;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ClientboundPackets1_20_3;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.util.ComponentConverter;
import com.viaversion.viaversion.rewriter.EntityRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.Key;
public final class EntityPacketRewriter1_20_3 extends EntityRewriter<ClientboundPackets1_20_2, Protocol1_20_3To1_20_2> {
@ -103,9 +103,9 @@ public final class EntityPacketRewriter1_20_3 extends EntityRewriter<Clientbound
filter().handler((event, meta) -> {
final MetaType type = meta.metaType();
if (type == Types1_20_2.META_TYPES.componentType) {
meta.setTypeAndValue(Types1_20_3.META_TYPES.componentType, ComponentConverter.jsonComponentToTag(meta.value()));
meta.setTypeAndValue(Types1_20_3.META_TYPES.componentType, ComponentUtil.jsonToTag(meta.value()));
} else if (type == Types1_20_2.META_TYPES.optionalComponentType) {
meta.setTypeAndValue(Types1_20_3.META_TYPES.optionalComponentType, ComponentConverter.jsonComponentToTag(meta.value()));
meta.setTypeAndValue(Types1_20_3.META_TYPES.optionalComponentType, ComponentUtil.jsonToTag(meta.value()));
} else if (type == Types1_20_2.META_TYPES.particleType) {
final Particle particle = (Particle) meta.getValue();
final ParticleMappings particleMappings = protocol.getMappingData().getParticleMappings();

Datei anzeigen

@ -1,313 +0,0 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2023 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_20_3to1_20_2.util;
import com.github.steveice10.opennbt.tag.builtin.ByteArrayTag;
import com.github.steveice10.opennbt.tag.builtin.ByteTag;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.DoubleTag;
import com.github.steveice10.opennbt.tag.builtin.FloatTag;
import com.github.steveice10.opennbt.tag.builtin.IntArrayTag;
import com.github.steveice10.opennbt.tag.builtin.IntTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.LongArrayTag;
import com.github.steveice10.opennbt.tag.builtin.LongTag;
import com.github.steveice10.opennbt.tag.builtin.NumberTag;
import com.github.steveice10.opennbt.tag.builtin.ShortTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.internal.LazilyParsedNumber;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.util.Pair;
import com.viaversion.viaversion.util.UUIDUtil;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Level;
import org.checkerframework.checker.nullness.qual.Nullable;
public final class ComponentConverter {
private static final boolean DEBUG = Boolean.getBoolean("viaversion.debug.components");
private static final Set<String> BOOLEAN_TYPES = new HashSet<>(Arrays.asList(
"interpret",
"bold",
"italic",
"underlined",
"strikethrough",
"obfuscated"
));
// Order is important
private static final List<Pair<String, String>> COMPONENT_TYPES = Arrays.asList(
new Pair<>("text", "text"),
new Pair<>("translatable", "translate"),
new Pair<>("score", "score"),
new Pair<>("selector", "selector"),
new Pair<>("keybind", "keybind"),
new Pair<>("nbt", "nbt")
);
public static @Nullable JsonElement tagComponentToJson(@Nullable final Tag tag) {
if (DEBUG) {
Via.getPlatform().getLogger().info("Converting tag to json: " + tag);
}
try {
return convertToJson(null, tag);
} catch (final Exception e) {
Via.getPlatform().getLogger().log(Level.SEVERE, "Error converting component: " + tag, e);
return new JsonPrimitive("<error>");
}
}
public static @Nullable Tag jsonComponentToTag(@Nullable final JsonElement component) {
if (DEBUG) {
Via.getPlatform().getLogger().info("Converting json to tag: " + component);
}
try {
return convertToTag(component);
} catch (final Exception e) {
Via.getPlatform().getLogger().log(Level.SEVERE, "Error converting component: " + component, e);
return new StringTag("<error>");
}
}
private static @Nullable Tag convertToTag(final @Nullable JsonElement element) {
if (element == null || element.isJsonNull()) {
return null;
} else if (element.isJsonObject()) {
final CompoundTag tag = new CompoundTag();
final JsonObject jsonObject = element.getAsJsonObject();
for (final Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
convertObjectEntry(entry.getKey(), entry.getValue(), tag);
}
addComponentType(jsonObject, tag);
return tag;
} else if (element.isJsonArray()) {
return convertJsonArray(element.getAsJsonArray());
} else if (element.isJsonPrimitive()) {
final JsonPrimitive primitive = element.getAsJsonPrimitive();
if (primitive.isString()) {
return new StringTag(primitive.getAsString());
} else if (primitive.isBoolean()) {
return new ByteTag((byte) (primitive.getAsBoolean() ? 1 : 0));
}
final Number number = primitive.getAsNumber();
if (number instanceof Integer) {
return new IntTag(number.intValue());
} else if (number instanceof Byte) {
return new ByteTag(number.byteValue());
} else if (number instanceof Short) {
return new ShortTag(number.shortValue());
} else if (number instanceof Long) {
return new LongTag(number.longValue());
} else if (number instanceof Double) {
return new DoubleTag(number.doubleValue());
} else if (number instanceof Float) {
return new FloatTag(number.floatValue());
} else if (number instanceof LazilyParsedNumber) {
// TODO: This might need better handling
return new IntTag(number.intValue());
}
return new IntTag(number.intValue()); // ???
}
throw new IllegalArgumentException("Unhandled json type " + element.getClass().getSimpleName() + " with value " + element.getAsString());
}
private static ListTag convertJsonArray(final JsonArray array) {
// TODO Number arrays?
final ListTag listTag = new ListTag();
boolean singleType = true;
for (final JsonElement entry : array) {
final Tag convertedEntryTag = convertToTag(entry);
if (listTag.getElementType() != null && listTag.getElementType() != convertedEntryTag.getClass()) {
singleType = false;
break;
}
listTag.add(convertedEntryTag);
}
if (singleType) {
return listTag;
}
// Generally, modern vanilla-esque serializers should not produce this format, so it should be rare
// Lists are only used for lists of components ("extra" and "with")
final ListTag processedListTag = new ListTag();
for (final JsonElement entry : array) {
final Tag convertedTag = convertToTag(entry);
if (convertedTag instanceof CompoundTag) {
processedListTag.add(convertedTag);
continue;
}
// Wrap all entries in compound tags, as lists can only consist of one type of tag
final CompoundTag compoundTag = new CompoundTag();
compoundTag.put("type", new StringTag("text"));
if (convertedTag instanceof ListTag) {
compoundTag.put("text", new StringTag());
compoundTag.put("extra", convertedTag);
} else {
compoundTag.put("text", new StringTag(convertedTag.asRawString()));
}
processedListTag.add(compoundTag);
}
return processedListTag;
}
/**
* Converts a json object entry to a tag entry.
*
* @param key key of the entry
* @param value value of the entry
* @param tag the resulting compound tag
*/
private static void convertObjectEntry(final String key, final JsonElement value, final CompoundTag tag) {
if ((key.equals("contents")) && value.isJsonObject()) {
// Store show_entity id as int array instead of uuid string
// Not really required, but we might as well make it more compact
final JsonObject hoverEvent = value.getAsJsonObject();
final JsonElement id = hoverEvent.get("id");
final UUID uuid;
if (id != null && id.isJsonPrimitive() && (uuid = UUIDUtil.parseUUID(id.getAsString())) != null) {
hoverEvent.remove("id");
final CompoundTag convertedTag = (CompoundTag) convertToTag(value);
convertedTag.put("id", new IntArrayTag(UUIDUtil.toIntArray(uuid)));
tag.put(key, convertedTag);
return;
}
}
tag.put(key, convertToTag(value));
}
private static void addComponentType(final JsonObject object, final CompoundTag tag) {
if (object.has("type")) {
return;
}
// Add the type to speed up deserialization and make DFU errors slightly more useful
for (final Pair<String, String> pair : COMPONENT_TYPES) {
if (object.has(pair.value())) {
tag.put("type", new StringTag(pair.key()));
return;
}
}
}
private static @Nullable JsonElement convertToJson(final @Nullable String key, final @Nullable Tag tag) {
if (tag == null) {
return null;
} else if (tag instanceof CompoundTag) {
final JsonObject object = new JsonObject();
if (!"value".equals(key)) {
removeComponentType(object);
}
for (final Map.Entry<String, Tag> entry : ((CompoundTag) tag).entrySet()) {
convertCompoundTagEntry(entry.getKey(), entry.getValue(), object);
}
return object;
} else if (tag instanceof ListTag) {
final ListTag list = (ListTag) tag;
final JsonArray array = new JsonArray();
for (final Tag listEntry : list) {
array.add(convertToJson(null, listEntry));
}
return array;
} else if (tag instanceof NumberTag) {
final NumberTag numberTag = (NumberTag) tag;
if (key != null && BOOLEAN_TYPES.contains(key)) {
// Booleans don't have a direct representation in nbt
return new JsonPrimitive(numberTag.asBoolean());
}
return new JsonPrimitive(numberTag.getValue());
} else if (tag instanceof StringTag) {
return new JsonPrimitive(((StringTag) tag).getValue());
} else if (tag instanceof ByteArrayTag) {
final ByteArrayTag arrayTag = (ByteArrayTag) tag;
final JsonArray array = new JsonArray();
for (final byte num : arrayTag.getValue()) {
array.add(num);
}
return array;
} else if (tag instanceof IntArrayTag) {
final IntArrayTag arrayTag = (IntArrayTag) tag;
final JsonArray array = new JsonArray();
for (final int num : arrayTag.getValue()) {
array.add(num);
}
return array;
} else if (tag instanceof LongArrayTag) {
final LongArrayTag arrayTag = (LongArrayTag) tag;
final JsonArray array = new JsonArray();
for (final long num : arrayTag.getValue()) {
array.add(num);
}
return array;
}
throw new IllegalArgumentException("Unhandled tag type " + tag.getClass().getSimpleName());
}
private static void convertCompoundTagEntry(final String key, final Tag tag, final JsonObject object) {
if ((key.equals("contents")) && tag instanceof CompoundTag) {
// Back to a UUID string
final CompoundTag showEntity = (CompoundTag) tag;
final Tag idTag = showEntity.get("id");
if (idTag instanceof IntArrayTag) {
showEntity.remove("id");
final JsonObject convertedElement = (JsonObject) convertToJson(key, tag);
final UUID uuid = UUIDUtil.fromIntArray(((IntArrayTag) idTag).getValue());
convertedElement.addProperty("id", uuid.toString());
object.add(key, convertedElement);
return;
}
}
// "":1 is a valid tag, but not a valid json component
object.add(key.isEmpty() ? "text" : key, convertToJson(key, tag));
}
private static void removeComponentType(final JsonObject object) {
final JsonElement type = object.remove("type");
if (type == null || !type.isJsonPrimitive()) {
return;
}
// Remove the other fields
final String typeString = type.getAsString();
for (final Pair<String, String> pair : COMPONENT_TYPES) {
if (!pair.key().equals(typeString)) {
object.remove(pair.value());
}
}
}
}

Datei anzeigen

@ -25,7 +25,6 @@ import com.viaversion.viaversion.api.minecraft.BlockChangeRecord;
import com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter;
import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_18;
import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.ClientboundPackets1_19_4;
import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.ServerboundPackets1_19_4;
@ -34,6 +33,7 @@ import com.viaversion.viaversion.protocols.protocol1_20to1_19_4.Protocol1_20To1_
import com.viaversion.viaversion.rewriter.BlockRewriter;
import com.viaversion.viaversion.rewriter.ItemRewriter;
import com.viaversion.viaversion.rewriter.RecipeRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.Key;
public final class InventoryPackets extends ItemRewriter<ClientboundPackets1_19_4, ServerboundPackets1_19_4, Protocol1_20To1_19_4> {
@ -173,7 +173,7 @@ public final class InventoryPackets extends ItemRewriter<ClientboundPackets1_19_
final ListTag messages = new ListTag(StringTag.class);
for (int i = 1; i < 5; i++) {
final Tag text = tag.remove("Text" + i);
messages.add(text != null ? text : new StringTag(ChatRewriter.emptyComponentString()));
messages.add(text != null ? text : new StringTag(ComponentUtil.emptyJsonComponentString()));
}
frontText.put("messages", messages);

Datei anzeigen

@ -0,0 +1,148 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2023 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.util;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.viaversion.viaversion.api.Via;
import net.lenni0451.mcstructs.snbt.SNbtSerializer;
import net.lenni0451.mcstructs.text.ATextComponent;
import net.lenni0451.mcstructs.text.Style;
import net.lenni0451.mcstructs.text.events.hover.AHoverEvent;
import net.lenni0451.mcstructs.text.events.hover.impl.TextHoverEvent;
import net.lenni0451.mcstructs.text.serializer.LegacyStringDeserializer;
import net.lenni0451.mcstructs.text.serializer.TextComponentCodec;
import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.logging.Level;
/**
* Component conversion utility, trying to divert most calls to the component library to this class instead for easy replacement.
*/
public final class ComponentUtil {
public static JsonObject emptyJsonComponent() {
return plainToJson("");
}
public static String emptyJsonComponentString() {
return "{\"text\":\"\"}";
}
public static JsonObject plainToJson(final String message) {
final JsonObject object = new JsonObject();
object.addProperty("text", message);
return object;
}
public static @Nullable JsonElement tagToJson(@Nullable final Tag tag) {
final ATextComponent component = TextComponentCodec.V1_20_3.deserializeNbtTree(NBTConverter.viaToMcStructs(tag));
return component != null ? SerializerVersion.V1_19_4.toJson(component) : null;
}
public static @Nullable Tag jsonToTag(@Nullable final JsonElement element) {
if (element == null) {
return null;
}
try {
final ATextComponent component = TextComponentSerializer.V1_19_4.deserialize(element);
return NBTConverter.mcStructsToVia(TextComponentCodec.V1_20_3.serializeNbt(component));
} catch (final Exception e) {
Via.getPlatform().getLogger().log(Level.SEVERE, "Error converting component: " + element, e);
return new StringTag("<error>");
}
}
public static @Nullable JsonElement convertJson(@Nullable final JsonElement element, final SerializerVersion from, final SerializerVersion to) {
final ATextComponent component = from.jsonSerializer.deserialize(element);
if (element == null) {
return null;
}
if (from.ordinal() >= SerializerVersion.V1_16.ordinal() && to.ordinal() < SerializerVersion.V1_16.ordinal()) {
// Convert hover event to legacy format
final Style style = component.getStyle();
final AHoverEvent hoverEvent = style.getHoverEvent();
if (hoverEvent != null && !(hoverEvent instanceof TextHoverEvent)) {
style.setHoverEvent(hoverEvent.toLegacy(to.jsonSerializer, to.snbtSerializer));
}
}
return to.toJson(component);
}
public static JsonElement legacyToJson(final String message) {
return SerializerVersion.V1_12.toJson(LegacyStringDeserializer.parse(message, true));
}
public static String legacyToJsonString(final String message) {
return legacyToJsonString(message, false);
}
public static String legacyToJsonString(final String message, final boolean itemData) {
final ATextComponent component = LegacyStringDeserializer.parse(message, true);
if (itemData) {
component.setParentStyle(new Style().setItalic(false));
}
return TextComponentSerializer.V1_12.serialize(component);
}
public static String jsonToLegacy(final String value) {
return TextComponentSerializer.V1_12.deserializeReader(value).asLegacyFormatString();
}
public static String jsonToLegacy(final JsonElement value) {
return TextComponentSerializer.V1_12.deserialize(value).asLegacyFormatString();
}
public enum SerializerVersion {
V1_8(TextComponentSerializer.V1_8, SNbtSerializer.V1_8),
V1_9(TextComponentSerializer.V1_9, SNbtSerializer.V1_8),
V1_12(TextComponentSerializer.V1_12, SNbtSerializer.V1_12),
V1_14(TextComponentSerializer.V1_14, SNbtSerializer.V1_14),
V1_15(TextComponentSerializer.V1_15, SNbtSerializer.V1_14),
V1_16(TextComponentSerializer.V1_16, SNbtSerializer.V1_14),
V1_17(TextComponentSerializer.V1_17, SNbtSerializer.V1_14),
V1_18(TextComponentSerializer.V1_18, SNbtSerializer.V1_14),
V1_19_4(TextComponentSerializer.V1_19_4, SNbtSerializer.V1_14),
V1_20_3(TextComponentCodec.V1_20_3, SNbtSerializer.V1_14);
private final TextComponentSerializer jsonSerializer;
private final SNbtSerializer<?> snbtSerializer;
private final TextComponentCodec codec;
SerializerVersion(final TextComponentSerializer jsonSerializer, final SNbtSerializer<?> snbtSerializer) {
this.jsonSerializer = jsonSerializer;
this.snbtSerializer = snbtSerializer;
this.codec = null;
}
SerializerVersion(final TextComponentCodec codec, final SNbtSerializer<?> snbtSerializer) {
this.codec = codec;
this.jsonSerializer = codec.asSerializer();
this.snbtSerializer = snbtSerializer;
}
public JsonElement toJson(final ATextComponent component) {
return jsonSerializer.serializeJson(component);
}
}
}

Datei anzeigen

@ -21,12 +21,9 @@ import com.google.gson.JsonElement;
import com.viaversion.viaversion.compatibility.YamlCompat;
import com.viaversion.viaversion.compatibility.unsafe.Yaml1Compat;
import com.viaversion.viaversion.compatibility.unsafe.Yaml2Compat;
import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@ -231,7 +228,7 @@ public abstract class Config {
public @Nullable JsonElement getSerializedComponent(String key) {
final Object o = this.config.get(key);
if (o != null && !((String) o).isEmpty()) {
return GsonComponentSerializer.gson().serializeToTree(LegacyComponentSerializer.legacySection().deserialize((String) o));
return ComponentUtil.legacyToJson((String) o);
} else {
return null;
}

Datei anzeigen

@ -0,0 +1,121 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2023 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.util;
import com.github.steveice10.opennbt.tag.builtin.ByteArrayTag;
import com.github.steveice10.opennbt.tag.builtin.ByteTag;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.DoubleTag;
import com.github.steveice10.opennbt.tag.builtin.FloatTag;
import com.github.steveice10.opennbt.tag.builtin.IntArrayTag;
import com.github.steveice10.opennbt.tag.builtin.IntTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.LongArrayTag;
import com.github.steveice10.opennbt.tag.builtin.LongTag;
import com.github.steveice10.opennbt.tag.builtin.NumberTag;
import com.github.steveice10.opennbt.tag.builtin.ShortTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import java.util.Map;
import net.lenni0451.mcstructs.nbt.INbtTag;
import org.checkerframework.checker.nullness.qual.Nullable;
final class NBTConverter {
static @Nullable Tag mcStructsToVia(@Nullable final INbtTag nbtTag) {
if (nbtTag == null) {
return null;
} else if (nbtTag.isByteTag()) {
return new ByteTag(nbtTag.asByteTag().getValue());
} else if (nbtTag.isShortTag()) {
return new ShortTag(nbtTag.asShortTag().getValue());
} else if (nbtTag.isIntTag()) {
return new IntTag(nbtTag.asIntTag().getValue());
} else if (nbtTag.isLongTag()) {
return new LongTag(nbtTag.asLongTag().getValue());
} else if (nbtTag.isFloatTag()) {
return new FloatTag(nbtTag.asFloatTag().getValue());
} else if (nbtTag.isDoubleTag()) {
return new DoubleTag(nbtTag.asDoubleTag().getValue());
} else if (nbtTag.isByteArrayTag()) {
return new ByteArrayTag(nbtTag.asByteArrayTag().getValue());
} else if (nbtTag.isStringTag()) {
return new StringTag(nbtTag.asStringTag().getValue());
} else if (nbtTag.isListTag()) {
final ListTag list = new ListTag();
for (final INbtTag t : nbtTag.asListTag().getValue()) {
list.add(mcStructsToVia(t));
}
return list;
} else if (nbtTag.isCompoundTag()) {
final Map<String, INbtTag> values = nbtTag.asCompoundTag().getValue();
final CompoundTag compound = new CompoundTag();
for (final Map.Entry<String, INbtTag> entry : values.entrySet()) {
compound.put(entry.getKey(), mcStructsToVia(entry.getValue()));
}
return compound;
} else if (nbtTag.isIntArrayTag()) {
return new IntArrayTag(nbtTag.asIntArrayTag().getValue());
} else if (nbtTag.isLongArrayTag()) {
return new LongArrayTag(nbtTag.asLongArrayTag().getValue());
} else {
throw new IllegalArgumentException("Unsupported tag type: " + nbtTag.getClass().getName());
}
}
static @Nullable INbtTag viaToMcStructs(@Nullable final Tag tag) {
if (tag == null) {
return null;
} else if (tag instanceof ByteTag) {
return new net.lenni0451.mcstructs.nbt.tags.ByteTag(((NumberTag) tag).asByte());
} else if (tag instanceof ShortTag) {
return new net.lenni0451.mcstructs.nbt.tags.ShortTag(((NumberTag) tag).asShort());
} else if (tag instanceof IntTag) {
return new net.lenni0451.mcstructs.nbt.tags.IntTag(((NumberTag) tag).asInt());
} else if (tag instanceof LongTag) {
return new net.lenni0451.mcstructs.nbt.tags.LongTag(((NumberTag) tag).asLong());
} else if (tag instanceof FloatTag) {
return new net.lenni0451.mcstructs.nbt.tags.FloatTag(((NumberTag) tag).asFloat());
} else if (tag instanceof DoubleTag) {
return new net.lenni0451.mcstructs.nbt.tags.DoubleTag(((NumberTag) tag).asDouble());
} else if (tag instanceof ByteArrayTag) {
return new net.lenni0451.mcstructs.nbt.tags.ByteArrayTag(((ByteArrayTag) tag).getValue());
} else if (tag instanceof StringTag) {
return new net.lenni0451.mcstructs.nbt.tags.StringTag(((StringTag) tag).getValue());
} else if (tag instanceof ListTag) {
final net.lenni0451.mcstructs.nbt.tags.ListTag<INbtTag> list = new net.lenni0451.mcstructs.nbt.tags.ListTag<>();
for (final Tag t : ((ListTag) tag).getValue()) {
list.add(viaToMcStructs(t));
}
return list;
} else if (tag instanceof CompoundTag) {
final Map<String, Tag> values = ((CompoundTag) tag).getValue();
final net.lenni0451.mcstructs.nbt.tags.CompoundTag compound = new net.lenni0451.mcstructs.nbt.tags.CompoundTag();
for (final Map.Entry<String, Tag> entry : values.entrySet()) {
compound.add(entry.getKey(), viaToMcStructs(entry.getValue()));
}
return compound;
} else if (tag instanceof IntArrayTag) {
return new net.lenni0451.mcstructs.nbt.tags.IntArrayTag(((IntArrayTag) tag).getValue());
} else if (tag instanceof LongArrayTag) {
return new net.lenni0451.mcstructs.nbt.tags.LongArrayTag(((LongArrayTag) tag).getValue());
} else {
throw new IllegalArgumentException("Unsupported tag type: " + tag.getClass().getName());
}
}
}

Datei anzeigen

@ -2,11 +2,11 @@ metadata.format.version = "1.1"
[versions]
adventure = "4.14.0"
gson = "2.10.1"
fastutil = "8.5.12"
flare = "2.0.1"
vianbt = "3.3.0"
mcstructs = "2.4.1"
# Common provided
netty = "4.0.20.Final"
@ -27,16 +27,12 @@ velocity = "3.1.1"
[libraries]
adventureApi = { group = "net.kyori", name = "adventure-api", version.ref = "adventure" }
adventureTextSerializerGson = { group = "net.kyori", name = "adventure-text-serializer-gson", version.ref = "adventure" }
adventureTextSerializerGsonLegacy = { group = "net.kyori", name = "adventure-text-serializer-gson-legacy-impl", version.ref = "adventure" }
adventureTextSerializerLegacy = { group = "net.kyori", name = "adventure-text-serializer-legacy", version.ref = "adventure" }
gson = { group = "com.google.code.gson", name = "gson", version.ref = "gson" }
fastutil = { group = "it.unimi.dsi", name = "fastutil", version.ref = "fastutil" }
flare = { group = "space.vectrix.flare", name = "flare", version.ref = "flare" }
flareFastutil = { group = "space.vectrix.flare", name = "flare-fastutil", version.ref = "flare" }
vianbt = { group = "com.viaversion", name = "nbt", version.ref = "vianbt" }
text = { group = "net.lenni0451.mcstructs", name = "text", version.ref = "mcstructs" }
netty = { group = "io.netty", name = "netty-all", version.ref = "netty" }
guava = { group = "com.google.guava", name = "guava", version.ref = "guava" }
@ -56,5 +52,4 @@ velocity = { group = "com.velocitypowered", name = "velocity-api", version.ref =
[bundles]
adventure = ["adventureApi", "adventureTextSerializerGson", "adventureTextSerializerGsonLegacy", "adventureTextSerializerLegacy"]
junit = ["jupiterApi", "jupiterEngine"]

Datei anzeigen

@ -27,7 +27,6 @@ rootProject.name = "viaversion-parent"
includeBuild("build-logic")
include("adventure")
include("compat", "compat:snakeyaml-compat-common", "compat:snakeyaml2-compat", "compat:snakeyaml1-compat", "compat:protocolsupport-compat")
setupViaSubproject("api")