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

236 lines
6.8 KiB
Java
Raw Normal View History

package techreborn.tiles.multiblock;
2015-04-28 00:51:06 +02:00
import net.minecraft.block.state.IBlockState;
2015-04-28 00:51:06 +02:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
2015-06-09 00:22:06 +02:00
import net.minecraft.inventory.ISidedInventory;
2015-04-28 00:51:06 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2015-06-25 17:40:39 +02:00
import net.minecraft.tileentity.TileEntity;
2015-11-23 15:45:16 +01:00
import net.minecraft.util.EnumFacing;
2016-03-15 09:13:05 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fluids.*;
import reborncore.api.power.EnumPowerTier;
import reborncore.api.recipe.IRecipeCrafterProvider;
import reborncore.api.tile.IInventoryProvider;
import reborncore.common.IWrenchable;
import reborncore.common.recipes.RecipeCrafter;
import reborncore.common.tile.TilePowerAcceptor;
2015-11-11 18:12:59 +01:00
import reborncore.common.util.FluidUtils;
2015-11-08 13:15:45 +01:00
import reborncore.common.util.Inventory;
import reborncore.common.util.Tank;
import techreborn.api.Reference;
2016-04-03 15:25:46 +02:00
import techreborn.api.recipe.ITileRecipeHandler;
import techreborn.api.recipe.machines.IndustrialGrinderRecipe;
2015-04-28 00:51:06 +02:00
import techreborn.init.ModBlocks;
import techreborn.init.ModFluids;
2015-04-28 00:51:06 +02:00
import static techreborn.tiles.multiblock.MultiblockChecker.*;
public class TileIndustrialGrinder extends TilePowerAcceptor implements IWrenchable, IFluidHandler, IInventoryProvider, ISidedInventory, ITileRecipeHandler<IndustrialGrinderRecipe>, IRecipeCrafterProvider {
2016-03-25 10:47:34 +01:00
public static final int TANK_CAPACITY = 16000;
public Inventory inventory = new Inventory(6, "TileGrinder", 64, this);
public Tank tank = new Tank("TileGrinder", TANK_CAPACITY, this);
public RecipeCrafter crafter;
public MultiblockChecker multiblockChecker;
2016-03-25 10:47:34 +01:00
public TileIndustrialGrinder() {
int[] inputs = new int[] {0};
int[] outputs = new int[] {2, 3, 4, 5};
2016-03-25 10:47:34 +01:00
crafter = new RecipeCrafter(Reference.industrialGrinderRecipe, this, 1, 4, inventory, inputs, outputs);
}
@Override
public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, EnumFacing side) {
return true;
2016-03-25 10:47:34 +01:00
}
@Override
public EnumFacing getFacing() {
2016-03-25 10:47:34 +01:00
return getFacingEnum();
}
@Override
public boolean wrenchCanRemove(EntityPlayer entityPlayer) {
return entityPlayer.isSneaking();
2016-03-25 10:47:34 +01:00
}
@Override
public float getWrenchDropRate() {
2016-03-25 10:47:34 +01:00
return 1.0F;
}
@Override
public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
2016-03-25 10:47:34 +01:00
return new ItemStack(ModBlocks.IndustrialGrinder, 1);
}
public boolean getMutliBlock() {
boolean down = multiblockChecker.checkRectY(1, 1, CASING_NORMAL, ZERO_OFFSET);
boolean up = multiblockChecker.checkRectY(1, 1, CASING_NORMAL, new BlockPos(0, 2, 0));
boolean blade = multiblockChecker.checkRingY(1, 1, CASING_REINFORCED, new BlockPos(0, 1, 0));
IBlockState centerBlock = multiblockChecker.getBlock(0, 1, 0);
boolean center = centerBlock.getBlock() == Blocks.WATER;
return down && center && blade && up;
2016-03-25 10:47:34 +01:00
}
@Override
public void update() {
super.update();
if(multiblockChecker == null) {
BlockPos pos = getPos().offset(getFacing().getOpposite(), 2).down();
multiblockChecker = new MultiblockChecker(worldObj, pos);
}
if (getMutliBlock()) {
2016-03-25 10:47:34 +01:00
crafter.updateEntity();
}
FluidUtils.drainContainers(this, inventory, 1, 5);
}
@Override
public void readFromNBT(NBTTagCompound tagCompound) {
2016-03-25 10:47:34 +01:00
super.readFromNBT(tagCompound);
tank.readFromNBT(tagCompound);
crafter.readFromNBT(tagCompound);
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tagCompound) {
2016-03-25 10:47:34 +01:00
super.writeToNBT(tagCompound);
tank.writeToNBT(tagCompound);
crafter.writeToNBT(tagCompound);
2016-05-18 17:53:54 +02:00
return tagCompound;
2016-03-25 10:47:34 +01:00
}
/* IFluidHandler */
@Override
public int fill(EnumFacing from, FluidStack resource, boolean doFill) {
if (resource != null && canFill(from, resource.getFluid())) {
2016-03-25 10:47:34 +01:00
int filled = tank.fill(resource, doFill);
tank.compareAndUpdate();
return filled;
}
return 0;
}
@Override
public FluidStack drain(EnumFacing from, FluidStack resource, boolean doDrain) {
if (resource == null || !resource.isFluidEqual(tank.getFluid())) {
2016-03-25 10:47:34 +01:00
return null;
}
FluidStack fluidStack = tank.drain(resource.amount, doDrain);
tank.compareAndUpdate();
return fluidStack;
}
@Override
public FluidStack drain(EnumFacing from, int maxDrain, boolean doDrain) {
2016-03-25 10:47:34 +01:00
FluidStack drained = tank.drain(maxDrain, doDrain);
tank.compareAndUpdate();
return drained;
}
@Override
public boolean canFill(EnumFacing from, Fluid fluid) {
return fluid == FluidRegistry.WATER || fluid == ModFluids.fluidMercury || fluid == ModFluids.fluidSodiumpersulfate;
2016-03-25 10:47:34 +01:00
}
@Override
public boolean canDrain(EnumFacing from, Fluid fluid) {
2016-03-25 10:47:34 +01:00
return false;
}
@Override
public FluidTankInfo[] getTankInfo(EnumFacing from) {
return new FluidTankInfo[]{tank.getInfo()};
2016-03-25 10:47:34 +01:00
}
// ISidedInventory
@Override
public int[] getSlotsForFace(EnumFacing side) {
return side == EnumFacing.DOWN ? new int[]{0, 1, 2, 3, 4, 5} : new int[]{0, 1, 2, 3, 4, 5};
2016-03-25 10:47:34 +01:00
}
@Override
public boolean canInsertItem(int slotIndex, ItemStack itemStack, EnumFacing side) {
2016-03-25 10:47:34 +01:00
if (slotIndex >= 2)
return false;
return isItemValidForSlot(slotIndex, itemStack);
}
@Override
public boolean canExtractItem(int slotIndex, ItemStack itemStack, EnumFacing side) {
2016-03-25 10:47:34 +01:00
return slotIndex == 2 || slotIndex == 3 || slotIndex == 4 || slotIndex == 5;
}
public int getProgressScaled(int scale) {
if (crafter.currentTickTime != 0) {
2016-03-25 10:47:34 +01:00
return crafter.currentTickTime * scale / crafter.currentNeededTicks;
}
return 0;
}
@Override
public double getMaxPower() {
return 64000;
2016-03-25 10:47:34 +01:00
}
@Override
public EnumPowerTier getTier() {
return EnumPowerTier.MEDIUM;
2016-03-25 10:47:34 +01:00
}
2016-04-03 15:25:46 +02:00
@Override
public boolean canCraft(TileEntity tile, IndustrialGrinderRecipe recipe) {
FluidStack recipeFluid = recipe.fluidStack;
FluidStack tankFluid = tank.getFluid();
if (recipe.fluidStack == null) {
return true;
}
if(tankFluid == null) {
return false;
}
if (tankFluid.isFluidEqual(recipeFluid)) {
if (tankFluid.amount >= recipeFluid.amount) {
return true;
}
}
return false;
}
2016-04-03 15:25:46 +02:00
@Override
public boolean onCraft(TileEntity tile, IndustrialGrinderRecipe recipe) {
FluidStack recipeFluid = recipe.fluidStack;
FluidStack tankFluid = tank.getFluid();
if (recipe.fluidStack == null) {
return true;
}
if(tankFluid == null) {
return false;
}
if (tankFluid.isFluidEqual(recipeFluid)) {
if (tankFluid.amount >= recipeFluid.amount) {
if(tankFluid.amount == recipeFluid.amount)
tank.setFluid(null);
else tankFluid.amount -= recipeFluid.amount;
return true;
}
}
return false;
2016-04-03 15:25:46 +02:00
}
@Override
public Inventory getInventory() {
return inventory;
}
@Override
public RecipeCrafter getRecipeCrafter() {
return crafter;
}
2015-04-28 00:51:06 +02:00
}