Compare commits

...

6 commits

Author SHA1 Message Date
Ell
cee38be5cd 1.19.0
**Due to a lot of internal changes, there may be newly introduced issues with autocrafting in this update. Please report anything you find to the issue tracker!**

Improvements
- Fully deliver items of canceled in-progress crafting recipes so that partial recipes don't get stuck in crafting containers
- Allow force-canceling in-progress crafts by holding shift on the Cancel button
- Cleaned up the output of the /prettypipes dump command
2024-11-28 12:55:05 +01:00
Ell
9fc0c12bf9 clean up the dump command's output 2024-11-28 12:42:18 +01:00
Ell
887bfed229 allow force-canceling crafts that are in progress 2024-11-28 12:33:06 +01:00
Ell
1b0c36d5e5 store active crafts globally in the network (and add a command to clear all) 2024-11-28 12:15:44 +01:00
Ell
0942be0725 mark crafting dependencies as in progress immediately so that they don't cause unfinished crafts 2024-11-28 12:02:14 +01:00
Ell
0eb8740fde pass active crafts when started 2024-11-28 11:55:55 +01:00
13 changed files with 283 additions and 225 deletions

View file

@ -34,8 +34,9 @@ mod_name=PrettyPipes
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=MIT
# The mod version. See https://semver.org/
mod_version=1.18.1
mod_release_state=STABLE
mod_version=1.19.0
# STABLE, BETA, ALPHA
mod_release_state=BETA
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html

View file

@ -2,6 +2,7 @@ package de.ellpeck.prettypipes.items;
import de.ellpeck.prettypipes.misc.DirectionSelector;
import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.network.ActiveCraft;
import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.core.BlockPos;
@ -10,7 +11,9 @@ import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.items.IItemHandler;
import org.apache.commons.lang3.tuple.Pair;
import java.util.Collection;
import java.util.List;
import java.util.Stack;
import java.util.function.Consumer;
@ -41,7 +44,7 @@ public interface IModule {
int getCraftableAmount(ItemStack module, PipeBlockEntity tile, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain);
ItemStack craft(ItemStack module, PipeBlockEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain);
Pair<ItemStack, Collection<ActiveCraft>> craft(ItemStack module, PipeBlockEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain);
Integer getCustomNextNode(ItemStack module, PipeBlockEntity tile, List<BlockPos> nodes, int index);

View file

@ -3,6 +3,7 @@ package de.ellpeck.prettypipes.items;
import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.misc.DirectionSelector;
import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.network.ActiveCraft;
import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.core.BlockPos;
@ -16,7 +17,9 @@ import net.minecraft.world.item.TooltipFlag;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
import net.neoforged.neoforge.items.IItemHandler;
import org.apache.commons.lang3.tuple.Pair;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Stack;
@ -89,8 +92,8 @@ public abstract class ModuleItem extends Item implements IModule {
}
@Override
public ItemStack craft(ItemStack module, PipeBlockEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) {
return stack;
public Pair<ItemStack, Collection<ActiveCraft>> craft(ItemStack module, PipeBlockEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) {
return Pair.of(stack, List.of());
}
@Override

View file

@ -19,31 +19,37 @@ public final class Events {
@SubscribeEvent
public static void onServerStarting(ServerStartingEvent event) {
event.getServer().getCommands().getDispatcher().register(Commands.literal(PrettyPipes.ID).requires(s -> s.hasPermission(2))
.then(Commands.literal("dump").executes(c -> {
var source = c.getSource();
var file = Paths.get('_' + PrettyPipes.ID + "dump.txt");
var dump = PipeNetwork.get(source.getLevel()).toString();
try {
Files.writeString(file, dump, StandardCharsets.UTF_8);
source.sendSuccess(() -> Component.literal("Wrote network dump to file " + file.toAbsolutePath()), true);
} catch (IOException e) {
source.sendFailure(Component.literal("Failed to write network dump to file " + file.toAbsolutePath()));
return -1;
}
return 0;
}))
.then(Commands.literal("uncache").executes(c -> {
var source = c.getSource();
PipeNetwork.get(source.getLevel()).clearCaches();
source.sendSuccess(() -> Component.literal("Cleared all pipe caches in the world"), true);
return 0;
}))
.then(Commands.literal("unlock").executes(c -> {
var source = c.getSource();
PipeNetwork.get(source.getLevel()).unlock();
source.sendSuccess(() -> Component.literal("Resolved all network locks in the world"), true);
return 0;
})));
.then(Commands.literal("dump").executes(c -> {
var source = c.getSource();
var file = Paths.get('_' + PrettyPipes.ID + "dump.txt");
var dump = PipeNetwork.get(source.getLevel()).toString();
try {
Files.writeString(file, dump, StandardCharsets.UTF_8);
source.sendSuccess(() -> Component.literal("Wrote network dump to file " + file.toAbsolutePath()), true);
} catch (IOException e) {
source.sendFailure(Component.literal("Failed to write network dump to file " + file.toAbsolutePath()));
return -1;
}
return 0;
}))
.then(Commands.literal("uncache").executes(c -> {
var source = c.getSource();
PipeNetwork.get(source.getLevel()).clearCaches();
source.sendSuccess(() -> Component.literal("Cleared all pipe caches in the world"), true);
return 0;
}))
.then(Commands.literal("unlock").executes(c -> {
var source = c.getSource();
PipeNetwork.get(source.getLevel()).unlock();
source.sendSuccess(() -> Component.literal("Resolved all network locks in the world"), true);
return 0;
}))
.then(Commands.literal("uncraft").executes(c -> {
var source = c.getSource();
PipeNetwork.get(source.getLevel()).cancelCrafts();
source.sendSuccess(() -> Component.literal("Canceled all active crafts in the world"), true);
return 0;
})));
}
}

View file

@ -0,0 +1,98 @@
package de.ellpeck.prettypipes.network;
import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.misc.ItemEquality;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.common.util.INBTSerializable;
import org.jetbrains.annotations.UnknownNullability;
import java.util.ArrayList;
import java.util.List;
public class ActiveCraft implements INBTSerializable<CompoundTag> {
public BlockPos pipe;
public int moduleSlot;
public List<ItemStack> travelingIngredients = new ArrayList<>();
public List<NetworkLock> ingredientsToRequest;
public BlockPos resultDestPipe;
public ItemStack resultStackRemain;
public boolean inProgress;
// we only remove canceled requests from the queue once their items are fully delivered to the crafting location, so that unfinished recipes don't get stuck in crafters etc.
public boolean canceled;
public ActiveCraft(BlockPos pipe, int moduleSlot, List<NetworkLock> ingredientsToRequest, BlockPos resultDestPipe, ItemStack resultStackRemain) {
this.pipe = pipe;
this.moduleSlot = moduleSlot;
this.ingredientsToRequest = ingredientsToRequest;
this.resultDestPipe = resultDestPipe;
this.resultStackRemain = resultStackRemain;
}
public ActiveCraft(HolderLookup.Provider provider, CompoundTag tag) {
this.deserializeNBT(provider, tag);
}
@Override
public @UnknownNullability CompoundTag serializeNBT(HolderLookup.Provider provider) {
var ret = new CompoundTag();
ret.putLong("pipe", this.pipe.asLong());
ret.putInt("module_slot", this.moduleSlot);
ret.put("ingredients_to_request", Utility.serializeAll(this.ingredientsToRequest, n -> n.serializeNBT(provider)));
ret.put("traveling_ingredients", Utility.serializeAll(this.travelingIngredients, s -> (CompoundTag) s.save(provider, new CompoundTag())));
ret.putLong("result_dest_pipe", this.resultDestPipe.asLong());
ret.put("result_stack_remain", this.resultStackRemain.saveOptional(provider));
ret.putBoolean("in_progress", this.inProgress);
ret.putBoolean("canceled", this.canceled);
return ret;
}
@Override
public void deserializeNBT(HolderLookup.Provider provider, CompoundTag nbt) {
this.pipe = BlockPos.of(nbt.getLong("pipe"));
this.moduleSlot = nbt.getInt("module_slot");
this.ingredientsToRequest = Utility.deserializeAll(nbt.getList("ingredients_to_request", Tag.TAG_COMPOUND), t -> new NetworkLock(provider, t));
this.travelingIngredients = Utility.deserializeAll(nbt.getList("traveling_ingredients", Tag.TAG_COMPOUND), t -> ItemStack.parse(provider, t).orElseThrow());
this.resultDestPipe = BlockPos.of(nbt.getLong("result_dest_pipe"));
this.resultStackRemain = ItemStack.parseOptional(provider, nbt.getCompound("result_stack_remain"));
this.inProgress = nbt.getBoolean("in_progress");
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))
return traveling;
}
return ItemStack.EMPTY;
}
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

@ -26,6 +26,7 @@ import net.minecraft.world.level.saveddata.SavedData;
import net.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.network.PacketDistributor;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import org.jgrapht.ListenableGraph;
import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
import org.jgrapht.event.GraphEdgeChangeEvent;
@ -54,6 +55,7 @@ public class PipeNetwork extends SavedData implements GraphListener<BlockPos, Ne
private final Map<BlockPos, PipeBlockEntity> tileCache = new HashMap<>();
private final ListMultimap<BlockPos, IPipeItem> pipeItems = ArrayListMultimap.create();
private final ListMultimap<BlockPos, NetworkLock> networkLocks = ArrayListMultimap.create();
private final ListMultimap<BlockPos, ActiveCraft> activeCrafts = ArrayListMultimap.create();
private Level level;
public PipeNetwork() {
@ -73,6 +75,8 @@ public class PipeNetwork extends SavedData implements GraphListener<BlockPos, Ne
this.pipeItems.put(item.getCurrentPipe(), item);
for (var lock : Utility.deserializeAll(nbt.getList("locks", Tag.TAG_COMPOUND), t -> new NetworkLock(provider, t)))
this.createNetworkLock(lock);
for (var craft : Utility.deserializeAll(nbt.getList("crafts", Tag.TAG_COMPOUND), t -> new ActiveCraft(provider, t)))
this.activeCrafts.put(craft.pipe, craft);
}
@Override
@ -100,12 +104,12 @@ public class PipeNetwork extends SavedData implements GraphListener<BlockPos, Ne
@Override
public String toString() {
return "PipeNetwork{" +
"\ngraph=" + this.graph +
",\nnodeToConnectedNodes=" + this.nodeToConnectedNodes +
",\ntileCache=" + this.tileCache.keySet() +
",\npipeItems=" + this.pipeItems +
",\nnetworkLocks=" + this.networkLocks + '}';
return "graph:\n\tnodes: " + this.graph.edgeSet() + "\n\tedges: " + this.graph.edgeSet() +
"\nnodeToConnectedNodes:\n" + PipeNetwork.toNewlineDelimitedString(this.nodeToConnectedNodes.entrySet()) +
"\ntileCache:\n" + PipeNetwork.toNewlineDelimitedString(this.tileCache.keySet()) +
"\npipeItems:\n" + PipeNetwork.toNewlineDelimitedString(this.pipeItems.entries()) +
"\nnetworkLocks:\n" + PipeNetwork.toNewlineDelimitedString(this.networkLocks.entries()) +
"\nactiveCrafts:\n" + PipeNetwork.toNewlineDelimitedString(this.activeCrafts.entries());
}
@Override
@ -120,6 +124,7 @@ public class PipeNetwork extends SavedData implements GraphListener<BlockPos, Ne
nbt.put("edges", edges);
nbt.put("items", Utility.serializeAll(this.pipeItems.values(), i -> i.serializeNBT(provider)));
nbt.put("locks", Utility.serializeAll(this.networkLocks.values(), l -> l.serializeNBT(provider)));
nbt.put("crafts", Utility.serializeAll(this.activeCrafts.values(), c -> c.serializeNBT(provider)));
return nbt;
}
@ -213,21 +218,58 @@ public class PipeNetwork extends SavedData implements GraphListener<BlockPos, Ne
return remain;
}
// check craftable items
return this.requestCraftedItem(destPipe, null, remain, new Stack<>(), equalityTypes);
return this.requestCraftedItem(destPipe, null, remain, new Stack<>(), equalityTypes).getLeft();
}
public ItemStack requestCraftedItem(BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain, ItemEquality... equalityTypes) {
public Triple<List<NetworkLock>, ItemStack, Collection<ActiveCraft>> requestLocksAndStartCrafting(BlockPos destPipe, Collection<NetworkLocation> locations, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain, ItemEquality... equalityTypes) {
List<NetworkLock> requests = new ArrayList<>();
var remain = stack.copy();
// check for existing items
for (var location : locations) {
var amount = location.getItemAmount(this.level, stack, equalityTypes);
if (amount <= 0)
continue;
amount -= this.getLockedAmount(location.getPos(), stack, null, equalityTypes);
if (amount > 0) {
if (remain.getCount() < amount)
amount = remain.getCount();
remain.shrink(amount);
while (amount > 0) {
var copy = stack.copy();
copy.setCount(Math.min(stack.getMaxStackSize(), amount));
var lock = new NetworkLock(location, copy);
this.createNetworkLock(lock);
requests.add(lock);
amount -= copy.getCount();
}
if (remain.isEmpty())
break;
}
}
if (!remain.isEmpty()) {
// check for craftable items
var started = this.requestCraftedItem(destPipe, unavailableConsumer, remain, dependencyChain, equalityTypes);
return Triple.of(requests, started.getLeft(), started.getRight());
} else {
return Triple.of(requests, remain, List.of());
}
}
public Pair<ItemStack, Collection<ActiveCraft>> requestCraftedItem(BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain, ItemEquality... equalityTypes) {
var crafts = new ArrayList<ActiveCraft>();
for (var craftable : this.getAllCraftables(destPipe)) {
if (!ItemEquality.compareItems(stack, craftable.getRight(), equalityTypes))
continue;
var pipe = this.getPipe(craftable.getLeft());
if (pipe == null)
continue;
stack = pipe.craft(destPipe, unavailableConsumer, stack, dependencyChain);
var started = pipe.craft(destPipe, unavailableConsumer, stack, dependencyChain);
stack = started.getLeft();
crafts.addAll(started.getRight());
if (stack.isEmpty())
break;
}
return stack;
return Pair.of(stack, crafts);
}
public ItemStack requestExistingItem(NetworkLocation location, BlockPos destPipe, BlockPos destInventory, NetworkLock ignoredLock, ItemStack stack, ItemEquality... equalityTypes) {
@ -283,18 +325,17 @@ public class PipeNetwork extends SavedData implements GraphListener<BlockPos, Ne
var craftingPipes = this.getAllCraftables(node).stream().map(c -> this.getPipe(c.getLeft())).distinct().iterator();
while (craftingPipes.hasNext()) {
var pipe = craftingPipes.next();
for (var craft : pipe.activeCrafts) {
var data = craft.getRight();
if (!includeCanceled && data.canceled)
for (var craft : pipe.getActiveCrafts()) {
if (!includeCanceled && craft.canceled)
continue;
// add up all the items that should go to the same location
var existing = items.stream()
.filter(s -> s.getLeft().equals(data.resultDestPipe) && ItemEquality.compareItems(s.getRight(), data.resultStackRemain, equalityTypes))
.filter(s -> s.getLeft().equals(craft.resultDestPipe) && ItemEquality.compareItems(s.getRight(), craft.resultStackRemain, equalityTypes))
.findFirst();
if (existing.isPresent()) {
existing.get().getRight().grow(data.resultStackRemain.getCount());
existing.get().getRight().grow(craft.resultStackRemain.getCount());
} else {
items.add(Pair.of(data.resultDestPipe, data.resultStackRemain.copy()));
items.add(Pair.of(craft.resultDestPipe, craft.resultStackRemain.copy()));
}
}
}
@ -418,6 +459,10 @@ public class PipeNetwork extends SavedData implements GraphListener<BlockPos, Ne
this.networkLocks.clear();
}
public void cancelCrafts() {
this.activeCrafts.entries().removeIf(c -> c.getValue().markCanceledOrResolve(this, true));
}
private List<NetworkEdge> createAllEdges(BlockPos pos, BlockState state, boolean ignoreCurrBlocked) {
this.startProfile("create_all_edges");
List<NetworkEdge> edges = new ArrayList<>();
@ -496,7 +541,7 @@ public class PipeNetwork extends SavedData implements GraphListener<BlockPos, Ne
this.startProfile("clear_node_cache");
// remove caches for the nodes
for (var node : nodes)
this.nodeToConnectedNodes.keySet().remove(node);
this.nodeToConnectedNodes.remove(node);
// remove caches that contain the nodes as a destination
this.nodeToConnectedNodes.values().removeIf(cached -> nodes.stream().anyMatch(cached::contains));
this.endProfile();
@ -506,6 +551,10 @@ public class PipeNetwork extends SavedData implements GraphListener<BlockPos, Ne
return this.pipeItems.get(pos);
}
public List<ActiveCraft> getActiveCrafts(BlockPos pos) {
return this.activeCrafts.get(pos);
}
public Stream<IPipeItem> getPipeItemsOnTheWay(BlockPos goalInv) {
this.startProfile("get_pipe_items_on_the_way");
var ret = this.pipeItems.values().stream().filter(i -> i.getDestInventory().equals(goalInv));
@ -544,4 +593,8 @@ public class PipeNetwork extends SavedData implements GraphListener<BlockPos, Ne
}
}
private static String toNewlineDelimitedString(Collection<?> collection) {
return collection.stream().map(c -> "\t" + c.toString()).collect(Collectors.joining("\n"));
}
}

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

@ -270,8 +270,8 @@ public class PipeBlock extends BaseEntityBlock implements SimpleWaterloggedBlock
network.onPipeChanged(pos, state);
if (worldIn.getBlockEntity(pos) instanceof PipeBlockEntity pipe) {
pipe.getItems().clear();
for (var craft : pipe.activeCrafts) {
for (var lock : craft.getRight().ingredientsToRequest)
for (var craft : pipe.getActiveCrafts()) {
for (var lock : craft.ingredientsToRequest)
network.resolveNetworkLock(lock);
}
}

View file

@ -5,10 +5,9 @@ import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.network.NetworkLock;
import de.ellpeck.prettypipes.network.ActiveCraft;
import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.pipe.containers.MainPipeContainer;
import de.ellpeck.prettypipes.pipe.modules.craft.CraftingModuleItem;
import de.ellpeck.prettypipes.pressurizer.PressurizerBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
@ -42,7 +41,6 @@ import net.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.items.ItemHandlerHelper;
import net.neoforged.neoforge.items.ItemStackHandler;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -72,11 +70,11 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC
PipeBlockEntity.this.setChanged();
}
};
public final List<Pair<Integer, CraftingModuleItem.ActiveCraft>> activeCrafts = new ArrayList<>();
public PressurizerBlockEntity pressurizer;
public BlockState cover;
public int moduleDropCheck;
protected List<IPipeItem> items;
private List<IPipeItem> itemCache;
private List<ActiveCraft> activeCraftCache;
private int lastItemAmount;
private int priority;
private final Lazy<Integer> workRandomizer = Lazy.of(() -> this.level.random.nextInt(200));
@ -101,14 +99,6 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC
compound.putInt("module_drop_check", this.moduleDropCheck);
if (this.cover != null)
compound.put("cover", NbtUtils.writeBlockState(this.cover));
var crafts = new ListTag();
for (var craft : this.activeCrafts) {
var tag = new CompoundTag();
tag.putInt("module_slot", craft.getLeft());
tag.put("data", craft.getRight().serializeNBT(provider));
crafts.add(tag);
}
compound.put("active_crafts", crafts);
}
@Override
@ -116,12 +106,6 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC
this.modules.deserializeNBT(provider, compound.getCompound("modules"));
this.moduleDropCheck = compound.getInt("module_drop_check");
this.cover = compound.contains("cover") ? NbtUtils.readBlockState(this.level != null ? this.level.holderLookup(Registries.BLOCK) : BuiltInRegistries.BLOCK.asLookup(), compound.getCompound("cover")) : null;
this.activeCrafts.clear();
var crafts = compound.getList("active_crafts", Tag.TAG_COMPOUND);
for (var i = 0; i < crafts.size(); i++) {
var tag = crafts.getCompound(i);
this.activeCrafts.add(Pair.of(tag.getInt("module_slot"), new CraftingModuleItem.ActiveCraft(provider, tag.getCompound("data"))));
}
super.loadAdditional(compound, provider);
}
@ -147,9 +131,15 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC
}
public List<IPipeItem> getItems() {
if (this.items == null)
this.items = PipeNetwork.get(this.level).getItemsInPipe(this.worldPosition);
return this.items;
if (this.itemCache == null)
this.itemCache = PipeNetwork.get(this.level).getItemsInPipe(this.worldPosition);
return this.itemCache;
}
public List<ActiveCraft> getActiveCrafts() {
if (this.activeCraftCache == null)
this.activeCraftCache = PipeNetwork.get(this.level).getActiveCrafts(this.worldPosition);
return this.activeCraftCache;
}
public void addNewItem(IPipeItem item) {
@ -296,15 +286,18 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC
return total;
}
public ItemStack craft(BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) {
public Pair<ItemStack, Collection<ActiveCraft>> craft(BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) {
var crafts = new ArrayList<ActiveCraft>();
var modules = this.streamModules().iterator();
while (modules.hasNext()) {
var module = modules.next();
stack = module.getRight().craft(module.getLeft(), this, destPipe, unavailableConsumer, stack, dependencyChain);
var started = module.getRight().craft(module.getLeft(), this, destPipe, unavailableConsumer, stack, dependencyChain);
stack = started.getLeft();
crafts.addAll(started.getRight());
if (stack.isEmpty())
break;
}
return stack;
return Pair.of(stack, crafts);
}
public IItemHandler getItemHandler(Direction dir) {

View file

@ -7,32 +7,25 @@ import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.items.ModuleTier;
import de.ellpeck.prettypipes.misc.EquatableItemStack;
import de.ellpeck.prettypipes.misc.ItemEquality;
import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.network.ActiveCraft;
import de.ellpeck.prettypipes.network.NetworkLock;
import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import de.ellpeck.prettypipes.terminal.CraftingTerminalBlockEntity;
import de.ellpeck.prettypipes.terminal.ItemTerminalBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.common.util.INBTSerializable;
import net.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.items.ItemHandlerHelper;
import net.neoforged.neoforge.items.ItemStackHandler;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import org.jetbrains.annotations.UnknownNullability;
import java.util.*;
import java.util.function.Consumer;
@ -77,15 +70,14 @@ public class CraftingModuleItem extends ModuleItem {
return;
var slot = tile.getModuleSlot(module);
var network = PipeNetwork.get(tile.getLevel());
if (!tile.activeCrafts.isEmpty()) {
var activeCraft = tile.activeCrafts.getFirst();
if (activeCraft.getLeft() == slot) {
var craftData = activeCraft.getRight();
var crafts = tile.getActiveCrafts();
if (!crafts.isEmpty()) {
var craft = crafts.getFirst();
if (craft.moduleSlot == slot) {
// process crafting ingredient requests
if (!craftData.ingredientsToRequest.isEmpty()) {
if (!craft.ingredientsToRequest.isEmpty()) {
network.startProfile("crafting_ingredients");
var lock = craftData.ingredientsToRequest.getFirst();
var lock = craft.ingredientsToRequest.getFirst();
var equalityTypes = ItemFilter.getEqualityTypes(tile);
var dest = tile.getAvailableDestination(Direction.values(), lock.stack, true, true);
if (dest != null) {
@ -94,12 +86,12 @@ public class CraftingModuleItem extends ModuleItem {
if (!ensureItemOrder || network.getPipeItemsOnTheWay(dest.getLeft()).findAny().isEmpty()) {
var requestRemain = network.requestExistingItem(lock.location, tile.getBlockPos(), dest.getLeft(), lock, dest.getRight(), equalityTypes);
network.resolveNetworkLock(lock);
craftData.ingredientsToRequest.remove(lock);
craftData.inProgress = true;
craft.ingredientsToRequest.remove(lock);
craft.inProgress = true;
var traveling = lock.stack.copy();
traveling.shrink(requestRemain.getCount());
craftData.travelingIngredients.add(traveling);
craft.travelingIngredients.add(traveling);
// if we couldn't fit all items into the destination, create another request for the rest
var remain = lock.stack.copy();
@ -107,8 +99,8 @@ public class CraftingModuleItem extends ModuleItem {
if (!remain.isEmpty()) {
var remainRequest = new NetworkLock(lock.location, remain);
// if we're ensuring item order, we need to insert the remaining request at the start so that it gets processed first
var index = ensureItemOrder ? 0 : craftData.ingredientsToRequest.size();
craftData.ingredientsToRequest.add(index, remainRequest);
var index = ensureItemOrder ? 0 : craft.ingredientsToRequest.size();
craft.ingredientsToRequest.add(index, remainRequest);
network.createNetworkLock(remainRequest);
}
}
@ -117,19 +109,19 @@ public class CraftingModuleItem extends ModuleItem {
}
// pull requested crafting results from the network once they are stored
if (!craftData.resultStackRemain.isEmpty()) {
if (!craft.resultStackRemain.isEmpty()) {
network.startProfile("crafting_results");
var items = network.getOrderedNetworkItems(tile.getBlockPos());
var equalityTypes = ItemFilter.getEqualityTypes(tile);
var destPipe = network.getPipe(craftData.resultDestPipe);
var destPipe = network.getPipe(craft.resultDestPipe);
if (destPipe != null) {
var dest = destPipe.getAvailableDestinationOrConnectable(craftData.resultStackRemain, true, true);
var dest = destPipe.getAvailableDestinationOrConnectable(craft.resultStackRemain, true, true);
if (dest != null) {
for (var item : items) {
var requestRemain = network.requestExistingItem(item, craftData.resultDestPipe, dest.getLeft(), null, dest.getRight(), equalityTypes);
craftData.resultStackRemain.shrink(dest.getRight().getCount() - requestRemain.getCount());
if (craftData.resultStackRemain.isEmpty()) {
tile.activeCrafts.remove(activeCraft);
var requestRemain = network.requestExistingItem(item, craft.resultDestPipe, dest.getLeft(), null, dest.getRight(), equalityTypes);
craft.resultStackRemain.shrink(dest.getRight().getCount() - requestRemain.getCount());
if (craft.resultStackRemain.isEmpty()) {
crafts.remove(craft);
break;
}
}
@ -174,11 +166,11 @@ public class CraftingModuleItem extends ModuleItem {
}
@Override
public ItemStack craft(ItemStack module, PipeBlockEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) {
public Pair<ItemStack, Collection<ActiveCraft>> craft(ItemStack module, PipeBlockEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) {
// check if we can craft the required amount of items
var craftableAmount = this.getCraftableAmount(module, tile, unavailableConsumer, stack, dependencyChain);
if (craftableAmount <= 0)
return stack;
return Pair.of(stack, List.of());
var slot = tile.getModuleSlot(module);
var network = PipeNetwork.get(tile.getLevel());
@ -192,6 +184,7 @@ public class CraftingModuleItem extends ModuleItem {
var toCraft = Math.min(craftableCrafts, requiredCrafts);
var locks = new ArrayList<NetworkLock>();
var crafts = new ArrayList<ActiveCraft>();
var contents = module.get(Contents.TYPE);
// if we're ensuring item order, all items for a single recipe should be sent in order first before starting on the next one!
for (var c = contents.ensureItemOrder ? toCraft : 1; c > 0; c--) {
@ -202,32 +195,37 @@ public class CraftingModuleItem extends ModuleItem {
var copy = in.copy();
if (!contents.ensureItemOrder)
copy.setCount(in.getCount() * toCraft);
var ret = ItemTerminalBlockEntity.requestItemLater(tile.getLevel(), tile.getBlockPos(), items, unavailableConsumer, copy, CraftingModuleItem.addDependency(dependencyChain, module), equalityTypes);
var ret = network.requestLocksAndStartCrafting(tile.getBlockPos(), items, unavailableConsumer, copy, CraftingModuleItem.addDependency(dependencyChain, module), equalityTypes);
locks.addAll(ret.getLeft());
crafts.addAll(ret.getRight());
}
}
// set crafting dependencies as in progress immediately so that, when canceling, they don't leave behind half-crafted inbetween dependencies
// TODO to be more optimal, we should really do this when setting the main craft as in progress, but that would require storing references to all of the dependencies
crafts.forEach(c -> c.inProgress = true);
var remain = stack.copy();
remain.shrink(resultAmount * toCraft);
var result = stack.copy();
result.shrink(remain.getCount());
var activeCraft = new ActiveCraft(locks, new ArrayList<>(), destPipe, result);
tile.activeCrafts.add(Pair.of(slot, activeCraft));
var activeCraft = new ActiveCraft(tile.getBlockPos(), slot, locks, destPipe, result);
tile.getActiveCrafts().add(activeCraft);
crafts.add(activeCraft);
return remain;
return Pair.of(remain, crafts);
}
@Override
public ItemStack store(ItemStack module, PipeBlockEntity tile, ItemStack stack, Direction direction) {
var slot = tile.getModuleSlot(module);
var equalityTypes = ItemFilter.getEqualityTypes(tile);
var matchingCraft = tile.activeCrafts.stream()
.filter(c -> c.getLeft() == slot && !c.getRight().getTravelingIngredient(stack, equalityTypes).isEmpty())
var crafts = tile.getActiveCrafts();
var craft = crafts.stream()
.filter(c -> c.moduleSlot == slot && !c.getTravelingIngredient(stack, equalityTypes).isEmpty())
.findAny().orElse(null);
if (matchingCraft != null) {
var data = matchingCraft.getRight();
data.travelingIngredients.remove(data.getTravelingIngredient(stack, equalityTypes));
if (craft != null) {
craft.travelingIngredients.remove(craft.getTravelingIngredient(stack, equalityTypes));
if (module.get(Contents.TYPE).insertSingles) {
var handler = tile.getItemHandler(direction);
@ -242,8 +240,8 @@ public class CraftingModuleItem extends ModuleItem {
}
// if we canceled the request and all input items are delivered (ie the machine actually got what it expected), remove it from the queue
if (data.canceled && data.travelingIngredients.size() <= 0 && data.ingredientsToRequest.size() <= 0)
tile.activeCrafts.remove(matchingCraft);
if (craft.canceled && craft.travelingIngredients.size() <= 0 && craft.ingredientsToRequest.size() <= 0)
crafts.remove(craft);
}
return stack;
}
@ -260,6 +258,7 @@ public class CraftingModuleItem extends ModuleItem {
}
private static Stack<ItemStack> addDependency(Stack<ItemStack> deps, ItemStack module) {
// noinspection unchecked
deps = (Stack<ItemStack>) deps.clone();
deps.push(module);
return deps;
@ -277,68 +276,4 @@ public class CraftingModuleItem extends ModuleItem {
}
public static class ActiveCraft implements INBTSerializable<CompoundTag> {
public List<NetworkLock> ingredientsToRequest;
public List<ItemStack> travelingIngredients;
public BlockPos resultDestPipe;
public ItemStack resultStackRemain;
public boolean inProgress;
// we only remove canceled requests from the queue once their items are fully delivered to the crafting location, so that unfinished recipes don't get stuck in crafters etc.
public boolean canceled;
public ActiveCraft(List<NetworkLock> ingredientsToRequest, List<ItemStack> travelingIngredients, BlockPos resultDestPipe, ItemStack resultStackRemain) {
this.ingredientsToRequest = ingredientsToRequest;
this.travelingIngredients = travelingIngredients;
this.resultDestPipe = resultDestPipe;
this.resultStackRemain = resultStackRemain;
}
public ActiveCraft(HolderLookup.Provider provider, CompoundTag tag) {
this.deserializeNBT(provider, tag);
}
@Override
public @UnknownNullability CompoundTag serializeNBT(HolderLookup.Provider provider) {
var ret = new CompoundTag();
ret.put("ingredients_to_request", Utility.serializeAll(this.ingredientsToRequest, n -> n.serializeNBT(provider)));
ret.put("traveling_ingredients", Utility.serializeAll(this.travelingIngredients, s -> (CompoundTag) s.save(provider, new CompoundTag())));
ret.putLong("result_dest_pipe", this.resultDestPipe.asLong());
ret.put("result_stack_remain", this.resultStackRemain.saveOptional(provider));
ret.putBoolean("in_progress", this.inProgress);
ret.putBoolean("canceled", this.canceled);
return ret;
}
@Override
public void deserializeNBT(HolderLookup.Provider provider, CompoundTag nbt) {
this.ingredientsToRequest = Utility.deserializeAll(nbt.getList("ingredients_to_request", Tag.TAG_COMPOUND), t -> new NetworkLock(provider, t));
this.travelingIngredients = Utility.deserializeAll(nbt.getList("traveling_ingredients", Tag.TAG_COMPOUND), t -> ItemStack.parse(provider, t).orElseThrow());
this.resultDestPipe = BlockPos.of(nbt.getLong("result_dest_pipe"));
this.resultStackRemain = ItemStack.parseOptional(provider, nbt.getCompound("result_stack_remain"));
this.inProgress = nbt.getBoolean("in_progress");
this.canceled = nbt.getBoolean("canceled");
}
public ItemStack getTravelingIngredient(ItemStack stack, ItemEquality... equalityTypes) {
for (var traveling : this.travelingIngredients) {
if (ItemEquality.compareItems(stack, traveling, equalityTypes))
return traveling;
}
return ItemStack.EMPTY;
}
public boolean markCanceledOrResolve(PipeNetwork network) {
if (this.inProgress) {
this.canceled = true;
return false;
} else {
for (var lock : this.ingredientsToRequest)
network.resolveNetworkLock(lock);
return true;
}
}
}
}

View file

@ -5,10 +5,7 @@ import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.misc.EquatableItemStack;
import de.ellpeck.prettypipes.misc.ItemEquality;
import de.ellpeck.prettypipes.network.NetworkItem;
import de.ellpeck.prettypipes.network.NetworkLocation;
import de.ellpeck.prettypipes.network.NetworkLock;
import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.network.*;
import de.ellpeck.prettypipes.packets.PacketNetworkItems;
import de.ellpeck.prettypipes.pipe.ConnectionType;
import de.ellpeck.prettypipes.pipe.IPipeConnectable;
@ -136,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;
@ -172,9 +169,10 @@ public class ItemTerminalBlockEntity extends BlockEntity implements IPipeConnect
public int requestItemImpl(ItemStack stack, Consumer<ItemStack> unavailableConsumer) {
var item = this.networkItems.get(new EquatableItemStack(stack, ItemEquality.NBT));
Collection<NetworkLocation> locations = item == null ? Collections.emptyList() : item.getLocations();
var ret = ItemTerminalBlockEntity.requestItemLater(this.level, this.getConnectedPipe().getBlockPos(), locations, unavailableConsumer, stack, new Stack<>(), ItemEquality.NBT);
var network = PipeNetwork.get(this.level);
var ret = network.requestLocksAndStartCrafting(this.getConnectedPipe().getBlockPos(), locations, unavailableConsumer, stack, new Stack<>(), ItemEquality.NBT);
this.existingRequests.addAll(ret.getLeft());
return stack.getCount() - ret.getRight().getCount();
return stack.getCount() - ret.getMiddle().getCount();
}
public Player[] getLookingPlayers() {
@ -209,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)
@ -217,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.activeCrafts.removeIf(c -> c.getRight().markCanceledOrResolve(network));
otherPipe.getActiveCrafts().removeIf(c -> c.markCanceledOrResolve(network, force));
}
var lookingPlayers = this.getLookingPlayers();
if (lookingPlayers.length > 0)
@ -269,38 +267,6 @@ public class ItemTerminalBlockEntity extends BlockEntity implements IPipeConnect
return true;
}
public static Pair<List<NetworkLock>, ItemStack> requestItemLater(Level world, BlockPos destPipe, Collection<NetworkLocation> locations, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain, ItemEquality... equalityTypes) {
List<NetworkLock> requests = new ArrayList<>();
var remain = stack.copy();
var network = PipeNetwork.get(world);
// check for existing items
for (var location : locations) {
var amount = location.getItemAmount(world, stack, equalityTypes);
if (amount <= 0)
continue;
amount -= network.getLockedAmount(location.getPos(), stack, null, equalityTypes);
if (amount > 0) {
if (remain.getCount() < amount)
amount = remain.getCount();
remain.shrink(amount);
while (amount > 0) {
var copy = stack.copy();
copy.setCount(Math.min(stack.getMaxStackSize(), amount));
var lock = new NetworkLock(location, copy);
network.createNetworkLock(lock);
requests.add(lock);
amount -= copy.getCount();
}
if (remain.isEmpty())
break;
}
}
// check for craftable items
if (!remain.isEmpty())
remain = network.requestCraftedItem(destPipe, unavailableConsumer, remain, dependencyChain, equalityTypes);
return Pair.of(requests, remain);
}
public static Consumer<ItemStack> onItemUnavailable(Player player, boolean ignore) {
return s -> {
if (ignore)

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