Skip to content

Commit

Permalink
added a button to send craft items back
Browse files Browse the repository at this point in the history
closes #215
  • Loading branch information
Ellpeck committed Nov 22, 2024
1 parent e0e0b55 commit 09a471c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ public boolean shouldTriggerClientSideContainerClosingOnOpen() {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;

Expand Down Expand Up @@ -139,6 +140,17 @@ public void requestCraftingItems(Player player, int maxAmount, boolean force) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
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;
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);
Expand All @@ -32,6 +35,9 @@ protected void init() {
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();
}

Expand All @@ -40,12 +46,14 @@ public void containerTick() {
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;
}
}

Expand Down

0 comments on commit 09a471c

Please sign in to comment.