2015-08-29 14:33:25 +02:00
|
|
|
/*
|
2016-05-16 22:52:27 +02:00
|
|
|
* This file ("TileEntityCoffeeMachine.java") is part of the Actually Additions mod for Minecraft.
|
2015-08-29 14:33:25 +02:00
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
* under the Actually Additions License to be found at
|
2016-05-16 22:52:27 +02:00
|
|
|
* http://ellpeck.de/actaddlicense
|
2015-08-29 14:33:25 +02:00
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2016-05-16 22:54:42 +02:00
|
|
|
* © 2015-2016 Ellpeck
|
2015-08-29 14:33:25 +02:00
|
|
|
*/
|
|
|
|
|
2016-01-05 04:47:35 +01:00
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
2015-06-12 19:12:06 +02:00
|
|
|
|
|
|
|
import cofh.api.energy.EnergyStorage;
|
|
|
|
import cofh.api.energy.IEnergyReceiver;
|
2016-05-14 13:51:18 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.recipe.CoffeeIngredient;
|
2016-01-23 11:02:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.ConfigValues;
|
2016-06-05 12:15:02 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.items.InitItems;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.items.ItemCoffee;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.items.metalists.TheMiscItems;
|
2016-05-01 22:26:26 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.misc.SoundHandler;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor;
|
2016-05-06 23:23:29 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.Util;
|
2015-06-12 19:12:06 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2016-01-07 19:47:53 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2016-05-01 22:26:26 +02:00
|
|
|
import net.minecraft.util.SoundCategory;
|
2015-06-28 03:12:32 +02:00
|
|
|
import net.minecraftforge.fluids.*;
|
2016-01-07 18:20:59 +01:00
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2015-06-12 19:12:06 +02:00
|
|
|
|
2016-06-05 02:16:52 +02:00
|
|
|
public class TileEntityCoffeeMachine extends TileEntityInventoryBase implements IButtonReactor, IEnergyReceiver, IFluidSaver, IEnergySaver{
|
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-12-01 19:48:09 +01:00
|
|
|
public static final int CACHE_USE = 15;
|
|
|
|
public static final int ENERGY_USED = 150;
|
|
|
|
public static final int WATER_USE = 500;
|
|
|
|
public static final int COFFEE_CACHE_MAX_AMOUNT = 300;
|
|
|
|
private static final int TIME_USED = 500;
|
2016-05-19 20:05:12 +02:00
|
|
|
public final EnergyStorage storage = new EnergyStorage(300000);
|
2016-06-05 02:16:52 +02:00
|
|
|
public final FluidTank tank = new FluidTank(4*Util.BUCKET){
|
|
|
|
@Override
|
|
|
|
public boolean canDrain(){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canFillFluidType(FluidStack fluid){
|
|
|
|
return fluid.getFluid() == FluidRegistry.WATER;
|
|
|
|
}
|
|
|
|
};
|
2015-06-12 19:12:06 +02:00
|
|
|
public int coffeeCacheAmount;
|
|
|
|
public int brewTime;
|
2015-10-03 10:16:18 +02:00
|
|
|
private int lastEnergy;
|
|
|
|
private int lastTank;
|
|
|
|
private int lastCoffeeAmount;
|
2015-07-02 04:21:21 +02:00
|
|
|
private int lastBrewTime;
|
2015-06-12 19:12:06 +02:00
|
|
|
|
|
|
|
public TileEntityCoffeeMachine(){
|
2016-05-06 23:23:29 +02:00
|
|
|
super(11, "coffeeMachine");
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|
|
|
|
|
2015-12-01 19:48:09 +01:00
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public int getCoffeeScaled(int i){
|
|
|
|
return this.coffeeCacheAmount*i/COFFEE_CACHE_MAX_AMOUNT;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public int getWaterScaled(int i){
|
|
|
|
return this.tank.getFluidAmount()*i/this.tank.getCapacity();
|
|
|
|
}
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public int getEnergyScaled(int i){
|
|
|
|
return this.storage.getEnergyStored()*i/this.storage.getMaxEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public int getBrewScaled(int i){
|
|
|
|
return this.brewTime*i/TIME_USED;
|
|
|
|
}
|
|
|
|
|
2016-02-01 20:32:49 +01:00
|
|
|
@Override
|
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, boolean sync){
|
|
|
|
super.writeSyncableNBT(compound, sync);
|
|
|
|
this.storage.writeToNBT(compound);
|
|
|
|
this.tank.writeToNBT(compound);
|
|
|
|
compound.setInteger("Cache", this.coffeeCacheAmount);
|
|
|
|
compound.setInteger("Time", this.brewTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
|
|
|
|
super.readSyncableNBT(compound, sync);
|
|
|
|
this.storage.readFromNBT(compound);
|
|
|
|
this.tank.readFromNBT(compound);
|
|
|
|
this.coffeeCacheAmount = compound.getInteger("Cache");
|
|
|
|
this.brewTime = compound.getInteger("Time");
|
|
|
|
}
|
|
|
|
|
2015-06-12 19:12:06 +02:00
|
|
|
@Override
|
|
|
|
public void updateEntity(){
|
2015-11-18 23:11:24 +01:00
|
|
|
super.updateEntity();
|
2016-05-02 17:46:53 +02:00
|
|
|
if(!this.worldObj.isRemote){
|
2015-06-12 19:12:06 +02:00
|
|
|
this.storeCoffee();
|
|
|
|
|
2015-12-03 18:40:43 +01:00
|
|
|
if(this.brewTime > 0 || this.isRedstonePowered){
|
2015-06-12 19:12:06 +02:00
|
|
|
this.brew();
|
|
|
|
}
|
2015-07-02 04:21:21 +02:00
|
|
|
|
2015-12-01 17:28:50 +01:00
|
|
|
if((this.coffeeCacheAmount != this.lastCoffeeAmount || this.storage.getEnergyStored() != this.lastEnergy || this.tank.getFluidAmount() != this.lastTank || this.brewTime != this.lastBrewTime) && this.sendUpdateWithInterval()){
|
2016-05-02 17:46:53 +02:00
|
|
|
this.lastCoffeeAmount = this.coffeeCacheAmount;
|
2015-07-02 04:21:21 +02:00
|
|
|
this.lastEnergy = this.storage.getEnergyStored();
|
|
|
|
this.lastTank = this.tank.getFluidAmount();
|
|
|
|
this.lastBrewTime = this.brewTime;
|
|
|
|
}
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public boolean isItemValidForSlot(int i, ItemStack stack){
|
2016-05-06 23:23:29 +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());
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
|
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){
|
2015-11-28 19:02:01 +01:00
|
|
|
int toAdd = 2;
|
|
|
|
if(toAdd <= COFFEE_CACHE_MAX_AMOUNT-this.coffeeCacheAmount){
|
2015-06-12 19:12:06 +02:00
|
|
|
this.slots[SLOT_COFFEE_BEANS].stackSize--;
|
2015-10-03 10:16:18 +02:00
|
|
|
if(this.slots[SLOT_COFFEE_BEANS].stackSize <= 0){
|
|
|
|
this.slots[SLOT_COFFEE_BEANS] = null;
|
|
|
|
}
|
2015-11-28 19:02:01 +01:00
|
|
|
this.coffeeCacheAmount += toAdd;
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void brew(){
|
2016-05-02 17:46:53 +02:00
|
|
|
if(!this.worldObj.isRemote){
|
2015-11-28 19:02:01 +01: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 >= CACHE_USE && this.tank.getFluid() != null && this.tank.getFluid().getFluid() == FluidRegistry.WATER && this.tank.getFluidAmount() >= WATER_USE){
|
|
|
|
if(this.storage.getEnergyStored() >= ENERGY_USED){
|
2016-06-05 12:15:02 +02:00
|
|
|
if(this.brewTime%30 == 0 && !ConfigBoolValues.LESS_SOUND.isEnabled()){
|
2016-05-01 22:26:26 +02:00
|
|
|
this.worldObj.playSound(null, this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), SoundHandler.coffeeMachine, SoundCategory.BLOCKS, 0.35F, 1.0F);
|
2015-12-13 14:36:34 +01:00
|
|
|
}
|
|
|
|
|
2015-06-13 12:52:22 +02:00
|
|
|
this.brewTime++;
|
2015-11-28 19:02:01 +01:00
|
|
|
this.storage.extractEnergy(ENERGY_USED, false);
|
|
|
|
if(this.brewTime >= TIME_USED){
|
2015-06-13 12:52:22 +02:00
|
|
|
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){
|
2016-01-05 04:47:35 +01:00
|
|
|
CoffeeIngredient ingredient = ItemCoffee.getIngredientFromStack(this.slots[i]);
|
2015-06-13 12:52:22 +02:00
|
|
|
if(ingredient != null){
|
2015-06-13 20:39:54 +02:00
|
|
|
if(ingredient.effect(output)){
|
|
|
|
this.slots[i].stackSize--;
|
2015-10-02 16:48:01 +02:00
|
|
|
if(this.slots[i].stackSize <= 0){
|
|
|
|
this.slots[i] = this.slots[i].getItem().getContainerItem(this.slots[i]);
|
|
|
|
}
|
2015-06-13 20:39:54 +02:00
|
|
|
}
|
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--;
|
2015-10-03 10:16:18 +02:00
|
|
|
if(this.slots[SLOT_INPUT].stackSize <= 0){
|
|
|
|
this.slots[SLOT_INPUT] = null;
|
|
|
|
}
|
2015-11-28 19:02:01 +01:00
|
|
|
this.coffeeCacheAmount -= CACHE_USE;
|
2016-06-05 02:16:52 +02:00
|
|
|
this.tank.drainInternal(WATER_USE, true);
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-02 16:48:01 +02:00
|
|
|
else{
|
|
|
|
this.brewTime = 0;
|
|
|
|
}
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public boolean canInsertItem(int slot, ItemStack stack, EnumFacing side){
|
2015-12-19 10:30:39 +01:00
|
|
|
return this.isItemValidForSlot(slot, stack);
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public boolean canExtractItem(int slot, ItemStack stack, EnumFacing side){
|
2016-05-06 23:23:29 +02:00
|
|
|
return slot == SLOT_OUTPUT || (slot >= 3 && slot < this.slots.length-2 && ItemCoffee.getIngredientFromStack(stack) == null);
|
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
|
2016-01-07 19:47:53 +01:00
|
|
|
public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate){
|
2015-06-12 19:12:06 +02:00
|
|
|
return this.storage.receiveEnergy(maxReceive, simulate);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-07 19:47:53 +01:00
|
|
|
public int getEnergyStored(EnumFacing from){
|
2015-06-12 19:12:06 +02:00
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-07 19:47:53 +01:00
|
|
|
public int getMaxEnergyStored(EnumFacing from){
|
2015-06-12 19:12:06 +02:00
|
|
|
return this.storage.getMaxEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-01-07 19:47:53 +01:00
|
|
|
public boolean canConnectEnergy(EnumFacing from){
|
2015-06-12 19:12:06 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-06-28 03:12:32 +02:00
|
|
|
|
2015-12-13 00:54:25 +01:00
|
|
|
@Override
|
|
|
|
public int getEnergy(){
|
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setEnergy(int energy){
|
|
|
|
this.storage.setEnergyStored(energy);
|
|
|
|
}
|
2015-12-15 18:51:13 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public FluidStack[] getFluids(){
|
|
|
|
return new FluidStack[]{this.tank.getFluid()};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setFluids(FluidStack[] fluids){
|
|
|
|
this.tank.setFluid(fluids[0]);
|
|
|
|
}
|
2016-06-05 02:16:52 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public FluidTank getFluidHandler(EnumFacing facing){
|
|
|
|
return facing != EnumFacing.DOWN ? this.tank : null;
|
|
|
|
}
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|