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;
|
2021-09-30 23:23:56 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
|
2022-01-15 19:38:00 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.crafting.FermentingRecipe;
|
2021-03-01 18:46:12 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.inventory.ContainerFermentingBarrel;
|
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.entity.player.PlayerInventory;
|
|
|
|
import net.minecraft.inventory.container.Container;
|
|
|
|
import net.minecraft.inventory.container.INamedContainerProvider;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2021-02-27 13:24:45 +01:00
|
|
|
import net.minecraft.util.Direction;
|
2021-03-01 18:46:12 +01:00
|
|
|
import net.minecraft.util.text.ITextComponent;
|
2022-01-15 19:38:00 +01:00
|
|
|
import net.minecraft.util.text.TranslationTextComponent;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
2021-09-30 23:23:56 +02:00
|
|
|
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
|
|
|
|
2021-09-30 23:23:56 +02:00
|
|
|
import javax.annotation.Nonnull;
|
2021-03-01 18:46:12 +01:00
|
|
|
import javax.annotation.Nullable;
|
2022-01-15 19:38:00 +01:00
|
|
|
import java.util.Optional;
|
2021-03-01 18:46:12 +01:00
|
|
|
|
|
|
|
public class TileEntityFermentingBarrel extends TileEntityBase implements ISharingFluidHandler, INamedContainerProvider {
|
2021-09-30 23:23:56 +02:00
|
|
|
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;
|
2015-07-02 04:21:21 +02:00
|
|
|
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
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public TileEntityFermentingBarrel() {
|
2021-09-30 23:23:56 +02:00
|
|
|
super(ActuallyBlocks.FERMENTING_BARREL.getTileEntityType());
|
2015-05-20 22:39:43 +02:00
|
|
|
}
|
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void writeSyncableNBT(CompoundNBT compound, NBTType type) {
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putInt("ProcessTime", this.currentProcessTime);
|
2021-09-30 23:23:56 +02:00
|
|
|
compound.put("tanks", tanks.writeNBT());
|
2022-01-15 19:38:00 +01:00
|
|
|
if (currentRecipe != null)
|
|
|
|
compound.putString("currentRecipe", currentRecipe.getId().toString());
|
2016-07-02 15:01:46 +02:00
|
|
|
super.writeSyncableNBT(compound, type);
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void readSyncableNBT(CompoundNBT compound, NBTType type) {
|
2021-02-27 16:33:00 +01:00
|
|
|
this.currentProcessTime = compound.getInt("ProcessTime");
|
2021-09-30 23:23:56 +02:00
|
|
|
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);
|
2016-12-03 12:31:17 +01:00
|
|
|
}
|
2016-07-02 15:01:46 +02:00
|
|
|
super.readSyncableNBT(compound, type);
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-20 22:39:43 +02:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public void updateEntity() {
|
2015-11-18 23:11:24 +01:00
|
|
|
super.updateEntity();
|
2022-01-15 19:38:00 +01:00
|
|
|
if (this.level.isClientSide)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (currentRecipe == null) {
|
2022-01-16 19:54:17 +01:00
|
|
|
//No recipe currently selected, check for one every 20 ticks
|
|
|
|
if (ticksElapsed % 20 == 0)
|
|
|
|
this.currentRecipe = ActuallyAdditionsAPI.FERMENTING_RECIPES.stream().filter(recipe -> recipe.matches(this.tanks.getFluidInTank(0), this.tanks.getFluidInTank(1))).findFirst().orElse(null);
|
2022-01-15 19:38:00 +01:00
|
|
|
} else {
|
|
|
|
if (this.tanks.getFluidInTank(0).getAmount() >= currentRecipe.getInput().getAmount() &&
|
|
|
|
this.tanks.getFluidInTank(0).getFluid().isSame(currentRecipe.getInput().getFluid()) &&
|
|
|
|
(this.tanks.getFluidInTank(1).getFluid().isSame(currentRecipe.getOutput().getFluid()) || tanks.getFluidInTank(1).isEmpty()) &&
|
|
|
|
currentRecipe.getOutput().getAmount() <= this.tanks.getTankCapacity(1) - this.tanks.getFluidInTank(1).getAmount()) {
|
|
|
|
|
2015-05-20 22:39:43 +02:00
|
|
|
this.currentProcessTime++;
|
2022-01-15 19:38:00 +01:00
|
|
|
if (this.currentProcessTime >= currentRecipe.getTime()) {
|
2015-05-20 22:39:43 +02:00
|
|
|
this.currentProcessTime = 0;
|
|
|
|
|
2022-01-15 19:38:00 +01:00
|
|
|
this.tanks.outputTank.fill(currentRecipe.getOutput().copy(), IFluidHandler.FluidAction.EXECUTE);
|
|
|
|
this.tanks.inputTank.getFluid().shrink(currentRecipe.getInput().getAmount());
|
2015-05-20 22:39:43 +02:00
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2015-10-02 16:48:01 +02:00
|
|
|
this.currentProcessTime = 0;
|
2022-01-15 19:38:00 +01:00
|
|
|
currentRecipe = null;
|
2015-10-02 16:48:01 +02:00
|
|
|
}
|
2022-01-15 19:38:00 +01:00
|
|
|
}
|
|
|
|
int compare = this.getComparatorStrength();
|
|
|
|
if (compare != this.lastCompare) {
|
|
|
|
this.lastCompare = compare;
|
2015-05-20 22:39:43 +02:00
|
|
|
|
2022-01-15 19:38:00 +01:00
|
|
|
this.setChanged();
|
|
|
|
}
|
2016-11-28 13:11:29 +01:00
|
|
|
|
2022-01-15 19:38:00 +01:00
|
|
|
if ((this.tanks.getFluidInTank(0).getAmount() != this.lastInput || this.tanks.getFluidInTank(1).getAmount() != this.lastOutput || this.currentProcessTime != this.lastProcessTime) && this.sendUpdateWithInterval()) {
|
|
|
|
this.lastProcessTime = this.currentProcessTime;
|
|
|
|
this.lastInput = this.tanks.getFluidInTank(0).getAmount();
|
|
|
|
this.lastOutput = this.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() {
|
2021-09-30 23:23:56 +02:00
|
|
|
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) {
|
2021-09-30 23:23:56 +02:00
|
|
|
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) {
|
2021-09-30 23:23:56 +02:00
|
|
|
return this.tanks.getFluidInTank(0).getAmount() * i / this.tanks.getTankCapacity(0);
|
2015-05-20 22:39:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-09-30 23:23:56 +02:00
|
|
|
public LazyOptional<IFluidHandler> getFluidHandler(Direction facing) {
|
|
|
|
return fluidOptional;
|
2015-05-20 22:39:43 +02:00
|
|
|
}
|
2015-12-15 18:51:13 +01:00
|
|
|
|
2016-06-10 21:52:37 +02:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public int getMaxFluidAmountToSplitShare() {
|
2021-09-30 23:23:56 +02:00
|
|
|
return tanks.getFluidInTank(1).getAmount();
|
2016-08-31 20:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean doesShareFluid() {
|
2016-08-31 20:16:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-27 13:24:45 +01:00
|
|
|
public Direction[] getFluidShareSides() {
|
|
|
|
return Direction.values();
|
2016-08-31 20:16:36 +02:00
|
|
|
}
|
2021-03-01 18:46:12 +01:00
|
|
|
|
2022-01-16 19:54:17 +01:00
|
|
|
@Nonnull
|
2021-03-01 18:46:12 +01:00
|
|
|
@Override
|
|
|
|
public ITextComponent getDisplayName() {
|
2022-01-15 19:38:00 +01:00
|
|
|
return new TranslationTextComponent("container.actuallyadditions.fermenting_barrel");
|
2021-03-01 18:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2022-01-16 19:54:17 +01:00
|
|
|
public Container createMenu(int windowId, @Nonnull PlayerInventory playerInventory, @Nonnull PlayerEntity p_createMenu_3_) {
|
2021-03-01 18:46:12 +01:00
|
|
|
return new ContainerFermentingBarrel(windowId, playerInventory, this);
|
|
|
|
}
|
2021-09-30 23:23:56 +02:00
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-09-30 23:23:56 +02:00
|
|
|
|
2022-01-16 19:54:17 +01:00
|
|
|
public static class FermentingBarrelMultiTank implements IFluidHandler {
|
2021-09-30 23:23:56 +02:00
|
|
|
|
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);
|
2021-09-30 23:23:56 +02:00
|
|
|
|
|
|
|
@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();
|
2021-09-30 23:23:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
2021-09-30 23:23:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int fill(FluidStack resource, FluidAction action) {
|
2022-01-15 19:38:00 +01:00
|
|
|
if (resource.isEmpty() || !validInput(resource))
|
|
|
|
return 0;
|
2021-09-30 23:23:56 +02:00
|
|
|
|
|
|
|
if(action.simulate())
|
|
|
|
{
|
2022-01-15 19:38:00 +01:00
|
|
|
if (inputTank.isEmpty())
|
2021-09-30 23:23:56 +02:00
|
|
|
return Math.min(capacity, resource.getAmount());
|
|
|
|
else
|
2022-01-15 19:38:00 +01:00
|
|
|
return Math.min(capacity - inputTank.getFluid().getAmount(), resource.getAmount());
|
2021-09-30 23:23:56 +02:00
|
|
|
}
|
|
|
|
else {
|
2022-01-15 19:38:00 +01:00
|
|
|
if (inputTank.isEmpty()) {
|
|
|
|
inputTank.fill(new FluidStack(resource, Math.min(capacity, resource.getAmount())), FluidAction.EXECUTE);
|
2021-09-30 23:23:56 +02:00
|
|
|
//TODO need to set the BE dirty.
|
2022-01-15 19:38:00 +01:00
|
|
|
return inputTank.getFluid().getAmount();
|
2021-09-30 23:23:56 +02:00
|
|
|
}
|
|
|
|
else {
|
2022-01-15 19:38:00 +01:00
|
|
|
int filledAmt = capacity - inputTank.getFluid().getAmount();
|
2021-09-30 23:23:56 +02:00
|
|
|
if (resource.getAmount() < filledAmt) {
|
2022-01-15 19:38:00 +01:00
|
|
|
inputTank.getFluid().grow(resource.getAmount());
|
2021-09-30 23:23:56 +02:00
|
|
|
filledAmt = resource.getAmount();
|
|
|
|
}
|
|
|
|
else
|
2022-01-15 19:38:00 +01:00
|
|
|
inputTank.getFluid().setAmount(capacity);
|
2021-09-30 23:23:56 +02:00
|
|
|
|
|
|
|
if (filledAmt > 0){
|
|
|
|
//TODO set BE dirty
|
|
|
|
}
|
|
|
|
return filledAmt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public CompoundNBT writeNBT() {
|
2022-01-15 19:38:00 +01:00
|
|
|
CompoundNBT inputNBT = new CompoundNBT();
|
|
|
|
inputTank.writeToNBT(inputNBT);
|
|
|
|
CompoundNBT outputNBT = new CompoundNBT();
|
|
|
|
outputTank.writeToNBT(outputNBT);
|
2021-09-30 23:23:56 +02:00
|
|
|
|
|
|
|
CompoundNBT nbt = new CompoundNBT();
|
2022-01-15 19:38:00 +01:00
|
|
|
nbt.put("inputTank", inputNBT);
|
|
|
|
nbt.put("outputTank", outputNBT);
|
2021-09-30 23:23:56 +02:00
|
|
|
return nbt;
|
|
|
|
}
|
|
|
|
|
2022-01-15 19:38:00 +01:00
|
|
|
public void readNBT(CompoundNBT nbt) {
|
|
|
|
inputTank.readFromNBT(nbt.getCompound("inputTank"));
|
|
|
|
outputTank.readFromNBT(nbt.getCompound("outputTank"));
|
|
|
|
}
|
|
|
|
|
2021-09-30 23:23:56 +02:00
|
|
|
@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())
|
2021-09-30 23:23:56 +02:00
|
|
|
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)
|
2021-09-30 23:23:56 +02:00
|
|
|
{
|
2022-01-15 19:38:00 +01:00
|
|
|
drained = outputTank.getFluid().getAmount();
|
2021-09-30 23:23:56 +02:00
|
|
|
}
|
2022-01-15 19:38:00 +01:00
|
|
|
FluidStack stack = new FluidStack(outputTank.getFluid(), drained);
|
2021-09-30 23:23:56 +02:00
|
|
|
if (action.execute() && drained > 0)
|
|
|
|
{
|
2022-01-15 19:38:00 +01:00
|
|
|
outputTank.getFluid().shrink(drained);
|
2021-09-30 23:23:56 +02:00
|
|
|
//TODO set BE dirty
|
|
|
|
}
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
}
|
2015-05-20 22:39:43 +02:00
|
|
|
}
|