mirror of
https://github.com/GTNewHorizons/ForestryMC.git
synced 2026-11-25 14:24:42 +00:00
Refactor out worktable canCraftRecipe logic to RecipeUtil
This commit is contained in:
1 parent
e281cb4591
commit
099f33549b
2 files changed
+61
-53
No files matched your search
@@ -12,12 +12,19 @@ package forestry.core.utils;
|
||||
|
||||
import forestry.api.recipes.RecipeManagers;
|
||||
import forestry.core.config.Defaults;
|
||||
import forestry.core.gui.ContainerDummy;
|
||||
import forestry.core.interfaces.IDescriptiveRecipe;
|
||||
import forestry.core.proxy.Proxies;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.inventory.InventoryCrafting;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.CraftingManager;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
public class RecipeUtil {
|
||||
|
||||
private static final Container DUMMY_CONTAINER = new ContainerDummy();
|
||||
|
||||
public static void injectLeveledRecipe(ItemStack resource, int fermentationValue, String output) {
|
||||
if (RecipeManagers.fermenterManager == null)
|
||||
return;
|
||||
@@ -67,4 +74,53 @@ public class RecipeUtil {
|
||||
result[9] = output;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static boolean canCraftRecipe(World world, ItemStack[] recipeItems, ItemStack recipeOutput, ItemStack[] availableItems) {
|
||||
// Need at least one matched set
|
||||
if (StackUtils.containsSets(recipeItems, availableItems, true, true) == 0)
|
||||
return false;
|
||||
|
||||
// Check that it doesn't make a different recipe.
|
||||
// For example:
|
||||
// Wood Logs are all ore dictionary equivalent with each other,
|
||||
// but an Oak Log shouldn't be able to make Ebony Wood Planks
|
||||
// because it makes Oak Wood Planks using the same recipe.
|
||||
// Strategy:
|
||||
// Create a fake crafting inventory using items we have in availableItems
|
||||
// in place of the ones in the saved crafting inventory.
|
||||
// Check that the recipe it makes is the same as the currentRecipe.
|
||||
InventoryCrafting crafting = new InventoryCrafting(DUMMY_CONTAINER, 3, 3);
|
||||
ItemStack[] stockCopy = StackUtils.condenseStacks(availableItems);
|
||||
|
||||
for (int slot = 0; slot < recipeItems.length; slot++) {
|
||||
ItemStack recipeStack = recipeItems[slot];
|
||||
if (recipeStack == null)
|
||||
continue;
|
||||
|
||||
// Use crafting equivalent (not oredict) items first
|
||||
for (ItemStack stockStack : stockCopy) {
|
||||
if (stockStack.stackSize > 0 && StackUtils.isCraftingEquivalent(recipeStack, stockStack, false, false)) {
|
||||
ItemStack stack = new ItemStack(stockStack.getItem(), 1, stockStack.getItemDamage());
|
||||
stockStack.stackSize--;
|
||||
crafting.setInventorySlotContents(slot, stack);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Use oredict items if crafting equivalent items aren't available
|
||||
if (crafting.getStackInSlot(slot) == null) {
|
||||
for (ItemStack stockStack : stockCopy) {
|
||||
if (stockStack.stackSize > 0 && StackUtils.isCraftingEquivalent(recipeStack, stockStack, true, true)) {
|
||||
ItemStack stack = new ItemStack(stockStack.getItem(), 1, stockStack.getItemDamage());
|
||||
stockStack.stackSize--;
|
||||
crafting.setInventorySlotContents(slot, stack);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ItemStack output = CraftingManager.getInstance().findMatchingRecipe(crafting, world);
|
||||
|
||||
return ItemStack.areItemStacksEqual(output, recipeOutput);
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,6 @@ package forestry.factory.gadgets;
|
||||
|
||||
import forestry.api.core.ForestryAPI;
|
||||
import forestry.core.gadgets.TileBase;
|
||||
import forestry.core.gui.ContainerDummy;
|
||||
import forestry.core.interfaces.ICrafter;
|
||||
import forestry.core.network.ForestryPacket;
|
||||
import forestry.core.network.GuiId;
|
||||
@@ -21,11 +20,10 @@ import forestry.core.network.PacketTileNBT;
|
||||
import forestry.core.network.PacketTileUpdate;
|
||||
import forestry.core.proxy.Proxies;
|
||||
import forestry.core.utils.InventoryAdapter;
|
||||
import forestry.core.utils.StackUtils;
|
||||
import forestry.core.utils.RecipeUtil;
|
||||
import forestry.core.utils.TileInventoryAdapter;
|
||||
import forestry.factory.recipes.RecipeMemory;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.inventory.InventoryCrafting;
|
||||
import net.minecraft.item.ItemStack;
|
||||
@@ -40,7 +38,6 @@ public class TileWorktable extends TileBase implements ICrafter {
|
||||
public final static int SLOT_CRAFTING_RESULT = 9;
|
||||
public final static short SLOT_INVENTORY_1 = 0;
|
||||
public final static short SLOT_INVENTORY_COUNT = 18;
|
||||
private final static Container DUMMY_CONTAINER = new ContainerDummy();
|
||||
|
||||
/* MEMBERS */
|
||||
private RecipeMemory.Recipe currentRecipe;
|
||||
@@ -156,56 +153,11 @@ public class TileWorktable extends TileBase implements ICrafter {
|
||||
if (currentRecipe == null)
|
||||
return false;
|
||||
|
||||
// Need at least one matched set
|
||||
ItemStack[] set = craftingInventory.getStacks(SLOT_CRAFTING_1, SLOT_CRAFTING_COUNT);
|
||||
ItemStack[] stock = accessibleInventory.getStacks(SLOT_INVENTORY_1, SLOT_INVENTORY_COUNT);
|
||||
if (StackUtils.containsSets(set, stock, true, true) == 0)
|
||||
return false;
|
||||
ItemStack[] recipeItems = craftingInventory.getStacks(SLOT_CRAFTING_1, SLOT_CRAFTING_COUNT);
|
||||
ItemStack[] inventory = accessibleInventory.getStacks(SLOT_INVENTORY_1, SLOT_INVENTORY_COUNT);
|
||||
ItemStack recipeOutput = currentRecipe.getRecipeOutput(worldObj);
|
||||
|
||||
// Check that it doesn't make a different recipe.
|
||||
// For example:
|
||||
// Wood Logs are all ore dictionary equivalent with each other,
|
||||
// but an Oak Log shouldn't be able to make Ebony Wood Planks
|
||||
// because it makes Oak Wood Planks using the same recipe.
|
||||
// Strategy:
|
||||
// Create a fake crafting inventory using items we have in stock
|
||||
// in place of the ones in the saved crafting inventory.
|
||||
// Check that the recipe it makes is the same as the currentRecipe.
|
||||
InventoryCrafting crafting = new InventoryCrafting(DUMMY_CONTAINER, 3, 3);
|
||||
ItemStack[] stockCopy = StackUtils.condenseStacks(stock);
|
||||
|
||||
for (int slot = 0; slot < currentCrafting.getSizeInventory(); slot++) {
|
||||
ItemStack recipeStack = currentCrafting.getStackInSlot(slot);
|
||||
if (recipeStack == null)
|
||||
continue;
|
||||
|
||||
// Use crafting equivalent (not oredict) items first
|
||||
for (ItemStack stockStack : stockCopy) {
|
||||
if (stockStack.stackSize > 0 && StackUtils.isCraftingEquivalent(recipeStack, stockStack, false, false)) {
|
||||
ItemStack stack = new ItemStack(stockStack.getItem(), 1, stockStack.getItemDamage());
|
||||
stockStack.stackSize--;
|
||||
crafting.setInventorySlotContents(slot, stack);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Use oredict items if crafting equivalent items aren't available
|
||||
if (crafting.getStackInSlot(slot) == null) {
|
||||
for (ItemStack stockStack : stockCopy) {
|
||||
if (stockStack.stackSize > 0 && StackUtils.isCraftingEquivalent(recipeStack, stockStack, true, true)) {
|
||||
ItemStack stack = new ItemStack(stockStack.getItem(), 1, stockStack.getItemDamage());
|
||||
stockStack.stackSize--;
|
||||
crafting.setInventorySlotContents(slot, stack);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ItemStack recipeOutput = CraftingManager.getInstance().findMatchingRecipe(crafting, worldObj);
|
||||
if (recipeOutput == null)
|
||||
return false;
|
||||
|
||||
return ItemStack.areItemStacksEqual(recipeOutput, currentRecipe.getRecipeOutput(worldObj));
|
||||
return RecipeUtil.canCraftRecipe(worldObj, recipeItems, recipeOutput, inventory);
|
||||
}
|
||||
|
||||
private boolean removeResources(EntityPlayer player) {
|
||||
|
||||
Reference in new issue
Block a user