Add a config to lock the rolling machine recipe + some optimisations #1412

This commit is contained in:
modmuss50 2018-01-31 13:47:48 +00:00
parent a3d2aa9907
commit 0b8f139e6d
2 changed files with 43 additions and 17 deletions

View file

@ -145,6 +145,16 @@ public class RollingMachineRecipe {
return ItemStack.EMPTY; return ItemStack.EMPTY;
} }
public IRecipe findMatchingRecipeObj(InventoryCrafting inv, World world) {
for (IRecipe irecipe : recipes.values()) {
if (irecipe.matches(inv, world)) {
return irecipe;
}
}
return null;
}
public HashMap<ResourceLocation, IRecipe> getRecipeList() { public HashMap<ResourceLocation, IRecipe> getRecipeList() {
return recipes; return recipes;
} }

View file

@ -28,6 +28,7 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container; import net.minecraft.inventory.Container;
import net.minecraft.inventory.InventoryCrafting; import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import reborncore.api.IToolDrop; import reborncore.api.IToolDrop;
@ -44,7 +45,7 @@ import techreborn.client.container.builder.ContainerBuilder;
import techreborn.init.ModBlocks; import techreborn.init.ModBlocks;
import techreborn.lib.ModInfo; import techreborn.lib.ModInfo;
import javax.annotation.Nonnull; import javax.annotation.Nullable;
//TODO add tick and power bars. //TODO add tick and power bars.
@ -67,10 +68,15 @@ public class TileRollingMachine extends TilePowerAcceptor
public Inventory inventory = new Inventory(3, "TileRollingMachine", 64, this); public Inventory inventory = new Inventory(3, "TileRollingMachine", 64, this);
public boolean isRunning; public boolean isRunning;
public int tickTime; public int tickTime;
@Nonnull @Nullable
public ItemStack currentRecipe; public IRecipe currentRecipe;
private int outputSlot; private int outputSlot;
//TODO make this per machine and set in the gui.
//Set this to true to lock the recipe
@ConfigRegistry(config = "machines", category = "rolling_machine", key = "lockRecipe", comment = "This locks 1 of each ingredient in the crafting matrix, this may allow for some automation.")
public static boolean lockRecipe = false;
public TileRollingMachine() { public TileRollingMachine() {
super(); super();
outputSlot = 0; outputSlot = 0;
@ -106,21 +112,20 @@ public class TileRollingMachine extends TilePowerAcceptor
super.update(); super.update();
this.charge(2); this.charge(2);
if (!this.world.isRemote) { if (!this.world.isRemote) {
this.currentRecipe = RollingMachineRecipe.instance.findMatchingRecipe(this.craftMatrix, this.world); this.currentRecipe = RollingMachineRecipe.instance.findMatchingRecipeObj(this.craftMatrix, this.world);
if (!this.currentRecipe.isEmpty() && this.canMake()) { if (!this.currentRecipe.getRecipeOutput().isEmpty() && this.canMake()) {
if (this.tickTime >= TileRollingMachine.runTime) { if (this.tickTime >= TileRollingMachine.runTime) {
this.currentRecipe = RollingMachineRecipe.instance.findMatchingRecipe(this.craftMatrix, this.world); if (!this.currentRecipe.getRecipeOutput().isEmpty()) {
if (!this.currentRecipe.isEmpty()) {
boolean hasCrafted = false; boolean hasCrafted = false;
if (this.inventory.getStackInSlot(outputSlot) == ItemStack.EMPTY) { if (this.inventory.getStackInSlot(outputSlot) == ItemStack.EMPTY) {
this.inventory.setInventorySlotContents(outputSlot, this.currentRecipe); this.inventory.setInventorySlotContents(outputSlot, this.currentRecipe.getRecipeOutput());
this.tickTime = 0; this.tickTime = 0;
hasCrafted = true; hasCrafted = true;
} else { } else {
if (this.inventory.getStackInSlot(outputSlot).getCount() + this.currentRecipe.getCount() <= this.currentRecipe if (this.inventory.getStackInSlot(outputSlot).getCount() + this.currentRecipe.getRecipeOutput().getCount() <= this.currentRecipe.getRecipeOutput()
.getMaxStackSize()) { .getMaxStackSize()) {
final ItemStack stack = this.inventory.getStackInSlot(outputSlot); final ItemStack stack = this.inventory.getStackInSlot(outputSlot);
stack.setCount(stack.getCount() + this.currentRecipe.getCount()); stack.setCount(stack.getCount() + this.currentRecipe.getRecipeOutput().getCount());
this.inventory.setInventorySlotContents(outputSlot, stack); this.inventory.setInventorySlotContents(outputSlot, stack);
this.tickTime = 0; this.tickTime = 0;
hasCrafted = true; hasCrafted = true;
@ -130,24 +135,24 @@ public class TileRollingMachine extends TilePowerAcceptor
for (int i = 0; i < this.craftMatrix.getSizeInventory(); i++) { for (int i = 0; i < this.craftMatrix.getSizeInventory(); i++) {
this.craftMatrix.decrStackSize(i, 1); this.craftMatrix.decrStackSize(i, 1);
} }
this.currentRecipe = ItemStack.EMPTY; this.currentRecipe = null;
} }
} }
} }
} }
if (!this.currentRecipe.isEmpty()) { if (this.currentRecipe != null) {
if (this.canUseEnergy(energyPerTick) && this.tickTime < TileRollingMachine.runTime && canMake()) { if (this.canUseEnergy(energyPerTick) && this.tickTime < TileRollingMachine.runTime && canMake()) {
this.useEnergy(energyPerTick); this.useEnergy(energyPerTick);
this.tickTime++; this.tickTime++;
} }
} }
if (this.currentRecipe.isEmpty()) { if (this.currentRecipe == null) {
this.tickTime = 0; this.tickTime = 0;
} }
} else { } else {
this.currentRecipe = RollingMachineRecipe.instance.findMatchingRecipe(this.craftMatrix, this.world); this.currentRecipe = RollingMachineRecipe.instance.findMatchingRecipeObj(this.craftMatrix, this.world);
if (!this.currentRecipe.isEmpty()) { if (this.currentRecipe == null) {
this.inventory.setInventorySlotContents(1, this.currentRecipe); this.inventory.setInventorySlotContents(1, this.currentRecipe.getRecipeOutput());
} else { } else {
this.inventory.setInventorySlotContents(1, ItemStack.EMPTY); this.inventory.setInventorySlotContents(1, ItemStack.EMPTY);
} }
@ -155,10 +160,20 @@ public class TileRollingMachine extends TilePowerAcceptor
} }
public boolean canMake() { public boolean canMake() {
ItemStack stack = RollingMachineRecipe.instance.findMatchingRecipe(this.craftMatrix, this.world); ItemStack stack = currentRecipe.getRecipeOutput();
if(stack.isEmpty()){ if(stack.isEmpty()){
return false; return false;
} }
if(lockRecipe){
for (int i = 0; i < 9; i++) {
ItemStack ingredient = craftMatrix.getStackInSlot(i);
if(!ingredient.isEmpty()) {
if(ingredient.getCount() == 1){
return false;
}
}
}
}
ItemStack output = getStackInSlot(outputSlot); ItemStack output = getStackInSlot(outputSlot);
if(output.isEmpty()){ if(output.isEmpty()){
return true; return true;
@ -241,4 +256,5 @@ public class TileRollingMachine extends TilePowerAcceptor
} }
} }
} }