Skip to content

Commit

Permalink
Fix bug with recipe transfer for modded crafting tables
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Dec 5, 2015
1 parent 0519306 commit 65b5751
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
Expand Up @@ -3,8 +3,8 @@
import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.minecraft.entity.player.EntityPlayer;
Expand All @@ -18,14 +18,14 @@
public class PacketRecipeTransfer extends PacketJEI {

private Map<Integer, ItemStack> recipeMap;
private Collection<Integer> craftingSlots;
private Collection<Integer> inventorySlots;
private List<Integer> craftingSlots;
private List<Integer> inventorySlots;

public PacketRecipeTransfer() {

}

public PacketRecipeTransfer(@Nonnull Map<Integer, ItemStack> recipeMap, @Nonnull Collection<Integer> craftingSlots, @Nonnull Collection<Integer> inventorySlots) {
public PacketRecipeTransfer(@Nonnull Map<Integer, ItemStack> recipeMap, @Nonnull List<Integer> craftingSlots, @Nonnull List<Integer> inventorySlots) {
this.recipeMap = recipeMap;
this.craftingSlots = craftingSlots;
this.inventorySlots = inventorySlots;
Expand Down
49 changes: 32 additions & 17 deletions src/main/java/mezz/jei/util/RecipeTransferUtil.java
Expand Up @@ -3,10 +3,12 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
Expand Down Expand Up @@ -101,40 +103,52 @@ private static boolean transferRecipe(@Nonnull RecipeLayout recipeLayout, @Nonnu
return false;
}

List<Integer> craftingSlotIndexes = new ArrayList<>(craftingSlots.keySet());
Collections.sort(craftingSlotIndexes);

List<Integer> inventorySlotIndexes = new ArrayList<>(inventorySlots.keySet());
Collections.sort(inventorySlotIndexes);

// check that the slots exist and can be altered
for (Map.Entry<Integer, ItemStack> entry : slotMap.entrySet()) {
int slotIndex = entry.getKey();
if (slotIndex >= container.inventorySlots.size()) {
int craftNumber = entry.getKey();
int slotNumber = craftingSlotIndexes.get(craftNumber);
if (slotNumber >= container.inventorySlots.size()) {
return false;
}
Slot slot = container.getSlot(slotIndex);
Slot slot = container.getSlot(slotNumber);
ItemStack stack = entry.getValue();
if (slot == null || !slot.isItemValid(stack)) {
return false;
}
}

if (doTransfer) {
PacketRecipeTransfer packet = new PacketRecipeTransfer(slotMap, craftingSlots.keySet(), inventorySlots.keySet());
PacketRecipeTransfer packet = new PacketRecipeTransfer(slotMap, craftingSlotIndexes, inventorySlotIndexes);
JustEnoughItems.common.sendPacketToServer(packet);
}

return true;
}

/**
* Build slot map (Crafting Slot Index -> ItemStack) for the recipe.
* Build slot map (Crafting Slot Number -> ItemStack) for the recipe.
* Based on slot position info from itemStackGroup the ingredients from the player's inventory in matchingStacks.
*/
@Nullable
private static Map<Integer, ItemStack> buildSlotMap(@Nonnull GuiItemStackGroup itemStackGroup, @Nonnull List<ItemStack> matchingStacks) {
Map<Integer, ItemStack> slotMap = new HashMap<>();
Map<Integer, GuiIngredient<ItemStack>> ingredientsMap = itemStackGroup.getGuiIngredients();

int recipeSlotNumber = -1;
SortedSet<Integer> keys = new TreeSet<>(ingredientsMap.keySet());
for (Integer key : keys) {

for (Map.Entry<Integer, GuiIngredient<ItemStack>> entry : itemStackGroup.getGuiIngredients().entrySet()) {
GuiIngredient<ItemStack> guiIngredient = entry.getValue();
GuiIngredient<ItemStack> guiIngredient = ingredientsMap.get(key);
if (!guiIngredient.isInput()) {
continue;
}
recipeSlotNumber++;

List<ItemStack> requiredStacks = guiIngredient.getAll();
if (requiredStacks.isEmpty()) {
Expand All @@ -143,7 +157,7 @@ private static Map<Integer, ItemStack> buildSlotMap(@Nonnull GuiItemStackGroup i

ItemStack matchingStack = StackUtil.containsStack(matchingStacks, requiredStacks);
if (matchingStack != null) {
slotMap.put(entry.getKey(), matchingStack);
slotMap.put(recipeSlotNumber, matchingStack);
matchingStacks.remove(matchingStack);
} else {
return null;
Expand All @@ -153,7 +167,7 @@ private static Map<Integer, ItemStack> buildSlotMap(@Nonnull GuiItemStackGroup i
return slotMap;
}

public static void setItems(@Nonnull EntityPlayer player, @Nonnull Map<Integer, ItemStack> slotMap, @Nonnull Collection<Integer> craftingSlots, @Nonnull Collection<Integer> inventorySlots) {
public static void setItems(@Nonnull EntityPlayer player, @Nonnull Map<Integer, ItemStack> slotMap, @Nonnull List<Integer> craftingSlots, @Nonnull List<Integer> inventorySlots) {
Container container = player.openContainer;

// remove required recipe items
Expand Down Expand Up @@ -184,8 +198,8 @@ public static void setItems(@Nonnull EntityPlayer player, @Nonnull Map<Integer,

// clear the crafting grid
List<ItemStack> clearedCraftingItems = new ArrayList<>();
for (Integer craftingSlotIndex : craftingSlots) {
Slot craftingSlot = container.getSlot(craftingSlotIndex);
for (Integer craftingSlotNumber : craftingSlots) {
Slot craftingSlot = container.getSlot(craftingSlotNumber);
if (craftingSlot != null && craftingSlot.getHasStack()) {
ItemStack craftingItem = craftingSlot.decrStackSize(Integer.MAX_VALUE);
clearedCraftingItems.add(craftingItem);
Expand All @@ -195,8 +209,9 @@ public static void setItems(@Nonnull EntityPlayer player, @Nonnull Map<Integer,
// put items into the crafting grid
for (Map.Entry<Integer, ItemStack> entry : slotMap.entrySet()) {
ItemStack stack = entry.getValue();
Integer slotIndex = entry.getKey();
Slot slot = container.getSlot(slotIndex);
Integer craftNumber = entry.getKey();
Integer slotNumber = craftingSlots.get(craftNumber);
Slot slot = container.getSlot(slotNumber);
slot.putStack(stack);
}

Expand Down Expand Up @@ -250,9 +265,9 @@ private static List<ItemStack> getMatchingItems(@Nonnull List<ItemStack> availab
}

@Nullable
private static Slot getSlotWithStack(@Nonnull Container container, @Nonnull Iterable<Integer> slotIndexes, @Nonnull ItemStack stack) {
for (Integer slotIndex : slotIndexes) {
Slot slot = container.getSlot(slotIndex);
private static Slot getSlotWithStack(@Nonnull Container container, @Nonnull Iterable<Integer> slotNumbers, @Nonnull ItemStack stack) {
for (Integer slotNumber : slotNumbers) {
Slot slot = container.getSlot(slotNumber);
if (slot != null) {
ItemStack slotStack = slot.getStack();
if (StackUtil.isIdentical(stack, slotStack)) {
Expand Down

0 comments on commit 65b5751

Please sign in to comment.