Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework tank logic #561

Merged
merged 12 commits into from
Feb 15, 2024
1 change: 1 addition & 0 deletions src/core/java/com/enderio/core/CoreNBTKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class CoreNBTKeys {
public static final String ITEM = "Item";
public static final String ITEMS = "Items";
public static final String FLUID = "Fluid";
public static final String FLUIDS = "Fluids";
public static final String ENERGY = "Energy";
public static final String BLOCK_ENTITY_TAG = BlockItem.BLOCK_ENTITY_TAG;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.enderio.base.common.util.ExperienceUtil;
import com.enderio.core.client.gui.widgets.EIOWidget;
import com.enderio.machines.common.io.fluid.MachineFluidTank;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
Expand All @@ -10,17 +11,16 @@
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.fluids.capability.templates.FluidTank;

import java.util.function.Supplier;

public class ExperienceCraftingWidget extends EIOWidget {
private static final ResourceLocation GUI_ICONS_LOCATION = new ResourceLocation("textures/gui/icons.png");
private final Screen displayOn;
private final Supplier<FluidTank> getFluid;
private final Supplier<MachineFluidTank> getFluid;
private final Supplier<Integer> maxXP;

public ExperienceCraftingWidget(Screen displayOn, Supplier<FluidTank> getFluid, Supplier<Integer> maxXP, int pX, int pY, int pWidth, int pHeight) {
public ExperienceCraftingWidget(Screen displayOn, Supplier<MachineFluidTank> getFluid, Supplier<Integer> maxXP, int pX, int pY, int pWidth, int pHeight) {
super(pX, pY, pWidth, pHeight);
this.displayOn = displayOn;
this.getFluid = getFluid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

import com.enderio.base.common.util.ExperienceUtil;
import com.enderio.core.client.gui.widgets.EIOWidget;
import com.enderio.machines.common.io.fluid.MachineFluidTank;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.fluids.capability.templates.FluidTank;

import java.util.function.Supplier;

public class ExperienceWidget extends EIOWidget {
private static final ResourceLocation GUI_ICONS_LOCATION = new ResourceLocation("textures/gui/icons.png");
private final Screen displayOn;
private final Supplier<FluidTank> getFluid;
private final Supplier<MachineFluidTank> getFluid;

public ExperienceWidget(Screen displayOn, Supplier<FluidTank> getFluid, int pX, int pY, int pWidth, int pHeight) {
public ExperienceWidget(Screen displayOn, Supplier<MachineFluidTank> getFluid, int pX, int pY, int pWidth, int pHeight) {
super(pX, pY, pWidth, pHeight);
this.displayOn = displayOn;
this.getFluid = getFluid;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.enderio.machines.client.gui.widget;

import com.enderio.core.client.gui.widgets.EIOWidget;
import com.enderio.machines.common.io.fluid.MachineFluidTank;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
Expand All @@ -14,17 +15,16 @@
import net.minecraft.util.FastColor;
import net.minecraftforge.client.extensions.common.IClientFluidTypeExtensions;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.templates.FluidTank;

import java.util.Arrays;
import java.util.function.Supplier;

public class FluidStackStaticWidget extends EIOWidget {

private final Screen displayOn;
private final Supplier<FluidTank> getFluid;
private final Supplier<MachineFluidTank> getFluid;

public FluidStackStaticWidget(Screen displayOn, Supplier<FluidTank> getFluid, int pX, int pY, int pWidth, int pHeight) {
public FluidStackStaticWidget(Screen displayOn, Supplier<MachineFluidTank> getFluid, int pX, int pY, int pWidth, int pHeight) {
super(pX, pY, pWidth, pHeight);
this.displayOn = displayOn;
this.getFluid = getFluid;
Expand All @@ -35,8 +35,8 @@ public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float
Minecraft minecraft = Minecraft.getInstance();
RenderSystem.defaultBlendFunc();
RenderSystem.enableDepthTest();
FluidTank fluidTank = getFluid.get();
if (!fluidTank.isEmpty()) {
MachineFluidTank fluidTank = getFluid.get();
if (!fluidTank.getFluid().isEmpty()) {
FluidStack fluidStack = fluidTank.getFluid();
IClientFluidTypeExtensions props = IClientFluidTypeExtensions.of(fluidStack.getFluid());
ResourceLocation still = props.getStillTexture(fluidStack);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.enderio.machines.client.gui.widget;

import com.enderio.core.client.gui.widgets.EIOWidget;
import com.enderio.machines.common.io.fluid.MachineFluidTank;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
Expand All @@ -14,17 +15,16 @@
import net.minecraft.util.FastColor;
import net.minecraftforge.client.extensions.common.IClientFluidTypeExtensions;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.templates.FluidTank;

import java.util.Arrays;
import java.util.function.Supplier;

public class FluidStackWidget extends EIOWidget {

private final Screen displayOn;
private final Supplier<FluidTank> getFluid;
private final Supplier<MachineFluidTank> getFluid;

public FluidStackWidget(Screen displayOn, Supplier<FluidTank> getFluid, int pX, int pY, int pWidth, int pHeight) {
public FluidStackWidget(Screen displayOn, Supplier<MachineFluidTank> getFluid, int pX, int pY, int pWidth, int pHeight) {
super(pX, pY, pWidth, pHeight);
this.displayOn = displayOn;
this.getFluid = getFluid;
Expand All @@ -35,8 +35,8 @@ public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float
Minecraft minecraft = Minecraft.getInstance();
RenderSystem.defaultBlendFunc();
RenderSystem.enableDepthTest();
FluidTank fluidTank = getFluid.get();
if (!fluidTank.isEmpty()) {
MachineFluidTank fluidTank = getFluid.get();
if (!fluidTank.getFluid().isEmpty()) {
FluidStack fluidStack = fluidTank.getFluid();
IClientFluidTypeExtensions props = IClientFluidTypeExtensions.of(fluidStack.getFluid());
ResourceLocation still = props.getStillTexture(fluidStack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.enderio.core.client.RenderUtil;
import com.enderio.machines.common.blockentity.FluidTankBlockEntity;
import com.enderio.machines.common.io.fluid.MachineFluidTank;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.Minecraft;
Expand All @@ -16,7 +17,6 @@
import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.client.extensions.common.IClientFluidTypeExtensions;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.templates.FluidTank;
import org.joml.Matrix3f;
import org.joml.Matrix4f;

Expand All @@ -29,10 +29,10 @@ public FluidTankBER(BlockEntityRendererProvider.Context context) {
public void render(FluidTankBlockEntity blockEntity, float partialTick, PoseStack poseStack, MultiBufferSource bufferSource, int packedLight,
int packedOverlay) {

FluidTank tank = blockEntity.getFluidTank();
MachineFluidTank tank = blockEntity.getFluidTank();

// Don't waste time if there's no fluid.
if (!tank.isEmpty()) {
if (!tank.getFluid().isEmpty()) {
FluidStack fluidStack = tank.getFluid();

// Use entity translucent cull sheet so fluid renders in Fabulous
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import com.enderio.machines.common.blockentity.base.PoweredMachineBlockEntity;
import com.enderio.machines.common.config.MachinesConfig;
import com.enderio.machines.common.io.FixedIOConfig;
import com.enderio.machines.common.io.fluid.MachineFluidHandler;
import com.enderio.machines.common.io.fluid.MachineFluidTank;
import com.enderio.machines.common.io.fluid.MachineTankLayout;
import com.enderio.machines.common.io.fluid.TankAccess;
import com.enderio.machines.common.io.item.MachineInventoryLayout;
import com.enderio.machines.common.menu.DrainMenu;
import net.minecraft.core.BlockPos;
Expand All @@ -29,7 +32,6 @@
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidType;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.templates.FluidTank;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
Expand All @@ -39,6 +41,7 @@ public class DrainBlockEntity extends PoweredMachineBlockEntity {
public static final String CONSUMED = "Consumed";
private static final QuadraticScalable ENERGY_CAPACITY = new QuadraticScalable(CapacitorModifier.ENERGY_CAPACITY, MachinesConfig.COMMON.ENERGY.DRAIN_CAPACITY);
private static final QuadraticScalable ENERGY_USAGE = new QuadraticScalable(CapacitorModifier.ENERGY_USE, MachinesConfig.COMMON.ENERGY.DRAIN_USAGE);
private static final TankAccess TANK = new TankAccess();
private static final int CAPACITY = 3 * FluidType.BUCKET_VOLUME;
private static final int ENERGY_PER_BUCKET = 1_500;
private List<BlockPos> positions;
Expand All @@ -50,7 +53,7 @@ public class DrainBlockEntity extends PoweredMachineBlockEntity {
public DrainBlockEntity(BlockEntityType<?> type,
BlockPos worldPosition, BlockState blockState) {
super(EnergyIOMode.Input, ENERGY_CAPACITY, ENERGY_USAGE, type, worldPosition, blockState);
addDataSlot(new FluidStackNetworkDataSlot(getFluidTankNN()::getFluid, getFluidTankNN()::setFluid));
addDataSlot(new FluidStackNetworkDataSlot(() -> TANK.getFluid(this), fluid -> TANK.setFluid(this, fluid)));

this.range = 5;

Expand All @@ -73,6 +76,26 @@ public void updateServerCallback() {
.build();
}

@Override
public @Nullable MachineTankLayout getTankLayout() {
return MachineTankLayout.builder().tank(TANK, CAPACITY, f -> type.isSame(f.getFluid())).build();
}

public MachineFluidTank getFluidTank() {
return TANK.getTank(this);
}

@Override
protected @Nullable MachineFluidHandler createFluidHandler(MachineTankLayout layout) {
return new MachineFluidHandler(getIOConfig(), layout) {
@Override
protected void onContentsChanged(int slot) {
super.onContentsChanged(slot);
updateMachineState(MachineState.FULL_TANK, TANK.getFluidAmount(this) >= TANK.getCapacity(this));
}
};
}

@Override
public void serverTick() {
if (isActive()) {
Expand All @@ -99,7 +122,7 @@ protected boolean isActive() {
}
updateMachineState(MachineState.NO_SOURCE, false);
type = fluidState.getType();
return getFluidTankNN().fill(new FluidStack(type, FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.SIMULATE) == FluidType.BUCKET_VOLUME;
return TANK.fill(this, new FluidStack(type, FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.SIMULATE) == FluidType.BUCKET_VOLUME;
}

public void drainFluids() {
Expand Down Expand Up @@ -128,17 +151,17 @@ public void drainFluids() {

//Not a valid fluid
FluidState fluidState = level.getFluidState(pos);
if (fluidState.isEmpty() || !fluidState.isSource() || !getFluidTankNN().isFluidValid(new FluidStack(fluidState.getType(),1))) {
if (fluidState.isEmpty() || !fluidState.isSource() || !TANK.isFluidValid(this, new FluidStack(fluidState.getType(), 1))) {
currentIndex++;
continue;
}

//Fluid found, try to consume it
fluidFound = true;
if (getFluidTankNN().fill(new FluidStack(fluidState.getType(), FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.SIMULATE) == FluidType.BUCKET_VOLUME) {
if (TANK.fill(this, new FluidStack(fluidState.getType(), FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.SIMULATE) == FluidType.BUCKET_VOLUME) {
if (consumed >= ENERGY_PER_BUCKET) {
level.setBlock(pos, Blocks.AIR.defaultBlockState(), Block.UPDATE_ALL);
getFluidTankNN().fill(new FluidStack(fluidState.getType(), FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.EXECUTE);
TANK.fill(this, new FluidStack(fluidState.getType(), FluidType.BUCKET_VOLUME), IFluidHandler.FluidAction.EXECUTE);
consumed -= ENERGY_PER_BUCKET;
currentIndex++;
} else {
Expand Down Expand Up @@ -183,17 +206,6 @@ private void updateLocations() {
positions.add(pos.immutable()); //Need to make it immutable
}
}

protected @Nullable FluidTank createFluidTank() {
return new MachineFluidTank(CAPACITY, f-> type.isSame(f.getFluid()),this) {
@Override
protected void onContentsChanged() {
setChanged();
updateMachineState(MachineState.FULL_TANK, getFluidAmount() >= getCapacity());
}
};
}

@Nullable
@Override
public AbstractContainerMenu createMenu(int containerId, Inventory playerInventory, Player player) {
Expand Down
Loading
Loading