Skip to content

Commit

Permalink
allow force-canceling crafts that are in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellpeck committed Nov 28, 2024
1 parent 1b0c36d commit 887bfed
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
23 changes: 18 additions & 5 deletions src/main/java/de/ellpeck/prettypipes/network/ActiveCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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;
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/de/ellpeck/prettypipes/network/PipeNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<NetworkEdge> createAllEdges(BlockPos pos, BlockState state, boolean ignoreCurrBlocked) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -207,15 +207,15 @@ private List<ItemStack> 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)
return;
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -364,8 +364,8 @@ public <T extends GuiEventListener & Renderable & NarratableEntry> T addRenderab

public Stream<ItemTerminalWidget> 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() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/assets/prettypipes/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 887bfed

Please sign in to comment.