Skip to content

Commit

Permalink
Refine fluid interaction slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Jul 21, 2024
1 parent a866cf4 commit e4fcf81
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ protected void renderTooltip(GuiGraphics gui, int x, int y) {
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {

for (var tankArea : tankAreas) {
if (tankArea.contains(mouseX - leftPos, mouseY - topPos)) {
menu.interactWithTank();
return true;
if (button == 0 || button == 1) {
for (var tankArea : tankAreas) {
if (tankArea.contains(mouseX - leftPos, mouseY - topPos)) {
menu.interactWithTank(button == 1);
return true;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.neoforged.neoforge.fluids.FluidActionResult;
import net.neoforged.neoforge.fluids.FluidUtil;
import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -80,9 +81,9 @@ public ItemStack quickMoveStack(Player playerIn, int index) {
return stack;
}

public void interactWithTank() {
public void interactWithTank(boolean drain) {
if (player.level().isClientSide()) {
PacketDistributor.sendToServer(new InteractWithTankPacket(containerId));
PacketDistributor.sendToServer(new InteractWithTankPacket(containerId, drain));
}

var carried = getCarried();
Expand All @@ -95,9 +96,16 @@ public void interactWithTank() {
return;
}

var result = FluidUtil.tryEmptyContainer(carried, tank, tank.getCapacity(), player, true);
if (!result.isSuccess()) {
FluidActionResult result;
if (drain) {
result = FluidUtil.tryFillContainer(carried, tank, tank.getCapacity(), player, true);
} else {
result = FluidUtil.tryEmptyContainer(carried, tank, tank.getCapacity(), player, true);

// If that didn't succeed, but the held item is *empty*, try filling it
if (!result.isSuccess() && FluidUtil.getFluidContained(carried).isEmpty()) {
result = FluidUtil.tryFillContainer(carried, tank, tank.getCapacity(), player, true);
}
}
if (result.isSuccess()) {
setCarried(result.getResult());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import owmii.powah.lib.logistics.inventory.AbstractTileContainer;
import owmii.powah.network.ServerboundPacket;

public record InteractWithTankPacket(int containerId) implements ServerboundPacket {
public record InteractWithTankPacket(int containerId, boolean drain) implements ServerboundPacket {

@Override
public Type<? extends CustomPacketPayload> type() {
Expand All @@ -20,12 +20,13 @@ public Type<? extends CustomPacketPayload> type() {

public static final StreamCodec<RegistryFriendlyByteBuf, InteractWithTankPacket> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.INT, InteractWithTankPacket::containerId,
ByteBufCodecs.BOOL, InteractWithTankPacket::drain,
InteractWithTankPacket::new);

@Override
public void handleOnServer(ServerPlayer player) {
if (player.containerMenu instanceof AbstractTileContainer<?> tileContainer && tileContainer.containerId == containerId) {
tileContainer.interactWithTank();
tileContainer.interactWithTank(drain);
}
}
}

0 comments on commit e4fcf81

Please sign in to comment.