From 90420ae79094680d1c0cc153343af702daf604f4 Mon Sep 17 00:00:00 2001 From: Drex Date: Fri, 1 Dec 2023 12:38:26 +0100 Subject: [PATCH] Update to 1.20.3 (#55) * Update to 23w41a * Fix not all components being translated * Improve codec mixin --- .../translations/impl/LocalizableText.java | 4 +-- .../impl/nbt/SignNbtLocalizer.java | 2 +- .../impl/nbt/StackNbtLocalizer.java | 4 +-- .../mixin/packet/TextSerializerMixin.java | 20 ----------- .../packet/TranslatableTextContentMixin.java | 35 +++++++++++++++++++ .../server_translations_api.mixin.json | 2 +- build.gradle | 10 +++--- 7 files changed, 46 insertions(+), 31 deletions(-) delete mode 100644 api/src/main/java/xyz/nucleoid/server/translations/mixin/packet/TextSerializerMixin.java create mode 100644 api/src/main/java/xyz/nucleoid/server/translations/mixin/packet/TranslatableTextContentMixin.java diff --git a/api/src/main/java/xyz/nucleoid/server/translations/impl/LocalizableText.java b/api/src/main/java/xyz/nucleoid/server/translations/impl/LocalizableText.java index c9d6482..ebf88cc 100644 --- a/api/src/main/java/xyz/nucleoid/server/translations/impl/LocalizableText.java +++ b/api/src/main/java/xyz/nucleoid/server/translations/impl/LocalizableText.java @@ -59,8 +59,8 @@ static Text asLocalizedFor(final Text text, final ServerLanguage language, final style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_ITEM, new HoverEvent.ItemStackContent(stack))); } else if (translateDeeply && style.getHoverEvent().getAction() == HoverEvent.Action.SHOW_ENTITY) { var value = style.getHoverEvent().getValue(HoverEvent.Action.SHOW_ENTITY); - if (value.name != null) { - style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_ENTITY, new HoverEvent.EntityContent(value.entityType, value.uuid, asLocalizedFor(value.name, language, true)))); + if (value.name.isPresent()) { + style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_ENTITY, new HoverEvent.EntityContent(value.entityType, value.uuid, asLocalizedFor(value.name.get(), language, true)))); } } } diff --git a/api/src/main/java/xyz/nucleoid/server/translations/impl/nbt/SignNbtLocalizer.java b/api/src/main/java/xyz/nucleoid/server/translations/impl/nbt/SignNbtLocalizer.java index a718e54..79ac8f9 100644 --- a/api/src/main/java/xyz/nucleoid/server/translations/impl/nbt/SignNbtLocalizer.java +++ b/api/src/main/java/xyz/nucleoid/server/translations/impl/nbt/SignNbtLocalizer.java @@ -35,7 +35,7 @@ private static void updateSide(NbtCompound nbt, LocalizationTarget target) { private static void updateLines(NbtList messages, LocalizationTarget target) { for (int i = 0; i < messages.size(); i++) { - messages.set(i, NbtString.of(Text.Serializer.toJson(Localization.text(Text.Serializer.fromLenientJson(messages.getString(i)), target)))); + messages.set(i, NbtString.of(Text.Serialization.toJsonString(Localization.text(Text.Serialization.fromLenientJson(messages.getString(i)), target)))); } } } diff --git a/api/src/main/java/xyz/nucleoid/server/translations/impl/nbt/StackNbtLocalizer.java b/api/src/main/java/xyz/nucleoid/server/translations/impl/nbt/StackNbtLocalizer.java index 5e4d612..b9c994c 100644 --- a/api/src/main/java/xyz/nucleoid/server/translations/impl/nbt/StackNbtLocalizer.java +++ b/api/src/main/java/xyz/nucleoid/server/translations/impl/nbt/StackNbtLocalizer.java @@ -91,7 +91,7 @@ private static void translateLore(NbtCompound display, ServerLanguage target) { private static String localizeTextJson(String json, ServerLanguage target) { Text text; try { - text = Text.Serializer.fromLenientJson(json); + text = Text.Serialization.fromLenientJson(json); } catch (Exception e) { text = null; } @@ -102,7 +102,7 @@ private static String localizeTextJson(String json, ServerLanguage target) { Text localized = LocalizableText.asLocalizedFor(text, target, true); if (!localized.equals(text)) { - return Text.Serializer.toJson(localized); + return Text.Serialization.toJsonString(localized); } else { return json; } diff --git a/api/src/main/java/xyz/nucleoid/server/translations/mixin/packet/TextSerializerMixin.java b/api/src/main/java/xyz/nucleoid/server/translations/mixin/packet/TextSerializerMixin.java deleted file mode 100644 index db84992..0000000 --- a/api/src/main/java/xyz/nucleoid/server/translations/mixin/packet/TextSerializerMixin.java +++ /dev/null @@ -1,20 +0,0 @@ -package xyz.nucleoid.server.translations.mixin.packet; - -import xyz.nucleoid.server.translations.api.LocalizationTarget; -import xyz.nucleoid.server.translations.impl.LocalizableText; -import net.minecraft.text.Text; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.ModifyVariable; - -@Mixin(Text.Serializer.class) -public abstract class TextSerializerMixin { - @ModifyVariable(method = "serialize(Lnet/minecraft/text/Text;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;", at = @At("HEAD"), ordinal = 0) - private Text stapi$serializeTranslatableText(Text text) { - LocalizationTarget target = LocalizationTarget.forPacket(); - if (target != null) { - return LocalizableText.asLocalizedFor(text, target.getLanguage(), false); - } - return text; - } -} diff --git a/api/src/main/java/xyz/nucleoid/server/translations/mixin/packet/TranslatableTextContentMixin.java b/api/src/main/java/xyz/nucleoid/server/translations/mixin/packet/TranslatableTextContentMixin.java new file mode 100644 index 0000000..c94d4a6 --- /dev/null +++ b/api/src/main/java/xyz/nucleoid/server/translations/mixin/packet/TranslatableTextContentMixin.java @@ -0,0 +1,35 @@ +package xyz.nucleoid.server.translations.mixin.packet; + +import com.mojang.datafixers.kinds.App; +import com.mojang.serialization.MapCodec; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.minecraft.text.TranslatableTextContent; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; +import xyz.nucleoid.server.translations.api.LocalizationTarget; + +import java.util.function.Function; + +@Mixin(TranslatableTextContent.class) +public abstract class TranslatableTextContentMixin { + + @Redirect( + method = "", + at = @At(value = "INVOKE", + target = "Lcom/mojang/serialization/codecs/RecordCodecBuilder;mapCodec(Ljava/util/function/Function;)Lcom/mojang/serialization/MapCodec;" + ) + ) + private static MapCodec stapi$addTranslationFallback(Function, ? extends App, TranslatableTextContent>> builder) { + return RecordCodecBuilder.mapCodec(builder).xmap(Function.identity(), (content) -> { + if (content.getFallback() == null) { + var target = LocalizationTarget.forPacket(); + if (target != null) { + return new TranslatableTextContent(content.getKey(), target.getLanguage().serverTranslations().get(content.getKey()), content.getArgs()); + } + } + return content; + }); + } + +} diff --git a/api/src/main/resources/server_translations_api.mixin.json b/api/src/main/resources/server_translations_api.mixin.json index 5ec4ee8..c408de3 100644 --- a/api/src/main/resources/server_translations_api.mixin.json +++ b/api/src/main/resources/server_translations_api.mixin.json @@ -9,7 +9,7 @@ "packet.BlockEntityDataMixin", "packet.BlockEntityUpdateS2CPacketMixin", "packet.PacketByteBufMixin", - "packet.TextSerializerMixin", + "packet.TranslatableTextContentMixin", "text.MutableTextMixin" ], "client": [ diff --git a/build.gradle b/build.gradle index 1938c42..dd64158 100644 --- a/build.gradle +++ b/build.gradle @@ -8,9 +8,9 @@ plugins { } class Globals { - static def baseVersion = "2.1.0" - static def mcVersion = "1.20.2-rc2" - static def yarnVersion = "+build.1" + static def baseVersion = "2.1.1" + static def mcVersion = "1.20.3-pre4" + static def yarnVersion = "+build.2" } archivesBaseName = "Server-Translations" @@ -49,11 +49,11 @@ allprojects { dependencies { minecraft "com.mojang:minecraft:$Globals.mcVersion" mappings "net.fabricmc:yarn:${Globals.mcVersion}${Globals.yarnVersion}:v2" - modImplementation "net.fabricmc:fabric-loader:0.14.22" + modImplementation "net.fabricmc:fabric-loader:0.14.23" modImplementation include("xyz.nucleoid:packet-tweaker:0.5.0+1.20.2-rc1") - modImplementation "net.fabricmc.fabric-api:fabric-api:0.88.5+1.20.2" + modImplementation "net.fabricmc.fabric-api:fabric-api:0.90.1+1.20.3" } processResources {