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
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 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
|
|
|
|
2016-05-14 13:51:18 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.recipe.CoffeeIngredient;
|
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-12-06 18:10:43 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.misc.SoundHandler;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor;
|
2018-08-10 05:04:07 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover;
|
2016-11-16 16:59:00 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
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;
|
2016-11-26 08:58:42 +01:00
|
|
|
import net.minecraftforge.energy.IEnergyStorage;
|
2016-11-26 21:32:27 +01:00
|
|
|
import net.minecraftforge.fluids.FluidRegistry;
|
|
|
|
import net.minecraftforge.fluids.FluidStack;
|
|
|
|
import net.minecraftforge.fluids.FluidTank;
|
2016-01-07 18:20:59 +01:00
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2019-03-03 23:24:25 +01:00
|
|
|
import net.minecraftforge.oredict.OreIngredient;
|
2015-06-12 19:12:06 +02:00
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
public class TileEntityCoffeeMachine extends TileEntityInventoryBase implements IButtonReactor, ISharingFluidHandler {
|
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;
|
2016-11-21 13:45:23 +01:00
|
|
|
public static final int CACHE_USE = 35;
|
2015-12-01 19:48:09 +01:00
|
|
|
public static final int ENERGY_USED = 150;
|
|
|
|
public static final int WATER_USE = 500;
|
|
|
|
public static final int COFFEE_CACHE_MAX_AMOUNT = 300;
|
2019-03-03 23:24:25 +01:00
|
|
|
public static final int TIME_USED = 500;
|
|
|
|
public static final OreIngredient COFFEE = new OreIngredient("cropCoffee");
|
2016-11-26 20:43:50 +01:00
|
|
|
public final CustomEnergyStorage storage = new CustomEnergyStorage(300000, 250, 0);
|
2018-08-10 05:04:07 +02:00
|
|
|
public final FluidTank tank = new FluidTank(4 * Util.BUCKET) {
|
2016-06-05 02:16:52 +02:00
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public boolean canDrain() {
|
2016-06-05 02:16:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public boolean canFillFluidType(FluidStack fluid) {
|
2016-06-05 02:16:52 +02:00
|
|
|
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
|
|
|
|
2018-08-10 05:04:07 +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)
|
2018-08-10 05:04:07 +02:00
|
|
|
public int getCoffeeScaled(int i) {
|
|
|
|
return this.coffeeCacheAmount * i / COFFEE_CACHE_MAX_AMOUNT;
|
2015-12-01 19:48:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
2018-08-10 05:04:07 +02:00
|
|
|
public int getWaterScaled(int i) {
|
|
|
|
return this.tank.getFluidAmount() * i / this.tank.getCapacity();
|
2015-12-01 19:48:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
2018-08-10 05:04:07 +02:00
|
|
|
public int getEnergyScaled(int i) {
|
|
|
|
return this.storage.getEnergyStored() * i / this.storage.getMaxEnergyStored();
|
2015-12-01 19:48:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
2018-08-10 05:04:07 +02:00
|
|
|
public int getBrewScaled(int i) {
|
|
|
|
return this.brewTime * i / TIME_USED;
|
2015-12-01 19:48:09 +01:00
|
|
|
}
|
|
|
|
|
2016-02-01 20:32:49 +01:00
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.writeSyncableNBT(compound, type);
|
2016-02-01 20:32:49 +01:00
|
|
|
this.storage.writeToNBT(compound);
|
|
|
|
this.tank.writeToNBT(compound);
|
2017-01-28 17:44:30 +01:00
|
|
|
compound.setInteger("Cache", this.coffeeCacheAmount);
|
2018-08-10 05:04:07 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2016-07-02 15:01:46 +02:00
|
|
|
compound.setInteger("Time", this.brewTime);
|
|
|
|
}
|
2016-02-01 20:32:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public void readSyncableNBT(NBTTagCompound compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.readSyncableNBT(compound, type);
|
2016-02-01 20:32:49 +01:00
|
|
|
this.storage.readFromNBT(compound);
|
|
|
|
this.tank.readFromNBT(compound);
|
2017-01-28 17:44:30 +01:00
|
|
|
this.coffeeCacheAmount = compound.getInteger("Cache");
|
2018-08-10 05:04:07 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2016-07-02 15:01:46 +02:00
|
|
|
this.brewTime = compound.getInteger("Time");
|
|
|
|
}
|
2016-02-01 20:32:49 +01:00
|
|
|
}
|
|
|
|
|
2015-06-12 19:12:06 +02:00
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public void updateEntity() {
|
2015-11-18 23:11:24 +01:00
|
|
|
super.updateEntity();
|
2018-08-10 05:04:07 +02:00
|
|
|
if (!this.world.isRemote) {
|
2015-06-12 19:12:06 +02:00
|
|
|
this.storeCoffee();
|
|
|
|
|
2018-08-10 05:04:07 +02: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
|
|
|
|
2018-08-10 05:04:07 +02: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
|
2018-08-10 05:04:07 +02:00
|
|
|
public IAcceptor getAcceptor() {
|
2019-03-03 23:24:25 +01:00
|
|
|
return (slot, stack, automation) -> !automation || slot >= 3 && ItemCoffee.getIngredientFromStack(stack) != null || slot == SLOT_COFFEE_BEANS && COFFEE.apply(stack) || slot == SLOT_INPUT && stack.getItem() == InitItems.itemMisc && stack.getItemDamage() == TheMiscItems.CUP.ordinal();
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
@Override
|
|
|
|
public IRemover getRemover() {
|
2019-02-27 19:53:05 +01:00
|
|
|
return (slot, automation) -> !automation || slot == SLOT_OUTPUT || slot >= 3 && slot < this.inv.getSlots() && ItemCoffee.getIngredientFromStack(this.inv.getStackInSlot(slot)) == null;
|
2018-08-10 05:04:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void storeCoffee() {
|
2019-03-03 23:24:25 +01:00
|
|
|
if (StackUtil.isValid(this.inv.getStackInSlot(SLOT_COFFEE_BEANS)) && COFFEE.apply(this.inv.getStackInSlot(SLOT_COFFEE_BEANS))) {
|
2015-11-28 19:02:01 +01:00
|
|
|
int toAdd = 2;
|
2018-08-10 05:04:07 +02:00
|
|
|
if (toAdd <= COFFEE_CACHE_MAX_AMOUNT - this.coffeeCacheAmount) {
|
2018-06-23 01:39:30 +02:00
|
|
|
this.inv.setStackInSlot(SLOT_COFFEE_BEANS, StackUtil.shrink(this.inv.getStackInSlot(SLOT_COFFEE_BEANS), 1));
|
2015-11-28 19:02:01 +01:00
|
|
|
this.coffeeCacheAmount += toAdd;
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
public void brew() {
|
|
|
|
if (!this.world.isRemote) {
|
2018-06-23 01:39:30 +02:00
|
|
|
ItemStack input = this.inv.getStackInSlot(SLOT_INPUT);
|
2018-08-10 05:04:07 +02:00
|
|
|
if (StackUtil.isValid(input) && input.getItem() == InitItems.itemMisc && input.getItemDamage() == TheMiscItems.CUP.ordinal() && !StackUtil.isValid(this.inv.getStackInSlot(SLOT_OUTPUT)) && 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) {
|
|
|
|
if (this.brewTime % 30 == 0) {
|
2018-09-26 09:53:48 +02:00
|
|
|
this.world.playSound(null, this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), SoundHandler.coffeeMachine, SoundCategory.BLOCKS, 0.1F, 1.0F);
|
2015-12-13 14:36:34 +01:00
|
|
|
}
|
|
|
|
|
2015-06-13 12:52:22 +02:00
|
|
|
this.brewTime++;
|
2016-11-23 18:10:55 +01:00
|
|
|
this.storage.extractEnergyInternal(ENERGY_USED, false);
|
2018-08-10 05:04:07 +02:00
|
|
|
if (this.brewTime >= TIME_USED) {
|
2015-06-13 12:52:22 +02:00
|
|
|
this.brewTime = 0;
|
|
|
|
ItemStack output = new ItemStack(InitItems.itemCoffee);
|
2018-08-10 05:04:07 +02:00
|
|
|
for (int i = 3; i < this.inv.getSlots(); i++) {
|
|
|
|
if (StackUtil.isValid(this.inv.getStackInSlot(i))) {
|
2018-06-23 01:39:30 +02:00
|
|
|
CoffeeIngredient ingredient = ItemCoffee.getIngredientFromStack(this.inv.getStackInSlot(i));
|
2018-08-10 05:04:07 +02:00
|
|
|
if (ingredient != null) {
|
|
|
|
if (ingredient.effect(output)) {
|
2018-06-23 01:39:30 +02:00
|
|
|
this.inv.setStackInSlot(i, StackUtil.shrinkForContainer(this.inv.getStackInSlot(i), 1));
|
2015-06-13 20:39:54 +02:00
|
|
|
}
|
2015-06-13 12:52:22 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
this.inv.setStackInSlot(SLOT_OUTPUT, output.copy());
|
|
|
|
this.inv.getStackInSlot(SLOT_INPUT).shrink(1);
|
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
|
|
|
}
|
|
|
|
}
|
2018-08-10 05:04:07 +02:00
|
|
|
} else {
|
2015-10-02 16:48:01 +02:00
|
|
|
this.brewTime = 0;
|
|
|
|
}
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public void onButtonPressed(int buttonID, EntityPlayer player) {
|
|
|
|
if (buttonID == 0 && this.brewTime <= 0) {
|
2015-06-12 19:12:06 +02:00
|
|
|
this.brew();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-05 02:16:52 +02:00
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public FluidTank getFluidHandler(EnumFacing facing) {
|
2016-12-05 14:16:53 +01:00
|
|
|
return this.tank;
|
2016-06-05 02:16:52 +02:00
|
|
|
}
|
2016-06-10 21:52:37 +02:00
|
|
|
|
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public int getMaxFluidAmountToSplitShare() {
|
2016-08-31 20:16:36 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public boolean doesShareFluid() {
|
2016-08-31 20:16:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public EnumFacing[] getFluidShareSides() {
|
2016-08-31 20:16:36 +02:00
|
|
|
return null;
|
|
|
|
}
|
2016-11-26 08:58:42 +01:00
|
|
|
|
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public IEnergyStorage getEnergyStorage(EnumFacing facing) {
|
2016-11-26 08:58:42 +01:00
|
|
|
return this.storage;
|
|
|
|
}
|
2015-06-12 19:12:06 +02:00
|
|
|
}
|