mirror of
https://github.com/ViaVersion/ViaVersion.git
synced 2024-12-31 21:07:49 +01:00
Add ComponentRewriter#convertLegacyEntityContents, cleanup 1.20.5 rewriter class
Moves the value->contents transition for entities to the common rewriter class for VB and cleans up the ComponentRewriter1_20_5 class to use the new handleShowItem
This commit is contained in:
parent
4773ba41cf
commit
bf9e4dd42c
@ -85,7 +85,6 @@ import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.data.PotionEffects1_2
|
|||||||
import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.data.Potions1_20_5;
|
import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.data.Potions1_20_5;
|
||||||
import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.storage.ArmorTrimStorage;
|
import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.storage.ArmorTrimStorage;
|
||||||
import com.viaversion.viaversion.rewriter.ComponentRewriter;
|
import com.viaversion.viaversion.rewriter.ComponentRewriter;
|
||||||
import com.viaversion.viaversion.util.ComponentUtil;
|
|
||||||
import com.viaversion.viaversion.util.Either;
|
import com.viaversion.viaversion.util.Either;
|
||||||
import com.viaversion.viaversion.util.Key;
|
import com.viaversion.viaversion.util.Key;
|
||||||
import com.viaversion.viaversion.util.SerializerVersion;
|
import com.viaversion.viaversion.util.SerializerVersion;
|
||||||
@ -178,93 +177,7 @@ public class ComponentRewriter1_20_5<C extends ClientboundPacketType> extends Co
|
|||||||
if (actionTag == null) {
|
if (actionTag == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (actionTag.getValue().equals("show_entity")) {
|
||||||
if (actionTag.getValue().equals("show_item")) {
|
|
||||||
final Tag valueTag = hoverEventTag.remove("value");
|
|
||||||
if (valueTag != null) { // Convert legacy hover event to new format for rewriting (Doesn't handle all cases, but good enough)
|
|
||||||
final CompoundTag tag = ComponentUtil.deserializeShowItem(valueTag, SerializerVersion.V1_20_3);
|
|
||||||
final CompoundTag contentsTag = new CompoundTag();
|
|
||||||
contentsTag.put("id", tag.getStringTag("id"));
|
|
||||||
contentsTag.put("count", new IntTag(tag.getByte("Count")));
|
|
||||||
if (tag.get("tag") instanceof CompoundTag) {
|
|
||||||
contentsTag.put("tag", new StringTag(SerializerVersion.V1_20_3.toSNBT(tag.getCompoundTag("tag"))));
|
|
||||||
}
|
|
||||||
hoverEventTag.put("contents", contentsTag);
|
|
||||||
}
|
|
||||||
|
|
||||||
final CompoundTag contentsTag = hoverEventTag.getCompoundTag("contents");
|
|
||||||
if (contentsTag == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final StringTag idTag = contentsTag.getStringTag("id");
|
|
||||||
if (idTag == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int itemId = Protocol1_20_3To1_20_5.MAPPINGS.getFullItemMappings().id(idTag.getValue());
|
|
||||||
if (itemId == -1) {
|
|
||||||
// Default to stone (anything that is not air)
|
|
||||||
itemId = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
final StringTag tag = (StringTag) contentsTag.remove("tag");
|
|
||||||
final CompoundTag tagTag;
|
|
||||||
try {
|
|
||||||
tagTag = tag != null ? (CompoundTag) SerializerVersion.V1_20_3.toTag(tag.getValue()) : null;
|
|
||||||
} catch (final Exception e) {
|
|
||||||
if (!Via.getConfig().isSuppressConversionWarnings()) {
|
|
||||||
protocol.getLogger().log(Level.WARNING, "Error reading NBT in show_item: " + contentsTag, e);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final Item dataItem = new DataItem();
|
|
||||||
dataItem.setIdentifier(itemId);
|
|
||||||
if (tagTag != null) { // We don't need to remap data if there is none
|
|
||||||
dataItem.setTag(tagTag);
|
|
||||||
}
|
|
||||||
|
|
||||||
final Item structuredItem = protocol.getItemRewriter().handleItemToClient(connection, dataItem);
|
|
||||||
if (structuredItem.amount() < 1) {
|
|
||||||
// Cannot be empty
|
|
||||||
structuredItem.setAmount(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (structuredItem.identifier() != 0) {
|
|
||||||
final String identifier = mappedIdentifier(structuredItem.identifier());
|
|
||||||
if (identifier != null) {
|
|
||||||
contentsTag.putString("id", identifier);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Cannot be air
|
|
||||||
contentsTag.putString("id", "minecraft:stone");
|
|
||||||
}
|
|
||||||
|
|
||||||
final Map<StructuredDataKey<?>, StructuredData<?>> data = structuredItem.dataContainer().data();
|
|
||||||
if (!data.isEmpty()) {
|
|
||||||
final CompoundTag components;
|
|
||||||
try {
|
|
||||||
components = toTag(connection, data, false);
|
|
||||||
} catch (final Exception e) {
|
|
||||||
if (!Via.getConfig().isSuppressConversionWarnings()) {
|
|
||||||
protocol.getLogger().log(Level.WARNING, "Error writing components in show_item!", e);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
contentsTag.put("components", components);
|
|
||||||
}
|
|
||||||
} else if (actionTag.getValue().equals("show_entity")) {
|
|
||||||
final Tag valueTag = hoverEventTag.remove("value");
|
|
||||||
if (valueTag != null) { // Convert legacy hover event to new format for rewriting (Doesn't handle all cases, but good enough)
|
|
||||||
final CompoundTag tag = ComponentUtil.deserializeShowItem(valueTag, SerializerVersion.V1_20_3);
|
|
||||||
final CompoundTag contentsTag = new CompoundTag();
|
|
||||||
contentsTag.put("type", tag.getStringTag("type"));
|
|
||||||
contentsTag.put("id", tag.getStringTag("id"));
|
|
||||||
contentsTag.put("name", SerializerVersion.V1_20_3.toTag(SerializerVersion.V1_20_3.toComponent(tag.getString("name"))));
|
|
||||||
hoverEventTag.put("contents", contentsTag);
|
|
||||||
}
|
|
||||||
|
|
||||||
final CompoundTag contentsTag = hoverEventTag.getCompoundTag("contents");
|
final CompoundTag contentsTag = hoverEventTag.getCompoundTag("contents");
|
||||||
if (contentsTag == null) {
|
if (contentsTag == null) {
|
||||||
return;
|
return;
|
||||||
@ -276,6 +189,69 @@ public class ComponentRewriter1_20_5<C extends ClientboundPacketType> extends Co
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void handleShowItem(final UserConnection connection, final CompoundTag itemTag, final @Nullable CompoundTag componentsTag) {
|
||||||
|
super.handleShowItem(connection, itemTag, componentsTag);
|
||||||
|
|
||||||
|
final StringTag idTag = itemTag.getStringTag("id");
|
||||||
|
if (idTag == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int itemId = Protocol1_20_3To1_20_5.MAPPINGS.getFullItemMappings().id(idTag.getValue());
|
||||||
|
if (itemId == -1) {
|
||||||
|
// Default to stone (anything that is not air)
|
||||||
|
itemId = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
final StringTag tag = (StringTag) itemTag.remove("tag");
|
||||||
|
final CompoundTag tagTag;
|
||||||
|
try {
|
||||||
|
tagTag = tag != null ? (CompoundTag) inputSerializerVersion().toTag(tag.getValue()) : null;
|
||||||
|
} catch (final Exception e) {
|
||||||
|
if (!Via.getConfig().isSuppressConversionWarnings()) {
|
||||||
|
protocol.getLogger().log(Level.WARNING, "Error reading NBT in show_item: " + itemTag, e);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final Item dataItem = new DataItem();
|
||||||
|
dataItem.setIdentifier(itemId);
|
||||||
|
if (tagTag != null) { // We don't need to remap data if there is none
|
||||||
|
dataItem.setTag(tagTag);
|
||||||
|
}
|
||||||
|
|
||||||
|
final Item structuredItem = protocol.getItemRewriter().handleItemToClient(connection, dataItem);
|
||||||
|
if (structuredItem.amount() < 1) {
|
||||||
|
// Cannot be empty
|
||||||
|
structuredItem.setAmount(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (structuredItem.identifier() != 0) {
|
||||||
|
final String identifier = mappedIdentifier(structuredItem.identifier());
|
||||||
|
if (identifier != null) {
|
||||||
|
itemTag.putString("id", identifier);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Cannot be air
|
||||||
|
itemTag.putString("id", "minecraft:stone");
|
||||||
|
}
|
||||||
|
|
||||||
|
final Map<StructuredDataKey<?>, StructuredData<?>> data = structuredItem.dataContainer().data();
|
||||||
|
if (!data.isEmpty()) {
|
||||||
|
final CompoundTag components;
|
||||||
|
try {
|
||||||
|
components = toTag(connection, data, false);
|
||||||
|
} catch (final Exception e) {
|
||||||
|
if (!Via.getConfig().isSuppressConversionWarnings()) {
|
||||||
|
protocol.getLogger().log(Level.WARNING, "Error writing components in show_item!", e);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
itemTag.put("components", components);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public CompoundTag toTag(final UserConnection connection, final Map<StructuredDataKey<?>, StructuredData<?>> data, final boolean empty) {
|
public CompoundTag toTag(final UserConnection connection, final Map<StructuredDataKey<?>, StructuredData<?>> data, final boolean empty) {
|
||||||
final CompoundTag tag = new CompoundTag();
|
final CompoundTag tag = new CompoundTag();
|
||||||
for (final Map.Entry<StructuredDataKey<?>, StructuredData<?>> entry : data.entrySet()) {
|
for (final Map.Entry<StructuredDataKey<?>, StructuredData<?>> entry : data.entrySet()) {
|
||||||
@ -1080,7 +1056,7 @@ public class ComponentRewriter1_20_5<C extends ClientboundPacketType> extends Co
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected StringTag convertComponent(final Tag value, final int max) {
|
protected StringTag convertComponent(final Tag value, final int max) {
|
||||||
final String json = serializerVersion().toString(serializerVersion().toComponent(value));
|
final String json = outputSerializerVersion().toString(outputSerializerVersion().toComponent(value));
|
||||||
return new StringTag(checkStringRange(0, max, json));
|
return new StringTag(checkStringRange(0, max, json));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1088,7 +1064,7 @@ public class ComponentRewriter1_20_5<C extends ClientboundPacketType> extends Co
|
|||||||
checkIntRange(0, maxLength, value.length);
|
checkIntRange(0, maxLength, value.length);
|
||||||
final ListTag<StringTag> listTag = new ListTag<>(StringTag.class);
|
final ListTag<StringTag> listTag = new ListTag<>(StringTag.class);
|
||||||
for (final Tag tag : value) {
|
for (final Tag tag : value) {
|
||||||
final String json = serializerVersion().toString(serializerVersion().toComponent(tag));
|
final String json = outputSerializerVersion().toString(outputSerializerVersion().toComponent(tag));
|
||||||
listTag.add(new StringTag(json));
|
listTag.add(new StringTag(json));
|
||||||
}
|
}
|
||||||
return listTag;
|
return listTag;
|
||||||
@ -1174,7 +1150,13 @@ public class ComponentRewriter1_20_5<C extends ClientboundPacketType> extends Co
|
|||||||
return converters != null ? converters.tagConverter : null;
|
return converters != null ? converters.tagConverter : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SerializerVersion serializerVersion() {
|
@Override
|
||||||
|
protected @Nullable SerializerVersion inputSerializerVersion() {
|
||||||
|
return SerializerVersion.V1_20_3;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected @Nullable SerializerVersion outputSerializerVersion() {
|
||||||
return SerializerVersion.V1_20_5;
|
return SerializerVersion.V1_20_5;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -371,6 +371,8 @@ public class ComponentRewriter<C extends ClientboundPacketType> implements com.v
|
|||||||
final Tag value = hoverEventTag.get("value");
|
final Tag value = hoverEventTag.get("value");
|
||||||
processTag(connection, value != null ? value : hoverEventTag.get("contents"));
|
processTag(connection, value != null ? value : hoverEventTag.get("contents"));
|
||||||
} else if (action.equals("show_entity")) {
|
} else if (action.equals("show_entity")) {
|
||||||
|
convertLegacyEntityContents(hoverEventTag);
|
||||||
|
|
||||||
final CompoundTag contents = hoverEventTag.getCompoundTag("contents");
|
final CompoundTag contents = hoverEventTag.getCompoundTag("contents");
|
||||||
if (contents != null) {
|
if (contents != null) {
|
||||||
processTag(connection, contents.get("name"));
|
processTag(connection, contents.get("name"));
|
||||||
@ -381,7 +383,7 @@ public class ComponentRewriter<C extends ClientboundPacketType> implements com.v
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (action.equals("show_item")) {
|
} else if (action.equals("show_item")) {
|
||||||
convertLegacyContents(hoverEventTag);
|
convertLegacyItemContents(hoverEventTag);
|
||||||
|
|
||||||
final CompoundTag contentsTag = hoverEventTag.getCompoundTag("contents");
|
final CompoundTag contentsTag = hoverEventTag.getCompoundTag("contents");
|
||||||
if (contentsTag == null) {
|
if (contentsTag == null) {
|
||||||
@ -482,7 +484,23 @@ public class ComponentRewriter<C extends ClientboundPacketType> implements com.v
|
|||||||
return inputSerializerVersion(); // Only matters if the nbt serializer changed
|
return inputSerializerVersion(); // Only matters if the nbt serializer changed
|
||||||
}
|
}
|
||||||
|
|
||||||
private void convertLegacyContents(final CompoundTag hoverEvent) {
|
private void convertLegacyEntityContents(final CompoundTag hoverEvent) {
|
||||||
|
if (inputSerializerVersion() == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final Tag valueTag = hoverEvent.remove("value");
|
||||||
|
if (valueTag != null) {
|
||||||
|
final CompoundTag tag = ComponentUtil.deserializeShowItem(valueTag, inputSerializerVersion());
|
||||||
|
final CompoundTag contentsTag = new CompoundTag();
|
||||||
|
contentsTag.put("type", tag.getStringTag("type"));
|
||||||
|
contentsTag.put("id", tag.getStringTag("id"));
|
||||||
|
contentsTag.put("name", outputSerializerVersion().toTag(outputSerializerVersion().toComponent(tag.getString("name"))));
|
||||||
|
hoverEvent.put("contents", contentsTag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void convertLegacyItemContents(final CompoundTag hoverEvent) {
|
||||||
if (inputSerializerVersion() == null) {
|
if (inputSerializerVersion() == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user