From 664a7cbf9d03c7f534b7f2a4c9891b680e410968 Mon Sep 17 00:00:00 2001 From: bl4ckscor3 Date: Sun, 26 May 2024 12:53:50 +0200 Subject: [PATCH] [1.20.4] Fix entities' item_handler_automation capability getting ignored in certain cases (#1008) --- .../neoforge/items/VanillaInventoryCodeHooks.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/neoforged/neoforge/items/VanillaInventoryCodeHooks.java b/src/main/java/net/neoforged/neoforge/items/VanillaInventoryCodeHooks.java index fbdc2e42fb..1ecc639c29 100644 --- a/src/main/java/net/neoforged/neoforge/items/VanillaInventoryCodeHooks.java +++ b/src/main/java/net/neoforged/neoforge/items/VanillaInventoryCodeHooks.java @@ -5,6 +5,7 @@ package net.neoforged.neoforge.items; +import java.util.Collections; import java.util.List; import java.util.Optional; import net.minecraft.core.BlockPos; @@ -209,10 +210,12 @@ private static Optional> getItemHandlerAt(Level world // Note: the isAlive check matches what vanilla does for hoppers in EntitySelector.CONTAINER_ENTITY_SELECTOR List list = worldIn.getEntities((Entity) null, new AABB(x - 0.5D, y - 0.5D, z - 0.5D, x + 0.5D, y + 0.5D, z + 0.5D), EntitySelector.ENTITY_STILL_ALIVE); if (!list.isEmpty()) { - var entity = list.get(worldIn.random.nextInt(list.size())); - var entityCap = entity.getCapability(Capabilities.ItemHandler.ENTITY_AUTOMATION, side); - if (entityCap != null) - return Optional.of(ImmutablePair.of(entityCap, entity)); + Collections.shuffle(list); + for (Entity entity : list) { + IItemHandler entityCap = entity.getCapability(Capabilities.ItemHandler.ENTITY_AUTOMATION, side); + if (entityCap != null) + return Optional.of(ImmutablePair.of(entityCap, entity)); + } } return Optional.empty();