Handle entity type changes in show_entity hover events

Fixes #4199
This commit is contained in:
Nassim Jahnke 2024-10-25 20:45:23 +02:00
parent 9bc943ea67
commit 7be7139241
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
4 changed files with 27 additions and 2 deletions

View File

@ -58,6 +58,14 @@ public interface EntityRewriter<T extends Protocol<?, ?, ?, ?>> extends Rewriter
*/
int newEntityId(int id);
/**
* Returns the mapped entity (or the same if it has not changed).
*
* @param identifier unmapped entity identifier
* @return mapped entity identifier
*/
String mappedEntityIdentifier(String identifier);
/**
* Handles and transforms entity data of an entity.
*

View File

@ -51,7 +51,8 @@ public interface ItemRewriter<T extends Protocol<?, ?, ?, ?>> extends Rewriter<T
@Nullable
Item handleItemToServer(UserConnection connection, @Nullable Item item);
void rewriteParticle(UserConnection connection, Particle particle);
default void rewriteParticle(UserConnection connection, Particle particle) {
}
/**
* Returns the item type of the current protocol.

View File

@ -390,6 +390,11 @@ public class ComponentRewriter<C extends ClientboundPacketType> implements com.v
final CompoundTag contents = hoverEventTag.getCompoundTag("contents");
if (contents != null) {
processTag(connection, contents.get("name"));
final StringTag typeTag = contents.getStringTag("type");
if (typeTag != null && protocol.getEntityRewriter() != null) {
typeTag.setValue(protocol.getEntityRewriter().mappedEntityIdentifier(typeTag.getValue()));
}
}
} else if (action.equals("show_item")) {
convertLegacyContents(hoverEventTag);

View File

@ -24,6 +24,7 @@ import com.viaversion.nbt.tag.NumberTag;
import com.viaversion.nbt.tag.Tag;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.data.FullMappings;
import com.viaversion.viaversion.api.data.Int2IntMapMappings;
import com.viaversion.viaversion.api.data.Mappings;
import com.viaversion.viaversion.api.data.ParticleMappings;
@ -60,7 +61,6 @@ import org.checkerframework.checker.nullness.qual.Nullable;
public abstract class EntityRewriter<C extends ClientboundPacketType, T extends Protocol<C, ?, ?, ?>>
extends RewriterBase<T> implements com.viaversion.viaversion.api.rewriter.EntityRewriter<T> {
private static final EntityData[] EMPTY_ARRAY = new EntityData[0];
protected final List<EntityDataFilter> entityDataFilters = new ArrayList<>();
protected final boolean trackMappedType;
protected Mappings typeMappings;
@ -156,6 +156,17 @@ public abstract class EntityRewriter<C extends ClientboundPacketType, T extends
return typeMappings != null ? typeMappings.getNewIdOrDefault(id, id) : id;
}
@Override
public String mappedEntityIdentifier(final String identifier) {
if (typeMappings instanceof final FullMappings fullMappings) {
final String mappedIdentifier = fullMappings.mappedIdentifier(identifier);
if (mappedIdentifier != null) {
return mappedIdentifier;
}
}
return identifier;
}
/**
* Maps an entity type.
*