package com.projectreddog.machinemod.tileentities; import com.projectreddog.machinemod.block.BlockMachineModPrimaryCrusher; import com.projectreddog.machinemod.iface.IFuelContainer; import com.projectreddog.machinemod.init.ModBlocks; import com.projectreddog.machinemod.init.ModItems; 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.nbt.NBTTagList; 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; public class TileEntityFermenter extends TileEntity implements ITickable, ISidedInventory, IFuelContainer { protected ItemStack[] inventory; private static int[] sideSlots = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; public AxisAlignedBB boundingBox; public final int maxFuelStorage = 1000; // store up to 1k public int fuelStorage = 0; public final int inventorySize = 9; public final int fuelAmountFromCorn = 250; public final int coolDownReset = 1200; public int cooldown = coolDownReset; public TileEntityFermenter() { inventory = new ItemStack[inventorySize]; } public int addFluid(int amount) { int returnAmount; if (canAcceptFluid()) { if (fuelStorage + amount > maxFuelStorage) { // fill to brim return amount left over returnAmount = (fuelStorage + amount - maxFuelStorage); fuelStorage = maxFuelStorage; } else { // not going to return any this container can hold all of the fuel fuelStorage = fuelStorage + amount; returnAmount = 0; } } else { returnAmount = amount; } return returnAmount; } public boolean canAcceptFluid() { if (fuelStorage < maxFuelStorage) { return true; } else { return false; } } @Override public void update() { if (!worldObj.isRemote) { // LogHelper.info("TE update entity called"); // LogHelper.info("TE update entity called"); cooldown = cooldown - 1; // LogHelper.info("TE FERMENTER CD" + cooldown); if (cooldown <= 0) { cooldown = coolDownReset; for (int i = 0; i < this.getSizeInventory(); i++) { ItemStack item = this.getStackInSlot(i); if (item != null) { if (item.getItem() == ModItems.cornseed) { produceFuel(i); // only process one at a time so dont check the next slot! i = this.getSizeInventory(); } } } } // always transfer fule transferFuel(); } } public boolean transferFuel() { if (this.fuelStorage > 0) { if (worldObj.getBlockState(this.pos.offset(this.outputDirection())).getBlock() == ModBlocks.machinedistiller) { // its a distiller so we can transfer fuel! TileEntityDistiller tED = (TileEntityDistiller) worldObj.getTileEntity(this.pos.offset(this.outputDirection())); if (tED.canAcceptFluid()) { tED.addFluid(1); this.fuelStorage = this.fuelStorage - 1; this.markDirty(); return true; } } } return false; } public boolean produceFuel(int slot) { // turn the corn in this slot into fuel! if (canAcceptFluid()) { decrStackSize(slot, 1); addFluid(fuelAmountFromCorn);// any excess will go to waste :O this.markDirty(); return true; } else { return false; } } protected ItemStack addToinventory(ItemStack is) { int i = getSizeInventory(); for (int j = 0; j < i && is != null && is.stackSize > 0; ++j) { if (is != null) { if (getStackInSlot(j) != null) { if (getStackInSlot(j).getItem() == is.getItem() && getStackInSlot(j).getItemDamage() == is.getItemDamage()) { // same item remove from is put into slot any amt not to // excede stack max if (getStackInSlot(j).stackSize < getStackInSlot(j).getMaxStackSize()) { // we have room to add to this stack if (is.stackSize <= getStackInSlot(j).getMaxStackSize() - getStackInSlot(j).stackSize) { // /all of the stack will fit in this slot do // so. setInventorySlotContents(j, new ItemStack(getStackInSlot(j).getItem(), getStackInSlot(j).stackSize + is.stackSize, is.getItemDamage())); is = null; } else { // we have more int countRemain = is.stackSize - (getStackInSlot(j).getMaxStackSize() - getStackInSlot(j).stackSize); setInventorySlotContents(j, new ItemStack(is.getItem(), getStackInSlot(j).getMaxStackSize(), is.getItemDamage())); is.stackSize = countRemain; } } } } else { // nothign in slot so set contents setInventorySlotContents(j, new ItemStack(is.getItem(), is.stackSize, is.getItemDamage())); is = null; } } } return is; } @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); // inventory fuelStorage = compound.getInteger(Reference.MACHINE_MOD_NBT_PREFIX + "FUEL_STORAGE"); cooldown = compound.getInteger(Reference.MACHINE_MOD_NBT_PREFIX + "COOL_DOWN"); NBTTagList tagList = compound.getTagList(Reference.MACHINE_MOD_NBT_PREFIX + "Inventory", compound.getId()); for (int i = 0; i < tagList.tagCount(); i++) { NBTTagCompound tag = (NBTTagCompound) tagList.getCompoundTagAt(i); byte slot = tag.getByte("Slot"); if (slot >= 0 && slot < inventory.length) { inventory[slot] = ItemStack.loadItemStackFromNBT(tag); } } } @Override public NBTTagCompound writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); // inventory compound.setInteger(Reference.MACHINE_MOD_NBT_PREFIX + "FUEL_STORAGE", fuelStorage); compound.setInteger(Reference.MACHINE_MOD_NBT_PREFIX + "COOL_DOWN", cooldown); NBTTagList itemList = new NBTTagList(); for (int i = 0; i < inventory.length; i++) { ItemStack stack = inventory[i]; if (stack != null) { NBTTagCompound tag = new NBTTagCompound(); tag.setByte("Slot", (byte) i); stack.writeToNBT(tag); itemList.appendTag(tag); } } compound.setTag(Reference.MACHINE_MOD_NBT_PREFIX + "Inventory", itemList); return compound; } @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 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 boolean isUseableByPlayer(EntityPlayer playerIn) { return playerIn.getDistanceSq(this.getPos().getX(), this.getPos().getY(), this.getPos().getZ()) < 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 int getField(int id) { switch (id) { case 0: return this.fuelStorage; default: break; } return 0; } @Override public void setField(int id, int value) { switch (id) { case 0: this.fuelStorage = value; break; default: break; } } @Override public int getFieldCount() { return 1; } @Override public void clear() { for (int i = 0; i < inventory.length; ++i) { inventory[i] = 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; } @Override public EnumFacing outputDirection() { EnumFacing ef = (EnumFacing) worldObj.getBlockState(this.getPos()).getValue(BlockMachineModPrimaryCrusher.FACING); // switch (ef) { // case NORTH: // return EnumFacing.SOUTH; // case SOUTH: // return EnumFacing.NORTH; // case EAST: // return EnumFacing.WEST; // case WEST: // return EnumFacing.EAST; // default: // return null; // } return ef; } }