TechReborn/src/main/java/techreborn/tiles/multiblock/TileIndustrialSawmill.java

293 lines
9.4 KiB
Java
Raw Normal View History

/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2017 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
2016-07-23 20:10:37 +02:00
package techreborn.tiles.multiblock;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
2017-07-03 21:26:55 +02:00
import reborncore.api.recipe.IBaseRecipeType;
import reborncore.api.recipe.RecipeHandler;
2016-07-23 20:10:37 +02:00
import reborncore.api.tile.IInventoryProvider;
import reborncore.common.IWrenchable;
import reborncore.common.powerSystem.TilePowerAcceptor;
2016-07-23 20:10:37 +02:00
import reborncore.common.util.FluidUtils;
import reborncore.common.util.Inventory;
2017-07-03 21:26:55 +02:00
import reborncore.common.util.ItemUtils;
2016-07-23 20:10:37 +02:00
import reborncore.common.util.Tank;
2017-07-03 21:26:55 +02:00
import techreborn.api.Reference;
import techreborn.api.recipe.BaseRecipe;
import techreborn.api.recipe.machines.IndustrialSawmillRecipe;
import techreborn.client.container.IContainerProvider;
import techreborn.client.container.builder.BuiltContainer;
import techreborn.client.container.builder.ContainerBuilder;
2016-07-23 20:10:37 +02:00
import techreborn.init.ModBlocks;
import techreborn.items.ItemDusts;
import java.util.Random;
public class TileIndustrialSawmill extends TilePowerAcceptor
2017-06-12 15:23:32 +02:00
implements IWrenchable, IInventoryProvider, IContainerProvider {
2017-04-07 17:22:37 +02:00
public static final int TANK_CAPACITY = 16000;
2016-07-23 20:10:37 +02:00
2016-10-08 21:46:16 +02:00
public Inventory inventory = new Inventory(5, "Sawmill", 64, this);
2017-04-07 17:22:37 +02:00
public Tank tank = new Tank("Sawmill", TileIndustrialSawmill.TANK_CAPACITY, this);
2016-07-23 20:10:37 +02:00
public int tickTime;
public MultiblockChecker multiblockChecker;
2017-07-03 21:26:55 +02:00
IndustrialSawmillRecipe smRecipe;
2016-07-23 20:10:37 +02:00
public TileIndustrialSawmill() {
2017-06-13 21:28:50 +02:00
super();
}
2016-07-23 20:10:37 +02:00
@Override
public void update() {
super.update();
if (this.getMutliBlock() && !world.isRemote) {
final ItemStack wood = this.inventory.getStackInSlot(0);
if (this.tickTime == 0) {
if (!wood.isEmpty()) {
2017-07-03 21:26:55 +02:00
for (IBaseRecipeType recipe : RecipeHandler.getRecipeClassFromName(Reference.industrialSawmillRecipe)) {
if (recipe instanceof IndustrialSawmillRecipe) {//Should always be true
IndustrialSawmillRecipe sawmillRecipe = (IndustrialSawmillRecipe) recipe;
Object woodRecipeObj = sawmillRecipe.getInputs().get(0);
if (isValidForRecipe(sawmillRecipe)) {
this.useEnergy(sawmillRecipe.euPerTick);
this.tickTime = 1;
smRecipe = sawmillRecipe;
}
2016-10-08 21:46:16 +02:00
}
}
}
2017-07-03 21:26:55 +02:00
} else if (smRecipe != null) {
if (canUseEnergy(smRecipe.euPerTick) && !tank.isEmpty() && tank.getFluid().amount >= 10 && isValidForRecipe(smRecipe)) {
tickTime++;
this.tank.drain(10, true);
2016-10-08 21:46:16 +02:00
}
2017-07-03 21:26:55 +02:00
if (tickTime > smRecipe.tickTime() && isValidForRecipe(smRecipe)) {
final Random rnd = this.world.rand;
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;
2016-10-08 21:46:16 +02:00
}
2017-07-03 21:26:55 +02:00
}
if (smRecipe == null) {
tickTime = 0;
2016-10-08 21:46:16 +02:00
}
}
FluidUtils.drainContainers(this.tank, this.inventory, 1, 4);
2016-07-23 20:10:37 +02:00
}
2017-07-03 21:26:55 +02:00
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) {
if (this.getStackInSlot(slot) == ItemStack.EMPTY)
this.setInventorySlotContents(slot, stack);
this.getStackInSlot(slot).grow(stack.getCount());
2016-10-08 21:46:16 +02:00
}
2016-07-23 20:10:37 +02:00
public boolean canAddOutput(final int slot, final int amount) {
final ItemStack stack = this.getStackInSlot(slot);
return stack == ItemStack.EMPTY || this.getInventoryStackLimit() - stack.getCount() >= amount;
2016-10-08 21:46:16 +02:00
}
2016-07-23 20:10:37 +02:00
@Override
public void validate() {
super.validate();
this.multiblockChecker = new MultiblockChecker(this.world, this.getPos().down(3));
2016-07-23 20:10:37 +02:00
}
public boolean getMutliBlock() {
final boolean down = this.multiblockChecker.checkRectY(1, 1, MultiblockChecker.STANDARD_CASING,
2017-06-12 15:23:32 +02:00
MultiblockChecker.ZERO_OFFSET);
final boolean up = this.multiblockChecker.checkRectY(1, 1, MultiblockChecker.STANDARD_CASING,
2017-06-12 15:23:32 +02:00
new BlockPos(0, 2, 0));
final boolean blade = this.multiblockChecker.checkRingY(1, 1, MultiblockChecker.REINFORCED_CASING,
2017-06-12 15:23:32 +02:00
new BlockPos(0, 1, 0));
final IBlockState centerBlock = this.multiblockChecker.getBlock(0, 1, 0);
final boolean center = centerBlock.getBlock() == Blocks.WATER;
2016-10-08 21:46:16 +02:00
return down && center && blade && up;
2016-07-23 20:10:37 +02:00
}
@Override
public boolean wrenchCanSetFacing(final EntityPlayer entityPlayer, final EnumFacing side) {
2016-07-23 20:10:37 +02:00
return false;
}
@Override
public EnumFacing getFacing() {
return this.getFacingEnum();
2016-07-23 20:10:37 +02:00
}
@Override
public boolean wrenchCanRemove(final EntityPlayer entityPlayer) {
2016-07-23 20:10:37 +02:00
return entityPlayer.isSneaking();
}
@Override
public float getWrenchDropRate() {
return 1.0F;
}
@Override
public ItemStack getWrenchDrop(final EntityPlayer entityPlayer) {
return new ItemStack(ModBlocks.INDUSTRIAL_SAWMILL, 1);
2016-07-23 20:10:37 +02:00
}
@Override
public void readFromNBT(final NBTTagCompound tagCompound) {
2016-07-23 20:10:37 +02:00
super.readFromNBT(tagCompound);
this.tank.readFromNBT(tagCompound);
this.tickTime = tagCompound.getInteger("tickTime");
2016-07-23 20:10:37 +02:00
}
@Override
public NBTTagCompound writeToNBT(final NBTTagCompound tagCompound) {
2016-07-23 20:10:37 +02:00
super.writeToNBT(tagCompound);
this.tank.writeToNBT(tagCompound);
tagCompound.setInteger("tickTime", this.tickTime);
2016-07-23 20:10:37 +02:00
return tagCompound;
}
@Override
public boolean hasCapability(final Capability<?> capability, final EnumFacing facing) {
2016-11-25 14:25:51 +01:00
if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) {
return true;
2016-07-23 20:10:37 +02:00
}
return super.hasCapability(capability, facing);
2016-07-23 20:10:37 +02:00
}
@Override
public <T> T getCapability(final Capability<T> capability, final EnumFacing facing) {
2016-11-25 14:25:51 +01:00
if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) {
return (T) this.tank;
2016-07-23 20:10:37 +02:00
}
return super.getCapability(capability, facing);
2016-07-23 20:10:37 +02:00
}
2016-10-08 21:46:16 +02:00
@Override
public int[] getSlotsForFace(final EnumFacing side) {
2017-07-03 20:52:28 +02:00
return new int[] { 0, 2, 3, 4 };
2016-10-08 21:46:16 +02:00
}
2016-07-23 20:10:37 +02:00
2016-10-08 21:46:16 +02:00
@Override
public boolean canInsertItem(final int index, final ItemStack itemStackIn, final EnumFacing direction) {
2016-10-08 21:46:16 +02:00
return index == 0;
}
2016-07-23 20:10:37 +02:00
2016-10-08 21:46:16 +02:00
@Override
public boolean canExtractItem(final int index, final ItemStack stack, final EnumFacing direction) {
2016-10-08 21:46:16 +02:00
return index >= 2;
}
2016-07-23 20:10:37 +02:00
public int getProgressScaled(final int scale) {
if (this.tickTime != 0) {
return this.tickTime * scale / 100;
2016-07-23 20:10:37 +02:00
}
return 0;
}
@Override
2017-04-11 20:12:32 +02:00
public double getBaseMaxPower() {
2016-07-23 20:10:37 +02:00
return 64000;
}
@Override
public boolean canAcceptEnergy(final EnumFacing direction) {
return true;
}
@Override
public boolean canProvideEnergy(final EnumFacing direction) {
return false;
}
@Override
2017-04-11 20:12:32 +02:00
public double getBaseMaxOutput() {
return 0;
}
@Override
2017-04-11 20:12:32 +02:00
public double getBaseMaxInput() {
return 64;
}
2016-07-23 20:10:37 +02:00
@Override
public Inventory getInventory() {
return this.inventory;
2016-07-23 20:10:37 +02:00
}
public int getTickTime() {
return tickTime;
}
public void setTickTime(int tickTime) {
this.tickTime = tickTime;
}
@Override
public BuiltContainer createContainer(final EntityPlayer player) {
return new ContainerBuilder("industrialsawmill").player(player.inventory).inventory(8, 84).hotbar(8, 142)
2017-06-12 15:23:32 +02:00
.addInventory().tile(this).slot(0, 32, 26).slot(1, 32, 44).outputSlot(2, 84, 35).outputSlot(3, 102, 35)
.outputSlot(4, 120, 35).syncEnergyValue().syncIntegerValue(this::getTickTime, this::setTickTime).addInventory().create();
}
2016-07-23 20:10:37 +02:00
}