From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: ishland Date: Sat, 1 Jan 2022 11:05:22 +0100 Subject: [PATCH] vmp: entity.iteration Copyright (c) 2021-2022 ishland Original code by RelativityMC, licensed under MIT You can find the original code on https://github.com/RelativityMC/VMP-fabric (Yarn mappings) diff --git a/src/main/java/com/ishland/vmp/common/general/collections/ITypeFilterableList.java b/src/main/java/com/ishland/vmp/common/general/collections/ITypeFilterableList.java new file mode 100644 index 0000000000000000000000000000000000000000..beaa0a60771b17b93e4074b272b503a2f6e4cf34 --- /dev/null +++ b/src/main/java/com/ishland/vmp/common/general/collections/ITypeFilterableList.java @@ -0,0 +1,8 @@ +package com.ishland.vmp.common.general.collections; + +public interface ITypeFilterableList { + + Object[] getBackingArray(); + + +} \ No newline at end of file diff --git a/src/main/java/net/minecraft/world/level/entity/EntitySection.java b/src/main/java/net/minecraft/world/level/entity/EntitySection.java index e9aee7d11798c3bd990466f101e9e342686de11c..13ebaf6aede402f5f702aae6c1e44445b00cd9bb 100644 --- a/src/main/java/net/minecraft/world/level/entity/EntitySection.java +++ b/src/main/java/net/minecraft/world/level/entity/EntitySection.java @@ -8,6 +8,10 @@ import net.minecraft.util.ClassInstanceMultiMap; import net.minecraft.util.VisibleForDebug; import net.minecraft.world.phys.AABB; import org.slf4j.Logger; +import com.ishland.vmp.common.general.collections.ITypeFilterableList; // Mirai +import it.unimi.dsi.fastutil.objects.ObjectArrayList; // Mirai +import net.minecraft.world.level.entity.EntityAccess; // Mirai +import net.minecraft.world.level.entity.EntityTypeTest; // Mirai public class EntitySection { private static final Logger LOGGER = LogUtils.getLogger(); @@ -45,27 +49,62 @@ public class EntitySection { return this.storage.remove(entity); } + // Mirai start + /** + * @author ishland + * @reason use array for iteration & inline math + */ public void getEntities(AABB box, Consumer action) { - for(T entityAccess : this.storage) { - if (entityAccess.getBoundingBox().intersects(box)) { - action.accept(entityAccess); + if (this.storage instanceof ITypeFilterableList iTypeFilterableList) { // use array for iteration + for (Object _entityLike : iTypeFilterableList.getBackingArray()) { + if (_entityLike != null) { + @SuppressWarnings("unchecked") T entityAccess = (T) _entityLike; + AABB box1 = entityAccess.getBoundingBox(); + if (box1.minX < box.maxX && box1.maxX > box.minX && box1.minY < box.maxY && box1.maxY > box.minY && box1.minZ < box.maxZ && box1.maxZ > box.minZ) { // inline math + action.accept(entityAccess); + } + } + } + } else { // fallback + for (T entityAccess : this.storage) { + AABB box1 = entityAccess.getBoundingBox(); + if (box1.minX < box.maxX && box1.maxX > box.minX && box1.minY < box.maxY && box1.maxY > box.minY && box1.minZ < box.maxZ && box1.maxZ > box.minZ) { // inline math + action.accept(entityAccess); + } } } - } + /** + * @author ishland + * @reason use array for iteration & inline math + */ public void getEntities(EntityTypeTest type, AABB box, Consumer action) { Collection collection = this.storage.find(type.getBaseClass()); if (!collection.isEmpty()) { - for(T entityAccess : collection) { - U entityAccess2 = (U)((EntityAccess)type.tryCast(entityAccess)); - if (entityAccess2 != null && entityAccess.getBoundingBox().intersects(box)) { - action.accept(entityAccess2); // Paper - decompile fix + if (collection instanceof ObjectArrayList objectArrayList) { // use array for iteration + for (Object _entityLike : objectArrayList.elements()) { + if (_entityLike != null) { + T entityAccess = (T) _entityLike; + U entityAccess2 = type.tryCast(entityAccess); + final AABB boundingBox = entityAccess.getBoundingBox(); + if (entityAccess2 != null && boundingBox.minX < box.maxX && boundingBox.maxX > box.minX && boundingBox.minY < box.maxY && boundingBox.maxY > box.minY && boundingBox.minZ < box.maxZ && boundingBox.maxZ > box.minZ) { // inline math + action.accept(entityAccess2); + } + } + } + } else { // fallback + for(T entityAccess : collection) { + U entityAccess2 = type.tryCast(entityAccess); + AABB box1 = entityAccess.getBoundingBox(); + if (entityAccess2 != null && box1.minX < box.maxX && box1.maxX > box.minX && box1.minY < box.maxY && box1.maxY > box.minY && box1.minZ < box.maxZ && box1.maxZ > box.minZ) { // inline math + action.accept(entityAccess2); + } } } - } } + // Mirai end public boolean isEmpty() { return this.storage.isEmpty();