From 8715c243c92d5dcabe9b39f077c9eb0101773b1a Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Fri, 10 Feb 2023 12:10:49 +0100 Subject: [PATCH] Improve additional metadata handling Fixes #527 --- .../api/rewriters/EntityRewriter.java | 28 ++++++++++-- .../api/rewriters/EntityRewriterBase.java | 45 ++++++++++++------- .../Protocol1_10To1_11.java | 2 +- .../Protocol1_11_1To1_12.java | 2 +- .../Protocol1_13_2To1_14.java | 2 +- .../Protocol1_15_2To1_16.java | 2 +- .../Protocol1_18_2To1_19.java | 2 +- .../packets/EntityPackets1_19.java | 2 +- .../Protocol1_19_1To1_19_3.java | 2 +- .../packets/EntityPackets1_19_3.java | 2 +- .../packets/EntityPackets1_19_4.java | 23 ++++++---- .../Protocol1_19To1_19_1.java | 2 +- .../packets/EntityPackets1_19_1.java | 2 +- 13 files changed, 79 insertions(+), 37 deletions(-) diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriter.java index 24f7e9e7..89074472 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriter.java @@ -57,10 +57,30 @@ public abstract class EntityRewriter { // Check against the UNMAPPED entity type - EntityType entityType = setOldEntityId(wrapper); + EntityType entityType = trackAndMapEntity(wrapper); if (entityType == fallingBlockType) { int blockState = wrapper.get(Type.INT, 0); wrapper.set(Type.INT, 0, protocol.getMappingData().getNewBlockStateId(blockState)); @@ -71,7 +91,7 @@ public abstract class EntityRewriter { // Check against the UNMAPPED entity type - EntityType entityType = setOldEntityId(wrapper); + EntityType entityType = trackAndMapEntity(wrapper); if (entityType == fallingBlockType) { int blockState = wrapper.get(Type.VAR_INT, 2); wrapper.set(Type.VAR_INT, 2, protocol.getMappingData().getNewBlockStateId(blockState)); @@ -86,7 +106,7 @@ public abstract class EntityRewriter setOldEntityId(wrapper)); + handler(wrapper -> trackAndMapEntity(wrapper)); } }); } @@ -114,7 +134,7 @@ public abstract class EntityRewriter metadataList, UserConnection connection) { + final TrackedEntity entity = tracker(connection).entity(entityId); + final boolean initialMetadata = !(entity != null && entity.hasSentMetadata()); super.handleMetadata(entityId, metadataList, connection); - EntityType type = tracker(connection).entityType(entityId); - if (type == null) { + if (entity == null) { return; // Don't handle untracked entities - basically always the fault of a plugin sending virtual entities through concurrency-unsafe handling } - EntityData entityData = entityDataForType(type); + final EntityData entityData = entityDataForType(entity.entityType()); // Set the mapped entity name if there is no custom name set already - Metadata meta = getMeta(displayNameIndex, metadataList); - if (meta != null && entityData != null && entityData.mobName() != null - && (meta.getValue() == null || meta.getValue().toString().isEmpty()) - && meta.metaType().typeId() == displayNameMetaType.typeId()) { - meta.setValue(entityData.mobName()); - if (ViaBackwards.getConfig().alwaysShowOriginalMobName()) { - removeMeta(displayVisibilityIndex, metadataList); - metadataList.add(new Metadata(displayVisibilityIndex, displayVisibilityMetaType, true)); + if (entityData != null && entityData.mobName() != null) { + final Metadata displayName = getMeta(displayNameIndex, metadataList); + if (initialMetadata) { + if (displayName == null) { + // Add it as new metadata + metadataList.add(new Metadata(displayNameIndex, displayNameMetaType, entityData.mobName())); + addDisplayVisibilityMeta(metadataList); + } else if (displayName.getValue() == null || displayName.getValue().toString().isEmpty()) { + // Overwrite the existing null/empty display name + displayName.setValue(entityData.mobName()); + addDisplayVisibilityMeta(metadataList); + } + } else if (displayName != null && (displayName.getValue() == null || displayName.getValue().toString().isEmpty())) { + // Overwrite null/empty display name + displayName.setValue(entityData.mobName()); + addDisplayVisibilityMeta(metadataList); } } // Add any other extra meta for mapped entities - //TODO only do this once for a first meta packet? - if (entityData != null && entityData.hasBaseMeta()) { + if (entityData != null && entityData.hasBaseMeta() && initialMetadata) { entityData.defaultMeta().createMeta(new WrappedMetadata(metadataList)); } } + private void addDisplayVisibilityMeta(List metadataList) { + if (ViaBackwards.getConfig().alwaysShowOriginalMobName()) { + removeMeta(displayVisibilityIndex, metadataList); + metadataList.add(new Metadata(displayVisibilityIndex, displayVisibilityMetaType, true)); + } + } + protected @Nullable Metadata getMeta(int metaIndex, List metadataList) { for (Metadata metadata : metadataList) { if (metadata.id() == metaIndex) { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/Protocol1_10To1_11.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/Protocol1_10To1_11.java index b14a3061..922a2a04 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/Protocol1_10To1_11.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/Protocol1_10To1_11.java @@ -60,7 +60,7 @@ public class Protocol1_10To1_11 extends BackwardsProtocol { final byte headYaw = wrapper.read(Type.BYTE); int data = wrapper.read(Type.VAR_INT); - final EntityType entityType = setOldEntityId(wrapper); + final EntityType entityType = trackAndMapEntity(wrapper); if (entityType.isOrHasParent(Entity1_19Types.LIVINGENTITY)) { wrapper.write(Type.BYTE, headYaw); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java index 88bd16d8..759c41e7 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java @@ -376,7 +376,7 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol { public EntityPackets1_19_4(final Protocol1_19_3To1_19_4 protocol) { - super(protocol); + super(protocol, Types1_19_3.META_TYPES.optionalComponentType, Types1_19_3.META_TYPES.booleanType); } @Override public void registerPackets() { registerTrackerWithData1_19(ClientboundPackets1_19_4.SPAWN_ENTITY, null); registerRemoveEntities(ClientboundPackets1_19_4.REMOVE_ENTITIES); - registerMetadataRewriter(ClientboundPackets1_19_4.ENTITY_METADATA, Types1_19_4.METADATA_LIST); + registerMetadataRewriter(ClientboundPackets1_19_4.ENTITY_METADATA, Types1_19_4.METADATA_LIST, Types1_19_3.METADATA_LIST); protocol.registerClientbound(ClientboundPackets1_19_4.JOIN_GAME, new PacketRemapper() { @Override @@ -102,7 +102,6 @@ public final class EntityPackets1_19_4 extends EntityRewriter { int id = meta.metaType().typeId(); if (id >= 25) { // Vector3f/quaternion types - event.cancel(); return; } else if (id >= 15) { // Optional block state - just map down to block state id--; @@ -112,27 +111,35 @@ public final class EntityPackets1_19_4 extends EntityRewriter { + // Send as custom display name + event.setIndex(2); + meta.setMetaType(Types1_19_3.META_TYPES.optionalComponentType); + event.createExtraMeta(new Metadata(3, Types1_19_4.META_TYPES.booleanType, true)); // Show custom name + })); + // TODO Maybe spawn an extra entity to ride the armor stand for blocks and items filter().filterFamily(Entity1_19_4Types.DISPLAY).handler((event, meta) -> { // Remove a large heap of display metadata if (event.index() > 7) { event.cancel(); } }); + filter().filterFamily(Entity1_19_4Types.ABSTRACT_HORSE).addIndex(18); // Owner UUID } @Override public void onMappingDataLoaded() { mapTypes(); - // TODO Use text/item/block - final EntityData.MetaCreator metaCreator = storage -> { + + final EntityData.MetaCreator displayMetaCreator = storage -> { storage.add(new Metadata(0, Types1_19_4.META_TYPES.byteType, (byte) 0x20)); // Invisible storage.add(new Metadata(5, Types1_19_4.META_TYPES.booleanType, true)); // No gravity storage.add(new Metadata(15, Types1_19_4.META_TYPES.byteType, (byte) (0x01 | 0x10))); // Small marker }; - mapEntityTypeWithData(Entity1_19_4Types.BLOCK_DISPLAY, Entity1_19_4Types.ARMOR_STAND).spawnMetadata(metaCreator); - mapEntityTypeWithData(Entity1_19_4Types.ITEM_DISPLAY, Entity1_19_4Types.ARMOR_STAND).spawnMetadata(metaCreator); - mapEntityTypeWithData(Entity1_19_4Types.TEXT_DISPLAY, Entity1_19_4Types.ARMOR_STAND).spawnMetadata(metaCreator); + mapEntityTypeWithData(Entity1_19_4Types.TEXT_DISPLAY, Entity1_19_4Types.ARMOR_STAND).spawnMetadata(displayMetaCreator); + mapEntityTypeWithData(Entity1_19_4Types.ITEM_DISPLAY, Entity1_19_4Types.ARMOR_STAND).spawnMetadata(displayMetaCreator); + mapEntityTypeWithData(Entity1_19_4Types.BLOCK_DISPLAY, Entity1_19_4Types.ARMOR_STAND).spawnMetadata(displayMetaCreator); } @Override diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/Protocol1_19To1_19_1.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/Protocol1_19To1_19_1.java index 3865910d..91204760 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/Protocol1_19To1_19_1.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/Protocol1_19To1_19_1.java @@ -349,7 +349,7 @@ public final class Protocol1_19To1_19_1 extends BackwardsProtocol { public EntityPackets1_19_1(final Protocol1_19To1_19_1 protocol) { - super(protocol); + super(protocol, Types1_19.META_TYPES.optionalComponentType, Types1_19.META_TYPES.booleanType); } @Override