From a866cf488ccc60f8febac7393456f59560477fd3 Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Sun, 21 Jul 2024 03:51:10 +0200 Subject: [PATCH] Re-Register Water als a Fluid Coolant and Allow Interacting with Tanks from UI --- gradle.properties | 6 +- settings.gradle | 2 +- src/main/java/owmii/powah/Powah.java | 2 + .../screen/container/MagmatorScreen.java | 49 +++-------- .../screen/container/ReactorScreen.java | 48 ++--------- .../client/screen/container/ThermoScreen.java | 48 ++--------- .../screen/container/AbstractTileScreen.java | 82 +++++++++++++++++++ .../owmii/powah/lib/client/util/Draw.java | 5 +- .../inventory/AbstractTileContainer.java | 29 ++++++- .../java/owmii/powah/network/Network.java | 2 + .../packet/InteractWithTankPacket.java | 31 +++++++ 11 files changed, 181 insertions(+), 123 deletions(-) create mode 100644 src/main/java/owmii/powah/network/packet/InteractWithTankPacket.java diff --git a/gradle.properties b/gradle.properties index f2655611..91c92aeb 100644 --- a/gradle.properties +++ b/gradle.properties @@ -19,12 +19,12 @@ minecraft_version=1.21 cloth_config_version=15.0.127 rei_version=16.0.729 -emi_version=1.1.7+1.21 -neoforge_version=21.0.22-beta +emi_version=1.1.10+1.21 +neoforge_version=21.0.113-beta curios_version=7.0.0-beta.3+1.20.4 # Switch between none, emi and rei -runtime_itemlist_mod=rei +runtime_itemlist_mod=emi ######################################################### # Parchment # diff --git a/settings.gradle b/settings.gradle index 24ac4e7d..8bb67daa 100644 --- a/settings.gradle +++ b/settings.gradle @@ -4,7 +4,7 @@ pluginManagement { maven { url "https://maven.architectury.dev/" } } plugins { - id 'net.neoforged.moddev' version '0.1.105' + id 'net.neoforged.moddev' version '1.0.13' id "me.shedaniel.unified-publishing" version "0.1.+" id 'com.diffplug.spotless' version '6.25.0' } diff --git a/src/main/java/owmii/powah/Powah.java b/src/main/java/owmii/powah/Powah.java index d78b3aec..8d24ed52 100644 --- a/src/main/java/owmii/powah/Powah.java +++ b/src/main/java/owmii/powah/Powah.java @@ -12,6 +12,7 @@ import net.minecraft.world.item.Items; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.entity.BlockEntityType; +import net.minecraft.world.level.material.Fluids; import net.neoforged.bus.api.IEventBus; import net.neoforged.fml.ModList; import net.neoforged.fml.common.Mod; @@ -80,6 +81,7 @@ public Powah(IEventBus modEventBus) { modEventBus.addListener((FMLCommonSetupEvent event) -> { // TODO: move to config + PowahAPI.registerCoolant(BuiltInRegistries.FLUID.getKey(Fluids.WATER), 1); PowahAPI.registerSolidCoolant(Blocks.SNOW_BLOCK, 48, -3); PowahAPI.registerSolidCoolant(Items.SNOWBALL, 12, -3); PowahAPI.registerSolidCoolant(Blocks.ICE, 48, -5); diff --git a/src/main/java/owmii/powah/client/screen/container/MagmatorScreen.java b/src/main/java/owmii/powah/client/screen/container/MagmatorScreen.java index 54c2a2b9..30781c9d 100644 --- a/src/main/java/owmii/powah/client/screen/container/MagmatorScreen.java +++ b/src/main/java/owmii/powah/client/screen/container/MagmatorScreen.java @@ -7,14 +7,11 @@ import net.minecraft.client.gui.GuiGraphics; import net.minecraft.network.chat.Component; import net.minecraft.world.entity.player.Inventory; -import net.minecraft.world.inventory.InventoryMenu; import owmii.powah.api.PowahAPI; import owmii.powah.block.magmator.MagmatorTile; -import owmii.powah.client.ClientUtils; import owmii.powah.client.screen.Textures; import owmii.powah.inventory.MagmatorContainer; import owmii.powah.lib.client.screen.container.AbstractEnergyScreen; -import owmii.powah.lib.client.util.Draw; import owmii.powah.lib.client.util.Text; import owmii.powah.lib.logistics.energy.Energy; import owmii.powah.util.Ticker; @@ -28,6 +25,18 @@ public MagmatorScreen(MagmatorContainer container, Inventory inv, Component titl if (this.te.isBurning()) { this.heat.setTicks(20); } + addTankArea( + te::getTank, + 157, + 5, + 14, + 65, + "info.lollipop.fluid", + (content, lines) -> { + lines.add(Component.translatable("info.lollipop.Gain").withStyle(ChatFormatting.GRAY).append(Text.COLON) + .append(Component.translatable("info.lollipop.fe.per.mb", PowahAPI.getMagmaticFluidHeat(content.getFluid()), "100") + .withStyle(ChatFormatting.DARK_GRAY))); + }); } @Override @@ -47,22 +56,6 @@ protected void drawBackground(GuiGraphics guiGraphics, float partialTicks, int m RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, this.heat.subSized()); Textures.MAGMATOR_BUFFER.draw(guiGraphics, this.leftPos + 83, this.topPos + 29); RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); - - var tank = this.te.getTank(); - if (!tank.isEmpty()) { - var fluidStack = tank.getFluid(); - var sprite = ClientUtils.getStillTexture(fluidStack); - if (sprite != null) { - int color = ClientUtils.getFluidColor(fluidStack); - float red = (color >> 16 & 0xFF) / 255.0F; - float green = (color >> 8 & 0xFF) / 255.0F; - float blue = (color & 0xFF) / 255.0F; - RenderSystem.setShaderColor(red, green, blue, 1.0F); - bindTexture(InventoryMenu.BLOCK_ATLAS); - Draw.gaugeV(sprite, this.leftPos + 157, this.topPos + 5, 14, 65, (int) tank.getCapacity(), (int) tank.getFluidAmount()); - RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); - } - } } @Override @@ -83,23 +76,5 @@ protected void renderTooltip(GuiGraphics gui, int mouseX, int mouseY) { .withStyle(ChatFormatting.DARK_GRAY))); gui.renderComponentTooltip(font, list, mouseX, mouseY); } - - var tank = this.te.getTank(); - if (isMouseOver(mouseX - 157, mouseY - 5, 14, 65)) { - List list = new ArrayList<>(); - if (!tank.isEmpty()) { - list.add(Component.translatable("info.lollipop.fluid").withStyle(ChatFormatting.GRAY).append(Text.COLON) - .append(tank.getFluid().getDisplayName().plainCopy().withStyle(ChatFormatting.GOLD))); - list.add(Component.translatable("info.lollipop.stored").withStyle(ChatFormatting.GRAY).append(Text.COLON) - .append(Util.formatTankContent(tank))); - list.add(Component.translatable("info.lollipop.Gain").withStyle(ChatFormatting.GRAY).append(Text.COLON) - .append(Component.translatable("info.lollipop.fe.per.mb", PowahAPI.getMagmaticFluidHeat(tank.getFluid().getFluid()), "100") - .withStyle(ChatFormatting.DARK_GRAY))); - } else { - list.add(Component.translatable("info.lollipop.fluid").withStyle(ChatFormatting.GRAY).append(Text.COLON) - .append(Component.literal("---").withStyle(ChatFormatting.DARK_GRAY))); - } - gui.renderComponentTooltip(font, list, mouseX, mouseY); - } } } diff --git a/src/main/java/owmii/powah/client/screen/container/ReactorScreen.java b/src/main/java/owmii/powah/client/screen/container/ReactorScreen.java index 798e8019..f3155208 100644 --- a/src/main/java/owmii/powah/client/screen/container/ReactorScreen.java +++ b/src/main/java/owmii/powah/client/screen/container/ReactorScreen.java @@ -1,21 +1,17 @@ package owmii.powah.client.screen.container; -import com.mojang.blaze3d.systems.RenderSystem; import java.util.ArrayList; import java.util.List; import net.minecraft.ChatFormatting; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.network.chat.Component; import net.minecraft.world.entity.player.Inventory; -import net.minecraft.world.inventory.InventoryMenu; import owmii.powah.api.PowahAPI; import owmii.powah.block.reactor.ReactorTile; -import owmii.powah.client.ClientUtils; import owmii.powah.client.screen.Textures; import owmii.powah.inventory.ReactorContainer; import owmii.powah.lib.client.screen.container.AbstractEnergyScreen; import owmii.powah.lib.client.screen.widget.IconButton; -import owmii.powah.lib.client.util.Draw; import owmii.powah.lib.client.util.Text; import owmii.powah.lib.logistics.energy.Energy; import owmii.powah.network.Network; @@ -27,6 +23,14 @@ public class ReactorScreen extends AbstractEnergyScreen { + lines.add(Component.translatable("info.lollipop.temperature").withStyle(ChatFormatting.GRAY).append(Text.COLON) + .append(Component + .translatable("info.lollipop.temperature.c", + "" + ChatFormatting.AQUA + PowahAPI.getCoolant(content.getFluid())) + .withStyle(ChatFormatting.DARK_GRAY))); + }); } @Override @@ -60,22 +64,6 @@ protected void drawBackground(GuiGraphics guiGraphics, float partialTicks, int m Textures.REACTOR_GAUGE_TEMP.drawScalableH(guiGraphics, this.te.temp.subSized(), this.leftPos + 114, this.topPos + 28); Textures.REACTOR_GEN_MODE_BG.draw(guiGraphics, this.modeButton.getX() - 4, this.modeButton.getY() - 4); - - var tank = this.te.getTank(); - if (!tank.isEmpty()) { - var fluidStack = tank.getFluid(); - var sprite = ClientUtils.getStillTexture(fluidStack); - if (sprite != null) { - int color = ClientUtils.getFluidColor(fluidStack); - float red = (color >> 16 & 0xFF) / 255.0F; - float green = (color >> 8 & 0xFF) / 255.0F; - float blue = (color & 0xFF) / 255.0F; - RenderSystem.setShaderColor(red, green, blue, 1.0F); - bindTexture(InventoryMenu.BLOCK_ATLAS); - Draw.gaugeV(sprite, this.leftPos + 157, this.topPos + 5, 14, 65, (int) tank.getCapacity(), (int) tank.getFluidAmount()); - RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); - } - } } @Override @@ -159,25 +147,5 @@ protected void renderTooltip(GuiGraphics gui, int mouseX, int mouseY) { list.add(Component.literal("" + ChatFormatting.AQUA + this.te.solidCoolantTemp + " C")); gui.renderComponentTooltip(font, list, mouseX, mouseY); } - - var tank = this.te.getTank(); - if (isMouseOver(mouseX - 157, mouseY - 5, 14, 65)) { - List list = new ArrayList<>(); - if (!tank.isEmpty()) { - list.add(Component.translatable("info.lollipop.coolant").withStyle(ChatFormatting.GRAY).append(Text.COLON) - .append(tank.getFluid().getDisplayName().plainCopy().withStyle(ChatFormatting.AQUA))); - list.add(Component.translatable("info.lollipop.stored").withStyle(ChatFormatting.GRAY).append(Text.COLON) - .append(Util.formatTankContent(tank))); - list.add(Component.translatable("info.lollipop.temperature").withStyle(ChatFormatting.GRAY).append(Text.COLON) - .append(Component - .translatable("info.lollipop.temperature.c", - "" + ChatFormatting.AQUA + PowahAPI.getCoolant(tank.getFluid().getFluid())) - .withStyle(ChatFormatting.DARK_GRAY))); - } else { - list.add(Component.translatable("info.lollipop.fluid").withStyle(ChatFormatting.GRAY).append(Text.COLON) - .append(Component.literal("---").withStyle(ChatFormatting.DARK_GRAY))); - } - gui.renderComponentTooltip(font, list, mouseX, mouseY); - } } } diff --git a/src/main/java/owmii/powah/client/screen/container/ThermoScreen.java b/src/main/java/owmii/powah/client/screen/container/ThermoScreen.java index 5698bead..25bacc8d 100644 --- a/src/main/java/owmii/powah/client/screen/container/ThermoScreen.java +++ b/src/main/java/owmii/powah/client/screen/container/ThermoScreen.java @@ -1,20 +1,16 @@ package owmii.powah.client.screen.container; -import com.mojang.blaze3d.systems.RenderSystem; import java.util.ArrayList; import java.util.List; import net.minecraft.ChatFormatting; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.network.chat.Component; import net.minecraft.world.entity.player.Inventory; -import net.minecraft.world.inventory.InventoryMenu; import owmii.powah.api.PowahAPI; import owmii.powah.block.thermo.ThermoTile; -import owmii.powah.client.ClientUtils; import owmii.powah.client.screen.Textures; import owmii.powah.inventory.ThermoContainer; import owmii.powah.lib.client.screen.container.AbstractEnergyScreen; -import owmii.powah.lib.client.util.Draw; import owmii.powah.lib.client.util.Text; import owmii.powah.lib.logistics.energy.Energy; import owmii.powah.util.Util; @@ -22,6 +18,14 @@ public class ThermoScreen extends AbstractEnergyScreen { public ThermoScreen(ThermoContainer container, Inventory inv, Component title) { super(container, inv, title, Textures.THERMO); + + addTankArea(te::getTank, 157, 5, 14, 65, "info.lollipop.coolant", (content, lines) -> { + lines.add(Component.translatable("info.lollipop.temperature").withStyle(ChatFormatting.GRAY).append(Text.COLON) + .append(Component + .translatable("info.lollipop.temperature.c", + "" + ChatFormatting.AQUA + PowahAPI.getCoolant(content.getFluid())) + .withStyle(ChatFormatting.DARK_GRAY))); + }); } @Override @@ -29,22 +33,6 @@ protected void drawBackground(GuiGraphics gui, float partialTicks, int mouseX, i super.drawBackground(gui, partialTicks, mouseX, mouseY); Textures.THERMO_GAUGE.drawScalableH(gui, this.te.getEnergy().subSized(), this.leftPos + 5, this.topPos + 5); - var tank = this.te.getTank(); - if (!tank.isEmpty()) { - var fluidStack = tank.getFluid(); - var sprite = ClientUtils.getStillTexture(fluidStack); - if (sprite != null) { - int color = ClientUtils.getFluidColor(fluidStack); - float red = (color >> 16 & 0xFF) / 255.0F; - float green = (color >> 8 & 0xFF) / 255.0F; - float blue = (color & 0xFF) / 255.0F; - RenderSystem.setShaderColor(red, green, blue, 1.0F); - bindTexture(InventoryMenu.BLOCK_ATLAS); - Draw.gaugeV(sprite, this.leftPos + 157, this.topPos + 5, 14, 65, (int) tank.getCapacity(), (int) tank.getFluidAmount()); - RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); - } - } - long percent = this.te.getGeneration() > 0 ? (100 * this.te.generating) / this.te.getGeneration() : 0; gui.drawString(font, percent + "%" + " (" + this.te.generating + " FE/t)", this.leftPos + 34, this.topPos + 10, 5592405, false); } @@ -67,25 +55,5 @@ protected void renderTooltip(GuiGraphics gui, int mouseX, int mouseY) { .withStyle(ChatFormatting.DARK_GRAY))); gui.renderComponentTooltip(font, list, mouseX, mouseY); } - - var tank = this.te.getTank(); - if (isMouseOver(mouseX - 157, mouseY - 5, 14, 65)) { - List list = new ArrayList<>(); - if (!tank.isEmpty()) { - list.add(Component.translatable("info.lollipop.coolant").withStyle(ChatFormatting.GRAY).append(Text.COLON) - .append(tank.getFluid().getDisplayName().plainCopy().withStyle(ChatFormatting.AQUA))); - list.add(Component.translatable("info.lollipop.stored").withStyle(ChatFormatting.GRAY).append(Text.COLON) - .append(Util.formatTankContent(tank))); - list.add(Component.translatable("info.lollipop.temperature").withStyle(ChatFormatting.GRAY).append(Text.COLON) - .append(Component - .translatable("info.lollipop.temperature.c", - "" + ChatFormatting.AQUA + PowahAPI.getCoolant(tank.getFluid().getFluid())) - .withStyle(ChatFormatting.DARK_GRAY))); - } else { - list.add(Component.translatable("info.lollipop.fluid").withStyle(ChatFormatting.GRAY).append(Text.COLON) - .append(Component.literal("---").withStyle(ChatFormatting.DARK_GRAY))); - } - gui.renderComponentTooltip(font, list, mouseX, mouseY); - } } } diff --git a/src/main/java/owmii/powah/lib/client/screen/container/AbstractTileScreen.java b/src/main/java/owmii/powah/lib/client/screen/container/AbstractTileScreen.java index b7ade108..1ee8e819 100644 --- a/src/main/java/owmii/powah/lib/client/screen/container/AbstractTileScreen.java +++ b/src/main/java/owmii/powah/lib/client/screen/container/AbstractTileScreen.java @@ -1,27 +1,90 @@ package owmii.powah.lib.client.screen.container; +import com.mojang.blaze3d.systems.RenderSystem; +import java.util.ArrayList; import java.util.List; +import java.util.function.BiConsumer; +import java.util.function.Supplier; +import net.minecraft.ChatFormatting; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.network.chat.Component; import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.inventory.InventoryMenu; +import net.neoforged.neoforge.fluids.FluidStack; +import owmii.powah.client.ClientUtils; import owmii.powah.lib.block.AbstractTileEntity; import owmii.powah.lib.block.IInventoryHolder; import owmii.powah.lib.client.screen.Texture; import owmii.powah.lib.client.screen.widget.IconButton; +import owmii.powah.lib.client.util.Draw; +import owmii.powah.lib.client.util.Text; +import owmii.powah.lib.logistics.fluid.Tank; import owmii.powah.lib.logistics.inventory.AbstractTileContainer; import owmii.powah.network.Network; import owmii.powah.network.packet.NextRedstoneModePacket; +import owmii.powah.util.Util; public class AbstractTileScreen & IInventoryHolder, C extends AbstractTileContainer> extends AbstractContainerScreen { protected final T te; protected IconButton redStoneButton = IconButton.EMPTY; + protected final List tankAreas = new ArrayList<>(); public AbstractTileScreen(C container, Inventory inv, Component title, Texture backGround) { super(container, inv, title, backGround); this.te = container.te; } + @Override + protected void renderTooltip(GuiGraphics gui, int x, int y) { + for (var tankArea : tankAreas) { + if (tankArea.contains(x - leftPos, y - topPos)) { + var tank = tankArea.tank().get(); + + List list = new ArrayList<>(); + if (!tank.isEmpty()) { + list.add(Component.translatable(tankArea.purposeTranslationKey()).withStyle(ChatFormatting.GRAY).append(Text.COLON) + .append(tank.getFluid().getHoverName().plainCopy().withStyle(ChatFormatting.AQUA))); + list.add(Component.translatable("info.lollipop.stored").withStyle(ChatFormatting.GRAY).append(Text.COLON) + .append(Util.formatTankContent(tank))); + tankArea.extraInfoSupplier().accept(tank.getFluid(), list); + } else { + list.add(Component.translatable("info.lollipop.fluid").withStyle(ChatFormatting.GRAY).append(Text.COLON) + .append(Component.literal("---").withStyle(ChatFormatting.DARK_GRAY))); + } + gui.renderComponentTooltip(font, list, x, y); + return; + } + } + + super.renderTooltip(gui, x, 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; + } + } + + return super.mouseClicked(mouseX, mouseY, button); + } + + protected record TankArea(Supplier tank, int x, int y, int width, int height, String purposeTranslationKey, + BiConsumer> extraInfoSupplier) { + boolean contains(double x, double y) { + return x >= this.x && y >= this.y && x < this.x + width && y < this.y + height; + } + } + + protected final void addTankArea(Supplier tank, int x, int y, int width, int height, String purposeTranslationKey, + BiConsumer> extraInfoSupplier) { + tankAreas.add(new TankArea(tank, x, y, width, height, purposeTranslationKey, extraInfoSupplier)); + } + protected void addRedstoneButton(int x, int y) { if (hasRedstone()) { this.redStoneButton = addRenderableWidget(new IconButton(this.leftPos + this.imageWidth + x + 2, this.topPos + y + 3, @@ -46,6 +109,25 @@ protected void drawBackground(GuiGraphics guiGraphics, float partialTicks, int m if (hasRedstone()) { Texture.REDSTONE_BTN_BG.draw(guiGraphics, this.redStoneButton.getX() - 2, this.redStoneButton.getY() - 4); // TODO } + + for (TankArea tankArea : tankAreas) { + var tank = tankArea.tank().get(); + if (!tank.isEmpty()) { + var fluidStack = tank.getFluid(); + var sprite = ClientUtils.getStillTexture(fluidStack); + if (sprite != null) { + int color = ClientUtils.getFluidColor(fluidStack); + float red = (color >> 16 & 0xFF) / 255.0F; + float green = (color >> 8 & 0xFF) / 255.0F; + float blue = (color & 0xFF) / 255.0F; + RenderSystem.setShaderColor(red, green, blue, 1.0F); + bindTexture(InventoryMenu.BLOCK_ATLAS); + Draw.gaugeV(sprite, this.leftPos + tankArea.x, this.topPos + tankArea.y, tankArea.width, tankArea.height(), tank.getCapacity(), + tank.getFluidAmount()); + RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); + } + } + } } protected boolean hasRedstone() { // TODO invert diff --git a/src/main/java/owmii/powah/lib/client/util/Draw.java b/src/main/java/owmii/powah/lib/client/util/Draw.java index eebc9f60..2d10f6bf 100644 --- a/src/main/java/owmii/powah/lib/client/util/Draw.java +++ b/src/main/java/owmii/powah/lib/client/util/Draw.java @@ -33,7 +33,10 @@ public static void gaugeV(TextureAtlasSprite sprite, int x, int y, int w, int h, buffer.addVertex(x, yy + m, 0).setUv(uMin, vMin); } } - BufferUploader.drawWithShader(buffer.buildOrThrow()); + var mesh = buffer.build(); + if (mesh != null) { + BufferUploader.drawWithShader(mesh); + } } } diff --git a/src/main/java/owmii/powah/lib/logistics/inventory/AbstractTileContainer.java b/src/main/java/owmii/powah/lib/logistics/inventory/AbstractTileContainer.java index eee1cd1b..ab93af65 100644 --- a/src/main/java/owmii/powah/lib/logistics/inventory/AbstractTileContainer.java +++ b/src/main/java/owmii/powah/lib/logistics/inventory/AbstractTileContainer.java @@ -10,9 +10,12 @@ 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.FluidUtil; +import net.neoforged.neoforge.network.PacketDistributor; import org.jetbrains.annotations.Nullable; import owmii.powah.lib.block.AbstractTileEntity; import owmii.powah.lib.block.IInventoryHolder; +import owmii.powah.network.packet.InteractWithTankPacket; public abstract class AbstractTileContainer & IInventoryHolder> extends AbstractContainer { public final T te; @@ -43,7 +46,7 @@ protected void init(Inventory inventory, T te) { if (tile instanceof AbstractTileEntity) return (T) tile; // What the hell is this? - return (T) new AbstractTileEntity(BlockEntityType.SIGN, pos, Blocks.AIR.defaultBlockState()); + return (T) new AbstractTileEntity<>(BlockEntityType.SIGN, pos, Blocks.AIR.defaultBlockState()); } @Override @@ -76,4 +79,28 @@ public ItemStack quickMoveStack(Player playerIn, int index) { } return stack; } + + public void interactWithTank() { + if (player.level().isClientSide()) { + PacketDistributor.sendToServer(new InteractWithTankPacket(containerId)); + } + + var carried = getCarried(); + if (carried.isEmpty()) { + return; + } + + var tank = te.getTank(); + if (tank.getCapacity() == 0) { + return; + } + + var result = FluidUtil.tryEmptyContainer(carried, tank, tank.getCapacity(), player, true); + if (!result.isSuccess()) { + result = FluidUtil.tryFillContainer(carried, tank, tank.getCapacity(), player, true); + } + if (result.isSuccess()) { + setCarried(result.getResult()); + } + } } diff --git a/src/main/java/owmii/powah/network/Network.java b/src/main/java/owmii/powah/network/Network.java index dfca3a1d..ce801255 100644 --- a/src/main/java/owmii/powah/network/Network.java +++ b/src/main/java/owmii/powah/network/Network.java @@ -5,6 +5,7 @@ import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent; import net.neoforged.neoforge.network.handling.IPayloadContext; import owmii.powah.Powah; +import owmii.powah.network.packet.InteractWithTankPacket; import owmii.powah.network.packet.NextEnergyConfigPacket; import owmii.powah.network.packet.NextRedstoneModePacket; import owmii.powah.network.packet.SetChannelPacket; @@ -18,6 +19,7 @@ public static void register(RegisterPayloadHandlersEvent event) { registrar.playToServer(NextRedstoneModePacket.TYPE, NextRedstoneModePacket.STREAM_CODEC, Network::handleServerbound); registrar.playToServer(SetChannelPacket.TYPE, SetChannelPacket.STREAM_CODEC, Network::handleServerbound); registrar.playToServer(SwitchGenModePacket.TYPE, SwitchGenModePacket.STREAM_CODEC, Network::handleServerbound); + registrar.playToServer(InteractWithTankPacket.TYPE, InteractWithTankPacket.STREAM_CODEC, Network::handleServerbound); } private static void handleServerbound(ServerboundPacket packet, IPayloadContext context) { diff --git a/src/main/java/owmii/powah/network/packet/InteractWithTankPacket.java b/src/main/java/owmii/powah/network/packet/InteractWithTankPacket.java new file mode 100644 index 00000000..60ed1ff6 --- /dev/null +++ b/src/main/java/owmii/powah/network/packet/InteractWithTankPacket.java @@ -0,0 +1,31 @@ +package owmii.powah.network.packet; + +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.server.level.ServerPlayer; +import owmii.powah.Powah; +import owmii.powah.lib.logistics.inventory.AbstractTileContainer; +import owmii.powah.network.ServerboundPacket; + +public record InteractWithTankPacket(int containerId) implements ServerboundPacket { + + @Override + public Type type() { + return TYPE; + } + + public static final Type TYPE = new Type<>(Powah.id("interact_with_tank")); + + public static final StreamCodec STREAM_CODEC = StreamCodec.composite( + ByteBufCodecs.INT, InteractWithTankPacket::containerId, + InteractWithTankPacket::new); + + @Override + public void handleOnServer(ServerPlayer player) { + if (player.containerMenu instanceof AbstractTileContainer tileContainer && tileContainer.containerId == containerId) { + tileContainer.interactWithTank(); + } + } +}