3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-03 14:50:30 +01:00

Slightly improve metadata iteration, remove redundant MetaHandlerEvent index

Lazily create the event if needed and share it with other filters when handling a metadata entry. Lastly, only add the additionally created meta once after the filter list, not once per filter.
Dieser Commit ist enthalten in:
KennyTV 2021-05-25 22:51:37 +02:00
Ursprung 57769c5671
Commit bc89f57088
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B
5 geänderte Dateien mit 46 neuen und 60 gelöschten Zeilen

Datei anzeigen

@ -110,9 +110,16 @@ public abstract class EntityRewriter<T extends Protocol> extends RewriterBase<T>
continue;
}
MetaHandlerEvent event = null;
for (MetaFilter filter : metadataFilters) {
if (filter.isFiltered(type, metadata)) {
MetaHandlerEvent event = new MetaHandlerEventImpl(connection, type, entityId, metadata.id(), metadata, metadataList);
if (!filter.isFiltered(type, metadata)) {
continue;
}
if (event == null) {
// Only initialize when needed and share event instance
event = new MetaHandlerEventImpl(connection, type, entityId, metadata, metadataList);
}
try {
filter.handler().handle(event, metadata);
} catch (Exception e) {
@ -126,21 +133,18 @@ public abstract class EntityRewriter<T extends Protocol> extends RewriterBase<T>
metadataList.remove(i--);
break;
}
}
Preconditions.checkArgument(event.index() == event.meta().id(), "Meta event id desync: Calls to meta().setId() are illegal");
if (event.extraMeta() != null) {
// Add newly created meta
if (event != null && event.extraMeta() != null) {
// Finally add newly created meta
metadataList.addAll(event.extraMeta());
event.clearExtraMeta();
}
}
}
i++;
}
}
@Deprecated
private boolean callOldMetaHandler(int entityId, EntityType type, Metadata metadata, List<Metadata> metadataList, UserConnection connection) {
private boolean callOldMetaHandler(int entityId, @Nullable EntityType type, Metadata metadata, List<Metadata> metadataList, UserConnection connection) {
try {
handleMetadata(entityId, type, metadata, metadataList, connection);
return true;
@ -498,14 +502,14 @@ public abstract class EntityRewriter<T extends Protocol> extends RewriterBase<T>
* Returns the entity tracker for the current protocol.
*
* @param connection user connection
* @param <T> entity tracker type
* @param <E> entity tracker type
* @return entity tracker
*/
public <T extends EntityTracker> T tracker(UserConnection connection) {
public <E extends EntityTracker> E tracker(UserConnection connection) {
return connection.getEntityTracker(protocol.getClass());
}
private void logException(Exception e, EntityType type, List<Metadata> metadataList, Metadata metadata) {
private void logException(Exception e, @Nullable EntityType type, List<Metadata> metadataList, Metadata metadata) {
if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) {
Logger logger = Via.getPlatform().getLogger();
logger.warning("An error occurred with entity metadata handler");

Datei anzeigen

@ -83,10 +83,12 @@ public class MetaFilter {
* @param metadata metadata
* @return whether the meta should be filtered
*/
public boolean isFiltered(EntityType type, Metadata metadata) {
public boolean isFiltered(@Nullable EntityType type, Metadata metadata) {
// First check if the filter has no type or the type is equal or part of the filtered parent types
// Applicable if no specific index is filtered or the indexes are equal
return (this.type == null || (filterFamily ? type.isOrHasParent(this.type) : this.type == type)) && (index == -1 || metadata.id() == index);
return (this.type == null
|| type != null && (this.filterFamily ? type.isOrHasParent(this.type) : this.type == type))
&& (this.index == -1 || metadata.id() == this.index);
}
@Override

Datei anzeigen

@ -27,7 +27,7 @@ public interface MetaHandler {
* Handles a metadata entry of an entity.
*
* @param event metadata event
* @param meta metadata, convenience field for {@link MetaHandlerEvent#meta()}
* @param meta metadata, convenience parameter for {@link MetaHandlerEvent#meta()}
*/
void handle(MetaHandlerEvent event, Metadata meta);
}

Datei anzeigen

@ -41,25 +41,29 @@ public interface MetaHandlerEvent {
int entityId();
/**
* Returns the entity type of the entity the metadata belongs to.
* Returns the entity type of the entity the metadata belongs to if tracked.
*
* @return entity type of the entity
* @return entity type of the entity if tracked, else null
*/
EntityType entityType();
@Nullable EntityType entityType();
/**
* Returns the metadata index.
*
* @return return meta index
*/
int index();
default int index() {
return meta().id();
}
/**
* Sets the metadata index.
*
* @param index new metadata index
*/
void setIndex(int index);
default void setIndex(int index) {
meta().setId(index);
}
/**
* Returns the metadata by the given index if present.
@ -71,7 +75,6 @@ public interface MetaHandlerEvent {
/**
* Returns the metadata.
* Do NOT call {@link Metadata#setId(int)} and instead use {@link MetaHandlerEvent#setIndex(int)}.
*
* @return return metadata
*/
@ -113,9 +116,4 @@ public interface MetaHandlerEvent {
* @param metadata metadata
*/
void createExtraMeta(Metadata metadata);
/**
* Clears the additional metadata.
*/
void clearExtraMeta();
}

Datei anzeigen

@ -34,14 +34,12 @@ public class MetaHandlerEventImpl implements MetaHandlerEvent {
private final List<Metadata> metadataList;
private final Metadata meta;
private List<Metadata> extraData;
private int index;
private boolean cancel;
public MetaHandlerEventImpl(UserConnection connection, EntityType entityType, int entityId, int index, Metadata meta, List<Metadata> metadataList) {
public MetaHandlerEventImpl(UserConnection connection, @Nullable EntityType entityType, int entityId, Metadata meta, List<Metadata> metadataList) {
this.connection = connection;
this.entityType = entityType;
this.entityId = entityId;
this.index = index;
this.meta = meta;
this.metadataList = metadataList;
}
@ -67,21 +65,10 @@ public class MetaHandlerEventImpl implements MetaHandlerEvent {
}
@Override
public EntityType entityType() {
public @Nullable EntityType entityType() {
return entityType;
}
@Override
public int index() {
return index;
}
@Override
public void setIndex(int index) {
this.index = index;
meta.setId(index);
}
@Override
public Metadata meta() {
return meta;
@ -111,9 +98,4 @@ public class MetaHandlerEventImpl implements MetaHandlerEvent {
public void createExtraMeta(Metadata metadata) {
(extraData != null ? extraData : (extraData = new ArrayList<>())).add(metadata);
}
@Override
public void clearExtraMeta() {
extraData = null;
}
}