Put enchantment level into translatables in enchantment rewriters (#807)

This commit is contained in:
EnZaXD 2024-07-05 16:10:00 +02:00 committed by GitHub
parent dd6bc4f1db
commit 16f1bdff02
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 39 additions and 7 deletions

View File

@ -17,12 +17,12 @@
*/
package com.viaversion.viabackwards.api.rewriters;
import com.viaversion.viabackwards.utils.ChatUtil;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.ListTag;
import com.viaversion.nbt.tag.NumberTag;
import com.viaversion.nbt.tag.StringTag;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.Key;
import java.util.ArrayList;
import java.util.HashMap;
@ -35,6 +35,8 @@ import java.util.Map;
*/
public class EnchantmentRewriter {
public static final String ENCHANTMENT_LEVEL_TRANSLATION = "enchantment.level.%s";
protected final Map<String, String> enchantmentMappings = new HashMap<>();
protected final BackwardsItemRewriter<?, ?, ?> itemRewriter;
private final boolean jsonFormat;
@ -103,9 +105,11 @@ public class EnchantmentRewriter {
NumberTag levelTag = enchantmentEntry.getNumberTag("lvl");
int level = levelTag != null ? levelTag.asInt() : 1;
String loreValue = remappedName + " " + getRomanNumber(level);
String loreValue;
if (jsonFormat) {
loreValue = ComponentUtil.legacyToJsonString(loreValue);
loreValue = ChatUtil.legacyToJsonString(remappedName, ENCHANTMENT_LEVEL_TRANSLATION.formatted(level), true);
} else {
loreValue = remappedName + " " + getRomanNumber(level);
}
loreToAdd.add(new StringTag(loreValue));
@ -157,7 +161,7 @@ public class EnchantmentRewriter {
case 8 -> "VIII";
case 9 -> "IX";
case 10 -> "X";
default -> Integer.toString(number);
default -> ENCHANTMENT_LEVEL_TRANSLATION.formatted(number); // Fallback to translation to match vanilla style
};
}
}

View File

@ -23,6 +23,7 @@ import com.viaversion.nbt.tag.ListTag;
import com.viaversion.nbt.tag.NumberTag;
import com.viaversion.nbt.tag.Tag;
import com.viaversion.viabackwards.api.data.BackwardsMappingData;
import com.viaversion.viabackwards.utils.ChatUtil;
import com.viaversion.viaversion.api.data.Mappings;
import com.viaversion.viaversion.api.minecraft.data.StructuredData;
import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer;
@ -37,6 +38,8 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static com.viaversion.viabackwards.api.rewriters.EnchantmentRewriter.ENCHANTMENT_LEVEL_TRANSLATION;
public class StructuredEnchantmentRewriter {
protected final BackwardsStructuredItemRewriter<?, ?, ?> itemRewriter;
@ -55,7 +58,7 @@ public class StructuredEnchantmentRewriter {
};
final DescriptionSupplier descriptionSupplier = (id, level) -> {
final String remappedName = mappingData.mappedEnchantmentName(id);
return ComponentUtil.jsonStringToTag(ComponentUtil.legacyToJsonString("§7" + remappedName + " " + EnchantmentRewriter.getRomanNumber(level), true));
return ComponentUtil.jsonStringToTag(ChatUtil.legacyToJsonString("§7" + remappedName, ENCHANTMENT_LEVEL_TRANSLATION.formatted(level), true));
};
rewriteEnchantmentsToClient(data, StructuredDataKey.ENCHANTMENTS, idRewriteFunction, descriptionSupplier, false);
rewriteEnchantmentsToClient(data, StructuredDataKey.STORED_ENCHANTMENTS, idRewriteFunction, descriptionSupplier, true);

View File

@ -53,6 +53,8 @@ import java.util.List;
import static com.viaversion.viaversion.protocols.v1_20_5to1_21.rewriter.BlockItemPacketRewriter1_21.downgradeItemData;
import static com.viaversion.viaversion.protocols.v1_20_5to1_21.rewriter.BlockItemPacketRewriter1_21.updateItemData;
import static com.viaversion.viabackwards.api.rewriters.EnchantmentRewriter.ENCHANTMENT_LEVEL_TRANSLATION;
public final class BlockItemPacketRewriter1_21 extends BackwardsStructuredItemRewriter<ClientboundPacket1_21, ServerboundPacket1_20_5, Protocol1_21To1_20_5> {
private final StructuredEnchantmentRewriter enchantmentRewriter = new StructuredEnchantmentRewriter(this);
@ -144,8 +146,8 @@ public final class BlockItemPacketRewriter1_21 extends BackwardsStructuredItemRe
}
final CompoundTag fullDescription = new CompoundTag();
fullDescription.putString("translate", "%s " + EnchantmentRewriter.getRomanNumber(level));
fullDescription.put("with", new ListTag<>(Arrays.asList(description)));
fullDescription.putString("translate", "%s %s");
fullDescription.put("with", new ListTag<>(Arrays.asList(description, new StringTag(ENCHANTMENT_LEVEL_TRANSLATION.formatted(level)))));
return fullDescription;
};
enchantmentRewriter.rewriteEnchantmentsToClient(data, StructuredDataKey.ENCHANTMENTS, idRewriteFunction, descriptionSupplier, false);

View File

@ -17,9 +17,15 @@
*/
package com.viaversion.viabackwards.utils;
import com.viaversion.viaversion.libs.mcstructs.text.ATextComponent;
import com.viaversion.viaversion.libs.mcstructs.text.Style;
import com.viaversion.viaversion.libs.mcstructs.text.components.TranslationComponent;
import com.viaversion.viaversion.libs.mcstructs.text.serializer.LegacyStringDeserializer;
import com.viaversion.viaversion.util.SerializerVersion;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.regex.Pattern;
public final class ChatUtil {
@ -30,6 +36,23 @@ public final class ChatUtil {
return removeUnusedColor(legacy, defaultColor, false);
}
public static String legacyToJsonString(String legacy, String translation, boolean itemData) {
return legacyToJsonString(legacy, text -> {
text.append(" ");
text.append(new TranslationComponent(translation));
}, itemData);
}
public static String legacyToJsonString(String legacy, Consumer<ATextComponent> consumer, boolean itemData) {
final ATextComponent component = LegacyStringDeserializer.parse(legacy, true);
consumer.accept(component);
if (itemData) {
component.setParentStyle((new Style()).setItalic(false));
}
return SerializerVersion.V1_12.toString(component);
}
private static class ChatFormattingState {
private final Set<Character> formatting;
private final char defaultColor;