From 887bfed2292dff8a8cd370e5702751268fefd97d Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 28 Nov 2024 12:33:06 +0100 Subject: [PATCH] allow force-canceling crafts that are in progress --- .../prettypipes/network/ActiveCraft.java | 23 +++++++++++++++---- .../prettypipes/network/PipeNetwork.java | 5 ++-- .../prettypipes/packets/PacketButton.java | 2 +- .../terminal/ItemTerminalBlockEntity.java | 6 ++--- .../terminal/containers/ItemTerminalGui.java | 8 +++---- .../assets/prettypipes/lang/en_us.json | 2 +- 6 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/main/java/de/ellpeck/prettypipes/network/ActiveCraft.java b/src/main/java/de/ellpeck/prettypipes/network/ActiveCraft.java index aad9b1b..6643a14 100644 --- a/src/main/java/de/ellpeck/prettypipes/network/ActiveCraft.java +++ b/src/main/java/de/ellpeck/prettypipes/network/ActiveCraft.java @@ -63,6 +63,19 @@ public void deserializeNBT(HolderLookup.Provider provider, CompoundTag nbt) { this.canceled = nbt.getBoolean("canceled"); } + @Override + public String toString() { + return "ActiveCraft{" + + "pipe=" + this.pipe + + ", moduleSlot=" + this.moduleSlot + + ", travelingIngredients=" + this.travelingIngredients + + ", ingredientsToRequest=" + this.ingredientsToRequest + + ", resultDestPipe=" + this.resultDestPipe + + ", resultStackRemain=" + this.resultStackRemain + + ", inProgress=" + this.inProgress + + ", canceled=" + this.canceled + '}'; + } + public ItemStack getTravelingIngredient(ItemStack stack, ItemEquality... equalityTypes) { for (var traveling : this.travelingIngredients) { if (ItemEquality.compareItems(stack, traveling, equalityTypes)) @@ -71,14 +84,14 @@ public ItemStack getTravelingIngredient(ItemStack stack, ItemEquality... equalit return ItemStack.EMPTY; } - public boolean markCanceledOrResolve(PipeNetwork network) { - if (this.inProgress) { - this.canceled = true; - return false; - } else { + public boolean markCanceledOrResolve(PipeNetwork network, boolean force) { + if (force || !this.inProgress) { for (var lock : this.ingredientsToRequest) network.resolveNetworkLock(lock); return true; + } else { + this.canceled = true; + return false; } } diff --git a/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java b/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java index 8eeea93..9c84a4a 100644 --- a/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java +++ b/src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java @@ -109,7 +109,8 @@ public String toString() { ",\nnodeToConnectedNodes=" + this.nodeToConnectedNodes + ",\ntileCache=" + this.tileCache.keySet() + ",\npipeItems=" + this.pipeItems + - ",\nnetworkLocks=" + this.networkLocks + '}'; + ",\nnetworkLocks=" + this.networkLocks + + ",\nactiveCrafts=" + this.activeCrafts + '}'; } @Override @@ -460,7 +461,7 @@ public void unlock() { } public void cancelCrafts() { - this.activeCrafts.entries().removeIf(c -> c.getValue().markCanceledOrResolve(this)); + this.activeCrafts.entries().removeIf(c -> c.getValue().markCanceledOrResolve(this, true)); } private List createAllEdges(BlockPos pos, BlockState state, boolean ignoreCurrBlocked) { diff --git a/src/main/java/de/ellpeck/prettypipes/packets/PacketButton.java b/src/main/java/de/ellpeck/prettypipes/packets/PacketButton.java index fe35174..3b556e5 100644 --- a/src/main/java/de/ellpeck/prettypipes/packets/PacketButton.java +++ b/src/main/java/de/ellpeck/prettypipes/packets/PacketButton.java @@ -127,7 +127,7 @@ public boolean shouldTriggerClientSideContainerClosingOnOpen() { }), CANCEL_CRAFTING((pos, data, player) -> { var tile = Utility.getBlockEntity(ItemTerminalBlockEntity.class, player.level(), pos); - tile.cancelCrafting(); + tile.cancelCrafting(data.getFirst() == 1); }), TAG_FILTER((pos, data, player) -> { var container = (FilterModifierModuleContainer) player.containerMenu; diff --git a/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlockEntity.java b/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlockEntity.java index f75a8bf..fe8c096 100644 --- a/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlockEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/terminal/ItemTerminalBlockEntity.java @@ -133,7 +133,7 @@ public void updateItems(Player... playersToSync) { if (playersToSync.length > 0) { var clientItems = this.networkItems.values().stream().map(NetworkItem::asStack).collect(Collectors.toList()); var clientCraftables = PipeNetwork.get(this.level).getAllCraftables(pipe.getBlockPos()).stream().map(Pair::getRight).collect(Collectors.toList()); - var currentlyCrafting = this.getCurrentlyCrafting(false).stream().sorted(Comparator.comparingInt(ItemStack::getCount).reversed()).collect(Collectors.toList()); + var currentlyCrafting = this.getCurrentlyCrafting(true).stream().sorted(Comparator.comparingInt(ItemStack::getCount).reversed()).collect(Collectors.toList()); for (var player : playersToSync) { if (!(player.containerMenu instanceof ItemTerminalContainer container)) continue; @@ -207,7 +207,7 @@ private List getCurrentlyCrafting(boolean includeCanceled) { return crafting.stream().map(Pair::getRight).collect(Collectors.toList()); } - public void cancelCrafting() { + public void cancelCrafting(boolean force) { var network = PipeNetwork.get(this.level); var pipe = this.getConnectedPipe(); if (pipe == null) @@ -215,7 +215,7 @@ public void cancelCrafting() { for (var craftable : network.getAllCraftables(pipe.getBlockPos())) { var otherPipe = network.getPipe(craftable.getLeft()); if (otherPipe != null) - otherPipe.getActiveCrafts().removeIf(c -> c.markCanceledOrResolve(network)); + otherPipe.getActiveCrafts().removeIf(c -> c.markCanceledOrResolve(network, force)); } var lookingPlayers = this.getLookingPlayers(); if (lookingPlayers.length > 0) diff --git a/src/main/java/de/ellpeck/prettypipes/terminal/containers/ItemTerminalGui.java b/src/main/java/de/ellpeck/prettypipes/terminal/containers/ItemTerminalGui.java index 2d9b1ff..b495eaf 100644 --- a/src/main/java/de/ellpeck/prettypipes/terminal/containers/ItemTerminalGui.java +++ b/src/main/java/de/ellpeck/prettypipes/terminal/containers/ItemTerminalGui.java @@ -161,7 +161,7 @@ public boolean mouseReleased(double mouseX, double mouseY, int button) { // and vanilla buttons are activated when the click starts, so we'll always invoke jei accidentally by default if (button == 0 && this.cancelCraftingButton.visible && this.cancelCraftingButton.isHovered()) { if (this.currentlyCrafting != null && !this.currentlyCrafting.isEmpty()) { - PacketDistributor.sendToServer(new PacketButton(this.menu.tile.getBlockPos(), PacketButton.ButtonResult.CANCEL_CRAFTING, List.of())); + PacketDistributor.sendToServer(new PacketButton(this.menu.tile.getBlockPos(), PacketButton.ButtonResult.CANCEL_CRAFTING, List.of(hasShiftDown() ? 1 : 0))); return true; } } @@ -235,7 +235,7 @@ public void updateWidgets() { } else if (search.startsWith("#")) { // search item description var hoverText = s.getLeft().getTooltipLines(Item.TooltipContext.of(this.minecraft.level), this.minecraft.player, - this.minecraft.options.advancedItemTooltips ? TooltipFlag.Default.ADVANCED : TooltipFlag.Default.NORMAL); + this.minecraft.options.advancedItemTooltips ? TooltipFlag.Default.ADVANCED : TooltipFlag.Default.NORMAL); toCompare = hoverText.stream().map(Component::getString).collect(Collectors.joining("\n")); search = search.substring(1); } else { @@ -364,8 +364,8 @@ public T addRenderab public Stream streamWidgets() { return this.renderables.stream() - .filter(w -> w instanceof ItemTerminalWidget) - .map(w -> (ItemTerminalWidget) w); + .filter(w -> w instanceof ItemTerminalWidget) + .map(w -> (ItemTerminalWidget) w); } public static int requestModifier() { diff --git a/src/main/resources/assets/prettypipes/lang/en_us.json b/src/main/resources/assets/prettypipes/lang/en_us.json index 8911144..bfab943 100644 --- a/src/main/resources/assets/prettypipes/lang/en_us.json +++ b/src/main/resources/assets/prettypipes/lang/en_us.json @@ -94,7 +94,7 @@ "info.prettypipes.energy": "%s / %s FE", "info.prettypipes.crafting": "Awaiting", "info.prettypipes.cancel_all": "Cancel", - "info.prettypipes.cancel_all.desc": "Stops waiting for current crafting outputs\nOngoing crafting operations are still completed", + "info.prettypipes.cancel_all.desc": "Stops waiting for current crafting outputs\nOngoing crafting operations are still completed\nHold Shift to include ongoing crafting operations,\nwhich may cause partial recipes to stay behind", "info.prettypipes.no_pipe_connected": "The terminal needs to be connected to a pipe network", "info.prettypipes.too_many_pipes_connected": "The terminal can only be connected to a single pipe at a time", "dir.prettypipes.up": "Up",