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

253 lines
8.6 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
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
2016-01-29 17:38:31 +01:00
import de.ellpeck.actuallyadditions.mod.fluids.InitFluids;
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;
import net.minecraft.util.Direction;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
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;
2015-05-20 22:39:43 +02:00
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class TileEntityFermentingBarrel extends TileEntityBase implements ISharingFluidHandler, INamedContainerProvider {
2015-05-20 22:39:43 +02:00
2015-12-01 19:48:09 +01:00
private static final int PROCESS_TIME = 100;
2016-06-05 02:16:52 +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;
2015-10-03 10:16:18 +02:00
private int lastCanola;
private int lastOil;
private int lastProcessTime;
2016-11-28 13:11:29 +01:00
private int lastCompare;
2015-05-20 22:39:43 +02:00
2019-05-02 09:10:29 +02:00
public TileEntityFermentingBarrel() {
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);
compound.put("tanks", tanks.writeNBT());
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");
if (compound.contains("tanks")) {
//TODO read
}
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() {
super.updateEntity();
if (!this.level.isClientSide) {
int produce = 80;
if (this.tanks.getFluidInTank(0).getAmount() >= produce && produce <= this.tanks.getTankCapacity(1) - this.tanks.getFluidInTank(1).getAmount()) {
2015-05-20 22:39:43 +02:00
this.currentProcessTime++;
2019-05-02 09:10:29 +02:00
if (this.currentProcessTime >= PROCESS_TIME) {
2015-05-20 22:39:43 +02:00
this.currentProcessTime = 0;
this.tanks.oilTank.grow(produce);
this.tanks.canolaTank.shrink(produce);
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;
}
2015-05-20 22:39:43 +02:00
2016-11-28 13:11:29 +01:00
int compare = this.getComparatorStrength();
2019-05-02 09:10:29 +02:00
if (compare != this.lastCompare) {
2016-11-28 13:11:29 +01:00
this.lastCompare = compare;
this.setChanged();
2016-11-28 13:11:29 +01:00
}
if ((this.tanks.getFluidInTank(0).getAmount() != this.lastCanola || this.tanks.getFluidInTank(1).getAmount() != this.lastOil || this.currentProcessTime != this.lastProcessTime) && this.sendUpdateWithInterval()) {
this.lastProcessTime = this.currentProcessTime;
this.lastCanola = this.tanks.getFluidInTank(0).getAmount();
this.lastOil = 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() {
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) {
return this.currentProcessTime * i / PROCESS_TIME;
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();
}
@Override
public ITextComponent getDisplayName() {
return StringTextComponent.EMPTY;
}
@Nullable
@Override
public Container createMenu(int windowId, PlayerInventory playerInventory, PlayerEntity p_createMenu_3_) {
return new ContainerFermentingBarrel(windowId, playerInventory, this);
}
public class FermentingBarrelMultiTank implements IFluidHandler {
2021-10-24 18:03:33 +02:00
public FluidStack canolaTank = new FluidStack(InitFluids.CANOLA_OIL.get(), 0);
public FluidStack oilTank = new FluidStack(InitFluids.REFINED_CANOLA_OIL.get(), 0);
private int capacity = FluidAttributes.BUCKET_VOLUME * 2;
@Override
public int getTanks() {
return 2;
}
@Nonnull
@Override
public FluidStack getFluidInTank(int tank) {
return tank == 0? canolaTank:oilTank;
}
@Override
public int getTankCapacity(int tank) {
return capacity;
}
@Override
public boolean isFluidValid(int tank, @Nonnull FluidStack stack) {
2021-10-24 18:03:33 +02:00
return tank == 0? stack.getFluid() == InitFluids.CANOLA_OIL.get():stack.getFluid() == InitFluids.REFINED_CANOLA_OIL.get();
}
@Override
public int fill(FluidStack resource, FluidAction action) {
2021-10-24 18:03:33 +02:00
if (resource.isEmpty() || resource.getFluid() != InitFluids.CANOLA_OIL.get())
return 0;
if(action.simulate())
{
if (canolaTank.isEmpty())
return Math.min(capacity, resource.getAmount());
else
return Math.min(capacity - canolaTank.getAmount(), resource.getAmount());
}
else {
if (canolaTank.isEmpty()) {
canolaTank = new FluidStack(resource, Math.min(capacity, resource.getAmount()));
//TODO need to set the BE dirty.
return canolaTank.getAmount();
}
else {
int filledAmt = capacity - canolaTank.getAmount();
if (resource.getAmount() < filledAmt) {
canolaTank.grow(resource.getAmount());
filledAmt = resource.getAmount();
}
else
canolaTank.setAmount(capacity);
if (filledAmt > 0){
//TODO set BE dirty
}
return filledAmt;
}
}
}
public CompoundNBT writeNBT() {
CompoundNBT canolaNBT = new CompoundNBT();
canolaTank.writeToNBT(canolaNBT);
CompoundNBT oilNBT = new CompoundNBT();
oilTank.writeToNBT(oilNBT);
CompoundNBT nbt = new CompoundNBT();
nbt.put("canolaTank", canolaNBT);
nbt.put("oilTank", oilNBT);
return nbt;
}
@Nonnull
@Override
public FluidStack drain(FluidStack resource, FluidAction action) {
2021-10-24 18:03:33 +02:00
if (resource.isEmpty() || resource.getFluid() != InitFluids.REFINED_CANOLA_OIL.get())
return FluidStack.EMPTY;
return drain(resource.getAmount(), action);
}
@Nonnull
@Override
public FluidStack drain(int maxDrain, FluidAction action) {
int drained = maxDrain;
if (oilTank.getAmount() < drained)
{
drained = oilTank.getAmount();
}
FluidStack stack = new FluidStack(oilTank, drained);
if (action.execute() && drained > 0)
{
oilTank.shrink(drained);
//TODO set BE dirty
}
return stack;
}
}
2015-05-20 22:39:43 +02:00
}