2015-06-12 19:12:06 +02:00
|
|
|
package ellpeck.actuallyadditions.tile;
|
|
|
|
|
|
|
|
import cofh.api.energy.EnergyStorage;
|
|
|
|
import cofh.api.energy.IEnergyReceiver;
|
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
|
|
|
import ellpeck.actuallyadditions.config.values.ConfigIntValues;
|
|
|
|
import ellpeck.actuallyadditions.items.InitItems;
|
|
|
|
import ellpeck.actuallyadditions.items.ItemCoffee;
|
|
|
|
import ellpeck.actuallyadditions.items.metalists.TheMiscItems;
|
|
|
|
import ellpeck.actuallyadditions.network.gui.IButtonReactor;
|
2015-07-02 04:21:21 +02:00
|
|
|
import ellpeck.actuallyadditions.network.sync.IPacketSyncerToClient;
|
|
|
|
import ellpeck.actuallyadditions.network.sync.PacketSyncerToClient;
|
2015-07-01 15:14:39 +02:00
|
|
|
import ellpeck.actuallyadditions.util.WorldUtil;
|
2015-06-12 19:12:06 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2015-06-28 03:12:32 +02:00
|
|
|
import net.minecraftforge.fluids.*;
|
2015-06-12 19:12:06 +02:00
|
|
|
|
2015-07-02 04:21:21 +02:00
|
|
|
public class TileEntityCoffeeMachine extends TileEntityInventoryBase implements IButtonReactor, IEnergyReceiver, IFluidHandler, IPacketSyncerToClient{
|
2015-06-12 19:12:06 +02:00
|
|
|
|
|
|
|
public static final int SLOT_COFFEE_BEANS = 0;
|
|
|
|
public static final int SLOT_INPUT = 1;
|
|
|
|
public static final int SLOT_OUTPUT = 2;
|
2015-06-28 03:12:32 +02:00
|
|
|
public static final int SLOT_WATER_INPUT = 11;
|
|
|
|
public static final int SLOT_WATER_OUTPUT = 12;
|
2015-06-12 19:12:06 +02:00
|
|
|
|
|
|
|
public EnergyStorage storage = new EnergyStorage(300000);
|
2015-07-02 04:21:21 +02:00
|
|
|
private int lastEnergy;
|
2015-06-28 03:12:32 +02:00
|
|
|
public FluidTank tank = new FluidTank(4*FluidContainerRegistry.BUCKET_VOLUME);
|
2015-07-02 04:21:21 +02:00
|
|
|
private int lastTank;
|
2015-06-12 19:12:06 +02:00
|
|
|
|
|
|
|
public static int energyUsePerTick = ConfigIntValues.COFFEE_MACHINE_ENERGY_USED.getValue();
|
2015-06-28 03:12:32 +02:00
|
|
|
public final int waterUsedPerCoffee = 500;
|
2015-06-12 19:12:06 +02:00
|
|
|
|
|
|
|
public final int coffeeCacheMaxAmount = 300;
|
|
|
|
public final int coffeeCacheAddPerItem = ConfigIntValues.COFFEE_CACHE_ADDED_PER_ITEM.getValue();
|
|
|
|
public final int coffeeCacheUsePerItem = ConfigIntValues.COFFEE_CACHE_USED_PER_ITEM.getValue();
|
|
|
|
public int coffeeCacheAmount;
|
2015-07-02 04:21:21 +02:00
|
|
|
private int lastCoffeeAmount;
|
2015-06-12 19:12:06 +02:00
|
|
|
|
|
|
|
public final int maxBrewTime = ConfigIntValues.COFFEE_MACHINE_TIME_USED.getValue();
|
|
|
|
public int brewTime;
|
2015-07-02 04:21:21 +02:00
|
|
|
private int lastBrewTime;
|
2015-06-12 19:12:06 +02:00
|
|
|
|
|
|
|
public TileEntityCoffeeMachine(){
|
2015-06-28 03:12:32 +02:00
|
|
|
super(13, "coffeeMachine");
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateEntity(){
|
|
|
|
if(!worldObj.isRemote){
|
|
|
|
this.storeCoffee();
|
|
|
|
|
2015-06-13 12:52:22 +02:00
|
|
|
if(this.brewTime > 0 || this.worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord)){
|
2015-06-12 19:12:06 +02:00
|
|
|
this.brew();
|
|
|
|
}
|
2015-07-02 04:21:21 +02:00
|
|
|
|
|
|
|
if(this.coffeeCacheAmount != this.lastCoffeeAmount || this.storage.getEnergyStored() != this.lastEnergy || this.tank.getFluidAmount() != this.lastTank || this.brewTime != this.lastBrewTime){
|
|
|
|
this.lastCoffeeAmount = coffeeCacheAmount;
|
|
|
|
this.lastEnergy = this.storage.getEnergyStored();
|
|
|
|
this.lastTank = this.tank.getFluidAmount();
|
|
|
|
this.lastBrewTime = this.brewTime;
|
|
|
|
this.sendUpdate();
|
|
|
|
}
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void storeCoffee(){
|
|
|
|
if(this.slots[SLOT_COFFEE_BEANS] != null && this.slots[SLOT_COFFEE_BEANS].getItem() == InitItems.itemCoffeeBean){
|
|
|
|
if(this.coffeeCacheAddPerItem <= this.coffeeCacheMaxAmount-this.coffeeCacheAmount){
|
|
|
|
this.slots[SLOT_COFFEE_BEANS].stackSize--;
|
|
|
|
if(this.slots[SLOT_COFFEE_BEANS].stackSize <= 0) this.slots[SLOT_COFFEE_BEANS] = null;
|
|
|
|
this.coffeeCacheAmount += this.coffeeCacheAddPerItem;
|
|
|
|
}
|
|
|
|
}
|
2015-06-28 03:12:32 +02:00
|
|
|
|
2015-07-01 15:14:39 +02:00
|
|
|
WorldUtil.emptyBucket(tank, slots, SLOT_WATER_INPUT, SLOT_WATER_OUTPUT, FluidRegistry.WATER);
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void brew(){
|
|
|
|
if(!worldObj.isRemote){
|
2015-06-28 03:12:32 +02:00
|
|
|
if(this.slots[SLOT_INPUT] != null && this.slots[SLOT_INPUT].getItem() == InitItems.itemMisc && this.slots[SLOT_INPUT].getItemDamage() == TheMiscItems.CUP.ordinal() && this.slots[SLOT_OUTPUT] == null && this.coffeeCacheAmount >= this.coffeeCacheUsePerItem && this.tank.getFluid() != null && this.tank.getFluid().getFluid() == FluidRegistry.WATER && this.tank.getFluidAmount() >= this.waterUsedPerCoffee){
|
2015-06-13 12:52:22 +02:00
|
|
|
if(this.storage.getEnergyStored() >= energyUsePerTick){
|
|
|
|
this.brewTime++;
|
|
|
|
this.storage.extractEnergy(energyUsePerTick, false);
|
|
|
|
if(this.brewTime >= this.maxBrewTime){
|
|
|
|
this.brewTime = 0;
|
|
|
|
ItemStack output = new ItemStack(InitItems.itemCoffee);
|
2015-06-28 03:12:32 +02:00
|
|
|
for(int i = 3; i < this.slots.length-2; i++){
|
2015-06-13 20:39:54 +02:00
|
|
|
if(this.slots[i] != null){
|
2015-06-13 12:52:22 +02:00
|
|
|
ItemCoffee.Ingredient ingredient = ItemCoffee.getIngredientFromStack(this.slots[i]);
|
|
|
|
if(ingredient != null){
|
2015-06-13 20:39:54 +02:00
|
|
|
if(ingredient.effect(output)){
|
|
|
|
this.slots[i].stackSize--;
|
|
|
|
if(this.slots[i].stackSize <= 0) this.slots[i] = this.slots[i].getItem().getContainerItem(this.slots[i]);
|
|
|
|
}
|
2015-06-13 12:52:22 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|
2015-06-13 12:52:22 +02:00
|
|
|
this.slots[SLOT_OUTPUT] = output.copy();
|
|
|
|
this.slots[SLOT_INPUT].stackSize--;
|
|
|
|
if(this.slots[SLOT_INPUT].stackSize <= 0) this.slots[SLOT_INPUT] = null;
|
|
|
|
this.coffeeCacheAmount -= this.coffeeCacheUsePerItem;
|
2015-06-28 03:12:32 +02:00
|
|
|
this.tank.drain(this.waterUsedPerCoffee, true);
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else this.brewTime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public int getCoffeeScaled(int i){
|
|
|
|
return this.coffeeCacheAmount * i / this.coffeeCacheMaxAmount;
|
|
|
|
}
|
|
|
|
|
2015-06-28 03:12:32 +02:00
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public int getWaterScaled(int i){
|
|
|
|
return this.tank.getFluidAmount() * i / this.tank.getCapacity();
|
|
|
|
}
|
|
|
|
|
2015-06-12 19:12:06 +02:00
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public int getEnergyScaled(int i){
|
2015-07-02 00:35:38 +02:00
|
|
|
return this.storage.getEnergyStored() * i / this.storage.getMaxEnergyStored();
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public int getBrewScaled(int i){
|
|
|
|
return this.brewTime * i / this.maxBrewTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToNBT(NBTTagCompound compound){
|
|
|
|
super.writeToNBT(compound);
|
|
|
|
this.storage.writeToNBT(compound);
|
2015-06-28 03:12:32 +02:00
|
|
|
this.tank.writeToNBT(compound);
|
2015-06-12 19:12:06 +02:00
|
|
|
compound.setInteger("Cache", this.coffeeCacheAmount);
|
|
|
|
compound.setInteger("Time", this.brewTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readFromNBT(NBTTagCompound compound){
|
|
|
|
super.readFromNBT(compound);
|
|
|
|
this.storage.readFromNBT(compound);
|
2015-06-28 03:12:32 +02:00
|
|
|
this.tank.readFromNBT(compound);
|
2015-06-12 19:12:06 +02:00
|
|
|
this.coffeeCacheAmount = compound.getInteger("Cache");
|
|
|
|
this.brewTime = compound.getInteger("Time");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int i, ItemStack stack){
|
2015-06-13 12:52:22 +02:00
|
|
|
return (i >= 3 && ItemCoffee.getIngredientFromStack(stack) != null) || (i == SLOT_COFFEE_BEANS && stack.getItem() == InitItems.itemCoffeeBean) || (i == SLOT_INPUT && stack.getItem() == InitItems.itemMisc && stack.getItemDamage() == TheMiscItems.CUP.ordinal());
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canInsertItem(int slot, ItemStack stack, int side){
|
|
|
|
return this.isItemValidForSlot(slot, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canExtractItem(int slot, ItemStack stack, int side){
|
2015-06-28 03:12:32 +02:00
|
|
|
return slot == SLOT_OUTPUT || (slot >= 3 && slot < this.slots.length-2 && ItemCoffee.getIngredientFromStack(stack) == null) || slot == SLOT_WATER_OUTPUT;
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onButtonPressed(int buttonID, EntityPlayer player){
|
|
|
|
if(buttonID == 0 && this.brewTime <= 0){
|
|
|
|
this.brew();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate){
|
|
|
|
return this.storage.receiveEnergy(maxReceive, simulate);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getEnergyStored(ForgeDirection from){
|
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getMaxEnergyStored(ForgeDirection from){
|
|
|
|
return this.storage.getMaxEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canConnectEnergy(ForgeDirection from){
|
|
|
|
return true;
|
|
|
|
}
|
2015-06-28 03:12:32 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int fill(ForgeDirection from, FluidStack resource, boolean doFill){
|
|
|
|
return resource.getFluid() == FluidRegistry.WATER && from != ForgeDirection.DOWN ? this.tank.fill(resource, doFill) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canFill(ForgeDirection from, Fluid fluid){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canDrain(ForgeDirection from, Fluid fluid){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public FluidTankInfo[] getTankInfo(ForgeDirection from){
|
|
|
|
return new FluidTankInfo[]{this.tank.getInfo()};
|
|
|
|
}
|
2015-07-02 04:21:21 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int[] getValues(){
|
|
|
|
return new int[]{this.tank.getFluidAmount(), this.tank.getFluid() == null ? -1 : this.tank.getFluid().getFluidID(), this.storage.getEnergyStored(), this.brewTime, this.coffeeCacheAmount};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setValues(int[] values){
|
|
|
|
if(values[1] != -1){
|
|
|
|
this.tank.setFluid(new FluidStack(FluidRegistry.getFluid(values[1]), values[0]));
|
|
|
|
}
|
|
|
|
else this.tank.setFluid(null);
|
|
|
|
this.storage.setEnergyStored(values[2]);
|
|
|
|
this.brewTime = values[3];
|
|
|
|
this.coffeeCacheAmount = values[4];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void sendUpdate(){
|
|
|
|
PacketSyncerToClient.sendPacket(this);
|
|
|
|
}
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|