Skip to content

Commit

Permalink
shuffle list instead of randomly picking and removing entries
Browse files Browse the repository at this point in the history
  • Loading branch information
bl4ckscor3 committed May 26, 2024
1 parent d9276f0 commit b5e2796
Showing 1 changed file with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -209,15 +210,12 @@ private static Optional<Pair<IItemHandler, Object>> getItemHandlerAt(Level world
// Note: the isAlive check matches what vanilla does for hoppers in EntitySelector.CONTAINER_ENTITY_SELECTOR
List<Entity> 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()) {
Entity entity;
IItemHandler entityCap;
do {
entity = list.remove(worldIn.random.nextInt(list.size()));
entityCap = entity.getCapability(Capabilities.ItemHandler.ENTITY_AUTOMATION, side);
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));
}
while (entityCap != null && !list.isEmpty());
if (entityCap != null)
return Optional.of(ImmutablePair.of(entityCap, entity));
}

return Optional.empty();
Expand Down

0 comments on commit b5e2796

Please sign in to comment.