package com.projectreddog.machinemod.tileentities;
import com.projectreddog.machinemod.init.ModBlocks;
import com.projectreddog.machinemod.reference.Reference;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.fluids.FluidEvent;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidTank;
public class TileEntityAsphaltMixer extends TileEntity implements ITickable, IFluidTank, ISidedInventory {
public final int maxOilStorage = 1000; // store up to 100k
public final int inventorySize = 6;
protected ItemStack[] inventory;
private static int[] sideSlots = new int[] { 0 };
public AxisAlignedBB boundingBox;
public int coolDownAmount = 5;
public int timeTillCoolDown = 0;
public final int BlastedStoneOreMultiplier = 3;
public final int VanillaOreMultiplier = 2;
public final int BlastedStoneCoalMultiplier = 3;
public final int BlastedStoneGemMultiplier = 2;
public final int BlastedStoneLapisMultiplier = 12;
public final int BlastedStoneRedstoneMultiplier = 8;
protected FluidStack fluid;// = new FluidStack(ModBlocks.fluidOil, 0);
public int transferOilAmount = 10;
public boolean firstTick = true;
public int remainBurnTime = 0;
public int fluidLevelAbove[];
public TileEntityAsphaltMixer() {
inventory = new ItemStack[inventorySize];
fluidLevelAbove = new int[1];
}
@Override
public void update() {
if (!worldObj.isRemote) {
if (firstTick) {
firstTick = !firstTick;
}
if (timeTillCoolDown > 0) {
timeTillCoolDown--;
return;
}
timeTillCoolDown = coolDownAmount;
// LogHelper.info("TE update entity called");
// boundingBox = new AxisAlignedBB(this.pos.north(3).west(3).down(1), this.pos.south(3).east(3).up(1));
// List list = worldObj.getEntitiesWithinAABB(EntitySemiTractor.class, boundingBox);
// processEntitiesInList(list);
} else {
// is not do nothing !
}
}
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
timeTillCoolDown = compound.getInteger(Reference.MACHINE_MOD_NBT_PREFIX + "COOLDOWN");
if (!compound.hasKey("Empty")) {
FluidStack fluid = FluidStack.loadFluidStackFromNBT(compound);
setFluid(fluid);
} else {
setFluid(null);
}
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
compound.setInteger(Reference.MACHINE_MOD_NBT_PREFIX + "COOLDOWN", timeTillCoolDown);
if (fluid != null) {
fluid.writeToNBT(compound);
} else {
compound.setString("Empty", "");
}
return compound;
}
@Override
public FluidStack getFluid() {
return fluid;
}
@Override
public int getFluidAmount() {
if (fluid == null) {
return 0;
}
return fluid.amount;
}
@Override
public int getCapacity() {
return this.maxOilStorage;
}
@Override
public FluidTankInfo getInfo() {
return new FluidTankInfo(this);
}
public void setFluid(FluidStack fluid) {
this.fluid = fluid;
}
@Override
public int fill(FluidStack resource, boolean doFill) {
if (resource == null) {
return 0;
}
if (!(resource.isFluidEqual(new FluidStack(ModBlocks.fluidBitumen, 1)))) {
return 0;
}
if (!doFill) {
if (fluid == null) {
return Math.min(maxOilStorage, resource.amount);
}
if (!fluid.isFluidEqual(resource)) {
return 0;
}
return Math.min(maxOilStorage - fluid.amount, resource.amount);
}
if (fluid == null) {
fluid = new FluidStack(resource, Math.min(maxOilStorage, resource.amount));
if (this != null) {
FluidEvent.fireEvent(new FluidEvent.FluidFillingEvent(fluid, this.getWorld(), this.getPos(), this, fluid.amount));
}
return fluid.amount;
}
if (!fluid.isFluidEqual(resource)) {
return 0;
}
int filled = maxOilStorage - fluid.amount;
if (resource.amount < filled) {
fluid.amount += resource.amount;
filled = resource.amount;
} else {
fluid.amount = maxOilStorage;
}
if (this != null) {
FluidEvent.fireEvent(new FluidEvent.FluidFillingEvent(fluid, this.getWorld(), this.getPos(), this, filled));
}
return filled;
}
@Override
public FluidStack drain(int maxDrain, boolean doDrain) {
if (fluid == null) {
return null;
}
int drained = maxDrain;
if (fluid.amount < drained) {
drained = fluid.amount;
}
FluidStack stack = new FluidStack(fluid, drained);
if (doDrain) {
fluid.amount -= drained;
if (fluid.amount <= 0) {
fluid = null;
}
if (this != null) {
FluidEvent.fireEvent(new FluidEvent.FluidDrainingEvent(fluid, this.getWorld(), this.getPos(), this, drained));
}
}
return stack;
}
@Override
public int getSizeInventory() {
return inventory.length;
}
@Override
public ItemStack getStackInSlot(int slot) {
return inventory[slot];
}
@Override
public ItemStack decrStackSize(int slot, int amt) {
ItemStack stack = getStackInSlot(slot);
if (stack != null) {
if (stack.stackSize <= amt) {
setInventorySlotContents(slot, null);
} else {
stack = stack.splitStack(amt);
if (stack.stackSize == 0) {
setInventorySlotContents(slot, null);
}
}
}
return stack;
}
@Override
public ItemStack removeStackFromSlot(int slot) {
ItemStack stack = getStackInSlot(slot);
if (stack != null) {
setInventorySlotContents(slot, null);
}
return stack;
}
@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
inventory[slot] = stack;
if (stack != null && stack.stackSize > getInventoryStackLimit()) {
stack.stackSize = getInventoryStackLimit();
}
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public void openInventory(EntityPlayer playerIn) {
}
@Override
public void closeInventory(EntityPlayer playerIn) {
}
@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {
return true;
}
@Override
public void clear() {
for (int i = 0; i < inventory.length; ++i) {
inventory[i] = null;
}
}
@Override
public String getName() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean hasCustomName() {
// TODO Auto-generated method stub
return false;
}
@Override
public ITextComponent getDisplayName() {
// TODO Auto-generated method stub
return null;
}
@Override
public int[] getSlotsForFace(EnumFacing side) {
if (side == EnumFacing.NORTH || side == EnumFacing.SOUTH || side == EnumFacing.EAST || side == EnumFacing.WEST) {
return sideSlots;
}
int[] topSlots2 = new int[] { 0 };
return topSlots2;
}
@Override
public boolean canInsertItem(int slot, ItemStack itemStackIn, EnumFacing direction) {
if (slot < inventorySize && (direction == EnumFacing.NORTH || direction == EnumFacing.SOUTH || direction == EnumFacing.EAST || direction == EnumFacing.WEST)) {
return true;
}
return false;
}
@Override
public boolean canExtractItem(int slot, ItemStack stack, EnumFacing direction) {
if (slot < inventorySize && (direction == EnumFacing.NORTH || direction == EnumFacing.SOUTH || direction == EnumFacing.EAST || direction == EnumFacing.WEST)) {
return true;
}
return false;
}
public int getField(int id) {
switch (id) {
case 0:
return this.remainBurnTime;
case 1:
return this.getFluidAmount();
case 2:
case 3:
case 4:
case 5:
TileEntity te = this.worldObj.getTileEntity(this.pos.up(id - 1));
if (te instanceof TileEntityAsphaltMixer) {
return ((TileEntityAsphaltMixer) te).getFluidAmount();
}
default:
break;
}
return 0;
}
public void setField(int id, int value) {
switch (id) {
case 0:
this.remainBurnTime = value;
break;
case 1:
case 2:
case 3:
case 4:
case 5:
fluidLevelAbove[id - 1] = value;
break;
default:
break;
}
}
public int getFieldCount() {
return 6;
}
@Override
public boolean isUseableByPlayer(EntityPlayer playerIn) {
return playerIn.getDistanceSq(this.getPos().getX(), this.getPos().getY(), this.getPos().getZ()) < 64;
}
}