allow force-canceling crafts that are in progress

This commit is contained in:
Ell 2024-11-28 12:33:06 +01:00
parent 1b0c36d5e5
commit 887bfed229
6 changed files with 30 additions and 16 deletions

View file

@ -63,6 +63,19 @@ public class ActiveCraft implements INBTSerializable<CompoundTag> {
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 class ActiveCraft implements INBTSerializable<CompoundTag> {
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;
}
}

View file

@ -109,7 +109,8 @@ public class PipeNetwork extends SavedData implements GraphListener<BlockPos, Ne
",\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 class PipeNetwork extends SavedData implements GraphListener<BlockPos, Ne
}
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) {

View file

@ -127,7 +127,7 @@ public record PacketButton(BlockPos pos, int result, List<Integer> data) impleme
}),
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;

View file

@ -133,7 +133,7 @@ public class ItemTerminalBlockEntity extends BlockEntity implements IPipeConnect
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 @@ public class ItemTerminalBlockEntity extends BlockEntity implements IPipeConnect
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 class ItemTerminalBlockEntity extends BlockEntity implements IPipeConnect
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)

View file

@ -161,7 +161,7 @@ public class ItemTerminalGui extends AbstractContainerScreen<ItemTerminalContain
// 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 class ItemTerminalGui extends AbstractContainerScreen<ItemTerminalContain
} 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 class ItemTerminalGui extends AbstractContainerScreen<ItemTerminalContain
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() {

View file

@ -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",