From 09a471c042573e78b3dcb882fb493dfeb5f82195 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 22 Nov 2024 13:41:13 +0100 Subject: [PATCH] added a button to send craft items back closes #215 --- .../prettypipes/packets/PacketButton.java | 4 ++++ .../terminal/CraftingTerminalBlockEntity.java | 12 ++++++++++++ .../terminal/containers/CraftingTerminalGui.java | 16 ++++++++++++---- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/ellpeck/prettypipes/packets/PacketButton.java b/src/main/java/de/ellpeck/prettypipes/packets/PacketButton.java index f7af1c6..fe35174 100644 --- a/src/main/java/de/ellpeck/prettypipes/packets/PacketButton.java +++ b/src/main/java/de/ellpeck/prettypipes/packets/PacketButton.java @@ -121,6 +121,10 @@ public record PacketButton(BlockPos pos, int result, List data) impleme var tile = Utility.getBlockEntity(CraftingTerminalBlockEntity.class, player.level(), pos); tile.requestCraftingItems(player, data.getFirst(), data.get(1) > 0); }), + CRAFT_TERMINAL_SEND_BACK((pos, data, player) -> { + var tile = Utility.getBlockEntity(CraftingTerminalBlockEntity.class, player.level(), pos); + tile.sendItemsBack(); + }), CANCEL_CRAFTING((pos, data, player) -> { var tile = Utility.getBlockEntity(ItemTerminalBlockEntity.class, player.level(), pos); tile.cancelCrafting(); diff --git a/src/main/java/de/ellpeck/prettypipes/terminal/CraftingTerminalBlockEntity.java b/src/main/java/de/ellpeck/prettypipes/terminal/CraftingTerminalBlockEntity.java index 1761173..328bbbe 100644 --- a/src/main/java/de/ellpeck/prettypipes/terminal/CraftingTerminalBlockEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/terminal/CraftingTerminalBlockEntity.java @@ -26,6 +26,7 @@ import net.minecraft.world.level.ChunkPos; import net.minecraft.world.level.block.state.BlockState; import net.neoforged.neoforge.items.ItemHandlerHelper; import net.neoforged.neoforge.items.ItemStackHandler; +import net.neoforged.neoforge.items.wrapper.RangedWrapper; import net.neoforged.neoforge.network.PacketDistributor; import org.apache.commons.lang3.mutable.MutableInt; @@ -139,6 +140,17 @@ public class CraftingTerminalBlockEntity extends ItemTerminalBlockEntity { network.endProfile(); } + public void sendItemsBack() { + var outputInventory = new RangedWrapper(this.items, 6, 12); + for (var i = 0; i < this.craftItems.getSlots(); i++) { + var stack = this.craftItems.getStackInSlot(i); + if (!stack.isEmpty()) { + var remain = ItemHandlerHelper.insertItemStacked(outputInventory, stack, false); + this.craftItems.setStackInSlot(i, remain); + } + } + } + @Override public void saveAdditional(CompoundTag compound, HolderLookup.Provider provider) { super.saveAdditional(compound, provider); diff --git a/src/main/java/de/ellpeck/prettypipes/terminal/containers/CraftingTerminalGui.java b/src/main/java/de/ellpeck/prettypipes/terminal/containers/CraftingTerminalGui.java index 0078032..01ead27 100644 --- a/src/main/java/de/ellpeck/prettypipes/terminal/containers/CraftingTerminalGui.java +++ b/src/main/java/de/ellpeck/prettypipes/terminal/containers/CraftingTerminalGui.java @@ -5,6 +5,7 @@ import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.packets.PacketButton; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.components.Button; +import net.minecraft.client.gui.components.Tooltip; import net.minecraft.client.gui.screens.Screen; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; @@ -12,11 +13,13 @@ import net.minecraft.world.entity.player.Inventory; import net.neoforged.neoforge.network.PacketDistributor; import java.util.Arrays; +import java.util.List; public class CraftingTerminalGui extends ItemTerminalGui { private static final ResourceLocation TEXTURE = ResourceLocation.fromNamespaceAndPath(PrettyPipes.ID, "textures/gui/crafting_terminal.png"); private Button requestButton; + private Button sendBackButton; public CraftingTerminalGui(ItemTerminalContainer screenContainer, Inventory inv, Component titleIn) { super(screenContainer, inv, titleIn); @@ -32,6 +35,9 @@ public class CraftingTerminalGui extends ItemTerminalGui { var force = Screen.hasAltDown() || InputConstants.isKeyDown(this.minecraft.getWindow().getWindow(), 259) ? 1 : 0; PacketDistributor.sendToServer(new PacketButton(this.menu.tile.getBlockPos(), PacketButton.ButtonResult.CRAFT_TERMINAL_REQUEST, Arrays.asList(amount, force))); }).bounds(this.leftPos + 8, this.topPos + 100, 50, 20).build()); + this.sendBackButton = this.addRenderableWidget(Button.builder(Component.literal(">"), button -> { + PacketDistributor.sendToServer(new PacketButton(this.menu.tile.getBlockPos(), PacketButton.ButtonResult.CRAFT_TERMINAL_SEND_BACK, List.of())); + }).bounds(this.leftPos + 47, this.topPos + 72, 12, 12).build()); this.tick(); } @@ -40,12 +46,14 @@ public class CraftingTerminalGui extends ItemTerminalGui { super.containerTick(); var tile = this.getCraftingContainer().getTile(); this.requestButton.active = false; + this.sendBackButton.active = false; for (var i = 0; i < tile.craftItems.getSlots(); i++) { - var stack = tile.getRequestedCraftItem(i); - if (!stack.isEmpty() && stack.getCount() < stack.getMaxStackSize()) { + var requestStack = tile.getRequestedCraftItem(i); + if (!requestStack.isEmpty() && requestStack.getCount() < requestStack.getMaxStackSize()) this.requestButton.active = true; - break; - } + var realStack = tile.craftItems.getStackInSlot(i); + if (!realStack.isEmpty()) + this.sendBackButton.active = true; } }