ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCoffeeMachine.java

261 lines
10 KiB
Java
Raw Normal View History

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;
2021-02-27 16:33:00 +01:00
import de.ellpeck.actuallyadditions.api.ActuallyTags;
import de.ellpeck.actuallyadditions.mod.AASounds;
2021-08-31 00:44:39 +02:00
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
import de.ellpeck.actuallyadditions.mod.crafting.CoffeeIngredientRecipe;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerCoffeeMachine;
import de.ellpeck.actuallyadditions.mod.items.ActuallyItems;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.items.ItemCoffee;
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;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeHolder;
2024-03-02 21:23:08 +01:00
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Fluids;
2024-03-04 20:21:48 +01:00
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
import net.neoforged.neoforge.energy.IEnergyStorage;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.FluidType;
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
import net.neoforged.neoforge.fluids.capability.templates.FluidTank;
2021-02-27 16:33:00 +01:00
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
2024-03-02 21:23:08 +01:00
public class TileEntityCoffeeMachine extends TileEntityInventoryBase implements MenuProvider, IButtonReactor, ISharingFluidHandler {
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;
2021-02-27 16:33:00 +01:00
2016-11-26 20:43:50 +01:00
public final CustomEnergyStorage storage = new CustomEnergyStorage(300000, 250, 0);
2021-02-27 16:33:00 +01:00
2024-03-03 01:20:53 +01:00
public final FluidTank tank = new FluidTank(4 * FluidType.BUCKET_VOLUME) {
2021-02-27 16:33:00 +01:00
@Nonnull
2016-06-05 02:16:52 +02:00
@Override
2021-02-27 16:33:00 +01:00
public FluidStack drain(int maxDrain, FluidAction action) {
return FluidStack.EMPTY;
2016-06-05 02:16:52 +02:00
}
2021-02-27 16:33:00 +01:00
@Nonnull
2016-06-05 02:16:52 +02:00
@Override
2021-02-27 16:33:00 +01:00
public FluidStack drain(FluidStack resource, FluidAction action) {
return FluidStack.EMPTY;
}
2021-02-27 16:33:00 +01:00
@Override
public boolean isFluidValid(FluidStack fluid) {
return fluid.getFluid() == Fluids.WATER;
2016-06-05 02:16:52 +02:00
}
};
2021-02-27 16:33:00 +01:00
public int coffeeCacheAmount;
public int brewTime;
2015-10-03 10:16:18 +02:00
private int lastEnergy;
private int lastTank;
private int lastCoffeeAmount;
private int lastBrewTime;
2024-03-02 21:23:08 +01:00
public TileEntityCoffeeMachine(BlockPos pos, BlockState state) {
super(ActuallyBlocks.COFFEE_MACHINE.getTileEntityType(), pos, state, 11);
}
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.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
}
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.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
}
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.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
}
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.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
}
@Override
2024-03-02 21:23:08 +01:00
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
super.writeSyncableNBT(compound, type);
this.storage.writeToNBT(compound);
this.tank.writeToNBT(compound);
2021-02-27 16:33:00 +01:00
compound.putInt("Cache", this.coffeeCacheAmount);
2018-08-10 05:04:07 +02:00
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
compound.putInt("Time", this.brewTime);
}
}
@Override
2024-03-02 21:23:08 +01:00
public void readSyncableNBT(CompoundTag compound, NBTType type) {
super.readSyncableNBT(compound, type);
this.storage.readFromNBT(compound);
this.tank.readFromNBT(compound);
2021-02-27 16:33:00 +01:00
this.coffeeCacheAmount = compound.getInt("Cache");
2018-08-10 05:04:07 +02:00
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
this.brewTime = compound.getInt("Time");
}
}
2024-03-02 21:23:08 +01:00
public static <T extends BlockEntity> void clientTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityCoffeeMachine tile) {
tile.clientTick();
}
}
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityCoffeeMachine tile) {
tile.serverTick();
tile.storeCoffee();
2024-03-02 21:23:08 +01:00
if (tile.brewTime > 0 || tile.isRedstonePowered) {
tile.brew();
}
2024-03-02 21:23:08 +01:00
if ((tile.coffeeCacheAmount != tile.lastCoffeeAmount || tile.storage.getEnergyStored() != tile.lastEnergy || tile.tank.getFluidAmount() != tile.lastTank || tile.brewTime != tile.lastBrewTime) && tile.sendUpdateWithInterval()) {
tile.lastCoffeeAmount = tile.coffeeCacheAmount;
tile.lastEnergy = tile.storage.getEnergyStored();
tile.lastTank = tile.tank.getFluidAmount();
tile.lastBrewTime = tile.brewTime;
}
}
}
2016-02-01 17:49:55 +01:00
@Override
2018-08-10 05:04:07 +02:00
public IAcceptor getAcceptor() {
return (slot, stack, automation) -> !automation || slot >= 3 && ItemCoffee.getIngredientRecipeFromStack(stack) != null || slot == SLOT_COFFEE_BEANS && stack.is(ActuallyTags.Items.COFFEE_BEANS) || slot == SLOT_INPUT && stack.getItem() == ActuallyItems.COFFEE_CUP.get();
2016-02-01 17:49:55 +01:00
}
2018-08-10 05:04:07 +02:00
@Override
public IRemover getRemover() {
return (slot, automation) -> !automation || slot == SLOT_OUTPUT || slot >= 3 && slot < this.inv.getSlots() && ItemCoffee.getIngredientRecipeFromStack(this.inv.getStackInSlot(slot)) == null;
2018-08-10 05:04:07 +02:00
}
public void storeCoffee() {
2024-03-02 21:23:08 +01:00
if (StackUtil.isValid(this.inv.getStackInSlot(SLOT_COFFEE_BEANS)) && this.inv.getStackInSlot(SLOT_COFFEE_BEANS).is(ActuallyTags.Items.COFFEE_BEANS)) {
int toAdd = 2;
2018-08-10 05:04:07 +02:00
if (toAdd <= COFFEE_CACHE_MAX_AMOUNT - this.coffeeCacheAmount) {
this.inv.setStackInSlot(SLOT_COFFEE_BEANS, StackUtil.shrink(this.inv.getStackInSlot(SLOT_COFFEE_BEANS), 1));
this.coffeeCacheAmount += toAdd;
}
}
}
2018-08-10 05:04:07 +02:00
public void brew() {
if (this.level.isClientSide) {
2021-02-27 16:33:00 +01:00
return;
}
ItemStack input = this.inv.getStackInSlot(SLOT_INPUT);
if (!input.isEmpty() && input.is(ActuallyItems.EMPTY_CUP) && this.inv.getStackInSlot(SLOT_OUTPUT).isEmpty() && this.coffeeCacheAmount >= CACHE_USE && this.tank.getFluid().getFluid() == Fluids.WATER && this.tank.getFluidAmount() >= WATER_USE) {
2021-02-27 16:33:00 +01:00
if (this.storage.getEnergyStored() >= ENERGY_USED) {
if (this.brewTime % 30 == 0) {
2024-03-02 21:23:08 +01:00
this.level.playSound(null, this.getBlockPos().getX(), this.getBlockPos().getY(), this.getBlockPos().getZ(), AASounds.COFFEE_MACHINE.get(), SoundSource.BLOCKS, 0.1F, 1.0F);
2021-02-27 16:33:00 +01:00
}
2021-02-27 16:33:00 +01:00
this.brewTime++;
this.storage.extractEnergyInternal(ENERGY_USED, false);
if (this.brewTime >= TIME_USED) {
this.brewTime = 0;
ItemStack output = new ItemStack(ActuallyItems.COFFEE_CUP.get());
2021-02-27 16:33:00 +01:00
for (int i = 3; i < this.inv.getSlots(); i++) {
if (StackUtil.isValid(this.inv.getStackInSlot(i))) {
RecipeHolder<CoffeeIngredientRecipe> recipeHolder = ItemCoffee.getIngredientRecipeFromStack(this.inv.getStackInSlot(i));
if (recipeHolder != null) {
if (recipeHolder.value().effect(output)) {
2021-02-27 16:33:00 +01:00
this.inv.setStackInSlot(i, StackUtil.shrinkForContainer(this.inv.getStackInSlot(i), 1));
}
}
}
}
2021-02-27 16:33:00 +01:00
this.inv.setStackInSlot(SLOT_OUTPUT, output.copy());
this.inv.getStackInSlot(SLOT_INPUT).shrink(1);
this.coffeeCacheAmount -= CACHE_USE;
this.tank.drain(WATER_USE, IFluidHandler.FluidAction.EXECUTE);
}
2015-10-02 16:48:01 +02:00
}
2021-02-27 16:33:00 +01:00
} else {
this.brewTime = 0;
}
}
@Override
2024-03-02 21:23:08 +01:00
public void onButtonPressed(int buttonID, Player player) {
2018-08-10 05:04:07 +02:00
if (buttonID == 0 && this.brewTime <= 0) {
this.brew();
}
}
2016-06-05 02:16:52 +02:00
@Override
2024-03-04 20:21:48 +01:00
public IFluidHandler getFluidHandler(Direction facing) {
return this.tank;
2016-06-05 02:16:52 +02:00
}
@Override
2018-08-10 05:04:07 +02:00
public int getMaxFluidAmountToSplitShare() {
return 0;
}
@Override
2018-08-10 05:04:07 +02:00
public boolean doesShareFluid() {
return false;
}
@Override
public Direction[] getFluidShareSides() {
return null;
}
2016-11-26 08:58:42 +01:00
@Override
2024-03-04 20:21:48 +01:00
public IEnergyStorage getEnergyStorage(Direction facing) {
return this.storage;
2016-11-26 08:58:42 +01:00
}
@Override
2024-03-02 21:23:08 +01:00
public Component getDisplayName() {
2024-03-03 01:20:53 +01:00
return Component.translatable("container.actuallyadditions.coffeeMachine");
}
@Nullable
@Override
2024-03-02 21:23:08 +01:00
public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player player) {
return new ContainerCoffeeMachine(windowId, playerInventory, this);
}
}