Compare commits

..

No commits in common. "cee38be5cd9c6ad15cbefc5bfff4c344afb2b3d5" and "805393ba6e0c5970414b52a23c04c730894bc861" have entirely different histories.

13 changed files with 225 additions and 283 deletions

View file

@ -34,9 +34,8 @@ 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.19.0
# STABLE, BETA, ALPHA
mod_release_state=BETA
mod_version=1.18.1
mod_release_state=STABLE
# 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,7 +2,6 @@ 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;
@ -11,9 +10,7 @@ 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;
@ -44,7 +41,7 @@ public interface IModule {
int getCraftableAmount(ItemStack module, PipeBlockEntity tile, 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);
ItemStack 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,7 +3,6 @@ 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;
@ -17,9 +16,7 @@ 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;
@ -92,8 +89,8 @@ public abstract class ModuleItem extends Item implements IModule {
}
@Override
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());
public ItemStack craft(ItemStack module, PipeBlockEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) {
return stack;
}
@Override

View file

@ -19,37 +19,31 @@ 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("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;
})));
.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;
})));
}
}

View file

@ -1,98 +0,0 @@
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,7 +26,6 @@ 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;
@ -55,7 +54,6 @@ 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() {
@ -75,8 +73,6 @@ 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
@ -104,12 +100,12 @@ public class PipeNetwork extends SavedData implements GraphListener<BlockPos, Ne
@Override
public String toString() {
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());
return "PipeNetwork{" +
"\ngraph=" + this.graph +
",\nnodeToConnectedNodes=" + this.nodeToConnectedNodes +
",\ntileCache=" + this.tileCache.keySet() +
",\npipeItems=" + this.pipeItems +
",\nnetworkLocks=" + this.networkLocks + '}';
}
@Override
@ -124,7 +120,6 @@ 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;
}
@ -218,58 +213,21 @@ public class PipeNetwork extends SavedData implements GraphListener<BlockPos, Ne
return remain;
}
// check craftable items
return this.requestCraftedItem(destPipe, null, remain, new Stack<>(), equalityTypes).getLeft();
return this.requestCraftedItem(destPipe, null, remain, new Stack<>(), 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>();
public ItemStack requestCraftedItem(BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain, ItemEquality... equalityTypes) {
for (var craftable : this.getAllCraftables(destPipe)) {
if (!ItemEquality.compareItems(stack, craftable.getRight(), equalityTypes))
continue;
var pipe = this.getPipe(craftable.getLeft());
if (pipe == null)
continue;
var started = pipe.craft(destPipe, unavailableConsumer, stack, dependencyChain);
stack = started.getLeft();
crafts.addAll(started.getRight());
stack = pipe.craft(destPipe, unavailableConsumer, stack, dependencyChain);
if (stack.isEmpty())
break;
}
return Pair.of(stack, crafts);
return stack;
}
public ItemStack requestExistingItem(NetworkLocation location, BlockPos destPipe, BlockPos destInventory, NetworkLock ignoredLock, ItemStack stack, ItemEquality... equalityTypes) {
@ -325,17 +283,18 @@ 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.getActiveCrafts()) {
if (!includeCanceled && craft.canceled)
for (var craft : pipe.activeCrafts) {
var data = craft.getRight();
if (!includeCanceled && data.canceled)
continue;
// add up all the items that should go to the same location
var existing = items.stream()
.filter(s -> s.getLeft().equals(craft.resultDestPipe) && ItemEquality.compareItems(s.getRight(), craft.resultStackRemain, equalityTypes))
.filter(s -> s.getLeft().equals(data.resultDestPipe) && ItemEquality.compareItems(s.getRight(), data.resultStackRemain, equalityTypes))
.findFirst();
if (existing.isPresent()) {
existing.get().getRight().grow(craft.resultStackRemain.getCount());
existing.get().getRight().grow(data.resultStackRemain.getCount());
} else {
items.add(Pair.of(craft.resultDestPipe, craft.resultStackRemain.copy()));
items.add(Pair.of(data.resultDestPipe, data.resultStackRemain.copy()));
}
}
}
@ -459,10 +418,6 @@ 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<>();
@ -541,7 +496,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.remove(node);
this.nodeToConnectedNodes.keySet().remove(node);
// remove caches that contain the nodes as a destination
this.nodeToConnectedNodes.values().removeIf(cached -> nodes.stream().anyMatch(cached::contains));
this.endProfile();
@ -551,10 +506,6 @@ 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));
@ -593,8 +544,4 @@ 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(data.getFirst() == 1);
tile.cancelCrafting();
}),
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.getActiveCrafts()) {
for (var lock : craft.ingredientsToRequest)
for (var craft : pipe.activeCrafts) {
for (var lock : craft.getRight().ingredientsToRequest)
network.resolveNetworkLock(lock);
}
}

View file

@ -5,9 +5,10 @@ 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.ActiveCraft;
import de.ellpeck.prettypipes.network.NetworkLock;
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;
@ -41,6 +42,7 @@ 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;
@ -70,11 +72,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;
private List<IPipeItem> itemCache;
private List<ActiveCraft> activeCraftCache;
protected List<IPipeItem> items;
private int lastItemAmount;
private int priority;
private final Lazy<Integer> workRandomizer = Lazy.of(() -> this.level.random.nextInt(200));
@ -99,6 +101,14 @@ 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
@ -106,6 +116,12 @@ 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);
}
@ -131,15 +147,9 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC
}
public List<IPipeItem> getItems() {
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;
if (this.items == null)
this.items = PipeNetwork.get(this.level).getItemsInPipe(this.worldPosition);
return this.items;
}
public void addNewItem(IPipeItem item) {
@ -286,18 +296,15 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC
return total;
}
public Pair<ItemStack, Collection<ActiveCraft>> craft(BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) {
var crafts = new ArrayList<ActiveCraft>();
public ItemStack craft(BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) {
var modules = this.streamModules().iterator();
while (modules.hasNext()) {
var module = modules.next();
var started = module.getRight().craft(module.getLeft(), this, destPipe, unavailableConsumer, stack, dependencyChain);
stack = started.getLeft();
crafts.addAll(started.getRight());
stack = module.getRight().craft(module.getLeft(), this, destPipe, unavailableConsumer, stack, dependencyChain);
if (stack.isEmpty())
break;
}
return Pair.of(stack, crafts);
return stack;
}
public IItemHandler getItemHandler(Direction dir) {

View file

@ -7,25 +7,32 @@ 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;
@ -70,14 +77,15 @@ public class CraftingModuleItem extends ModuleItem {
return;
var slot = tile.getModuleSlot(module);
var network = PipeNetwork.get(tile.getLevel());
var crafts = tile.getActiveCrafts();
if (!crafts.isEmpty()) {
var craft = crafts.getFirst();
if (craft.moduleSlot == slot) {
if (!tile.activeCrafts.isEmpty()) {
var activeCraft = tile.activeCrafts.getFirst();
if (activeCraft.getLeft() == slot) {
var craftData = activeCraft.getRight();
// process crafting ingredient requests
if (!craft.ingredientsToRequest.isEmpty()) {
if (!craftData.ingredientsToRequest.isEmpty()) {
network.startProfile("crafting_ingredients");
var lock = craft.ingredientsToRequest.getFirst();
var lock = craftData.ingredientsToRequest.getFirst();
var equalityTypes = ItemFilter.getEqualityTypes(tile);
var dest = tile.getAvailableDestination(Direction.values(), lock.stack, true, true);
if (dest != null) {
@ -86,12 +94,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);
craft.ingredientsToRequest.remove(lock);
craft.inProgress = true;
craftData.ingredientsToRequest.remove(lock);
craftData.inProgress = true;
var traveling = lock.stack.copy();
traveling.shrink(requestRemain.getCount());
craft.travelingIngredients.add(traveling);
craftData.travelingIngredients.add(traveling);
// if we couldn't fit all items into the destination, create another request for the rest
var remain = lock.stack.copy();
@ -99,8 +107,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 : craft.ingredientsToRequest.size();
craft.ingredientsToRequest.add(index, remainRequest);
var index = ensureItemOrder ? 0 : craftData.ingredientsToRequest.size();
craftData.ingredientsToRequest.add(index, remainRequest);
network.createNetworkLock(remainRequest);
}
}
@ -109,19 +117,19 @@ public class CraftingModuleItem extends ModuleItem {
}
// pull requested crafting results from the network once they are stored
if (!craft.resultStackRemain.isEmpty()) {
if (!craftData.resultStackRemain.isEmpty()) {
network.startProfile("crafting_results");
var items = network.getOrderedNetworkItems(tile.getBlockPos());
var equalityTypes = ItemFilter.getEqualityTypes(tile);
var destPipe = network.getPipe(craft.resultDestPipe);
var destPipe = network.getPipe(craftData.resultDestPipe);
if (destPipe != null) {
var dest = destPipe.getAvailableDestinationOrConnectable(craft.resultStackRemain, true, true);
var dest = destPipe.getAvailableDestinationOrConnectable(craftData.resultStackRemain, true, true);
if (dest != null) {
for (var item : items) {
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);
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);
break;
}
}
@ -166,11 +174,11 @@ public class CraftingModuleItem extends ModuleItem {
}
@Override
public Pair<ItemStack, Collection<ActiveCraft>> craft(ItemStack module, PipeBlockEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<ItemStack> dependencyChain) {
public ItemStack 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 Pair.of(stack, List.of());
return stack;
var slot = tile.getModuleSlot(module);
var network = PipeNetwork.get(tile.getLevel());
@ -184,7 +192,6 @@ 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--) {
@ -195,37 +202,32 @@ public class CraftingModuleItem extends ModuleItem {
var copy = in.copy();
if (!contents.ensureItemOrder)
copy.setCount(in.getCount() * toCraft);
var ret = network.requestLocksAndStartCrafting(tile.getBlockPos(), items, unavailableConsumer, copy, CraftingModuleItem.addDependency(dependencyChain, module), equalityTypes);
var ret = ItemTerminalBlockEntity.requestItemLater(tile.getLevel(), 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(tile.getBlockPos(), slot, locks, destPipe, result);
tile.getActiveCrafts().add(activeCraft);
crafts.add(activeCraft);
var activeCraft = new ActiveCraft(locks, new ArrayList<>(), destPipe, result);
tile.activeCrafts.add(Pair.of(slot, activeCraft));
return Pair.of(remain, crafts);
return remain;
}
@Override
public ItemStack store(ItemStack module, PipeBlockEntity tile, ItemStack stack, Direction direction) {
var slot = tile.getModuleSlot(module);
var equalityTypes = ItemFilter.getEqualityTypes(tile);
var crafts = tile.getActiveCrafts();
var craft = crafts.stream()
.filter(c -> c.moduleSlot == slot && !c.getTravelingIngredient(stack, equalityTypes).isEmpty())
var matchingCraft = tile.activeCrafts.stream()
.filter(c -> c.getLeft() == slot && !c.getRight().getTravelingIngredient(stack, equalityTypes).isEmpty())
.findAny().orElse(null);
if (craft != null) {
craft.travelingIngredients.remove(craft.getTravelingIngredient(stack, equalityTypes));
if (matchingCraft != null) {
var data = matchingCraft.getRight();
data.travelingIngredients.remove(data.getTravelingIngredient(stack, equalityTypes));
if (module.get(Contents.TYPE).insertSingles) {
var handler = tile.getItemHandler(direction);
@ -240,8 +242,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 (craft.canceled && craft.travelingIngredients.size() <= 0 && craft.ingredientsToRequest.size() <= 0)
crafts.remove(craft);
if (data.canceled && data.travelingIngredients.size() <= 0 && data.ingredientsToRequest.size() <= 0)
tile.activeCrafts.remove(matchingCraft);
}
return stack;
}
@ -258,7 +260,6 @@ 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;
@ -276,4 +277,68 @@ 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,7 +5,10 @@ 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.*;
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.packets.PacketNetworkItems;
import de.ellpeck.prettypipes.pipe.ConnectionType;
import de.ellpeck.prettypipes.pipe.IPipeConnectable;
@ -133,7 +136,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(true).stream().sorted(Comparator.comparingInt(ItemStack::getCount).reversed()).collect(Collectors.toList());
var currentlyCrafting = this.getCurrentlyCrafting(false).stream().sorted(Comparator.comparingInt(ItemStack::getCount).reversed()).collect(Collectors.toList());
for (var player : playersToSync) {
if (!(player.containerMenu instanceof ItemTerminalContainer container))
continue;
@ -169,10 +172,9 @@ 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 network = PipeNetwork.get(this.level);
var ret = network.requestLocksAndStartCrafting(this.getConnectedPipe().getBlockPos(), locations, unavailableConsumer, stack, new Stack<>(), ItemEquality.NBT);
var ret = ItemTerminalBlockEntity.requestItemLater(this.level, this.getConnectedPipe().getBlockPos(), locations, unavailableConsumer, stack, new Stack<>(), ItemEquality.NBT);
this.existingRequests.addAll(ret.getLeft());
return stack.getCount() - ret.getMiddle().getCount();
return stack.getCount() - ret.getRight().getCount();
}
public Player[] getLookingPlayers() {
@ -207,7 +209,7 @@ public class ItemTerminalBlockEntity extends BlockEntity implements IPipeConnect
return crafting.stream().map(Pair::getRight).collect(Collectors.toList());
}
public void cancelCrafting(boolean force) {
public void cancelCrafting() {
var network = PipeNetwork.get(this.level);
var pipe = this.getConnectedPipe();
if (pipe == null)
@ -215,7 +217,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, force));
otherPipe.activeCrafts.removeIf(c -> c.getRight().markCanceledOrResolve(network));
}
var lookingPlayers = this.getLookingPlayers();
if (lookingPlayers.length > 0)
@ -267,6 +269,38 @@ 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(hasShiftDown() ? 1 : 0)));
PacketDistributor.sendToServer(new PacketButton(this.menu.tile.getBlockPos(), PacketButton.ButtonResult.CANCEL_CRAFTING, List.of()));
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\nHold Shift to include ongoing crafting operations,\nwhich may cause partial recipes to stay behind",
"info.prettypipes.cancel_all.desc": "Stops waiting for current crafting outputs\nOngoing crafting operations are still completed",
"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",