Search in sources :

Example 1 with MetaFilter

use of com.viaversion.viaversion.rewriter.meta.MetaFilter in project ViaVersion by ViaVersion.

the class EntityRewriter method handleMetadata.

@Override
public void handleMetadata(int entityId, List<Metadata> metadataList, UserConnection connection) {
    EntityType type = tracker(connection).entityType(entityId);
    // Count index for fast removal
    int i = 0;
    for (Metadata metadata : metadataList.toArray(EMPTY_ARRAY)) {
        // Call handlers implementing the old handleMetadata
        if (!callOldMetaHandler(entityId, type, metadata, metadataList, connection)) {
            metadataList.remove(i--);
            continue;
        }
        MetaHandlerEvent event = null;
        for (MetaFilter filter : metadataFilters) {
            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) {
                logException(e, type, metadataList, metadata);
                metadataList.remove(i--);
                break;
            }
            if (event.cancelled()) {
                // Remove meta, decrease list index counter, and break current filter loop
                metadataList.remove(i--);
                break;
            }
        }
        if (event != null && event.extraMeta() != null) {
            // Finally, add newly created meta
            metadataList.addAll(event.extraMeta());
        }
        i++;
    }
}
Also used : EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) MetaFilter(com.viaversion.viaversion.rewriter.meta.MetaFilter) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) MetaHandlerEvent(com.viaversion.viaversion.rewriter.meta.MetaHandlerEvent) MetaHandlerEventImpl(com.viaversion.viaversion.rewriter.meta.MetaHandlerEventImpl)

Aggregations

EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)1 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)1 MetaFilter (com.viaversion.viaversion.rewriter.meta.MetaFilter)1 MetaHandlerEvent (com.viaversion.viaversion.rewriter.meta.MetaHandlerEvent)1 MetaHandlerEventImpl (com.viaversion.viaversion.rewriter.meta.MetaHandlerEventImpl)1