Rewrite the sawmill logic, fixes #1180

This commit is contained in:
modmuss50 2017-07-03 20:26:55 +01:00
parent 407aa3ed66
commit 79a8fa647b
No known key found for this signature in database
GPG key ID: 203A5ED4D3E48BEA

View file

@ -34,13 +34,18 @@ import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.oredict.OreDictionary; import reborncore.api.recipe.IBaseRecipeType;
import reborncore.api.recipe.RecipeHandler;
import reborncore.api.tile.IInventoryProvider; import reborncore.api.tile.IInventoryProvider;
import reborncore.common.IWrenchable; import reborncore.common.IWrenchable;
import reborncore.common.powerSystem.TilePowerAcceptor; import reborncore.common.powerSystem.TilePowerAcceptor;
import reborncore.common.util.FluidUtils; import reborncore.common.util.FluidUtils;
import reborncore.common.util.Inventory; import reborncore.common.util.Inventory;
import reborncore.common.util.ItemUtils;
import reborncore.common.util.Tank; import reborncore.common.util.Tank;
import techreborn.api.Reference;
import techreborn.api.recipe.BaseRecipe;
import techreborn.api.recipe.machines.IndustrialSawmillRecipe;
import techreborn.client.container.IContainerProvider; import techreborn.client.container.IContainerProvider;
import techreborn.client.container.builder.BuiltContainer; import techreborn.client.container.builder.BuiltContainer;
import techreborn.client.container.builder.ContainerBuilder; import techreborn.client.container.builder.ContainerBuilder;
@ -58,6 +63,7 @@ public class TileIndustrialSawmill extends TilePowerAcceptor
public int tickTime; public int tickTime;
public MultiblockChecker multiblockChecker; public MultiblockChecker multiblockChecker;
IndustrialSawmillRecipe smRecipe;
public TileIndustrialSawmill() { public TileIndustrialSawmill() {
super(); super();
@ -71,38 +77,68 @@ public class TileIndustrialSawmill extends TilePowerAcceptor
final ItemStack wood = this.inventory.getStackInSlot(0); final ItemStack wood = this.inventory.getStackInSlot(0);
if (this.tickTime == 0) { if (this.tickTime == 0) {
if (!wood.isEmpty()) { if (!wood.isEmpty()) {
for (final int id : OreDictionary.getOreIDs(wood)) { for (IBaseRecipeType recipe : RecipeHandler.getRecipeClassFromName(Reference.industrialSawmillRecipe)) {
final String name = OreDictionary.getOreName(id); if (recipe instanceof IndustrialSawmillRecipe) {//Should always be true
if (name.equals("logWood") && this.canAddOutput(2, 10) && this.canAddOutput(3, 5) IndustrialSawmillRecipe sawmillRecipe = (IndustrialSawmillRecipe) recipe;
&& this.canAddOutput(4, 3) && this.canUseEnergy(128.0F) && !this.tank.isEmpty() Object woodRecipeObj = sawmillRecipe.getInputs().get(0);
&& this.tank.getFluid().amount >= 1000) { if (isValidForRecipe(sawmillRecipe)) {
wood.shrink(1); this.useEnergy(sawmillRecipe.euPerTick);
if (wood.getCount() == 0) this.syncWithAll();
this.setInventorySlotContents(0, ItemStack.EMPTY); this.tickTime = 1;
this.tank.drain(1000, true); smRecipe = sawmillRecipe;
this.useEnergy(128.0F); }
this.syncWithAll();
this.tickTime = 1;
} }
} }
} }
} else if (++this.tickTime > 100) { } else if (smRecipe != null) {
final Random rnd = this.world.rand; if (canUseEnergy(smRecipe.euPerTick) && !tank.isEmpty() && tank.getFluid().amount >= 10 && isValidForRecipe(smRecipe)) {
this.addOutput(2, new ItemStack(Blocks.PLANKS, 6 + rnd.nextInt(4))); tickTime++;
if (rnd.nextInt(4) != 0) { this.tank.drain(10, true);
final ItemStack pulp = ItemDusts.getDustByName("sawDust", 2 + rnd.nextInt(3));
this.addOutput(3, pulp);
} }
if (rnd.nextInt(3) == 0) { if (tickTime > smRecipe.tickTime() && isValidForRecipe(smRecipe)) {
final ItemStack paper = new ItemStack(Items.PAPER, 1 + rnd.nextInt(2)); final Random rnd = this.world.rand;
this.addOutput(4, paper); this.addOutput(2, smRecipe.getOutput(0));
Object woodRecipeObj = smRecipe.getInputs().get(0);
if (woodRecipeObj instanceof ItemStack) {
ItemStack woodStack = (ItemStack) woodRecipeObj;
wood.shrink(woodStack.getCount());
} else {
wood.shrink(1);
}
if (rnd.nextInt(4) != 0) {
final ItemStack pulp = ItemDusts.getDustByName("sawDust", 2 + rnd.nextInt(3)).copy();
if (this.canAddOutput(2, pulp.getCount())) {
this.addOutput(3, pulp);
}
}
if (rnd.nextInt(3) == 0) {
final ItemStack paper = new ItemStack(Items.PAPER, 1 + rnd.nextInt(2)).copy();
if (this.canAddOutput(4, paper.getCount())) {
this.addOutput(4, paper);
}
}
this.tickTime = 0;
} else if (!isValidForRecipe(smRecipe)) {
smRecipe = null;
tickTime = 0;
} }
this.tickTime = 0; }
if (smRecipe == null) {
tickTime = 0;
} }
} }
FluidUtils.drainContainers(this.tank, this.inventory, 1, 4); FluidUtils.drainContainers(this.tank, this.inventory, 1, 4);
} }
public boolean isValidForRecipe(BaseRecipe baseRecipe) {
ItemStack inputStack = this.inventory.getStackInSlot(0);
int inputCount = 1;
if (baseRecipe.getInputs().get(0) instanceof ItemStack) {
inputCount = ((ItemStack) baseRecipe.getInputs().get(0)).getCount();
}
return ItemUtils.isInputEqual(baseRecipe.getInputs().get(0), inputStack, true, true, false) && inputStack.getCount() >= inputCount && this.canAddOutput(2, baseRecipe.getOutput(0).getCount()) && !this.tank.isEmpty();
}
public void addOutput(final int slot, final ItemStack stack) { public void addOutput(final int slot, final ItemStack stack) {
if (this.getStackInSlot(slot) == ItemStack.EMPTY) if (this.getStackInSlot(slot) == ItemStack.EMPTY)
this.setInventorySlotContents(slot, stack); this.setInventorySlotContents(slot, stack);