Handle item changes in show item text components

This commit is contained in:
Nassim Jahnke 2024-05-29 19:50:40 +02:00
parent 8fb814ef11
commit a9b9c48b26
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
26 changed files with 157 additions and 211 deletions

View File

@ -65,7 +65,8 @@ public abstract class BackwardsProtocol<CU extends ClientboundPacketType, CM ext
return null; return null;
} }
public @Nullable TranslatableRewriter<CU> getTranslatableRewriter() { @Override
public @Nullable TranslatableRewriter<CU> getComponentRewriter() {
return null; return null;
} }
} }

View File

@ -17,16 +17,6 @@
*/ */
package com.viaversion.viabackwards.api.rewriters; package com.viaversion.viabackwards.api.rewriters;
import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viabackwards.api.data.MappedItem;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.libs.gson.JsonElement;
import com.viaversion.nbt.tag.ByteTag; import com.viaversion.nbt.tag.ByteTag;
import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.IntTag; import com.viaversion.nbt.tag.IntTag;
@ -34,6 +24,13 @@ 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.nbt.tag.Tag; import com.viaversion.nbt.tag.Tag;
import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viabackwards.api.data.MappedItem;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
import com.viaversion.viaversion.api.type.Type;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
public class BackwardsItemRewriter<C extends ClientboundPacketType, S extends ServerboundPacketType, public class BackwardsItemRewriter<C extends ClientboundPacketType, S extends ServerboundPacketType,
@ -54,11 +51,11 @@ public class BackwardsItemRewriter<C extends ClientboundPacketType, S extends Se
} }
CompoundTag display = item.tag() != null ? item.tag().getCompoundTag("display") : null; CompoundTag display = item.tag() != null ? item.tag().getCompoundTag("display") : null;
if (protocol.getTranslatableRewriter() != null && display != null) { if (protocol.getComponentRewriter() != null && display != null) {
// Handle name and lore components // Handle name and lore components
StringTag name = display.getStringTag("Name"); StringTag name = display.getStringTag("Name");
if (name != null) { if (name != null) {
String newValue = protocol.getTranslatableRewriter().processText(connection, name.getValue()).toString(); String newValue = protocol.getComponentRewriter().processText(connection, name.getValue()).toString();
if (!newValue.equals(name.getValue())) { if (!newValue.equals(name.getValue())) {
saveStringTag(display, name, "Name"); saveStringTag(display, name, "Name");
} }
@ -70,7 +67,7 @@ public class BackwardsItemRewriter<C extends ClientboundPacketType, S extends Se
if (lore != null) { if (lore != null) {
boolean changed = false; boolean changed = false;
for (StringTag loreEntry : lore) { for (StringTag loreEntry : lore) {
String newValue = protocol.getTranslatableRewriter().processText(connection, loreEntry.getValue()).toString(); String newValue = protocol.getComponentRewriter().processText(connection, loreEntry.getValue()).toString();
if (!changed && !newValue.equals(loreEntry.getValue())) { if (!changed && !newValue.equals(loreEntry.getValue())) {
// Backup original lore before doing any modifications // Backup original lore before doing any modifications
changed = true; changed = true;
@ -125,92 +122,4 @@ public class BackwardsItemRewriter<C extends ClientboundPacketType, S extends Se
} }
return item; return item;
} }
@Override
public void registerAdvancements(C packetType) {
protocol.registerClientbound(packetType, new PacketHandlers() {
@Override
public void register() {
handler(wrapper -> {
wrapper.passthrough(Types.BOOLEAN); // Reset/clear
final int size = wrapper.passthrough(Types.VAR_INT); // Mapping size
for (int i = 0; i < size; i++) {
wrapper.passthrough(Types.STRING); // Identifier
wrapper.passthrough(Types.OPTIONAL_STRING); // Parent
// Display data
if (wrapper.passthrough(Types.BOOLEAN)) {
final JsonElement title = wrapper.passthrough(Types.COMPONENT);
final JsonElement description = wrapper.passthrough(Types.COMPONENT);
final TranslatableRewriter<C> translatableRewriter = protocol.getTranslatableRewriter();
if (translatableRewriter != null) {
translatableRewriter.processText(wrapper.user(), title);
translatableRewriter.processText(wrapper.user(), description);
}
final Item icon = handleItemToClient(wrapper.user(), wrapper.read(itemType()));
wrapper.write(mappedItemType(), icon);
wrapper.passthrough(Types.VAR_INT); // Frame type
int flags = wrapper.passthrough(Types.INT); // Flags
if ((flags & 1) != 0) {
wrapper.passthrough(Types.STRING); // Background texture
}
wrapper.passthrough(Types.FLOAT); // X
wrapper.passthrough(Types.FLOAT); // Y
}
wrapper.passthrough(Types.STRING_ARRAY); // Criteria
final int arrayLength = wrapper.passthrough(Types.VAR_INT);
for (int array = 0; array < arrayLength; array++) {
wrapper.passthrough(Types.STRING_ARRAY); // String array
}
}
});
}
});
}
@Override
public void registerAdvancements1_20_3(final C packetType) {
// Insert translatable rewriter
protocol.registerClientbound(packetType, wrapper -> {
wrapper.passthrough(Types.BOOLEAN); // Reset/clear
final int size = wrapper.passthrough(Types.VAR_INT); // Mapping size
for (int i = 0; i < size; i++) {
wrapper.passthrough(Types.STRING); // Identifier
wrapper.passthrough(Types.OPTIONAL_STRING); // Parent
// Display data
if (wrapper.passthrough(Types.BOOLEAN)) {
final Tag title = wrapper.passthrough(Types.TAG);
final Tag description = wrapper.passthrough(Types.TAG);
final TranslatableRewriter<C> translatableRewriter = protocol.getTranslatableRewriter();
if (translatableRewriter != null) {
translatableRewriter.processTag(wrapper.user(), title);
translatableRewriter.processTag(wrapper.user(), description);
}
final Item icon = handleItemToClient(wrapper.user(), wrapper.read(itemType()));
wrapper.write(mappedItemType(), icon);
wrapper.passthrough(Types.VAR_INT); // Frame type
final int flags = wrapper.passthrough(Types.INT);
if ((flags & 1) != 0) {
wrapper.passthrough(Types.STRING); // Background texture
}
wrapper.passthrough(Types.FLOAT); // X
wrapper.passthrough(Types.FLOAT); // Y
}
final int requirements = wrapper.passthrough(Types.VAR_INT);
for (int array = 0; array < requirements; array++) {
wrapper.passthrough(Types.STRING_ARRAY);
}
wrapper.passthrough(Types.BOOLEAN); // Send telemetry
}
});
}
} }

View File

@ -75,29 +75,6 @@ public abstract class BackwardsItemRewriterBase<C extends ClientboundPacketType,
} }
} }
protected void saveGenericTagList(CompoundTag tag, List<Tag> original, String name) {
// List tags cannot contain tags of different types, so we have to store them a bit more awkwardly as an indexed compound tag
String backupName = nbtTagName(name);
if (!tag.contains(backupName)) {
CompoundTag output = new CompoundTag();
for (int i = 0; i < original.size(); i++) {
output.put(Integer.toString(i), original.get(i));
}
tag.put(backupName, output);
}
}
protected List<Tag> removeGenericTagList(CompoundTag tag, String name) {
String backupName = nbtTagName(name);
CompoundTag data = tag.getCompoundTag(backupName);
if (data == null) {
return null;
}
tag.remove(backupName);
return new ArrayList<>(data.values());
}
protected void restoreDisplayTag(Item item) { protected void restoreDisplayTag(Item item) {
if (item.tag() == null) return; if (item.tag() == null) return;
@ -129,17 +106,6 @@ public abstract class BackwardsItemRewriterBase<C extends ClientboundPacketType,
} }
} }
public <T extends Tag> @Nullable ListTag<T> removeListTag(CompoundTag tag, String tagName, Class<T> tagType) {
String backupName = nbtTagName(tagName);
ListTag<T> data = tag.getListTag(backupName, tagType);
if (data == null) {
return null;
}
tag.remove(backupName);
return data;
}
@Override @Override
public String nbtTagName() { public String nbtTagName() {
return "VB|" + protocol.getClass().getSimpleName(); return "VB|" + protocol.getClass().getSimpleName();

View File

@ -19,6 +19,7 @@ package com.viaversion.viabackwards.api.rewriters;
import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.IntTag; import com.viaversion.nbt.tag.IntTag;
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.BackwardsProtocol; import com.viaversion.viabackwards.api.BackwardsProtocol;
@ -33,12 +34,13 @@ import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.rewriter.StructuredItemRewriter;
import java.util.ArrayList;
import java.util.List;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import static com.viaversion.viaversion.rewriter.StructuredItemRewriter.updateItemComponents;
public class BackwardsStructuredItemRewriter<C extends ClientboundPacketType, S extends ServerboundPacketType, public class BackwardsStructuredItemRewriter<C extends ClientboundPacketType, S extends ServerboundPacketType,
T extends BackwardsProtocol<C, ?, ?, S>> extends BackwardsItemRewriter<C, S, T> { T extends BackwardsProtocol<C, ?, ?, S>> extends StructuredItemRewriter<C, S, T> {
public BackwardsStructuredItemRewriter(final T protocol, final Type<Item> itemType, final Type<Item[]> itemArrayType) { public BackwardsStructuredItemRewriter(final T protocol, final Type<Item> itemType, final Type<Item[]> itemArrayType) {
super(protocol, itemType, itemArrayType); super(protocol, itemType, itemArrayType);
@ -62,12 +64,12 @@ public class BackwardsStructuredItemRewriter<C extends ClientboundPacketType, S
dataContainer.updateIds(protocol, mappings::getNewId); dataContainer.updateIds(protocol, mappings::getNewId);
} }
if (protocol.getTranslatableRewriter() != null) { if (protocol.getComponentRewriter() != null) {
// Handle name and lore components // Handle name and lore components
final StructuredData<Tag> customNameData = dataContainer.getNonEmpty(StructuredDataKey.CUSTOM_NAME); final StructuredData<Tag> customNameData = dataContainer.getNonEmpty(StructuredDataKey.CUSTOM_NAME);
if (customNameData != null) { if (customNameData != null) {
final Tag originalName = customNameData.value().copy(); final Tag originalName = customNameData.value().copy();
protocol.getTranslatableRewriter().processTag(connection, customNameData.value()); protocol.getComponentRewriter().processTag(connection, customNameData.value());
if (!customNameData.value().equals(originalName)) { if (!customNameData.value().equals(originalName)) {
saveTag(createCustomTag(item), originalName, "Name"); saveTag(createCustomTag(item), originalName, "Name");
} }
@ -76,7 +78,7 @@ public class BackwardsStructuredItemRewriter<C extends ClientboundPacketType, S
final StructuredData<Tag[]> loreData = dataContainer.getNonEmpty(StructuredDataKey.LORE); final StructuredData<Tag[]> loreData = dataContainer.getNonEmpty(StructuredDataKey.LORE);
if (loreData != null) { if (loreData != null) {
for (final Tag tag : loreData.value()) { for (final Tag tag : loreData.value()) {
protocol.getTranslatableRewriter().processTag(connection, tag); protocol.getComponentRewriter().processTag(connection, tag);
} }
} }
} }
@ -141,7 +143,7 @@ public class BackwardsStructuredItemRewriter<C extends ClientboundPacketType, S
} }
} }
restoreDisplayTag(item); restoreTextComponents(item);
updateItemComponents(connection, dataContainer, this::handleItemToServer, mappingData != null ? mappingData::getOldItemId : null); updateItemComponents(connection, dataContainer, this::handleItemToServer, mappingData != null ? mappingData::getOldItemId : null);
return item; return item;
} }
@ -151,45 +153,50 @@ public class BackwardsStructuredItemRewriter<C extends ClientboundPacketType, S
return customData != null ? customData.value() : null; return customData != null ? customData.value() : null;
} }
protected CompoundTag createCustomTag(final Item item) { protected void saveListTag(CompoundTag tag, ListTag<?> original, String name) {
final StructuredDataContainer data = item.dataContainer(); // Multiple places might try to backup data
final StructuredData<CompoundTag> customData = data.getNonEmpty(StructuredDataKey.CUSTOM_DATA); String backupName = nbtTagName(name);
if (customData != null) { if (!tag.contains(backupName)) {
return customData.value(); tag.put(backupName, original.copy());
}
}
public <T extends Tag> @Nullable ListTag<T> removeListTag(CompoundTag tag, String tagName, Class<T> tagType) {
String backupName = nbtTagName(tagName);
ListTag<T> data = tag.getListTag(backupName, tagType);
if (data == null) {
return null;
} }
final CompoundTag tag = new CompoundTag(); tag.remove(backupName);
data.set(StructuredDataKey.CUSTOM_DATA, tag); return data;
return tag; }
protected void saveGenericTagList(CompoundTag tag, List<Tag> original, String name) {
// List tags cannot contain tags of different types, so we have to store them a bit more awkwardly as an indexed compound tag
String backupName = nbtTagName(name);
if (!tag.contains(backupName)) {
CompoundTag output = new CompoundTag();
for (int i = 0; i < original.size(); i++) {
output.put(Integer.toString(i), original.get(i));
}
tag.put(backupName, output);
}
}
protected List<Tag> removeGenericTagList(CompoundTag tag, String name) {
String backupName = nbtTagName(name);
CompoundTag data = tag.getCompoundTag(backupName);
if (data == null) {
return null;
}
tag.remove(backupName);
return new ArrayList<>(data.values());
} }
@Override @Override
protected void restoreDisplayTag(final Item item) { public String nbtTagName() {
final StructuredDataContainer data = item.dataContainer(); return "VB|" + protocol.getClass().getSimpleName();
final StructuredData<CompoundTag> customData = data.getNonEmpty(StructuredDataKey.CUSTOM_DATA);
if (customData == null) {
return;
}
// Remove custom name
if (customData.value().remove(nbtTagName("customName")) != null) {
data.remove(StructuredDataKey.CUSTOM_NAME);
} else {
final Tag name = removeBackupTag(customData.value(), "Name");
if (name != null) {
data.set(StructuredDataKey.CUSTOM_NAME, name);
}
}
}
protected void saveTag(CompoundTag customData, Tag tag, String name) {
String backupName = nbtTagName(name);
if (!customData.contains(backupName)) {
customData.put(backupName, tag);
}
}
protected @Nullable Tag removeBackupTag(CompoundTag customData, String tagName) {
return customData.remove(nbtTagName(tagName));
} }
} }

View File

@ -187,7 +187,7 @@ public abstract class EntityRewriterBase<C extends ClientboundPacketType, T exte
rewriteParticle(event.user(), meta.value()); rewriteParticle(event.user(), meta.value());
} else if (type == optionalComponentType || type == componentType) { } else if (type == optionalComponentType || type == componentType) {
JsonElement text = meta.value(); JsonElement text = meta.value();
protocol.getTranslatableRewriter().processText(event.user(), text); protocol.getComponentRewriter().processText(event.user(), text);
} }
}); });
} }
@ -221,7 +221,7 @@ public abstract class EntityRewriterBase<C extends ClientboundPacketType, T exte
rewriteParticle(event.user(), particle); rewriteParticle(event.user(), particle);
} }
} else if (type == optionalComponentType || type == componentType) { } else if (type == optionalComponentType || type == componentType) {
protocol.getTranslatableRewriter().processTag(event.user(), meta.value()); protocol.getComponentRewriter().processTag(event.user(), meta.value());
} }
}); });
} }

View File

@ -17,6 +17,11 @@
*/ */
package com.viaversion.viabackwards.api.rewriters; package com.viaversion.viabackwards.api.rewriters;
import com.viaversion.nbt.tag.ByteTag;
import com.viaversion.nbt.tag.CompoundTag;
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.api.data.BackwardsMappingData;
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;
@ -27,11 +32,6 @@ import com.viaversion.viaversion.api.minecraft.item.data.Enchantments;
import com.viaversion.viaversion.libs.fastutil.ints.Int2IntMap; import com.viaversion.viaversion.libs.fastutil.ints.Int2IntMap;
import com.viaversion.viaversion.libs.fastutil.ints.IntIntPair; import com.viaversion.viaversion.libs.fastutil.ints.IntIntPair;
import com.viaversion.viaversion.libs.fastutil.objects.ObjectIterator; import com.viaversion.viaversion.libs.fastutil.objects.ObjectIterator;
import com.viaversion.nbt.tag.ByteTag;
import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.ListTag;
import com.viaversion.nbt.tag.NumberTag;
import com.viaversion.nbt.tag.Tag;
import com.viaversion.viaversion.rewriter.IdRewriteFunction; import com.viaversion.viaversion.rewriter.IdRewriteFunction;
import com.viaversion.viaversion.util.ComponentUtil; import com.viaversion.viaversion.util.ComponentUtil;
import java.util.ArrayList; import java.util.ArrayList;
@ -40,10 +40,10 @@ import java.util.List;
public class StructuredEnchantmentRewriter { public class StructuredEnchantmentRewriter {
protected final BackwardsItemRewriter<?, ?, ?> itemRewriter; protected final BackwardsStructuredItemRewriter<?, ?, ?> itemRewriter;
private boolean rewriteIds = true; private boolean rewriteIds = true;
public StructuredEnchantmentRewriter(final BackwardsItemRewriter<?, ?, ?> itemRewriter) { public StructuredEnchantmentRewriter(final BackwardsStructuredItemRewriter<?, ?, ?> itemRewriter) {
this.itemRewriter = itemRewriter; this.itemRewriter = itemRewriter;
} }

View File

@ -191,7 +191,7 @@ public class Protocol1_14To1_13_2 extends BackwardsProtocol<ClientboundPackets1_
} }
@Override @Override
public TranslatableRewriter<ClientboundPackets1_14> getTranslatableRewriter() { public TranslatableRewriter<ClientboundPackets1_14> getComponentRewriter() {
return translatableRewriter; return translatableRewriter;
} }
} }

View File

@ -127,7 +127,7 @@ public class Protocol1_15To1_14_4 extends BackwardsProtocol<ClientboundPackets1_
} }
@Override @Override
public TranslatableRewriter<ClientboundPackets1_15> getTranslatableRewriter() { public TranslatableRewriter<ClientboundPackets1_15> getComponentRewriter() {
return translatableRewriter; return translatableRewriter;
} }
} }

View File

@ -127,7 +127,7 @@ public class Protocol1_16_2To1_16_1 extends BackwardsProtocol<ClientboundPackets
} }
@Override @Override
public TranslatableRewriter<ClientboundPackets1_16_2> getTranslatableRewriter() { public TranslatableRewriter<ClientboundPackets1_16_2> getComponentRewriter() {
return translatableRewriter; return translatableRewriter;
} }

View File

@ -177,7 +177,7 @@ public class Protocol1_16To1_15_2 extends BackwardsProtocol<ClientboundPackets1_
} }
@Override @Override
public TranslatableRewriter1_16 getTranslatableRewriter() { public TranslatableRewriter1_16 getComponentRewriter() {
return translatableRewriter; return translatableRewriter;
} }

View File

@ -283,7 +283,7 @@ public class BlockItemPacketRewriter1_16 extends BackwardsItemRewriter<Clientbou
ListTag<StringTag> pagesTag = tag.getListTag("pages", StringTag.class); ListTag<StringTag> pagesTag = tag.getListTag("pages", StringTag.class);
if (pagesTag != null) { if (pagesTag != null) {
for (StringTag page : pagesTag) { for (StringTag page : pagesTag) {
JsonElement jsonElement = protocol.getTranslatableRewriter().processText(connection, page.getValue()); JsonElement jsonElement = protocol.getComponentRewriter().processText(connection, page.getValue());
page.setValue(jsonElement.toString()); page.setValue(jsonElement.toString());
} }
} }

View File

@ -215,14 +215,14 @@ public class EntityPacketRewriter1_16 extends EntityRewriter<ClientboundPackets1
wrapper.passthrough(Types.VAR_INT); wrapper.passthrough(Types.VAR_INT);
wrapper.passthrough(Types.VAR_INT); wrapper.passthrough(Types.VAR_INT);
// Display Name // Display Name
protocol.getTranslatableRewriter().processText(wrapper.user(), wrapper.passthrough(Types.OPTIONAL_COMPONENT)); protocol.getComponentRewriter().processText(wrapper.user(), wrapper.passthrough(Types.OPTIONAL_COMPONENT));
} else if (action == 1) { // Update Game Mode } else if (action == 1) { // Update Game Mode
wrapper.passthrough(Types.VAR_INT); wrapper.passthrough(Types.VAR_INT);
} else if (action == 2) { // Update Ping } else if (action == 2) { // Update Ping
wrapper.passthrough(Types.VAR_INT); wrapper.passthrough(Types.VAR_INT);
} else if (action == 3) { // Update Display Name } else if (action == 3) { // Update Display Name
// Display name // Display name
protocol.getTranslatableRewriter().processText(wrapper.user(), wrapper.passthrough(Types.OPTIONAL_COMPONENT)); protocol.getComponentRewriter().processText(wrapper.user(), wrapper.passthrough(Types.OPTIONAL_COMPONENT));
} // 4 = Remove Player } // 4 = Remove Player
} }
}); });
@ -243,7 +243,7 @@ public class EntityPacketRewriter1_16 extends EntityRewriter<ClientboundPackets1
} else if (type == Types1_14.ENTITY_DATA_TYPES.optionalComponentType) { } else if (type == Types1_14.ENTITY_DATA_TYPES.optionalComponentType) {
JsonElement text = meta.value(); JsonElement text = meta.value();
if (text != null) { if (text != null) {
protocol.getTranslatableRewriter().processText(event.user(), text); protocol.getComponentRewriter().processText(event.user(), text);
} }
} }
}); });

View File

@ -231,7 +231,7 @@ public final class Protocol1_17To1_16_4 extends BackwardsProtocol<ClientboundPac
} }
@Override @Override
public TranslatableRewriter<ClientboundPackets1_17> getTranslatableRewriter() { public TranslatableRewriter<ClientboundPackets1_17> getComponentRewriter() {
return translatableRewriter; return translatableRewriter;
} }

View File

@ -152,7 +152,7 @@ public final class Protocol1_18To1_17_1 extends BackwardsProtocol<ClientboundPac
} }
@Override @Override
public TranslatableRewriter<ClientboundPackets1_18> getTranslatableRewriter() { public TranslatableRewriter<ClientboundPackets1_18> getComponentRewriter() {
return translatableRewriter; return translatableRewriter;
} }

View File

@ -395,7 +395,7 @@ public final class Protocol1_19_1To1_19 extends BackwardsProtocol<ClientboundPac
} }
@Override @Override
public TranslatableRewriter<ClientboundPackets1_19_1> getTranslatableRewriter() { public TranslatableRewriter<ClientboundPackets1_19_1> getComponentRewriter() {
return translatableRewriter; return translatableRewriter;
} }

View File

@ -403,7 +403,7 @@ public final class Protocol1_19_3To1_19_1 extends BackwardsProtocol<ClientboundP
} }
@Override @Override
public TranslatableRewriter<ClientboundPackets1_19_3> getTranslatableRewriter() { public TranslatableRewriter<ClientboundPackets1_19_3> getComponentRewriter() {
return translatableRewriter; return translatableRewriter;
} }

View File

@ -136,7 +136,7 @@ public final class Protocol1_19_4To1_19_3 extends BackwardsProtocol<ClientboundP
} }
@Override @Override
public TranslatableRewriter<ClientboundPackets1_19_4> getTranslatableRewriter() { public TranslatableRewriter<ClientboundPackets1_19_4> getComponentRewriter() {
return translatableRewriter; return translatableRewriter;
} }

View File

@ -21,7 +21,6 @@ import com.viaversion.viabackwards.api.rewriters.BackwardsItemRewriter;
import com.viaversion.viabackwards.protocol.v1_19_4to1_19_3.Protocol1_19_4To1_19_3; import com.viaversion.viabackwards.protocol.v1_19_4to1_19_3.Protocol1_19_4To1_19_3;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_18; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_18;
import com.viaversion.viaversion.protocols.v1_19_1to1_19_3.packet.ServerboundPackets1_19_3; import com.viaversion.viaversion.protocols.v1_19_1to1_19_3.packet.ServerboundPackets1_19_3;
@ -60,7 +59,7 @@ public final class BlockItemPacketRewriter1_19_4 extends BackwardsItemRewriter<C
wrapper.set(Types.VAR_INT, 1, windowType - 1); wrapper.set(Types.VAR_INT, 1, windowType - 1);
} }
protocol.getTranslatableRewriter().processText(wrapper.user(), wrapper.get(Types.COMPONENT, 0)); protocol.getComponentRewriter().processText(wrapper.user(), wrapper.get(Types.COMPONENT, 0));
}); });
} }
}); });

View File

@ -159,7 +159,7 @@ public final class EntityPacketRewriter1_19_4 extends EntityRewriter<Clientbound
event.createExtraData(new EntityData(3, Types1_19_3.ENTITY_DATA_TYPES.booleanType, true)); // Show custom name event.createExtraData(new EntityData(3, Types1_19_3.ENTITY_DATA_TYPES.booleanType, true)); // Show custom name
final JsonElement element = meta.value(); final JsonElement element = meta.value();
protocol.getTranslatableRewriter().processText(event.user(), element); protocol.getComponentRewriter().processText(event.user(), element);
})); }));
filter().type(EntityTypes1_19_4.DISPLAY).handler((event, meta) -> { filter().type(EntityTypes1_19_4.DISPLAY).handler((event, meta) -> {
// TODO Maybe spawn an extra entity to ride the armor stand for blocks and items // TODO Maybe spawn an extra entity to ride the armor stand for blocks and items

View File

@ -372,7 +372,7 @@ public final class Protocol1_19To1_18_2 extends BackwardsProtocol<ClientboundPac
} }
@Override @Override
public TranslatableRewriter<ClientboundPackets1_19> getTranslatableRewriter() { public TranslatableRewriter<ClientboundPackets1_19> getComponentRewriter() {
return translatableRewriter; return translatableRewriter;
} }

View File

@ -398,7 +398,7 @@ public final class Protocol1_20_3To1_20_2 extends BackwardsProtocol<ClientboundP
} }
@Override @Override
public TranslatableRewriter<ClientboundPacket1_20_3> getTranslatableRewriter() { public TranslatableRewriter<ClientboundPacket1_20_3> getComponentRewriter() {
return translatableRewriter; return translatableRewriter;
} }

View File

@ -254,7 +254,7 @@ public final class Protocol1_20_5To1_20_3 extends BackwardsProtocol<ClientboundP
} }
@Override @Override
public TranslatableRewriter<ClientboundPacket1_20_5> getTranslatableRewriter() { public TranslatableRewriter<ClientboundPacket1_20_5> getComponentRewriter() {
return translatableRewriter; return translatableRewriter;
} }

View File

@ -113,7 +113,7 @@ public final class Protocol1_20To1_19_4 extends BackwardsProtocol<ClientboundPac
} }
@Override @Override
public TranslatableRewriter<ClientboundPackets1_19_4> getTranslatableRewriter() { public TranslatableRewriter<ClientboundPackets1_19_4> getComponentRewriter() {
return translatableRewriter; return translatableRewriter;
} }

View File

@ -22,6 +22,7 @@ import com.viaversion.viabackwards.api.data.BackwardsMappingData;
import com.viaversion.viabackwards.api.rewriters.SoundRewriter; import com.viaversion.viabackwards.api.rewriters.SoundRewriter;
import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter;
import com.viaversion.viabackwards.protocol.v1_21to1_20_5.rewriter.BlockItemPacketRewriter1_21; import com.viaversion.viabackwards.protocol.v1_21to1_20_5.rewriter.BlockItemPacketRewriter1_21;
import com.viaversion.viabackwards.protocol.v1_21to1_20_5.rewriter.ComponentRewriter1_21;
import com.viaversion.viabackwards.protocol.v1_21to1_20_5.rewriter.EntityPacketRewriter1_21; import com.viaversion.viabackwards.protocol.v1_21to1_20_5.rewriter.EntityPacketRewriter1_21;
import com.viaversion.viabackwards.protocol.v1_21to1_20_5.storage.EnchantmentsPaintingsStorage; import com.viaversion.viabackwards.protocol.v1_21to1_20_5.storage.EnchantmentsPaintingsStorage;
import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.connection.UserConnection;
@ -42,7 +43,6 @@ import com.viaversion.viaversion.protocols.v1_20_5to1_21.Protocol1_20_5To1_21;
import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundConfigurationPackets1_21; import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundConfigurationPackets1_21;
import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPacket1_21; import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPacket1_21;
import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPackets1_21; import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPackets1_21;
import com.viaversion.viaversion.rewriter.ComponentRewriter.ReadType;
import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter;
import com.viaversion.viaversion.rewriter.TagRewriter; import com.viaversion.viaversion.rewriter.TagRewriter;
@ -53,7 +53,7 @@ public final class Protocol1_21To1_20_5 extends BackwardsProtocol<ClientboundPac
public static final BackwardsMappingData MAPPINGS = new BackwardsMappingData("1.21", "1.20.5", Protocol1_20_5To1_21.class); public static final BackwardsMappingData MAPPINGS = new BackwardsMappingData("1.21", "1.20.5", Protocol1_20_5To1_21.class);
private final EntityPacketRewriter1_21 entityRewriter = new EntityPacketRewriter1_21(this); private final EntityPacketRewriter1_21 entityRewriter = new EntityPacketRewriter1_21(this);
private final BlockItemPacketRewriter1_21 itemRewriter = new BlockItemPacketRewriter1_21(this); private final BlockItemPacketRewriter1_21 itemRewriter = new BlockItemPacketRewriter1_21(this);
private final TranslatableRewriter<ClientboundPacket1_21> translatableRewriter = new TranslatableRewriter<>(this, ReadType.NBT); private final TranslatableRewriter<ClientboundPacket1_21> translatableRewriter = new ComponentRewriter1_21(this);
private final TagRewriter<ClientboundPacket1_21> tagRewriter = new TagRewriter<>(this); private final TagRewriter<ClientboundPacket1_21> tagRewriter = new TagRewriter<>(this);
public Protocol1_21To1_20_5() { public Protocol1_21To1_20_5() {
@ -198,7 +198,7 @@ public final class Protocol1_21To1_20_5 extends BackwardsProtocol<ClientboundPac
} }
@Override @Override
public TranslatableRewriter<ClientboundPacket1_21> getTranslatableRewriter() { public TranslatableRewriter<ClientboundPacket1_21> getComponentRewriter() {
return translatableRewriter; return translatableRewriter;
} }

View File

@ -0,0 +1,64 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2024 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.viabackwards.protocol.v1_21to1_20_5.rewriter;
import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.IntArrayTag;
import com.viaversion.nbt.tag.ListTag;
import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter;
import com.viaversion.viabackwards.protocol.v1_21to1_20_5.Protocol1_21To1_20_5;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.protocols.v1_20_5to1_21.Protocol1_20_5To1_21;
import com.viaversion.viaversion.protocols.v1_20_5to1_21.data.AttributeModifierMappings1_21;
import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPacket1_21;
import com.viaversion.viaversion.util.SerializerVersion;
import com.viaversion.viaversion.util.TagUtil;
import com.viaversion.viaversion.util.UUIDUtil;
import java.util.UUID;
public final class ComponentRewriter1_21 extends TranslatableRewriter<ClientboundPacket1_21> {
public ComponentRewriter1_21(final Protocol1_21To1_20_5 protocol) {
super(protocol, ReadType.NBT);
}
@Override
protected void handleShowItem(final UserConnection connection, final CompoundTag componentsTag) {
final CompoundTag attributeModifiers = TagUtil.getNamespacedCompoundTag(componentsTag, "attribute_modifiers");
if (attributeModifiers != null) {
final ListTag<CompoundTag> modifiers = attributeModifiers.getListTag("modifiers", CompoundTag.class);
for (final CompoundTag modifier : modifiers) {
final String id = modifier.getString("id");
final UUID uuid = Protocol1_20_5To1_21.mapAttributeId(id);
final String name = AttributeModifierMappings1_21.idToName(id);
modifier.put("uuid", new IntArrayTag(UUIDUtil.toIntArray(uuid)));
modifier.putString("name", name != null ? name : id);
}
}
}
@Override
protected SerializerVersion inputSerializerVersion() {
return SerializerVersion.V1_20_5;
}
@Override
protected SerializerVersion outputSerializerVersion() {
return SerializerVersion.V1_20_5;
}
}

View File

@ -113,7 +113,7 @@ public final class Protocol1_98To1_99 extends BackwardsProtocol<ClientboundPacke
} }
@Override @Override
public TranslatableRewriter<ClientboundPacket1_20_5> getTranslatableRewriter() { public TranslatableRewriter<ClientboundPacket1_20_5> getComponentRewriter() {
return translatableRewriter; return translatableRewriter;
} }