Skip to content

Commit

Permalink
Fix up possible problem with slots that don't properly get owned by t…
Browse files Browse the repository at this point in the history
…heir container?

Signed-off-by: cpw <cpw+github@weeksfamily.ca>
  • Loading branch information
cpw committed Jun 17, 2024
1 parent 9dfce2c commit 99a2650
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/main/java/cpw/mods/inventorysorter/ContainerContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.*;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.neoforged.neoforge.items.SlotItemHandler;
import org.apache.logging.log4j.*;

import java.util.*;
Expand All @@ -26,10 +27,21 @@ class ContainerContext
static boolean validSlot(Slot slot) {
// Skip slots without an inventory - they're probably dummy slots
return slot != null && slot.container != null
// don't skip slots that are slotitemhandlers with a container size > 0
&& (slot instanceof SlotItemHandler || slot.container.getContainerSize() > 0)
// Skip blacklisted slots
&& !InventorySorter.INSTANCE.isSlotBlacklisted(slot);
}

private Container findMatchingContainer(final Map<Container, InventoryHandler.InventoryMapping> mapping, final Slot sl) {
for (Map.Entry<Container, InventoryHandler.InventoryMapping> container : mapping.entrySet()) {
if (sl.isSameInventory(container.getValue().slot)) {
return container.getKey();
}
}
return sl.container;
}

public ContainerContext(Slot slot, ServerPlayer playerEntity)
{
this.slot = slot;
Expand All @@ -38,7 +50,8 @@ public ContainerContext(Slot slot, ServerPlayer playerEntity)
final AbstractContainerMenu openContainer = playerEntity.containerMenu;
openContainer.slots.stream().filter(ContainerContext::validSlot).forEach(sl->
{
final InventoryHandler.InventoryMapping inventoryMapping = mapping.computeIfAbsent(sl.container, k -> new InventoryHandler.InventoryMapping(sl.container, openContainer, sl.container, sl));
final Container matchingContainer = findMatchingContainer(mapping, sl);
final InventoryHandler.InventoryMapping inventoryMapping = mapping.computeIfAbsent(matchingContainer, k -> new InventoryHandler.InventoryMapping(matchingContainer, openContainer, matchingContainer, sl));
inventoryMapping.addSlot(sl);
if (sl == slot)
{
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/cpw/mods/inventorysorter/InventoryHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public enum InventoryHandler
{
INSTANCE;
public final Method mergeStack = getMergeStackMethod();
private final Method mergeStack = getMergeStackMethod();

private Method getMergeStackMethod()
{
Expand Down Expand Up @@ -93,11 +93,12 @@ void moveItemToOtherInventory(ContainerContext ctx, ItemStack is, int targetLow,
}
}

static Map<Container,ImmutableList<Container>> preferredOrders = ImmutableMap.of(
static final Map<Container,ImmutableList<Container>> preferredOrders = ImmutableMap.of(
ContainerContext.PLAYER_HOTBAR, ImmutableList.of(ContainerContext.PLAYER_OFFHAND, ContainerContext.PLAYER_MAIN),
ContainerContext.PLAYER_OFFHAND, ImmutableList.of(ContainerContext.PLAYER_HOTBAR, ContainerContext.PLAYER_MAIN),
ContainerContext.PLAYER_MAIN, ImmutableList.of(ContainerContext.PLAYER_OFFHAND, ContainerContext.PLAYER_HOTBAR)
);

Slot findStackWithItem(ItemStack is, final ContainerContext ctx)
{
if (is.getMaxStackSize() == 1) return null;
Expand Down

0 comments on commit 99a2650

Please sign in to comment.