Skip to content

Commit

Permalink
fix fluid locking (#1196)
Browse files Browse the repository at this point in the history
  • Loading branch information
screret committed May 10, 2024
1 parent 3637692 commit 92bae21
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 45 deletions.
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dependencyResolutionManagement {
def architecturyPluginVersion = "3.4-SNAPSHOT"
def architecturyLoomVersion = "1.5-SNAPSHOT"
def macheteVersion = "1.+"
def ldLibVersion = "1.0.25.f"
def ldLibVersion = "1.0.25.g"
def mixinextrasVersion = "0.2.0"

forge {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public WidgetGroup openConfigurator(int x, int y) {
fluidStorageSlots[index] = new FluidStorage(maxStackSize);
fluidStorageSlots[index].setFluid(matches[index]);

var tank = new ScrollablePhantomFluidWidget(fluidStorageSlots[index], i * 18, j * 18) {
var tank = new ScrollablePhantomFluidWidget(fluidStorageSlots[index], 0, i * 18, j * 18, 18, 18, () -> fluidStorageSlots[index].getFluid(), (fluid) -> fluidStorageSlots[index].setFluid(fluid)) {
@Override
public void updateScreen() {
super.updateScreen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,22 @@
import com.lowdragmc.lowdraglib.gui.widget.PhantomFluidWidget;
import com.lowdragmc.lowdraglib.side.fluid.FluidHelper;
import com.lowdragmc.lowdraglib.side.fluid.FluidStack;
import com.lowdragmc.lowdraglib.side.fluid.IFluidStorage;
import com.lowdragmc.lowdraglib.side.fluid.IFluidTransfer;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.util.Mth;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.jetbrains.annotations.Nullable;

import java.util.function.Consumer;
import java.util.function.Supplier;

public class ScrollablePhantomFluidWidget extends PhantomFluidWidget {
private static final int SCROLL_ACTION_ID = 0x0001_0001;
private static final long MILLIBUCKETS = FluidHelper.getBucket() / 1000;


public ScrollablePhantomFluidWidget() {
}

public ScrollablePhantomFluidWidget(IFluidStorage fluidTank, int x, int y) {
super(fluidTank, x, y);
}

public ScrollablePhantomFluidWidget(@Nullable IFluidStorage fluidTank, int x, int y, int width, int height) {
super(fluidTank, x, y, width, height);
public ScrollablePhantomFluidWidget(@Nullable IFluidTransfer fluidTank, int tank, int x, int y, int width, int height, Supplier<FluidStack> phantomFluidGetter, Consumer<FluidStack> phantomFluidSetter) {
super(fluidTank, tank, x, y, width, height, phantomFluidGetter, phantomFluidSetter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ public class NotifiableFluidTank extends NotifiableRecipeHandlerTrait<FluidIngre
protected boolean allowSameFluids; // Can different tanks be filled with the same fluid. It should be determined while creating tanks.
private Boolean isEmpty;

@Persisted @DescSynced
@Getter
private boolean locked = false;
@Persisted @DescSynced
@Getter
protected FluidStorage lockedFluid = new FluidStorage(FluidHelper.getBucket());
Expand Down Expand Up @@ -156,28 +153,45 @@ public static List<FluidIngredient> handleIngredient(IO io, List<FluidIngredient

@Override
public boolean test(FluidIngredient ingredient) {
return !this.locked || ingredient.test(this.lockedFluid.getFluid());
return !this.isLocked() || ingredient.test(this.lockedFluid.getFluid());
}

@Override
public int getPriority() {
return !locked || lockedFluid.getFluid().isEmpty() ? super.getPriority() : Integer.MAX_VALUE - getTanks();
return !isLocked() || lockedFluid.getFluid().isEmpty() ? super.getPriority() : Integer.MAX_VALUE - getTanks();
}

public boolean isLocked() {
return !lockedFluid.getFluid().isEmpty();
}

public void setLocked(boolean locked) {
if (this.locked == locked) return;
this.locked = locked;
if (this.isLocked() == locked) return;
FluidStack fluidStack = getStorages()[0].getFluid();
if (locked && !fluidStack.isEmpty()) {
this.lockedFluid.setFluid(fluidStack.copy());
this.lockedFluid.getFluid().setAmount(1);
onContentsChanged();
setFilter(stack -> stack.isFluidEqual(this.lockedFluid.getFluid()));
return;
} else {
this.lockedFluid.setFluid(FluidStack.empty());
setFilter(stack -> true);
onContentsChanged();
}
}

public void setLocked(boolean locked, FluidStack fluidStack) {
if (this.isLocked() == locked) return;
if (locked && !fluidStack.isEmpty()) {
this.lockedFluid.setFluid(fluidStack.copy());
this.lockedFluid.getFluid().setAmount(1);
onContentsChanged();
setFilter(stack -> stack.isFluidEqual(this.lockedFluid.getFluid()));
} else {
this.lockedFluid.setFluid(FluidStack.empty());
setFilter(stack -> true);
onContentsChanged();
}
this.lockedFluid.setFluid(FluidStack.empty());
setFilter(stack -> true);
onContentsChanged();
}

public NotifiableFluidTank setFilter(Predicate<FluidStack> filter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,31 +155,30 @@ protected Widget createSingleSlotGUI() {
// Add input/output-specific widgets
if (this.io == IO.OUT) {
// if this is an output hatch, assign tankWidget to the phantom widget displaying the locked fluid...
group.addWidget(tankWidget = new PhantomFluidWidget(this.tank.getLockedFluid(), 67, 40, 18, 18)
.setIFluidStackUpdater(f -> {
if (this.tank.getFluidInTank(0).getAmount() != 0) {
return;
}
if (f.isEmpty()) {
this.tank.setLocked(false);
} else {
this.tank.setLocked(true);
FluidStack newFluid = f.copy();
newFluid.setAmount(1);
this.tank.getLockedFluid().setFluid(newFluid);
}
}).setShowAmount(true).setDrawHoverTips(false).setBackground(GuiTextures.FLUID_SLOT));
group.addWidget(tankWidget = new PhantomFluidWidget(this.tank.getLockedFluid(), 0, 67, 40, 18, 18,
() -> this.tank.getLockedFluid().getFluid(), f -> {
if (!this.tank.getFluidInTank(0).isEmpty()) {
return;
}
if (f == null || f.isEmpty()) {
this.tank.setLocked(false);
} else {
FluidStack newFluid = f.copy();
newFluid.setAmount(1);
this.tank.setLocked(true, newFluid);
}
}).setShowAmount(true).setDrawHoverTips(true).setBackground(GuiTextures.FLUID_SLOT));

group.addWidget(new ToggleButtonWidget(7, 40, 18, 18,
GuiTextures.BUTTON_LOCK, this.tank::isLocked, this.tank::setLocked)
.setTooltipText("gtceu.gui.fluid_lock.tooltip")
.setShouldUseBaseBackground())
// ...and add the actual tank widget separately.
.addWidget(new TankWidget(tank.getStorages()[0], 67, 22, 18, 18, true, io.support(IO.IN))
.setShowAmount(true).setDrawHoverTips(false).setBackground(GuiTextures.FLUID_SLOT));
.setShowAmount(true).setDrawHoverTips(true).setBackground(GuiTextures.FLUID_SLOT));
} else {
group.addWidget(tankWidget = new TankWidget(tank.getStorages()[0], 67, 22, 18, 18, true, io.support(IO.IN))
.setShowAmount(true).setDrawHoverTips(false).setBackground(GuiTextures.FLUID_SLOT));
.setShowAmount(true).setDrawHoverTips(true).setBackground(GuiTextures.FLUID_SLOT));
}

group.addWidget(new LabelWidget(8, 8, "gtceu.gui.fluid_amount"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ protected void checkAutoOutput() {
@Override
public WidgetGroup createUIWidget() {
var group = new WidgetGroup(0, 0, 176, 131);
group.addWidget(new PhantomFluidWidget(this.cache.getStorages()[0], 36, 6, 18, 18).setShowAmount(false).setBackground(GuiTextures.FLUID_SLOT));
group.addWidget(new PhantomFluidWidget(this.cache.getStorages()[0], 0, 36, 6, 18, 18, () -> this.cache.getStorages()[0].getFluid(), (fluid) -> this.cache.getStorages()[0].setFluid(fluid))
.setShowAmount(false).setBackground(GuiTextures.FLUID_SLOT));
group.addWidget(new LabelWidget(7, 9, "gtceu.creative.tank.fluid"));
group.addWidget(new ImageWidget(7, 45, 154, 14, GuiTextures.DISPLAY));
group.addWidget(new TextFieldWidget(9, 47, 152, 10, () -> String.valueOf(mBPerCycle), value -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,8 @@ protected void setLocked(boolean locked) {
if (!stored.isEmpty() && locked) {
var copied = stored.copy();
copied.setAmount(cache.getLockedFluid().getCapacity());
cache.getLockedFluid().setFluid(copied);
cache.setLocked(true);
cache.setLocked(true, copied);
} else if (!locked) {
cache.getLockedFluid().setFluid(FluidStack.empty());
cache.setLocked(false);
}
}
Expand All @@ -314,7 +312,7 @@ public Widget createUIWidget() {
).setTextColor(-1).setDropShadow(true))
.addWidget(new TankWidget(cache.getStorages()[0], 68, 23, true, true)
.setBackground(GuiTextures.FLUID_SLOT))
.addWidget(new PhantomFluidWidget(cache.getLockedFluid(), 68, 41, 18, 18)
.addWidget(new PhantomFluidWidget(cache.getLockedFluid(), 0, 68, 41, 18, 18, () -> cache.getLockedFluid().getFluid(), (fluid) -> cache.getLockedFluid().setFluid(fluid))
.setShowAmount(false)
.setBackground(ColorPattern.T_GRAY.rectTexture()))
.addWidget(new ToggleButtonWidget(4, 41, 18, 18,
Expand Down

0 comments on commit 92bae21

Please sign in to comment.