From f85410d12bd6c65d26c43af0f89a250289f62772 Mon Sep 17 00:00:00 2001 From: Spottedleaf Date: Sat, 10 Jun 2023 13:42:03 -0700 Subject: [PATCH] Fix incorrect collision shape for hopper item suck It shouldn't cost too much more to check the correct shape, provided that it is cached and we use the overall AABB to collect possible entities to check against. The issues with the old check code is that it will use two getEntitiesOfClass calls plus the addition of streams _and_ the toAabbs() logic on VoxelShape. The new code caches toAabbs, uses one getEntitiesOfClass call, and does not use streams. Then compared to 1.12, we are only performing two additional AABB checks per item. --- patches/server/Optimize-Hoppers.patch | 33 +++++++++++++++++++++------ 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/patches/server/Optimize-Hoppers.patch b/patches/server/Optimize-Hoppers.patch index 66e145c8cd..0198d1f7cf 100644 --- a/patches/server/Optimize-Hoppers.patch +++ b/patches/server/Optimize-Hoppers.patch @@ -510,19 +510,38 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 flag = true; } else if (HopperBlockEntity.canMergeItems(itemstack1, stack)) { @@ -0,0 +0,0 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen + // CraftBukkit end } ++ // Paper start - optimize hopper item suck in ++ static final AABB HOPPER_ITEM_SUCK_OVERALL = Hopper.SUCK.bounds(); ++ static final AABB[] HOPPER_ITEM_SUCK_INDIVIDUAL = Hopper.SUCK.toAabbs().toArray(new AABB[0]); ++ // Paper end - optimize hopper item suck in ++ public static List getItemsAtAndAbove(Level world, Hopper hopper) { - return (List) hopper.getSuckShape().toAabbs().stream().flatMap((axisalignedbb) -> { - return world.getEntitiesOfClass(ItemEntity.class, axisalignedbb.move(hopper.getLevelX() - 0.5D, hopper.getLevelY() - 0.5D, hopper.getLevelZ() - 0.5D), EntitySelector.ENTITY_STILL_ALIVE).stream(); - }).collect(Collectors.toList()); -+ // Paper start - Optimize item suck in. remove streams, restore 1.12 checks. Seriously checking the bowl?! -+ double x = hopper.getLevelX(); -+ double y = hopper.getLevelY(); -+ double z = hopper.getLevelZ(); -+ AABB bb = new AABB(x - 0.5D, y, z - 0.5D, x + 0.5D, y + 1.5D, z + 0.5D); -+ return world.getEntitiesOfClass(ItemEntity.class, bb, Entity::isAlive); -+ // Paper end ++ // Paper start - optimize hopper item suck in ++ // eliminate multiple getEntitiesOfClass() but maintain the voxelshape collision by moving ++ // the individual AABB checks into the predicate ++ final double shiftX = hopper.getLevelX() - 0.5D; ++ final double shiftY = hopper.getLevelY() - 0.5D; ++ final double shiftZ = hopper.getLevelZ() - 0.5D; ++ return world.getEntitiesOfClass(ItemEntity.class, HOPPER_ITEM_SUCK_OVERALL.move(shiftX, shiftY, shiftZ), (final Entity entity) -> { ++ if (!entity.isAlive()) { // EntitySelector.ENTITY_STILL_ALIVE ++ return false; ++ } ++ ++ for (final AABB aabb : HOPPER_ITEM_SUCK_INDIVIDUAL) { ++ if (aabb.move(shiftX, shiftY, shiftZ).intersects(entity.getBoundingBox())) { ++ return true; ++ } ++ } ++ ++ return false; ++ }); ++ // Paper end - optimize hopper item suck in } @Nullable