BlueGit

timfa containerpipes

Optimize crafter craft checks and cache recipe filters for 1.8.3.

Cut per-transfer crafter lookups, craft on insert with sleep probes, and keep slot locks/grid cache event-driven.

Co-authored-by: Cursor <cursoragent@cursor.com>

timfa Tim Falken · pushed by timfa

Authored 2026-08-20 · 2 months ago

Hash
25391a6367086e01f65be3a27045903e82456ad4
Parent
bd07c028
7 files changed +118 −43
README.md +1 −1
@@ -1,4 +1,4 @@
1 # ContainerPipes 1.8.2
1 # ContainerPipes 1.8.3
2 2
3 3 Paper plugin that connects containers with visible pipes and moves items (or cauldron liquids) between them automatically.
4 4
@@ -1,4 +1,4 @@
1# ContainerPipes 1.8.2
1# ContainerPipes 1.8.3
2
2
3Paper plugin that connects containers with visible pipes and moves items (or cauldron liquids) between them automatically.
3Paper plugin that connects containers with visible pipes and moves items (or cauldron liquids) between them automatically.
4
4
build.gradle.kts +1 −1
@@ -1,6 +1,6 @@
1 1 plugins { java }
2 2 group = "nl.timfalken"
3 version = "1.8.2"
3 version = "1.8.3"
4 4 repositories {
5 5 maven {
6 6 name = "papermc"
@@ -1,6 +1,6 @@
1plugins { java }
1plugins { java }
2group = "nl.timfalken"
2group = "nl.timfalken"
3version = "1.8.2"
3version = "1.8.3"
4repositories {
4repositories {
5 maven {
5 maven {
6 name = "papermc"
6 name = "papermc"
src/README-1.8.2.md src/README-1.8.3.md renamed +1 −1
@@ -1,4 +1,4 @@
1 # ContainerPipes 1.8.2
1 # ContainerPipes 1.8.3
2 2
3 3 Builds on 1.7.4 (dispenser bucket wake + sleeping-probe CME fix).
4 4
@@ -1,4 +1,4 @@
1# ContainerPipes 1.8.2
1# ContainerPipes 1.8.3
2
2
3Builds on 1.7.4 (dispenser bucket wake + sleeping-probe CME fix).
3Builds on 1.7.4 (dispenser bucket wake + sleeping-probe CME fix).
4
4
src/main/java/nl/timfalken/containerpipes/ContainerPipesPlugin.java +1 −1
@@ -65,7 +65,7 @@ public final class ContainerPipesPlugin extends JavaPlugin implements Listener,
65 65 loadConnections();
66 66 startTasks();
67 67 Bukkit.getScheduler().runTask(this, this::refreshLoadedDisplays);
68 getLogger().info("ContainerPipes 1.8.2 enabled with " + connections.size() + " connection(s).");
68 getLogger().info("ContainerPipes 1.8.3 enabled with " + connections.size() + " connection(s).");
69 69 AdvancedEndpointsPatch.enable(this);
70 70 CrafterPipeManager.enable(this);
71 71 }
@@ -65,7 +65,7 @@ public final class ContainerPipesPlugin extends JavaPlugin implements Listener,
65 loadConnections();
65 loadConnections();
66 startTasks();
66 startTasks();
67 Bukkit.getScheduler().runTask(this, this::refreshLoadedDisplays);
67 Bukkit.getScheduler().runTask(this, this::refreshLoadedDisplays);
68 getLogger().info("ContainerPipes 1.8.2 enabled with " + connections.size() + " connection(s).");
68 getLogger().info("ContainerPipes 1.8.3 enabled with " + connections.size() + " connection(s).");
69 AdvancedEndpointsPatch.enable(this);
69 AdvancedEndpointsPatch.enable(this);
70 CrafterPipeManager.enable(this);
70 CrafterPipeManager.enable(this);
71 }
71 }
src/main/java/nl/timfalken/containerpipes/CrafterPipeManager.java +100 −21
@@ -44,7 +44,6 @@ public final class CrafterPipeManager implements Listener {
44 44 private static boolean scheduled;
45 45 private static boolean needsRetry;
46 46 private static int orchestratorCount;
47 private static int idleAfter = 3;
48 47 private static int sleepProbe = 200;
49 48
50 49 record CrafterPos(UUID world, int x, int y, int z) {
@@ -74,6 +73,10 @@ public final class CrafterPipeManager implements Listener {
74 73 final List<PipeRef> incoming = new ArrayList<>();
75 74 final ItemStack[] recipe = new ItemStack[9];
76 75 int recipeMask;
76 EnumSet<Material> recipeMaterials = EnumSet.noneOf(Material.class);
77 ItemStack[] gridCache;
78 boolean gridDirty = true;
79 boolean craftCheckPending;
77 80 boolean sleeping;
78 81 int failed;
79 82 int sleepUntil;
@@ -85,6 +88,7 @@ public final class CrafterPipeManager implements Listener {
85 88 void setRecipe(List<ItemStack> filters) {
86 89 recipeMask = 0;
87 90 Arrays.fill(recipe, null);
91 recipeMaterials = EnumSet.noneOf(Material.class);
88 92 if (filters == null || filters.size() != 9) return;
89 93 for (int slot = 0; slot < 9; slot++) {
90 94 ItemStack item = filters.get(slot);
@@ -93,6 +97,7 @@ public final class CrafterPipeManager implements Listener {
93 97 one.setAmount(1);
94 98 recipe[slot] = one;
95 99 recipeMask |= 1 << slot;
100 recipeMaterials.add(one.getType());
96 101 }
97 102 }
98 103
@@ -100,6 +105,8 @@ public final class CrafterPipeManager implements Listener {
100 105 sleeping = false;
101 106 failed = 0;
102 107 sleepUntil = 0;
108 craftCheckPending = true;
109 gridDirty = true;
103 110 }
104 111 }
105 112
@@ -155,6 +162,12 @@ public final class CrafterPipeManager implements Listener {
155 162 for (PipeRef incoming : network.incoming) incomingToOrchestrated.add(incoming.id);
156 163 }
157 164 applySlotLocksNow(network);
165 if (network.orchestrator != null) {
166 network.gridDirty = true;
167 network.craftCheckPending = true;
168 network.sleeping = false;
169 network.sleepUntil = 0;
170 }
158 171 }
159 172 dirty = false;
160 173 } finally {
@@ -180,12 +193,8 @@ public final class CrafterPipeManager implements Listener {
180 193 /** Material filter for incoming pipes whose destination is an orchestrated crafter. */
181 194 public static EnumSet<Material> recipeMaterials(Block crafterBlock) {
182 195 Network network = networkAt(crafterBlock);
183 if (network == null || network.orchestrator == null) return null;
184 EnumSet<Material> materials = EnumSet.noneOf(Material.class);
185 for (ItemStack template : network.recipe) {
186 if (template != null) materials.add(template.getType());
187 }
188 return materials.isEmpty() ? null : materials;
196 if (network == null || network.orchestrator == null || network.recipeMaterials.isEmpty()) return null;
197 return network.recipeMaterials;
189 198 }
190 199
191 200 public static boolean insertRecipeItem(Inventory inventory, Block block, ItemStack source) {
@@ -200,6 +209,7 @@ public final class CrafterPipeManager implements Listener {
200 209 one.setAmount(1);
201 210 Crafter crafter = liveCrafter(block);
202 211 int maxStack = Math.min(one.getMaxStackSize(), inventory.getMaxStackSize());
212 ensureGridCache(network, inventory);
203 213
204 214 for (int slot = 0; slot < 9; slot++) {
205 215 ItemStack template = network.recipe[slot];
@@ -210,6 +220,8 @@ public final class CrafterPipeManager implements Listener {
210 220 ItemStack actual = inventory.getItem(slot);
211 221 if (validItem(actual)) continue;
212 222 inventory.setItem(slot, one.clone());
223 network.gridCache[slot] = one.clone();
224 afterIngredientChange(network, block);
213 225 return true;
214 226 }
215 227
@@ -233,9 +245,31 @@ public final class CrafterPipeManager implements Listener {
233 245 ItemStack merged = existing.clone();
234 246 merged.setAmount(existing.getAmount() + 1);
235 247 inventory.setItem(bestSlot, merged);
248 network.gridCache[bestSlot] = merged.clone();
249 afterIngredientChange(network, block);
236 250 return true;
237 251 }
238 252
253 /** After a pipe (or GUI) changes the crafter grid: craft immediately if complete. */
254 private static void afterIngredientChange(Network network, Block block) {
255 RunResult result = tryCraft(plugin, network);
256 int now = Bukkit.getCurrentTick();
257 if (result == RunResult.MOVED) {
258 network.sleeping = false;
259 network.failed = 0;
260 // Remaining stacks may still form a full recipe; drain on the next planner tick.
261 network.craftCheckPending = true;
262 } else if (result == RunResult.CRAFT_FAILED) {
263 network.craftCheckPending = false;
264 network.sleeping = true;
265 network.sleepUntil = now + sleepProbe;
266 } else {
267 network.craftCheckPending = false;
268 network.sleeping = true;
269 network.sleepUntil = now + sleepProbe;
270 }
271 }
272
239 273 private static void scheduleRebuild(Object owner) {
240 274 Plugin p = plugin != null ? plugin : owner instanceof Plugin pl ? pl : null;
241 275 if (p == null || scheduled) return;
@@ -327,21 +361,31 @@ public final class CrafterPipeManager implements Listener {
327 361 int now = Bukkit.getCurrentTick();
328 362 for (Network network : networks.values()) {
329 363 if (network.orchestrator == null) continue;
330 if (network.sleeping && now < network.sleepUntil) continue;
364 if (network.sleeping) {
365 if (now < network.sleepUntil) continue;
366 // Periodic sleep probe: player/hopper may have filled the grid without our insert path.
367 network.sleeping = false;
368 network.craftCheckPending = true;
369 network.gridDirty = true;
370 }
371 if (!network.craftCheckPending) continue;
372 network.craftCheckPending = false;
331 373 try {
332 374 RunResult result = tryCraft(owner, network);
333 375 if (result == RunResult.MOVED) {
334 376 network.sleeping = false;
335 377 network.failed = 0;
378 network.craftCheckPending = true;
336 379 } else if (result == RunResult.CRAFT_FAILED) {
337 380 network.sleeping = true;
338 381 network.sleepUntil = now + sleepProbe;
339 } else if (++network.failed >= idleAfter) {
382 } else {
340 383 network.sleeping = true;
341 384 network.sleepUntil = now + sleepProbe;
342 385 }
343 386 } catch (Throwable t) {
344 387 if (plugin != null) plugin.getLogger().warning("Crafter orchestration failed: " + t);
388 network.gridDirty = true;
345 389 network.sleeping = true;
346 390 network.sleepUntil = now + sleepProbe;
347 391 }
@@ -368,7 +412,7 @@ public final class CrafterPipeManager implements Listener {
368 412
369 413 // Paper skips ingredient consumption when the result is empty, so apply
370 414 // the vanilla remainder ourselves, then suppress the world/front drop.
371 consumeCraftGrid(block);
415 consumeCraftGrid(block, network);
372 416 event.setResult(new ItemStack(Material.AIR));
373 417 if (network.attemptingCraft) {
374 418 network.craftSucceeded = true;
@@ -387,7 +431,7 @@ public final class CrafterPipeManager implements Listener {
387 431 OptimizedTransferPlanner.invalidateBlock(plugin, block);
388 432 }
389 433
390 private static void consumeCraftGrid(Block block) {
434 private static void consumeCraftGrid(Block block, Network network) {
391 435 Inventory inventory = HopperStyleEndpoints.inventoryOf(block);
392 436 if (inventory == null) return;
393 437 ItemStack[] matrix = new ItemStack[9];
@@ -410,25 +454,44 @@ public final class CrafterPipeManager implements Listener {
410 454 ItemStack[] remaining = preview == null ? null : preview.getResultingMatrix();
411 455 if (remaining != null) {
412 456 int limit = Math.min(9, inventory.getSize());
457 ItemStack[] cache = new ItemStack[9];
413 458 for (int slot = 0; slot < limit; slot++) {
414 459 ItemStack item = slot < remaining.length ? remaining[slot] : null;
415 inventory.setItem(slot, validItem(item) ? item.clone() : null);
460 if (validItem(item)) {
461 ItemStack copy = item.clone();
462 inventory.setItem(slot, copy);
463 cache[slot] = copy.clone();
464 } else {
465 inventory.setItem(slot, null);
466 cache[slot] = null;
467 }
416 468 }
469 network.gridCache = cache;
470 network.gridDirty = false;
417 471 return;
418 472 }
419 473
420 474 int limit = Math.min(9, inventory.getSize());
475 ItemStack[] cache = new ItemStack[9];
421 476 for (int slot = 0; slot < limit; slot++) {
422 477 ItemStack have = inventory.getItem(slot);
423 if (!validItem(have)) continue;
478 if (!validItem(have)) {
479 cache[slot] = null;
480 continue;
481 }
424 482 ItemStack leftover = craftingRemainder(have);
425 if (have.getAmount() <= 1) inventory.setItem(slot, leftover);
426 else {
483 if (have.getAmount() <= 1) {
484 inventory.setItem(slot, leftover);
485 cache[slot] = leftover == null ? null : leftover.clone();
486 } else {
427 487 ItemStack reduced = have.clone();
428 488 reduced.setAmount(have.getAmount() - 1);
429 489 inventory.setItem(slot, reduced);
490 cache[slot] = reduced.clone();
430 491 }
431 492 }
493 network.gridCache = cache;
494 network.gridDirty = false;
432 495 }
433 496
434 497 private static ItemStack craftingRemainder(ItemStack item) {
@@ -461,7 +524,10 @@ public final class CrafterPipeManager implements Listener {
461 524 if (network == null || network.orchestrator == null) return;
462 525 Bukkit.getScheduler().runTask(plugin, () -> {
463 526 Network live = networkAt(block);
464 if (live != null && live.orchestrator != null) applySlotLocksNow(live);
527 if (live == null || live.orchestrator == null) return;
528 applySlotLocksNow(live);
529 live.gridDirty = true;
530 afterIngredientChange(live, block);
465 531 });
466 532 }
467 533
@@ -475,18 +541,23 @@ public final class CrafterPipeManager implements Listener {
475 541 if (block.getType() != Material.CRAFTER) return RunResult.NO_MOVE;
476 542 if (isBusy(block) || network.attemptingCraft) return RunResult.NO_MOVE;
477 543
478 applySlotLocksNow(network);
479
480 544 Inventory crafterInv = HopperStyleEndpoints.inventoryOf(block);
481 545 if (crafterInv == null) return RunResult.NO_MOVE;
482 546
483 ItemStack[] grid = readGrid(crafterInv);
547 ItemStack[] grid = ensureGridCache(network, crafterInv);
484 548 if (hasJunk(grid, network.recipe)) return RunResult.CRAFT_FAILED;
485 549 if (!gridMatchesRecipe(grid, network.recipe)) return RunResult.NO_MOVE;
486 550
487 551 return pulseCraft(network, block) ? RunResult.MOVED : RunResult.NO_MOVE;
488 552 }
489 553
554 private static ItemStack[] ensureGridCache(Network network, Inventory inventory) {
555 if (!network.gridDirty && network.gridCache != null) return network.gridCache;
556 network.gridCache = readGrid(inventory);
557 network.gridDirty = false;
558 return network.gridCache;
559 }
560
490 561 private static ItemStack[] readGrid(Inventory inventory) {
491 562 ItemStack[] storage = inventory.getStorageContents();
492 563 ItemStack[] grid = new ItemStack[9];
@@ -600,11 +671,20 @@ public final class CrafterPipeManager implements Listener {
600 671 }
601 672
602 673 private static boolean insertIntoCrafter(Inventory inventory, ItemStack source) {
674 Block block = null;
675 try {
676 var location = inventory.getLocation();
677 if (location != null && location.getWorld() != null) block = location.getBlock();
678 } catch (Throwable ignored) {}
679 boolean orchestrated = block != null && isOrchestrated(block);
603 680 int amount = source.getAmount();
604 681 for (int i = 0; i < amount; i++) {
605 682 ItemStack one = source.clone();
606 683 one.setAmount(1);
607 if (!AdvancedEndpointsPatch.insertOneCrafter(inventory, one)) return false;
684 boolean ok = orchestrated
685 ? insertRecipeItem(inventory, block, one)
686 : AdvancedEndpointsPatch.insertOneCrafter(inventory, one);
687 if (!ok) return false;
608 688 }
609 689 return amount > 0;
610 690 }
@@ -775,7 +855,6 @@ public final class CrafterPipeManager implements Listener {
775 855 private static void reloadPerf(Object owner) {
776 856 Plugin configOwner = owner instanceof Plugin p ? p : plugin;
777 857 if (configOwner == null) return;
778 idleAfter = Math.max(1, configOwner.getConfig().getInt("performance.idle-after-failed-attempts", 3));
779 858 sleepProbe = Math.max(1, configOwner.getConfig().getInt("performance.sleeping-probe-interval-ticks", 200));
780 859 }
781 860
@@ -44,7 +44,6 @@ public final class CrafterPipeManager implements Listener {
44 private static boolean scheduled;
44 private static boolean scheduled;
45 private static boolean needsRetry;
45 private static boolean needsRetry;
46 private static int orchestratorCount;
46 private static int orchestratorCount;
47 private static int idleAfter = 3;
48 private static int sleepProbe = 200;
47 private static int sleepProbe = 200;
49
48
50 record CrafterPos(UUID world, int x, int y, int z) {
49 record CrafterPos(UUID world, int x, int y, int z) {
@@ -74,6 +73,10 @@ public final class CrafterPipeManager implements Listener {
74 final List<PipeRef> incoming = new ArrayList<>();
73 final List<PipeRef> incoming = new ArrayList<>();
75 final ItemStack[] recipe = new ItemStack[9];
74 final ItemStack[] recipe = new ItemStack[9];
76 int recipeMask;
75 int recipeMask;
76 EnumSet<Material> recipeMaterials = EnumSet.noneOf(Material.class);
77 ItemStack[] gridCache;
78 boolean gridDirty = true;
79 boolean craftCheckPending;
77 boolean sleeping;
80 boolean sleeping;
78 int failed;
81 int failed;
79 int sleepUntil;
82 int sleepUntil;
@@ -85,6 +88,7 @@ public final class CrafterPipeManager implements Listener {
85 void setRecipe(List<ItemStack> filters) {
88 void setRecipe(List<ItemStack> filters) {
86 recipeMask = 0;
89 recipeMask = 0;
87 Arrays.fill(recipe, null);
90 Arrays.fill(recipe, null);
91 recipeMaterials = EnumSet.noneOf(Material.class);
88 if (filters == null || filters.size() != 9) return;
92 if (filters == null || filters.size() != 9) return;
89 for (int slot = 0; slot < 9; slot++) {
93 for (int slot = 0; slot < 9; slot++) {
90 ItemStack item = filters.get(slot);
94 ItemStack item = filters.get(slot);
@@ -93,6 +97,7 @@ public final class CrafterPipeManager implements Listener {
93 one.setAmount(1);
97 one.setAmount(1);
94 recipe[slot] = one;
98 recipe[slot] = one;
95 recipeMask |= 1 << slot;
99 recipeMask |= 1 << slot;
100 recipeMaterials.add(one.getType());
96 }
101 }
97 }
102 }
98
103
@@ -100,6 +105,8 @@ public final class CrafterPipeManager implements Listener {
100 sleeping = false;
105 sleeping = false;
101 failed = 0;
106 failed = 0;
102 sleepUntil = 0;
107 sleepUntil = 0;
108 craftCheckPending = true;
109 gridDirty = true;
103 }
110 }
104 }
111 }
105
112
@@ -155,6 +162,12 @@ public final class CrafterPipeManager implements Listener {
155 for (PipeRef incoming : network.incoming) incomingToOrchestrated.add(incoming.id);
162 for (PipeRef incoming : network.incoming) incomingToOrchestrated.add(incoming.id);
156 }
163 }
157 applySlotLocksNow(network);
164 applySlotLocksNow(network);
165 if (network.orchestrator != null) {
166 network.gridDirty = true;
167 network.craftCheckPending = true;
168 network.sleeping = false;
169 network.sleepUntil = 0;
170 }
158 }
171 }
159 dirty = false;
172 dirty = false;
160 } finally {
173 } finally {
@@ -180,12 +193,8 @@ public final class CrafterPipeManager implements Listener {
180 /** Material filter for incoming pipes whose destination is an orchestrated crafter. */
193 /** Material filter for incoming pipes whose destination is an orchestrated crafter. */
181 public static EnumSet<Material> recipeMaterials(Block crafterBlock) {
194 public static EnumSet<Material> recipeMaterials(Block crafterBlock) {
182 Network network = networkAt(crafterBlock);
195 Network network = networkAt(crafterBlock);
183 if (network == null || network.orchestrator == null) return null;
196 if (network == null || network.orchestrator == null || network.recipeMaterials.isEmpty()) return null;
184 EnumSet<Material> materials = EnumSet.noneOf(Material.class);
197 return network.recipeMaterials;
185 for (ItemStack template : network.recipe) {
186 if (template != null) materials.add(template.getType());
187 }
188 return materials.isEmpty() ? null : materials;
189 }
198 }
190
199
191 public static boolean insertRecipeItem(Inventory inventory, Block block, ItemStack source) {
200 public static boolean insertRecipeItem(Inventory inventory, Block block, ItemStack source) {
@@ -200,6 +209,7 @@ public final class CrafterPipeManager implements Listener {
200 one.setAmount(1);
209 one.setAmount(1);
201 Crafter crafter = liveCrafter(block);
210 Crafter crafter = liveCrafter(block);
202 int maxStack = Math.min(one.getMaxStackSize(), inventory.getMaxStackSize());
211 int maxStack = Math.min(one.getMaxStackSize(), inventory.getMaxStackSize());
212 ensureGridCache(network, inventory);
203
213
204 for (int slot = 0; slot < 9; slot++) {
214 for (int slot = 0; slot < 9; slot++) {
205 ItemStack template = network.recipe[slot];
215 ItemStack template = network.recipe[slot];
@@ -210,6 +220,8 @@ public final class CrafterPipeManager implements Listener {
210 ItemStack actual = inventory.getItem(slot);
220 ItemStack actual = inventory.getItem(slot);
211 if (validItem(actual)) continue;
221 if (validItem(actual)) continue;
212 inventory.setItem(slot, one.clone());
222 inventory.setItem(slot, one.clone());
223 network.gridCache[slot] = one.clone();
224 afterIngredientChange(network, block);
213 return true;
225 return true;
214 }
226 }
215
227
@@ -233,9 +245,31 @@ public final class CrafterPipeManager implements Listener {
233 ItemStack merged = existing.clone();
245 ItemStack merged = existing.clone();
234 merged.setAmount(existing.getAmount() + 1);
246 merged.setAmount(existing.getAmount() + 1);
235 inventory.setItem(bestSlot, merged);
247 inventory.setItem(bestSlot, merged);
248 network.gridCache[bestSlot] = merged.clone();
249 afterIngredientChange(network, block);
236 return true;
250 return true;
237 }
251 }
238
252
253 /** After a pipe (or GUI) changes the crafter grid: craft immediately if complete. */
254 private static void afterIngredientChange(Network network, Block block) {
255 RunResult result = tryCraft(plugin, network);
256 int now = Bukkit.getCurrentTick();
257 if (result == RunResult.MOVED) {
258 network.sleeping = false;
259 network.failed = 0;
260 // Remaining stacks may still form a full recipe; drain on the next planner tick.
261 network.craftCheckPending = true;
262 } else if (result == RunResult.CRAFT_FAILED) {
263 network.craftCheckPending = false;
264 network.sleeping = true;
265 network.sleepUntil = now + sleepProbe;
266 } else {
267 network.craftCheckPending = false;
268 network.sleeping = true;
269 network.sleepUntil = now + sleepProbe;
270 }
271 }
272
239 private static void scheduleRebuild(Object owner) {
273 private static void scheduleRebuild(Object owner) {
240 Plugin p = plugin != null ? plugin : owner instanceof Plugin pl ? pl : null;
274 Plugin p = plugin != null ? plugin : owner instanceof Plugin pl ? pl : null;
241 if (p == null || scheduled) return;
275 if (p == null || scheduled) return;
@@ -327,21 +361,31 @@ public final class CrafterPipeManager implements Listener {
327 int now = Bukkit.getCurrentTick();
361 int now = Bukkit.getCurrentTick();
328 for (Network network : networks.values()) {
362 for (Network network : networks.values()) {
329 if (network.orchestrator == null) continue;
363 if (network.orchestrator == null) continue;
330 if (network.sleeping && now < network.sleepUntil) continue;
364 if (network.sleeping) {
365 if (now < network.sleepUntil) continue;
366 // Periodic sleep probe: player/hopper may have filled the grid without our insert path.
367 network.sleeping = false;
368 network.craftCheckPending = true;
369 network.gridDirty = true;
370 }
371 if (!network.craftCheckPending) continue;
372 network.craftCheckPending = false;
331 try {
373 try {
332 RunResult result = tryCraft(owner, network);
374 RunResult result = tryCraft(owner, network);
333 if (result == RunResult.MOVED) {
375 if (result == RunResult.MOVED) {
334 network.sleeping = false;
376 network.sleeping = false;
335 network.failed = 0;
377 network.failed = 0;
378 network.craftCheckPending = true;
336 } else if (result == RunResult.CRAFT_FAILED) {
379 } else if (result == RunResult.CRAFT_FAILED) {
337 network.sleeping = true;
380 network.sleeping = true;
338 network.sleepUntil = now + sleepProbe;
381 network.sleepUntil = now + sleepProbe;
339 } else if (++network.failed >= idleAfter) {
382 } else {
340 network.sleeping = true;
383 network.sleeping = true;
341 network.sleepUntil = now + sleepProbe;
384 network.sleepUntil = now + sleepProbe;
342 }
385 }
343 } catch (Throwable t) {
386 } catch (Throwable t) {
344 if (plugin != null) plugin.getLogger().warning("Crafter orchestration failed: " + t);
387 if (plugin != null) plugin.getLogger().warning("Crafter orchestration failed: " + t);
388 network.gridDirty = true;
345 network.sleeping = true;
389 network.sleeping = true;
346 network.sleepUntil = now + sleepProbe;
390 network.sleepUntil = now + sleepProbe;
347 }
391 }
@@ -368,7 +412,7 @@ public final class CrafterPipeManager implements Listener {
368
412
369 // Paper skips ingredient consumption when the result is empty, so apply
413 // Paper skips ingredient consumption when the result is empty, so apply
370 // the vanilla remainder ourselves, then suppress the world/front drop.
414 // the vanilla remainder ourselves, then suppress the world/front drop.
371 consumeCraftGrid(block);
415 consumeCraftGrid(block, network);
372 event.setResult(new ItemStack(Material.AIR));
416 event.setResult(new ItemStack(Material.AIR));
373 if (network.attemptingCraft) {
417 if (network.attemptingCraft) {
374 network.craftSucceeded = true;
418 network.craftSucceeded = true;
@@ -387,7 +431,7 @@ public final class CrafterPipeManager implements Listener {
387 OptimizedTransferPlanner.invalidateBlock(plugin, block);
431 OptimizedTransferPlanner.invalidateBlock(plugin, block);
388 }
432 }
389
433
390 private static void consumeCraftGrid(Block block) {
434 private static void consumeCraftGrid(Block block, Network network) {
391 Inventory inventory = HopperStyleEndpoints.inventoryOf(block);
435 Inventory inventory = HopperStyleEndpoints.inventoryOf(block);
392 if (inventory == null) return;
436 if (inventory == null) return;
393 ItemStack[] matrix = new ItemStack[9];
437 ItemStack[] matrix = new ItemStack[9];
@@ -410,25 +454,44 @@ public final class CrafterPipeManager implements Listener {
410 ItemStack[] remaining = preview == null ? null : preview.getResultingMatrix();
454 ItemStack[] remaining = preview == null ? null : preview.getResultingMatrix();
411 if (remaining != null) {
455 if (remaining != null) {
412 int limit = Math.min(9, inventory.getSize());
456 int limit = Math.min(9, inventory.getSize());
457 ItemStack[] cache = new ItemStack[9];
413 for (int slot = 0; slot < limit; slot++) {
458 for (int slot = 0; slot < limit; slot++) {
414 ItemStack item = slot < remaining.length ? remaining[slot] : null;
459 ItemStack item = slot < remaining.length ? remaining[slot] : null;
415 inventory.setItem(slot, validItem(item) ? item.clone() : null);
460 if (validItem(item)) {
461 ItemStack copy = item.clone();
462 inventory.setItem(slot, copy);
463 cache[slot] = copy.clone();
464 } else {
465 inventory.setItem(slot, null);
466 cache[slot] = null;
467 }
416 }
468 }
469 network.gridCache = cache;
470 network.gridDirty = false;
417 return;
471 return;
418 }
472 }
419
473
420 int limit = Math.min(9, inventory.getSize());
474 int limit = Math.min(9, inventory.getSize());
475 ItemStack[] cache = new ItemStack[9];
421 for (int slot = 0; slot < limit; slot++) {
476 for (int slot = 0; slot < limit; slot++) {
422 ItemStack have = inventory.getItem(slot);
477 ItemStack have = inventory.getItem(slot);
423 if (!validItem(have)) continue;
478 if (!validItem(have)) {
479 cache[slot] = null;
480 continue;
481 }
424 ItemStack leftover = craftingRemainder(have);
482 ItemStack leftover = craftingRemainder(have);
425 if (have.getAmount() <= 1) inventory.setItem(slot, leftover);
483 if (have.getAmount() <= 1) {
426 else {
484 inventory.setItem(slot, leftover);
485 cache[slot] = leftover == null ? null : leftover.clone();
486 } else {
427 ItemStack reduced = have.clone();
487 ItemStack reduced = have.clone();
428 reduced.setAmount(have.getAmount() - 1);
488 reduced.setAmount(have.getAmount() - 1);
429 inventory.setItem(slot, reduced);
489 inventory.setItem(slot, reduced);
490 cache[slot] = reduced.clone();
430 }
491 }
431 }
492 }
493 network.gridCache = cache;
494 network.gridDirty = false;
432 }
495 }
433
496
434 private static ItemStack craftingRemainder(ItemStack item) {
497 private static ItemStack craftingRemainder(ItemStack item) {
@@ -461,7 +524,10 @@ public final class CrafterPipeManager implements Listener {
461 if (network == null || network.orchestrator == null) return;
524 if (network == null || network.orchestrator == null) return;
462 Bukkit.getScheduler().runTask(plugin, () -> {
525 Bukkit.getScheduler().runTask(plugin, () -> {
463 Network live = networkAt(block);
526 Network live = networkAt(block);
464 if (live != null && live.orchestrator != null) applySlotLocksNow(live);
527 if (live == null || live.orchestrator == null) return;
528 applySlotLocksNow(live);
529 live.gridDirty = true;
530 afterIngredientChange(live, block);
465 });
531 });
466 }
532 }
467
533
@@ -475,18 +541,23 @@ public final class CrafterPipeManager implements Listener {
475 if (block.getType() != Material.CRAFTER) return RunResult.NO_MOVE;
541 if (block.getType() != Material.CRAFTER) return RunResult.NO_MOVE;
476 if (isBusy(block) || network.attemptingCraft) return RunResult.NO_MOVE;
542 if (isBusy(block) || network.attemptingCraft) return RunResult.NO_MOVE;
477
543
478 applySlotLocksNow(network);
479
480 Inventory crafterInv = HopperStyleEndpoints.inventoryOf(block);
544 Inventory crafterInv = HopperStyleEndpoints.inventoryOf(block);
481 if (crafterInv == null) return RunResult.NO_MOVE;
545 if (crafterInv == null) return RunResult.NO_MOVE;
482
546
483 ItemStack[] grid = readGrid(crafterInv);
547 ItemStack[] grid = ensureGridCache(network, crafterInv);
484 if (hasJunk(grid, network.recipe)) return RunResult.CRAFT_FAILED;
548 if (hasJunk(grid, network.recipe)) return RunResult.CRAFT_FAILED;
485 if (!gridMatchesRecipe(grid, network.recipe)) return RunResult.NO_MOVE;
549 if (!gridMatchesRecipe(grid, network.recipe)) return RunResult.NO_MOVE;
486
550
487 return pulseCraft(network, block) ? RunResult.MOVED : RunResult.NO_MOVE;
551 return pulseCraft(network, block) ? RunResult.MOVED : RunResult.NO_MOVE;
488 }
552 }
489
553
554 private static ItemStack[] ensureGridCache(Network network, Inventory inventory) {
555 if (!network.gridDirty && network.gridCache != null) return network.gridCache;
556 network.gridCache = readGrid(inventory);
557 network.gridDirty = false;
558 return network.gridCache;
559 }
560
490 private static ItemStack[] readGrid(Inventory inventory) {
561 private static ItemStack[] readGrid(Inventory inventory) {
491 ItemStack[] storage = inventory.getStorageContents();
562 ItemStack[] storage = inventory.getStorageContents();
492 ItemStack[] grid = new ItemStack[9];
563 ItemStack[] grid = new ItemStack[9];
@@ -600,11 +671,20 @@ public final class CrafterPipeManager implements Listener {
600 }
671 }
601
672
602 private static boolean insertIntoCrafter(Inventory inventory, ItemStack source) {
673 private static boolean insertIntoCrafter(Inventory inventory, ItemStack source) {
674 Block block = null;
675 try {
676 var location = inventory.getLocation();
677 if (location != null && location.getWorld() != null) block = location.getBlock();
678 } catch (Throwable ignored) {}
679 boolean orchestrated = block != null && isOrchestrated(block);
603 int amount = source.getAmount();
680 int amount = source.getAmount();
604 for (int i = 0; i < amount; i++) {
681 for (int i = 0; i < amount; i++) {
605 ItemStack one = source.clone();
682 ItemStack one = source.clone();
606 one.setAmount(1);
683 one.setAmount(1);
607 if (!AdvancedEndpointsPatch.insertOneCrafter(inventory, one)) return false;
684 boolean ok = orchestrated
685 ? insertRecipeItem(inventory, block, one)
686 : AdvancedEndpointsPatch.insertOneCrafter(inventory, one);
687 if (!ok) return false;
608 }
688 }
609 return amount > 0;
689 return amount > 0;
610 }
690 }
@@ -775,7 +855,6 @@ public final class CrafterPipeManager implements Listener {
775 private static void reloadPerf(Object owner) {
855 private static void reloadPerf(Object owner) {
776 Plugin configOwner = owner instanceof Plugin p ? p : plugin;
856 Plugin configOwner = owner instanceof Plugin p ? p : plugin;
777 if (configOwner == null) return;
857 if (configOwner == null) return;
778 idleAfter = Math.max(1, configOwner.getConfig().getInt("performance.idle-after-failed-attempts", 3));
779 sleepProbe = Math.max(1, configOwner.getConfig().getInt("performance.sleeping-probe-interval-ticks", 200));
858 sleepProbe = Math.max(1, configOwner.getConfig().getInt("performance.sleeping-probe-interval-ticks", 200));
780 }
859 }
781
860
src/main/java/nl/timfalken/containerpipes/OptimizedTransferPlanner.java +13 −17
@@ -507,15 +507,20 @@ public final class OptimizedTransferPlanner {
507 507 reserved.clear();
508 508 destinationKeys.clear();
509 509 for (ConnectionView connection : connections) {
510 if (CrafterPipeManager.isFilteredCrafterOutput(connection.id)) continue;
510 // Crafter output pipes never pull from the grid; results route via CrafterCraftEvent.
511 if (CrafterPipeManager.usesCraftingGrid(connection.id)) continue;
511 512 Block destinationBlock = blockAt(connection.destination);
512 513 EnumSet<Material> crafterRecipe = destinationBlock == null
513 514 ? null : CrafterPipeManager.recipeMaterials(destinationBlock);
514 515 if (crafterRecipe != null && !crafterRecipe.isEmpty()) {
516 connection.transferFilters = crafterRecipe;
515 517 filtered.add(connection);
516 518 reserved.addAll(crafterRecipe);
517 } else if (connection.filters.isEmpty()) unfiltered.add(connection);
518 else {
519 } else if (connection.filters.isEmpty()) {
520 connection.transferFilters = connection.filters;
521 unfiltered.add(connection);
522 } else {
523 connection.transferFilters = connection.filters;
519 524 filtered.add(connection);
520 525 reserved.addAll(connection.filters);
521 526 }
@@ -697,7 +702,7 @@ public final class OptimizedTransferPlanner {
697 702 for (ConnectionView connection : filtered) {
698 703 if (stack == null || stack.getAmount() <= 0 || cycleBudget[slot] <= 0) break;
699 704 if (usedConnections.containsKey(connection.raw)
700 || !transferFilters(connection).contains(type)) continue;
705 || !connection.transferFilters.contains(type)) continue;
701 706 TransferResult result = transferCached(connection, stack, cycleDestinations, key);
702 707 if (!result.moved) continue;
703 708 usedConnections.put(connection.raw, Boolean.TRUE);
@@ -817,9 +822,6 @@ public final class OptimizedTransferPlanner {
817 822 ignored -> new DestinationState(destination));
818 823 if (!state.insertOne(source)) return TransferResult.NO_MOVE;
819 824 syncIncomingCache(destinationKey, state);
820 if (destination.getType() == InventoryType.CRAFTER) {
821 CrafterPipeManager.invalidateBlock(destinationBlock);
822 }
823 825 afterTransfer(connection, inventoryAt(connection.source), destination);
824 826 return new TransferResult(true, destinationKey);
825 827 }
@@ -968,19 +970,10 @@ public final class OptimizedTransferPlanner {
968 970
969 971 boolean allowed(ConnectionView connection, ItemStack item, boolean filteredPath, Set<Material> reserved) {
970 972 if (item == null || item.getType().isAir() || item.getAmount() <= 0) return false;
971 if (filteredPath) return transferFilters(connection).contains(item.getType());
973 if (filteredPath) return connection.transferFilters.contains(item.getType());
972 974 return !reserved.contains(item.getType());
973 975 }
974 976
975 /** Crafter-bound pipes use the orchestrator recipe, not the connection's own filter list. */
976 EnumSet<Material> transferFilters(ConnectionView connection) {
977 Block destinationBlock = blockAt(connection.destination);
978 EnumSet<Material> crafterRecipe = destinationBlock == null
979 ? null : CrafterPipeManager.recipeMaterials(destinationBlock);
980 if (crafterRecipe != null && !crafterRecipe.isEmpty()) return crafterRecipe;
981 return connection.filters;
982 }
983
984 977 void afterTransfer(ConnectionView connection, Inventory source, Inventory destination) {
985 978 ContainerKey sourceKey = source == null ? null : inventoryKey(source);
986 979 ContainerKey destinationKey = destination == null ? null : inventoryKey(destination);
@@ -1087,6 +1080,8 @@ public final class OptimizedTransferPlanner {
1087 1080 final EndpointView source;
1088 1081 final EndpointView destination;
1089 1082 final EnumSet<Material> filters;
1083 /** Resolved once per topology rebuild: pipe filter or orchestrated crafter recipe. */
1084 EnumSet<Material> transferFilters;
1090 1085
1091 1086 ConnectionView(Object raw, long id, EndpointView source, EndpointView destination, EnumSet<Material> filters) {
1092 1087 this.raw = raw;
@@ -1094,6 +1089,7 @@ public final class OptimizedTransferPlanner {
1094 1089 this.source = source;
1095 1090 this.destination = destination;
1096 1091 this.filters = filters;
1092 this.transferFilters = filters;
1097 1093 }
1098 1094
1099 1095 static ConnectionView read(Object raw) throws Exception {
@@ -507,15 +507,20 @@ public final class OptimizedTransferPlanner {
507 reserved.clear();
507 reserved.clear();
508 destinationKeys.clear();
508 destinationKeys.clear();
509 for (ConnectionView connection : connections) {
509 for (ConnectionView connection : connections) {
510 if (CrafterPipeManager.isFilteredCrafterOutput(connection.id)) continue;
510 // Crafter output pipes never pull from the grid; results route via CrafterCraftEvent.
511 if (CrafterPipeManager.usesCraftingGrid(connection.id)) continue;
511 Block destinationBlock = blockAt(connection.destination);
512 Block destinationBlock = blockAt(connection.destination);
512 EnumSet<Material> crafterRecipe = destinationBlock == null
513 EnumSet<Material> crafterRecipe = destinationBlock == null
513 ? null : CrafterPipeManager.recipeMaterials(destinationBlock);
514 ? null : CrafterPipeManager.recipeMaterials(destinationBlock);
514 if (crafterRecipe != null && !crafterRecipe.isEmpty()) {
515 if (crafterRecipe != null && !crafterRecipe.isEmpty()) {
516 connection.transferFilters = crafterRecipe;
515 filtered.add(connection);
517 filtered.add(connection);
516 reserved.addAll(crafterRecipe);
518 reserved.addAll(crafterRecipe);
517 } else if (connection.filters.isEmpty()) unfiltered.add(connection);
519 } else if (connection.filters.isEmpty()) {
518 else {
520 connection.transferFilters = connection.filters;
521 unfiltered.add(connection);
522 } else {
523 connection.transferFilters = connection.filters;
519 filtered.add(connection);
524 filtered.add(connection);
520 reserved.addAll(connection.filters);
525 reserved.addAll(connection.filters);
521 }
526 }
@@ -697,7 +702,7 @@ public final class OptimizedTransferPlanner {
697 for (ConnectionView connection : filtered) {
702 for (ConnectionView connection : filtered) {
698 if (stack == null || stack.getAmount() <= 0 || cycleBudget[slot] <= 0) break;
703 if (stack == null || stack.getAmount() <= 0 || cycleBudget[slot] <= 0) break;
699 if (usedConnections.containsKey(connection.raw)
704 if (usedConnections.containsKey(connection.raw)
700 || !transferFilters(connection).contains(type)) continue;
705 || !connection.transferFilters.contains(type)) continue;
701 TransferResult result = transferCached(connection, stack, cycleDestinations, key);
706 TransferResult result = transferCached(connection, stack, cycleDestinations, key);
702 if (!result.moved) continue;
707 if (!result.moved) continue;
703 usedConnections.put(connection.raw, Boolean.TRUE);
708 usedConnections.put(connection.raw, Boolean.TRUE);
@@ -817,9 +822,6 @@ public final class OptimizedTransferPlanner {
817 ignored -> new DestinationState(destination));
822 ignored -> new DestinationState(destination));
818 if (!state.insertOne(source)) return TransferResult.NO_MOVE;
823 if (!state.insertOne(source)) return TransferResult.NO_MOVE;
819 syncIncomingCache(destinationKey, state);
824 syncIncomingCache(destinationKey, state);
820 if (destination.getType() == InventoryType.CRAFTER) {
821 CrafterPipeManager.invalidateBlock(destinationBlock);
822 }
823 afterTransfer(connection, inventoryAt(connection.source), destination);
825 afterTransfer(connection, inventoryAt(connection.source), destination);
824 return new TransferResult(true, destinationKey);
826 return new TransferResult(true, destinationKey);
825 }
827 }
@@ -968,19 +970,10 @@ public final class OptimizedTransferPlanner {
968
970
969 boolean allowed(ConnectionView connection, ItemStack item, boolean filteredPath, Set<Material> reserved) {
971 boolean allowed(ConnectionView connection, ItemStack item, boolean filteredPath, Set<Material> reserved) {
970 if (item == null || item.getType().isAir() || item.getAmount() <= 0) return false;
972 if (item == null || item.getType().isAir() || item.getAmount() <= 0) return false;
971 if (filteredPath) return transferFilters(connection).contains(item.getType());
973 if (filteredPath) return connection.transferFilters.contains(item.getType());
972 return !reserved.contains(item.getType());
974 return !reserved.contains(item.getType());
973 }
975 }
974
976
975 /** Crafter-bound pipes use the orchestrator recipe, not the connection's own filter list. */
976 EnumSet<Material> transferFilters(ConnectionView connection) {
977 Block destinationBlock = blockAt(connection.destination);
978 EnumSet<Material> crafterRecipe = destinationBlock == null
979 ? null : CrafterPipeManager.recipeMaterials(destinationBlock);
980 if (crafterRecipe != null && !crafterRecipe.isEmpty()) return crafterRecipe;
981 return connection.filters;
982 }
983
984 void afterTransfer(ConnectionView connection, Inventory source, Inventory destination) {
977 void afterTransfer(ConnectionView connection, Inventory source, Inventory destination) {
985 ContainerKey sourceKey = source == null ? null : inventoryKey(source);
978 ContainerKey sourceKey = source == null ? null : inventoryKey(source);
986 ContainerKey destinationKey = destination == null ? null : inventoryKey(destination);
979 ContainerKey destinationKey = destination == null ? null : inventoryKey(destination);
@@ -1087,6 +1080,8 @@ public final class OptimizedTransferPlanner {
1087 final EndpointView source;
1080 final EndpointView source;
1088 final EndpointView destination;
1081 final EndpointView destination;
1089 final EnumSet<Material> filters;
1082 final EnumSet<Material> filters;
1083 /** Resolved once per topology rebuild: pipe filter or orchestrated crafter recipe. */
1084 EnumSet<Material> transferFilters;
1090
1085
1091 ConnectionView(Object raw, long id, EndpointView source, EndpointView destination, EnumSet<Material> filters) {
1086 ConnectionView(Object raw, long id, EndpointView source, EndpointView destination, EnumSet<Material> filters) {
1092 this.raw = raw;
1087 this.raw = raw;
@@ -1094,6 +1089,7 @@ public final class OptimizedTransferPlanner {
1094 this.source = source;
1089 this.source = source;
1095 this.destination = destination;
1090 this.destination = destination;
1096 this.filters = filters;
1091 this.filters = filters;
1092 this.transferFilters = filters;
1097 }
1093 }
1098
1094
1099 static ConnectionView read(Object raw) throws Exception {
1095 static ConnectionView read(Object raw) throws Exception {
src/main/resources/plugin.yml +1 −1
@@ -1,5 +1,5 @@
1 1 name: ContainerPipes
2 version: '1.8.2'
2 version: '1.8.3'
3 3 main: nl.timfalken.containerpipes.ContainerPipesPlugin
4 4 api-version: '26.2'
5 5 author: Tim Falken
@@ -1,5 +1,5 @@
1name: ContainerPipes
1name: ContainerPipes
2version: '1.8.2'
2version: '1.8.3'
3main: nl.timfalken.containerpipes.ContainerPipesPlugin
3main: nl.timfalken.containerpipes.ContainerPipesPlugin
4api-version: '26.2'
4api-version: '26.2'
5author: Tim Falken
5author: Tim Falken