Mirror von
https://github.com/ViaVersion/ViaBackwards.git
synchronisiert 2024-11-16 04:50:10 +01:00
Put enchantment level into translatables in enchantment rewriters (#807)
Dieser Commit ist enthalten in:
Ursprung
dd6bc4f1db
Commit
16f1bdff02
@ -17,12 +17,12 @@
|
|||||||
*/
|
*/
|
||||||
package com.viaversion.viabackwards.api.rewriters;
|
package com.viaversion.viabackwards.api.rewriters;
|
||||||
|
|
||||||
|
import com.viaversion.viabackwards.utils.ChatUtil;
|
||||||
import com.viaversion.viaversion.api.minecraft.item.Item;
|
import com.viaversion.viaversion.api.minecraft.item.Item;
|
||||||
import com.viaversion.nbt.tag.CompoundTag;
|
import com.viaversion.nbt.tag.CompoundTag;
|
||||||
import com.viaversion.nbt.tag.ListTag;
|
import com.viaversion.nbt.tag.ListTag;
|
||||||
import com.viaversion.nbt.tag.NumberTag;
|
import com.viaversion.nbt.tag.NumberTag;
|
||||||
import com.viaversion.nbt.tag.StringTag;
|
import com.viaversion.nbt.tag.StringTag;
|
||||||
import com.viaversion.viaversion.util.ComponentUtil;
|
|
||||||
import com.viaversion.viaversion.util.Key;
|
import com.viaversion.viaversion.util.Key;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -35,6 +35,8 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
public class EnchantmentRewriter {
|
public class EnchantmentRewriter {
|
||||||
|
|
||||||
|
public static final String ENCHANTMENT_LEVEL_TRANSLATION = "enchantment.level.%s";
|
||||||
|
|
||||||
protected final Map<String, String> enchantmentMappings = new HashMap<>();
|
protected final Map<String, String> enchantmentMappings = new HashMap<>();
|
||||||
protected final BackwardsItemRewriter<?, ?, ?> itemRewriter;
|
protected final BackwardsItemRewriter<?, ?, ?> itemRewriter;
|
||||||
private final boolean jsonFormat;
|
private final boolean jsonFormat;
|
||||||
@ -103,9 +105,11 @@ public class EnchantmentRewriter {
|
|||||||
|
|
||||||
NumberTag levelTag = enchantmentEntry.getNumberTag("lvl");
|
NumberTag levelTag = enchantmentEntry.getNumberTag("lvl");
|
||||||
int level = levelTag != null ? levelTag.asInt() : 1;
|
int level = levelTag != null ? levelTag.asInt() : 1;
|
||||||
String loreValue = remappedName + " " + getRomanNumber(level);
|
String loreValue;
|
||||||
if (jsonFormat) {
|
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));
|
loreToAdd.add(new StringTag(loreValue));
|
||||||
@ -157,7 +161,7 @@ public class EnchantmentRewriter {
|
|||||||
case 8 -> "VIII";
|
case 8 -> "VIII";
|
||||||
case 9 -> "IX";
|
case 9 -> "IX";
|
||||||
case 10 -> "X";
|
case 10 -> "X";
|
||||||
default -> Integer.toString(number);
|
default -> ENCHANTMENT_LEVEL_TRANSLATION.formatted(number); // Fallback to translation to match vanilla style
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ import com.viaversion.nbt.tag.ListTag;
|
|||||||
import com.viaversion.nbt.tag.NumberTag;
|
import com.viaversion.nbt.tag.NumberTag;
|
||||||
import com.viaversion.nbt.tag.Tag;
|
import com.viaversion.nbt.tag.Tag;
|
||||||
import com.viaversion.viabackwards.api.data.BackwardsMappingData;
|
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.data.Mappings;
|
||||||
import com.viaversion.viaversion.api.minecraft.data.StructuredData;
|
import com.viaversion.viaversion.api.minecraft.data.StructuredData;
|
||||||
import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer;
|
import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer;
|
||||||
@ -37,6 +38,8 @@ import java.util.ArrayList;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static com.viaversion.viabackwards.api.rewriters.EnchantmentRewriter.ENCHANTMENT_LEVEL_TRANSLATION;
|
||||||
|
|
||||||
public class StructuredEnchantmentRewriter {
|
public class StructuredEnchantmentRewriter {
|
||||||
|
|
||||||
protected final BackwardsStructuredItemRewriter<?, ?, ?> itemRewriter;
|
protected final BackwardsStructuredItemRewriter<?, ?, ?> itemRewriter;
|
||||||
@ -55,7 +58,7 @@ public class StructuredEnchantmentRewriter {
|
|||||||
};
|
};
|
||||||
final DescriptionSupplier descriptionSupplier = (id, level) -> {
|
final DescriptionSupplier descriptionSupplier = (id, level) -> {
|
||||||
final String remappedName = mappingData.mappedEnchantmentName(id);
|
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.ENCHANTMENTS, idRewriteFunction, descriptionSupplier, false);
|
||||||
rewriteEnchantmentsToClient(data, StructuredDataKey.STORED_ENCHANTMENTS, idRewriteFunction, descriptionSupplier, true);
|
rewriteEnchantmentsToClient(data, StructuredDataKey.STORED_ENCHANTMENTS, idRewriteFunction, descriptionSupplier, true);
|
||||||
|
@ -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.downgradeItemData;
|
||||||
import static com.viaversion.viaversion.protocols.v1_20_5to1_21.rewriter.BlockItemPacketRewriter1_21.updateItemData;
|
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> {
|
public final class BlockItemPacketRewriter1_21 extends BackwardsStructuredItemRewriter<ClientboundPacket1_21, ServerboundPacket1_20_5, Protocol1_21To1_20_5> {
|
||||||
|
|
||||||
private final StructuredEnchantmentRewriter enchantmentRewriter = new StructuredEnchantmentRewriter(this);
|
private final StructuredEnchantmentRewriter enchantmentRewriter = new StructuredEnchantmentRewriter(this);
|
||||||
@ -144,8 +146,8 @@ public final class BlockItemPacketRewriter1_21 extends BackwardsStructuredItemRe
|
|||||||
}
|
}
|
||||||
|
|
||||||
final CompoundTag fullDescription = new CompoundTag();
|
final CompoundTag fullDescription = new CompoundTag();
|
||||||
fullDescription.putString("translate", "%s " + EnchantmentRewriter.getRomanNumber(level));
|
fullDescription.putString("translate", "%s %s");
|
||||||
fullDescription.put("with", new ListTag<>(Arrays.asList(description)));
|
fullDescription.put("with", new ListTag<>(Arrays.asList(description, new StringTag(ENCHANTMENT_LEVEL_TRANSLATION.formatted(level)))));
|
||||||
return fullDescription;
|
return fullDescription;
|
||||||
};
|
};
|
||||||
enchantmentRewriter.rewriteEnchantmentsToClient(data, StructuredDataKey.ENCHANTMENTS, idRewriteFunction, descriptionSupplier, false);
|
enchantmentRewriter.rewriteEnchantmentsToClient(data, StructuredDataKey.ENCHANTMENTS, idRewriteFunction, descriptionSupplier, false);
|
||||||
|
@ -17,9 +17,15 @@
|
|||||||
*/
|
*/
|
||||||
package com.viaversion.viabackwards.utils;
|
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.HashSet;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.function.Consumer;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public final class ChatUtil {
|
public final class ChatUtil {
|
||||||
@ -30,6 +36,23 @@ public final class ChatUtil {
|
|||||||
return removeUnusedColor(legacy, defaultColor, false);
|
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 static class ChatFormattingState {
|
||||||
private final Set<Character> formatting;
|
private final Set<Character> formatting;
|
||||||
private final char defaultColor;
|
private final char defaultColor;
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren