Clean Up Carpenter and Fabricator RecipeManagers

This commit is contained in:
Nedelosk committed 2017-06-23 18:50:51 +02:00
1 parent 7b7e7eb7e7
commit 81e2e18089
5 files changed
+126 -79

No files matched your search

@@ -0,0 +1,42 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.core.recipes;
import javax.annotation.Nullable;
import forestry.api.recipes.IForestryRecipe;
public class RecipePair<R extends IForestryRecipe> {
public static final RecipePair EMPTY = new RecipePair(null, null);
@Nullable
private final R recipe;
@Nullable
private final String[][] oreDictEntries;
public RecipePair(R recipe, String[][] oreDictEntries) {
this.recipe = recipe;
this.oreDictEntries = oreDictEntries;
}
public boolean isEmpty(){
return recipe == null;
}
public R getRecipe() {
return recipe;
}
public String[][] getOreDictEntries() {
return oreDictEntries;
}
}
@@ -11,23 +11,23 @@
package forestry.factory.recipes;
import javax.annotation.Nullable;
import org.apache.commons.lang3.tuple.Pair;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import forestry.api.recipes.ICarpenterManager;
import forestry.api.recipes.ICarpenterRecipe;
import forestry.api.recipes.IDescriptiveRecipe;
import forestry.core.recipes.RecipePair;
import forestry.core.recipes.RecipeUtil;
import forestry.core.recipes.ShapedRecipeCustom;
import forestry.core.utils.ItemStackUtil;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
public class CarpenterRecipeManager implements ICarpenterManager {
@@ -51,14 +51,14 @@ public class CarpenterRecipeManager implements ICarpenterManager {
}
@Nullable
public static Pair<ICarpenterRecipe, String[][]> findMatchingRecipe(@Nullable FluidStack liquid, ItemStack item, IInventory inventorycrafting) {
public static RecipePair<ICarpenterRecipe> findMatchingRecipe(@Nullable FluidStack liquid, ItemStack item, IInventory inventorycrafting) {
for (ICarpenterRecipe recipe : recipes) {
String[][] resourceDicts = matches(recipe, liquid, item, inventorycrafting);
if (resourceDicts != null) {
return Pair.of(recipe, resourceDicts);
return new RecipePair(recipe, resourceDicts);
}
}
return Pair.of(null, null);
return RecipePair.EMPTY;
}
public static String[][] matches(@Nullable ICarpenterRecipe recipe, @Nullable FluidStack resource, ItemStack item, IInventory inventoryCrafting) {
@@ -11,26 +11,25 @@
package forestry.factory.recipes;
import javax.annotation.Nullable;
import org.apache.commons.lang3.tuple.Pair;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import forestry.api.recipes.IFabricatorManager;
import forestry.api.recipes.IFabricatorRecipe;
import forestry.core.recipes.RecipeUtil;
import forestry.core.recipes.ShapedRecipeCustom;
import forestry.core.utils.ItemStackUtil;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraftforge.fluids.FluidStack;
public class FabricatorRecipeManager implements IFabricatorManager {
import forestry.api.recipes.IFabricatorManager;
import forestry.api.recipes.IFabricatorRecipe;
import forestry.core.recipes.RecipePair;
import forestry.core.recipes.RecipeUtil;
import forestry.core.recipes.ShapedRecipeCustom;
import forestry.core.utils.ItemStackUtil;
public static final Pair EMPTY_RECIPE = Pair.of(null, null);
public class FabricatorRecipeManager implements IFabricatorManager {
private static final Set<IFabricatorRecipe> recipes = new HashSet<>();
@Override
@@ -43,7 +42,7 @@ public class FabricatorRecipeManager implements IFabricatorManager {
}
@Nullable
public static Pair<IFabricatorRecipe, String[][]> findMatchingRecipe(ItemStack plan, IInventory resources) {
public static RecipePair<IFabricatorRecipe> findMatchingRecipe(ItemStack plan, IInventory resources) {
ItemStack[][] gridResources = RecipeUtil.getResources(resources);
for (IFabricatorRecipe recipe : recipes) {
@@ -52,11 +51,11 @@ public class FabricatorRecipeManager implements IFabricatorManager {
}
String[][] oreDicts = RecipeUtil.matches(recipe.getIngredients(), recipe.getOreDicts(), recipe.getWidth(), recipe.getHeight(), gridResources);
if (oreDicts != null) {
return Pair.of(recipe, oreDicts);
return new RecipePair<>(recipe, oreDicts);
}
}
return EMPTY_RECIPE;
return RecipePair.EMPTY;
}
public static boolean isPlan(ItemStack plan) {
@@ -13,26 +13,6 @@ package forestry.factory.tiles;
import javax.annotation.Nullable;
import java.io.IOException;
import forestry.api.core.IErrorLogic;
import forestry.api.recipes.ICarpenterRecipe;
import forestry.core.config.Constants;
import forestry.core.errors.EnumErrorCode;
import forestry.core.fluids.FilteredTank;
import forestry.core.fluids.FluidHelper;
import forestry.core.fluids.TankManager;
import forestry.core.inventory.InventoryAdapterTile;
import forestry.core.inventory.wrappers.InventoryMapper;
import forestry.core.network.PacketBufferForestry;
import forestry.core.render.TankRenderInfo;
import forestry.core.tiles.IItemStackDisplay;
import forestry.core.tiles.ILiquidTankTile;
import forestry.core.tiles.TilePowered;
import forestry.core.utils.InventoryUtil;
import forestry.factory.gui.ContainerCarpenter;
import forestry.factory.gui.GuiCarpenter;
import forestry.factory.inventory.InventoryCarpenter;
import forestry.factory.inventory.InventoryGhostCrafting;
import forestry.factory.recipes.CarpenterRecipeManager;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
@@ -43,12 +23,35 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.NonNullList;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.apache.commons.lang3.tuple.Pair;
import forestry.api.core.IErrorLogic;
import forestry.api.recipes.ICarpenterRecipe;
import forestry.core.config.Constants;
import forestry.core.errors.EnumErrorCode;
import forestry.core.fluids.FilteredTank;
import forestry.core.fluids.FluidHelper;
import forestry.core.fluids.TankManager;
import forestry.core.inventory.InventoryAdapterTile;
import forestry.core.inventory.wrappers.InventoryMapper;
import forestry.core.network.PacketBufferForestry;
import forestry.core.recipes.RecipePair;
import forestry.core.render.TankRenderInfo;
import forestry.core.tiles.IItemStackDisplay;
import forestry.core.tiles.ILiquidTankTile;
import forestry.core.tiles.TilePowered;
import forestry.core.utils.InventoryUtil;
import forestry.factory.gui.ContainerCarpenter;
import forestry.factory.gui.GuiCarpenter;
import forestry.factory.inventory.InventoryCarpenter;
import forestry.factory.inventory.InventoryGhostCrafting;
import forestry.factory.recipes.CarpenterRecipeManager;
public class TileCarpenter extends TilePowered implements ISidedInventory, ILiquidTankTile, IItemStackDisplay {
private static final int TICKS_PER_RECIPE_TIME = 1;
@@ -118,11 +121,11 @@ public class TileCarpenter extends TilePowered implements ISidedInventory, ILiqu
}
if (CarpenterRecipeManager.matches(currentRecipe, resourceTank.getFluid(), getBoxStack(), craftingInventory) == null) {
Pair<ICarpenterRecipe, String[][]> recipePair = CarpenterRecipeManager.findMatchingRecipe(resourceTank.getFluid(), getBoxStack(), craftingInventory);
currentRecipe = recipePair.getLeft();
oreDicts = recipePair.getRight();
RecipePair<ICarpenterRecipe> recipePair = CarpenterRecipeManager.findMatchingRecipe(resourceTank.getFluid(), getBoxStack(), craftingInventory);
currentRecipe = recipePair.getRecipe();
oreDicts = recipePair.getOreDictEntries();
if (currentRecipe != null) {
if (!recipePair.isEmpty()) {
int recipeTime = currentRecipe.getPackagingTime();
setTicksPerWorkCycle(recipeTime * TICKS_PER_RECIPE_TIME);
setEnergyPerWorkCycle(recipeTime * ENERGY_PER_RECIPE_TIME);
@@ -13,6 +13,26 @@ package forestry.factory.tiles;
import javax.annotation.Nullable;
import java.io.IOException;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IContainerListener;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.NonNullList;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.api.core.IErrorLogic;
import forestry.api.recipes.IFabricatorRecipe;
import forestry.api.recipes.IFabricatorSmeltingRecipe;
@@ -27,6 +47,7 @@ import forestry.core.inventory.watchers.ISlotPickupWatcher;
import forestry.core.inventory.wrappers.InventoryMapper;
import forestry.core.items.ICraftingPlan;
import forestry.core.network.PacketBufferForestry;
import forestry.core.recipes.RecipePair;
import forestry.core.tiles.ILiquidTankTile;
import forestry.core.tiles.TilePowered;
import forestry.core.utils.InventoryUtil;
@@ -36,24 +57,6 @@ import forestry.factory.inventory.InventoryFabricator;
import forestry.factory.inventory.InventoryGhostCrafting;
import forestry.factory.recipes.FabricatorRecipeManager;
import forestry.factory.recipes.FabricatorSmeltingRecipeManager;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IContainerListener;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.NonNullList;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.apache.commons.lang3.tuple.Pair;
public class TileFabricator extends TilePowered implements ISlotPickupWatcher, ILiquidTankTile, ISidedInventory {
private static final int MAX_HEAT = 5000;
@@ -166,20 +169,20 @@ public class TileFabricator extends TilePowered implements ISlotPickupWatcher, I
}
@Nullable
private Pair<IFabricatorRecipe, String[][]> getRecipe() {
private RecipePair getRecipe() {
IInventoryAdapter inventory = getInternalInventory();
ItemStack plan = inventory.getStackInSlot(InventoryFabricator.SLOT_PLAN);
FluidStack liquid = moltenTank.getFluid();
Pair<IFabricatorRecipe, String[][]> recipePair = FabricatorRecipeManager.findMatchingRecipe(plan, craftingInventory);
IFabricatorRecipe recipe = recipePair.getLeft();
RecipePair<IFabricatorRecipe> recipePair = FabricatorRecipeManager.findMatchingRecipe(plan, craftingInventory);
IFabricatorRecipe recipe = recipePair.getRecipe();
if (liquid != null && recipe != null && !liquid.containsFluid(recipe.getLiquid())) {
return FabricatorRecipeManager.EMPTY_RECIPE;
return RecipePair.EMPTY;
}
return recipePair;
}
public ItemStack getResult(Pair<IFabricatorRecipe, String[][]> myRecipePair) {
IFabricatorRecipe myRecipe = myRecipePair.getLeft();
public ItemStack getResult(RecipePair<IFabricatorRecipe> myRecipePair) {
IFabricatorRecipe myRecipe = myRecipePair.getRecipe();
if (myRecipe == null) {
return ItemStack.EMPTY;
}
@@ -196,18 +199,18 @@ public class TileFabricator extends TilePowered implements ISlotPickupWatcher, I
}
private void craftResult() {
Pair<IFabricatorRecipe, String[][]> myRecipePair = getRecipe();
RecipePair<IFabricatorRecipe> myRecipePair = getRecipe();
ItemStack craftResult = getResult(myRecipePair);
IFabricatorRecipe myRecipe = myRecipePair.getLeft();
IFabricatorRecipe myRecipe = myRecipePair.getRecipe();
if (myRecipe != null && !craftResult.isEmpty() && getStackInSlot(InventoryFabricator.SLOT_RESULT).isEmpty()) {
FluidStack liquid = myRecipe.getLiquid();
// Remove resources
NonNullList<ItemStack> crafting = InventoryUtil.getStacks(craftingInventory, InventoryGhostCrafting.SLOT_CRAFTING_1, InventoryGhostCrafting.SLOT_CRAFTING_COUNT);
if (removeFromInventory(crafting, myRecipePair.getRight(), myRecipe, false)) {
if (removeFromInventory(crafting, myRecipePair.getOreDictEntries(), myRecipe, false)) {
FluidStack drained = moltenTank.drainInternal(liquid, false);
if (drained != null && drained.isFluidStackIdentical(liquid)) {
removeFromInventory(crafting, myRecipePair.getRight(), myRecipe, true);
removeFromInventory(crafting, myRecipePair.getOreDictEntries(), myRecipe, true);
moltenTank.drain(liquid.amount, true);
// Damage plan
@@ -237,11 +240,11 @@ public class TileFabricator extends TilePowered implements ISlotPickupWatcher, I
boolean hasResources = true;
ItemStack plan = getStackInSlot(InventoryFabricator.SLOT_PLAN);
Pair<IFabricatorRecipe, String[][]> recipePair = FabricatorRecipeManager.findMatchingRecipe(plan, craftingInventory);
IFabricatorRecipe recipe = recipePair.getLeft();
RecipePair<IFabricatorRecipe> recipePair = FabricatorRecipeManager.findMatchingRecipe(plan, craftingInventory);
IFabricatorRecipe recipe = recipePair.getRecipe();
if (recipe != null) {
NonNullList<ItemStack> crafting = InventoryUtil.getStacks(craftingInventory, InventoryGhostCrafting.SLOT_CRAFTING_1, InventoryGhostCrafting.SLOT_CRAFTING_COUNT);
hasResources = removeFromInventory(crafting, recipePair.getRight(), recipe, false);
hasResources = removeFromInventory(crafting, recipePair.getOreDictEntries(), recipe, false);
FluidStack toDrain = recipe.getLiquid();
FluidStack drained = moltenTank.drainInternal(toDrain, false);
hasLiquidResources = drained != null && drained.isFluidStackIdentical(toDrain);