Skip to content

Commit 80965a4

Browse files
committed
Fix handling of restricted slots in recipe transfers
1 parent 55b258f commit 80965a4

File tree

2 files changed

+33
-51
lines changed

2 files changed

+33
-51
lines changed

Common/src/main/java/mezz/jei/common/transfer/BasicRecipeTransferHandlerServer.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static void setItems(
6666
List<ItemStack> clearedCraftingItems = clearCraftingGrid(craftingSlots, player);
6767

6868
// put items into the crafting grid
69-
List<ItemStack> remainderItems = putItemsIntoCraftingGrid(recipeSlotToTakenStacks, requireCompleteSets, player);
69+
List<ItemStack> remainderItems = putItemsIntoCraftingGrid(recipeSlotToTakenStacks, requireCompleteSets);
7070

7171
// put leftover items back into the inventory
7272
stowItems(player, inventorySlots, clearedCraftingItems);
@@ -100,11 +100,13 @@ private static int getSlotStackLimit(
100100
private static List<ItemStack> clearCraftingGrid(List<Slot> craftingSlots, Player player) {
101101
List<ItemStack> clearedCraftingItems = new ArrayList<>();
102102
for (Slot craftingSlot : craftingSlots) {
103-
if (!craftingSlot.allowModification(player)) {
103+
if (!craftingSlot.mayPickup(player)) {
104104
continue;
105105
}
106-
if (craftingSlot.hasItem()) {
107-
ItemStack craftingItem = craftingSlot.remove(Integer.MAX_VALUE);
106+
107+
ItemStack item = craftingSlot.getItem();
108+
if (!item.isEmpty() && craftingSlot.mayPlace(item)) {
109+
ItemStack craftingItem = craftingSlot.safeTake(Integer.MAX_VALUE, Integer.MAX_VALUE, player);
108110
clearedCraftingItems.add(craftingItem);
109111
}
110112
}
@@ -113,20 +115,15 @@ private static List<ItemStack> clearCraftingGrid(List<Slot> craftingSlots, Playe
113115

114116
private static List<ItemStack> putItemsIntoCraftingGrid(
115117
Map<Slot, ItemStack> recipeSlotToTakenStacks,
116-
boolean requireCompleteSets,
117-
Player player
118+
boolean requireCompleteSets
118119
) {
119120
final int slotStackLimit = getSlotStackLimit(recipeSlotToTakenStacks, requireCompleteSets);
120121
List<ItemStack> remainderItems = new ArrayList<>();
121122

122123
recipeSlotToTakenStacks.forEach((slot, stack) -> {
123-
if (slot.getItem().isEmpty() && slot.allowModification(player) && slot.mayPlace(stack)) {
124-
ItemStack remainder = slot.safeInsert(stack, slotStackLimit);
125-
if (!remainder.isEmpty()) {
126-
remainderItems.add(remainder);
127-
}
128-
} else {
129-
remainderItems.add(stack);
124+
ItemStack remainder = slot.safeInsert(stack, slotStackLimit);
125+
if (!remainder.isEmpty()) {
126+
remainderItems.add(remainder);
130127
}
131128
});
132129

@@ -232,18 +229,18 @@ private static Map<Slot, ItemStack> removeOneSetOfItemsFromInventory(
232229
final Slot hint = entry.getValue().hint;
233230

234231
// Locate a slot that has what we need.
235-
final Slot slot = getSlotWithStack(player, requiredStack, craftingSlots, inventorySlots, hint)
232+
final Slot sourceSlot = getSlotWithStack(player, requiredStack, craftingSlots, inventorySlots, hint)
236233
.orElse(null);
237-
if (slot != null) {
234+
if (sourceSlot != null) {
238235
// the item was found
239236

240237
// Keep a copy of the slot's original contents in case we need to roll back.
241-
if (originalSlotContents != null && !originalSlotContents.containsKey(slot)) {
242-
originalSlotContents.put(slot, slot.getItem().copy());
238+
if (originalSlotContents != null && !originalSlotContents.containsKey(sourceSlot)) {
239+
originalSlotContents.put(sourceSlot, sourceSlot.getItem().copy());
243240
}
244241

245242
// Reduce the size of the found slot.
246-
ItemStack removedItemStack = slot.remove(1);
243+
ItemStack removedItemStack = sourceSlot.safeTake(1, Integer.MAX_VALUE, player);
247244
foundItemsInSet.put(recipeSlot, removedItemStack);
248245
} else {
249246
// We can't find any more slots to fulfill the requirements.
@@ -253,7 +250,8 @@ private static Map<Slot, ItemStack> removeOneSetOfItemsFromInventory(
253250
// slot changes we've made during this set iteration.
254251
for (Map.Entry<Slot, ItemStack> slotEntry : originalSlotContents.entrySet()) {
255252
ItemStack stack = slotEntry.getValue();
256-
slotEntry.getKey().set(stack);
253+
Slot slot = slotEntry.getKey();
254+
slot.set(stack);
257255
}
258256
return Map.of();
259257
}
@@ -291,10 +289,7 @@ private static Optional<Slot> getSlotWithStack(Player player, ItemStack stack, L
291289
}
292290

293291
private static Optional<Slot> getValidatedHintSlot(Player player, ItemStack stack, Slot hint) {
294-
if (hint.mayPickup(player) &&
295-
!hint.getItem().isEmpty() &&
296-
ItemStack.isSameItemSameComponents(stack, hint.getItem())
297-
) {
292+
if (isValidAndMatches(player, hint, stack)) {
298293
return Optional.of(hint);
299294
}
300295

@@ -303,7 +298,7 @@ private static Optional<Slot> getValidatedHintSlot(Player player, ItemStack stac
303298

304299
private static void stowItems(Player player, List<Slot> inventorySlots, List<ItemStack> itemStacks) {
305300
for (ItemStack itemStack : itemStacks) {
306-
ItemStack remainder = stowItem(inventorySlots, itemStack);
301+
ItemStack remainder = stowItem(player, inventorySlots, itemStack);
307302
if (!remainder.isEmpty()) {
308303
if (!player.getInventory().add(remainder)) {
309304
player.drop(remainder, false);
@@ -312,18 +307,21 @@ private static void stowItems(Player player, List<Slot> inventorySlots, List<Ite
312307
}
313308
}
314309

315-
private static ItemStack stowItem(Collection<Slot> slots, ItemStack stack) {
310+
private static ItemStack stowItem(Player player, Collection<Slot> slots, ItemStack stack) {
316311
if (stack.isEmpty()) {
317312
return ItemStack.EMPTY;
318313
}
319314

320-
final ItemStack remainder = stack.copy();
315+
ItemStack remainder = stack.copy();
321316

322317
// Add to existing stacks first
323318
for (Slot slot : slots) {
319+
if (!slot.mayPickup(player)) {
320+
continue;
321+
}
324322
final ItemStack inventoryStack = slot.getItem();
325323
if (!inventoryStack.isEmpty() && inventoryStack.isStackable()) {
326-
slot.safeInsert(remainder);
324+
remainder = slot.safeInsert(remainder);
327325
if (remainder.isEmpty()) {
328326
return ItemStack.EMPTY;
329327
}
@@ -333,7 +331,7 @@ private static ItemStack stowItem(Collection<Slot> slots, ItemStack stack) {
333331
// Try adding to empty slots
334332
for (Slot slot : slots) {
335333
if (slot.getItem().isEmpty()) {
336-
slot.safeInsert(remainder);
334+
remainder = slot.safeInsert(remainder);
337335
if (remainder.isEmpty()) {
338336
return ItemStack.EMPTY;
339337
}
@@ -352,13 +350,15 @@ private static ItemStack stowItem(Collection<Slot> slots, ItemStack stack) {
352350
*/
353351
private static Optional<Slot> getSlotWithStack(Player player, Collection<Slot> slots, ItemStack itemStack) {
354352
return slots.stream()
355-
.filter(slot -> {
356-
ItemStack slotStack = slot.getItem();
357-
return ItemStack.isSameItemSameComponents(itemStack, slotStack) &&
358-
slot.mayPickup(player);
359-
})
353+
.filter(slot -> isValidAndMatches(player, slot, itemStack))
360354
.findFirst();
361355
}
362356

357+
private static boolean isValidAndMatches(Player player, Slot slot, ItemStack stack) {
358+
ItemStack containedStack = slot.getItem();
359+
return ItemStack.isSameItemSameComponents(stack, containedStack) &&
360+
slot.allowModification(player);
361+
}
362+
363363
private record ItemStackWithSlotHint(Slot hint, ItemStack stack) {}
364364
}

Common/src/main/java/mezz/jei/common/transfer/RecipeTransferUtil.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -141,24 +141,6 @@ public static boolean validateSlots(
141141
}
142142
}
143143

144-
// check that all slots are interactable (can be picked up, and not output slots)
145-
{
146-
List<Integer> invalidModificationSlots = Stream.concat(
147-
craftingSlots.stream(),
148-
inventorySlots.stream()
149-
)
150-
.filter(s -> !s.allowModification(player))
151-
.map(slot -> slot.index)
152-
.toList();
153-
if (!invalidModificationSlots.isEmpty()) {
154-
LOGGER.error(
155-
"Transfer request has invalid slots, they do not allow modification: {}",
156-
StringUtil.intsToString(invalidModificationSlots)
157-
);
158-
return false;
159-
}
160-
}
161-
162144
// check that all slots are real (not output slots)
163145
{
164146
List<Integer> invalidFakeSlots = Stream.concat(

0 commit comments

Comments
 (0)