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

295 lines
11 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityFermentingBarrel.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-05-20 22:39:43 +02:00
2022-01-15 19:38:00 +01:00
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
2022-01-15 19:38:00 +01:00
import de.ellpeck.actuallyadditions.mod.crafting.FermentingRecipe;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerFermentingBarrel;
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.network.chat.TranslatableComponent;
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.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
2021-02-27 16:33:00 +01:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fluids.FluidAttributes;
2016-11-26 21:32:27 +01:00
import net.minecraftforge.fluids.FluidStack;
2016-06-05 02:16:52 +02:00
import net.minecraftforge.fluids.capability.IFluidHandler;
2022-01-07 00:16:37 +01:00
import net.minecraftforge.fluids.capability.templates.FluidTank;
2015-05-20 22:39:43 +02:00
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
2022-01-15 19:38:00 +01:00
import java.util.Optional;
2024-03-02 21:23:08 +01:00
public class TileEntityFermentingBarrel extends TileEntityBase implements ISharingFluidHandler, MenuProvider {
public final FermentingBarrelMultiTank tanks = new FermentingBarrelMultiTank();
public final LazyOptional<IFluidHandler> fluidOptional = LazyOptional.of(()->tanks);
2016-12-05 14:16:53 +01:00
2015-05-20 22:39:43 +02:00
public int currentProcessTime;
2022-01-15 19:38:00 +01:00
private int lastInput;
private int lastOutput;
private int lastProcessTime;
2016-11-28 13:11:29 +01:00
private int lastCompare;
2022-01-15 19:38:00 +01:00
private FermentingRecipe currentRecipe;
2015-05-20 22:39:43 +02:00
2024-03-02 21:23:08 +01:00
public TileEntityFermentingBarrel(BlockPos pos, BlockState state) {
super(ActuallyBlocks.FERMENTING_BARREL.getTileEntityType(), pos, state);
2015-05-20 22:39:43 +02:00
}
2016-02-01 17:49:55 +01:00
@Override
2024-03-02 21:23:08 +01:00
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
2021-02-27 16:33:00 +01:00
compound.putInt("ProcessTime", this.currentProcessTime);
compound.put("tanks", tanks.writeNBT());
2022-01-15 19:38:00 +01:00
if (currentRecipe != null)
compound.putString("currentRecipe", currentRecipe.getId().toString());
super.writeSyncableNBT(compound, type);
2016-02-01 17:49:55 +01:00
}
@Override
2024-03-02 21:23:08 +01:00
public void readSyncableNBT(CompoundTag compound, NBTType type) {
2021-02-27 16:33:00 +01:00
this.currentProcessTime = compound.getInt("ProcessTime");
if (compound.contains("tanks")) {
2022-01-15 19:38:00 +01:00
tanks.readNBT(compound.getCompound("tanks"));
}
if (compound.contains("currentRecipe")) {
this.currentRecipe = ActuallyAdditionsAPI.FERMENTING_RECIPES.stream().filter(recipe -> recipe.getId().toString().equals(compound.getString("currentRecipe"))).findFirst().orElse(null);
}
super.readSyncableNBT(compound, type);
2016-02-01 17:49:55 +01:00
}
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 TileEntityFermentingBarrel tile) {
tile.clientTick();
}
}
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityFermentingBarrel tile) {
tile.serverTick();
if (tile.currentRecipe == null) {
//No recipe currently selected, check for one every 20 ticks
if (tile.ticksElapsed % 20 == 0)
tile.currentRecipe = ActuallyAdditionsAPI.FERMENTING_RECIPES.stream().filter(recipe -> recipe.matches(tile.tanks.getFluidInTank(0), tile.tanks.getFluidInTank(1))).findFirst().orElse(null);
2019-05-02 09:10:29 +02:00
} else {
2024-03-02 21:23:08 +01:00
if (tile.tanks.getFluidInTank(0).getAmount() >= tile.currentRecipe.getInput().getAmount() &&
tile.tanks.getFluidInTank(0).getFluid().isSame(tile.currentRecipe.getInput().getFluid()) &&
(tile.tanks.getFluidInTank(1).getFluid().isSame(tile.currentRecipe.getOutput().getFluid()) || tile.tanks.getFluidInTank(1).isEmpty()) &&
tile.currentRecipe.getOutput().getAmount() <= tile.tanks.getTankCapacity(1) - tile.tanks.getFluidInTank(1).getAmount()) {
tile.currentProcessTime++;
if (tile.currentProcessTime >= tile.currentRecipe.getTime()) {
tile.currentProcessTime = 0;
tile.tanks.outputTank.fill(tile.currentRecipe.getOutput().copy(), IFluidHandler.FluidAction.EXECUTE);
tile.tanks.inputTank.getFluid().shrink(tile.currentRecipe.getInput().getAmount());
}
} else {
tile.currentProcessTime = 0;
tile.currentRecipe = null;
}
2015-10-02 16:48:01 +02:00
}
2024-03-02 21:23:08 +01:00
int compare = tile.getComparatorStrength();
if (compare != tile.lastCompare) {
tile.lastCompare = compare;
2015-05-20 22:39:43 +02:00
2024-03-02 21:23:08 +01:00
tile.setChanged();
}
2016-11-28 13:11:29 +01:00
2024-03-02 21:23:08 +01:00
if ((tile.tanks.getFluidInTank(0).getAmount() != tile.lastInput || tile.tanks.getFluidInTank(1).getAmount() != tile.lastOutput || tile.currentProcessTime != tile.lastProcessTime) && tile.sendUpdateWithInterval()) {
tile.lastProcessTime = tile.currentProcessTime;
tile.lastInput = tile.tanks.getFluidInTank(0).getAmount();
tile.lastOutput = tile.tanks.getFluidInTank(1).getAmount();
}
2015-05-20 22:39:43 +02:00
}
}
2016-11-28 13:11:29 +01:00
@Override
2019-05-02 09:10:29 +02:00
public int getComparatorStrength() {
float calc = (float) this.tanks.getFluidInTank(1).getAmount() / (float) this.tanks.getTankCapacity(1) * 15F;
2019-05-02 09:10:29 +02:00
return (int) calc;
2016-11-28 13:11:29 +01:00
}
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
2019-05-02 09:10:29 +02:00
public int getProcessScaled(int i) {
2022-01-15 19:38:00 +01:00
if (currentRecipe != null)
return this.currentProcessTime * i / currentRecipe.getTime();
else
return this.currentProcessTime * i / 100;
2015-12-01 19:48:09 +01:00
}
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
2019-05-02 09:10:29 +02:00
public int getOilTankScaled(int i) {
return this.tanks.getFluidInTank(1).getAmount() * i / this.tanks.getTankCapacity(1);
2015-05-20 22:39:43 +02:00
}
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
2019-05-02 09:10:29 +02:00
public int getCanolaTankScaled(int i) {
return this.tanks.getFluidInTank(0).getAmount() * i / this.tanks.getTankCapacity(0);
2015-05-20 22:39:43 +02:00
}
@Override
public LazyOptional<IFluidHandler> getFluidHandler(Direction facing) {
return fluidOptional;
2015-05-20 22:39:43 +02:00
}
@Override
2019-05-02 09:10:29 +02:00
public int getMaxFluidAmountToSplitShare() {
return tanks.getFluidInTank(1).getAmount();
}
@Override
2019-05-02 09:10:29 +02:00
public boolean doesShareFluid() {
return true;
}
@Override
public Direction[] getFluidShareSides() {
return Direction.values();
}
2022-01-16 19:54:17 +01:00
@Nonnull
@Override
2024-03-02 21:23:08 +01:00
public Component getDisplayName() {
return new TranslatableComponent("container.actuallyadditions.fermenting_barrel");
}
@Nullable
@Override
2024-03-02 21:23:08 +01:00
public AbstractContainerMenu createMenu(int windowId, @Nonnull Inventory playerInventory, @Nonnull Player p_createMenu_3_) {
return new ContainerFermentingBarrel(windowId, playerInventory, this);
}
2022-01-15 19:38:00 +01:00
public static boolean validInput(FluidStack stack) {
return getRecipeForInput(stack).isPresent();
}
public static Optional<FermentingRecipe> getRecipeForInput(FluidStack stack) {
return ActuallyAdditionsAPI.FERMENTING_RECIPES.stream().filter(recipe -> recipe.matches(stack)).findFirst();
}
2022-01-16 19:54:17 +01:00
public static class FermentingBarrelMultiTank implements IFluidHandler {
2022-01-16 19:54:17 +01:00
private final int capacity = FluidAttributes.BUCKET_VOLUME * 2;
2022-01-15 19:38:00 +01:00
public FluidTank inputTank = new FluidTank(capacity);
public FluidTank outputTank = new FluidTank(capacity);
@Override
public int getTanks() {
return 2;
}
@Nonnull
@Override
public FluidStack getFluidInTank(int tank) {
2022-01-15 19:38:00 +01:00
return tank == 0 ? inputTank.getFluid() : outputTank.getFluid();
}
@Override
public int getTankCapacity(int tank) {
return capacity;
}
@Override
public boolean isFluidValid(int tank, @Nonnull FluidStack stack) {
2022-01-15 19:38:00 +01:00
return tank == 0 && TileEntityFermentingBarrel.validInput(stack);
}
@Override
public int fill(FluidStack resource, FluidAction action) {
2022-01-15 19:38:00 +01:00
if (resource.isEmpty() || !validInput(resource))
return 0;
if(action.simulate())
{
2022-01-15 19:38:00 +01:00
if (inputTank.isEmpty())
return Math.min(capacity, resource.getAmount());
else
2022-01-15 19:38:00 +01:00
return Math.min(capacity - inputTank.getFluid().getAmount(), resource.getAmount());
}
else {
2022-01-15 19:38:00 +01:00
if (inputTank.isEmpty()) {
inputTank.fill(new FluidStack(resource, Math.min(capacity, resource.getAmount())), FluidAction.EXECUTE);
//TODO need to set the BE dirty.
2022-01-15 19:38:00 +01:00
return inputTank.getFluid().getAmount();
}
else {
2022-01-15 19:38:00 +01:00
int filledAmt = capacity - inputTank.getFluid().getAmount();
if (resource.getAmount() < filledAmt) {
2022-01-15 19:38:00 +01:00
inputTank.getFluid().grow(resource.getAmount());
filledAmt = resource.getAmount();
}
else
2022-01-15 19:38:00 +01:00
inputTank.getFluid().setAmount(capacity);
if (filledAmt > 0){
//TODO set BE dirty
}
return filledAmt;
}
}
}
2024-03-02 21:23:08 +01:00
public CompoundTag writeNBT() {
CompoundTag inputNBT = new CompoundTag();
2022-01-15 19:38:00 +01:00
inputTank.writeToNBT(inputNBT);
2024-03-02 21:23:08 +01:00
CompoundTag outputNBT = new CompoundTag();
2022-01-15 19:38:00 +01:00
outputTank.writeToNBT(outputNBT);
2024-03-02 21:23:08 +01:00
CompoundTag nbt = new CompoundTag();
2022-01-15 19:38:00 +01:00
nbt.put("inputTank", inputNBT);
nbt.put("outputTank", outputNBT);
return nbt;
}
2024-03-02 21:23:08 +01:00
public void readNBT(CompoundTag nbt) {
2022-01-15 19:38:00 +01:00
inputTank.readFromNBT(nbt.getCompound("inputTank"));
outputTank.readFromNBT(nbt.getCompound("outputTank"));
}
@Nonnull
@Override
public FluidStack drain(FluidStack resource, FluidAction action) {
2022-01-15 19:38:00 +01:00
if (resource.isEmpty() || resource.getFluid() != outputTank.getFluid().getFluid())
return FluidStack.EMPTY;
return drain(resource.getAmount(), action);
}
@Nonnull
@Override
public FluidStack drain(int maxDrain, FluidAction action) {
int drained = maxDrain;
2022-01-15 19:38:00 +01:00
if (outputTank.getFluid().getAmount() < drained)
{
2022-01-15 19:38:00 +01:00
drained = outputTank.getFluid().getAmount();
}
2022-01-15 19:38:00 +01:00
FluidStack stack = new FluidStack(outputTank.getFluid(), drained);
if (action.execute() && drained > 0)
{
2022-01-15 19:38:00 +01:00
outputTank.getFluid().shrink(drained);
//TODO set BE dirty
}
return stack;
}
}
2015-05-20 22:39:43 +02:00
}