mirror of
https://github.com/GTNewHorizons/ForestryMC.git
synced 2026-11-21 12:32:23 +00:00
Clean up Rain Tank implementation
This commit is contained in:
1 parent
d0c9a77528
commit
065344ba5e
4 files changed
+102
-60
No files matched your search
@@ -0,0 +1,65 @@
|
||||
package forestry.core.fluids;
|
||||
|
||||
import forestry.core.utils.ItemStackUtil;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.FluidTank;
|
||||
|
||||
/**
|
||||
* Helper to slowly fill containers from a machine's internal tank.
|
||||
* Moves filled container from inputSlot to outputSlot.
|
||||
*/
|
||||
public class ContainerFiller {
|
||||
private final FluidTank fluidTank;
|
||||
private final int fillingTime;
|
||||
private final IInventory inventory;
|
||||
private final int inputSlot;
|
||||
private final int outputSlot;
|
||||
|
||||
private ItemStack usedInput;
|
||||
private int fillingProgress;
|
||||
|
||||
public ContainerFiller(FluidTank fluidTank, int fillingTime, IInventory inventory, int inputSlot, int outputSlot) {
|
||||
this.fluidTank = fluidTank;
|
||||
this.fillingTime = fillingTime;
|
||||
this.inventory = inventory;
|
||||
this.inputSlot = inputSlot;
|
||||
this.outputSlot = outputSlot;
|
||||
}
|
||||
|
||||
public void updateServerSide() {
|
||||
ItemStack input = inventory.getStackInSlot(inputSlot);
|
||||
if (usedInput == null || !ItemStackUtil.isIdenticalItem(usedInput, input)) {
|
||||
fillingProgress = 0;
|
||||
usedInput = input;
|
||||
}
|
||||
|
||||
if (usedInput != null) {
|
||||
FluidStack tankContents = fluidTank.getFluid();
|
||||
if (tankContents != null && tankContents.amount > 0) {
|
||||
if (fillingProgress == 0) {
|
||||
Fluid tankFluid = tankContents.getFluid();
|
||||
FluidHelper.FillStatus canFill = FluidHelper.fillContainers(fluidTank, inventory, inputSlot, outputSlot, tankFluid, false);
|
||||
if (canFill == FluidHelper.FillStatus.SUCCESS) {
|
||||
fillingProgress = 1;
|
||||
}
|
||||
} else {
|
||||
fillingProgress++;
|
||||
if (fillingProgress >= fillingTime) {
|
||||
Fluid tankFluid = tankContents.getFluid();
|
||||
FluidHelper.FillStatus filled = FluidHelper.fillContainers(fluidTank, inventory, inputSlot, outputSlot, tankFluid, true);
|
||||
if (filled == FluidHelper.FillStatus.SUCCESS) {
|
||||
fillingProgress = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int getFillingProgress() {
|
||||
return fillingProgress;
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@ public class GuiRaintank extends GuiForestryTitled<ContainerRaintank, TileRainta
|
||||
|
||||
if (inventory.isFilling()) {
|
||||
int progress = inventory.getFillProgressScaled(24);
|
||||
drawTexturedModalRect(guiLeft + 80, guiTop + 39, 176, 74, 24 - progress, 16);
|
||||
drawTexturedModalRect(guiLeft + 80, guiTop + 39, 176, 74, progress, 16);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,17 +14,6 @@ import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.IOException;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.ISidedInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
|
||||
|
||||
import forestry.api.core.IErrorLogic;
|
||||
import forestry.core.config.Constants;
|
||||
import forestry.core.errors.EnumErrorCode;
|
||||
@@ -41,6 +30,15 @@ import forestry.factory.gui.ContainerBottler;
|
||||
import forestry.factory.gui.GuiBottler;
|
||||
import forestry.factory.inventory.InventoryBottler;
|
||||
import forestry.factory.recipes.BottlerRecipe;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.ISidedInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
|
||||
|
||||
public class TileBottler extends TilePowered implements ISidedInventory, ILiquidTankTile {
|
||||
private static final int TICKS_PER_RECIPE_TIME = 5;
|
||||
@@ -129,11 +127,12 @@ public class TileBottler extends TilePowered implements ISidedInventory, ILiquid
|
||||
@Override
|
||||
public boolean hasResourcesMin(float percentage) {
|
||||
IInventoryAdapter inventory = getInternalInventory();
|
||||
if (inventory.getStackInSlot(InventoryBottler.SLOT_INPUT_EMPTY_CAN) == null) {
|
||||
ItemStack emptyCan = inventory.getStackInSlot(InventoryBottler.SLOT_INPUT_EMPTY_CAN);
|
||||
if (emptyCan == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (float) inventory.getStackInSlot(InventoryBottler.SLOT_INPUT_EMPTY_CAN).stackSize / (float) inventory.getStackInSlot(InventoryBottler.SLOT_INPUT_EMPTY_CAN).getMaxStackSize() > percentage;
|
||||
return (float) emptyCan.stackSize / (float) emptyCan.getMaxStackSize() > percentage;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -14,43 +14,41 @@ import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.IOException;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.inventory.IContainerListener;
|
||||
import net.minecraft.inventory.ISidedInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.biome.Biome;
|
||||
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.fluids.FluidRegistry;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
|
||||
|
||||
import forestry.api.core.IErrorLogic;
|
||||
import forestry.core.config.Constants;
|
||||
import forestry.core.errors.EnumErrorCode;
|
||||
import forestry.core.fluids.FluidHelper;
|
||||
import forestry.core.fluids.ContainerFiller;
|
||||
import forestry.core.fluids.TankManager;
|
||||
import forestry.core.fluids.tanks.FilteredTank;
|
||||
import forestry.core.network.DataInputStreamForestry;
|
||||
import forestry.core.network.DataOutputStreamForestry;
|
||||
import forestry.core.tiles.ILiquidTankTile;
|
||||
import forestry.core.tiles.TileBase;
|
||||
import forestry.core.utils.ItemStackUtil;
|
||||
import forestry.factory.gui.ContainerRaintank;
|
||||
import forestry.factory.gui.GuiRaintank;
|
||||
import forestry.factory.inventory.InventoryRaintank;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.inventory.IContainerListener;
|
||||
import net.minecraft.inventory.ISidedInventory;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.biome.Biome;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.fluids.FluidRegistry;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
|
||||
|
||||
public class TileRaintank extends TileBase implements ISidedInventory, ILiquidTankTile {
|
||||
private static final FluidStack STACK_WATER = new FluidStack(FluidRegistry.WATER, Constants.RAINTANK_AMOUNT_PER_UPDATE);
|
||||
|
||||
private final FilteredTank resourceTank;
|
||||
private final TankManager tankManager;
|
||||
private int fillingTime;
|
||||
private ItemStack usedEmpty;
|
||||
private final ContainerFiller containerFiller;
|
||||
|
||||
// client
|
||||
private int fillingProgress;
|
||||
|
||||
public TileRaintank() {
|
||||
super("raintank");
|
||||
@@ -59,6 +57,8 @@ public class TileRaintank extends TileBase implements ISidedInventory, ILiquidTa
|
||||
resourceTank = new FilteredTank(Constants.RAINTANK_TANK_CAPACITY).setFilters(FluidRegistry.WATER);
|
||||
|
||||
tankManager = new TankManager(this, resourceTank);
|
||||
|
||||
containerFiller = new ContainerFiller(resourceTank, Constants.RAINTANK_FILLING_TIME, this, InventoryRaintank.SLOT_RESOURCE, InventoryRaintank.SLOT_PRODUCT);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@@ -108,51 +108,29 @@ public class TileRaintank extends TileBase implements ISidedInventory, ILiquidTa
|
||||
if (!errorLogic.hasErrors()) {
|
||||
resourceTank.fillInternal(STACK_WATER, true);
|
||||
}
|
||||
|
||||
if (!ItemStackUtil.isIdenticalItem(usedEmpty, getStackInSlot(InventoryRaintank.SLOT_RESOURCE))) {
|
||||
fillingTime = 0;
|
||||
usedEmpty = null;
|
||||
}
|
||||
|
||||
if (usedEmpty == null) {
|
||||
usedEmpty = getStackInSlot(InventoryRaintank.SLOT_RESOURCE);
|
||||
}
|
||||
|
||||
if (!isFilling()) {
|
||||
FluidHelper.FillStatus canFill = FluidHelper.fillContainers(tankManager, this, InventoryRaintank.SLOT_RESOURCE, InventoryRaintank.SLOT_PRODUCT, FluidRegistry.WATER, false);
|
||||
if (canFill == FluidHelper.FillStatus.SUCCESS) {
|
||||
fillingTime = Constants.RAINTANK_FILLING_TIME;
|
||||
}
|
||||
} else {
|
||||
fillingTime--;
|
||||
if (fillingTime <= 0) {
|
||||
FluidHelper.FillStatus filled = FluidHelper.fillContainers(tankManager, this, InventoryRaintank.SLOT_RESOURCE, InventoryRaintank.SLOT_PRODUCT, FluidRegistry.WATER, true);
|
||||
if (filled == FluidHelper.FillStatus.SUCCESS) {
|
||||
fillingTime = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
containerFiller.updateServerSide();
|
||||
}
|
||||
|
||||
public boolean isFilling() {
|
||||
return fillingTime > 0;
|
||||
return fillingProgress > 0;
|
||||
}
|
||||
|
||||
public int getFillProgressScaled(int i) {
|
||||
return fillingTime * i / Constants.RAINTANK_FILLING_TIME;
|
||||
return fillingProgress * i / Constants.RAINTANK_FILLING_TIME;
|
||||
}
|
||||
|
||||
/* SMP GUI */
|
||||
public void getGUINetworkData(int i, int j) {
|
||||
switch (i) {
|
||||
case 0:
|
||||
fillingTime = j;
|
||||
fillingProgress = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void sendGUINetworkData(Container container, IContainerListener iCrafting) {
|
||||
iCrafting.sendProgressBarUpdate(container, 0, fillingTime);
|
||||
iCrafting.sendProgressBarUpdate(container, 0, containerFiller.getFillingProgress());
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
|
||||
Reference in new issue
Block a user