Fix hopper task termination after single execution and refactor for Folia thread-safety#92
Conversation
- Fix bug where hopper tasks would stop after one execution by ensuring proper task restart in startHopperTask - Wrap restartHopperForSpawner in location-based scheduler for Folia thread-safety - Add atomic task replacement: stopHopperTask before starting new one to prevent race conditions - Add comprehensive null checks throughout all hopper methods - Improve error handling with try-catch blocks and detailed logging - Optimize cleanup method to safely cancel all tasks - Add validation checks in processChunkHoppers and onChunkUnload - Ensure thread-safe access to activeHoppers ConcurrentHashMap Co-authored-by: ptthanh02 <73684260+ptthanh02@users.noreply.github.com>
- Add cleanup of old HopperHandler before creating new one in setUpHopperHandler - Prevents resource leaks and duplicate tasks when config is reloaded - Ensures all old hopper tasks are properly cancelled before restart Co-authored-by: ptthanh02 <73684260+ptthanh02@users.noreply.github.com>
- Rename tileEntities to hopperStates for better clarity - Inline checkDelay variable for better readability Co-authored-by: ptthanh02 <73684260+ptthanh02@users.noreply.github.com>
|
@copilot > Task :api:javadocJar
|
…ities - Change BlockState[] to Collection<BlockState> for getTileEntities return type - Replace .length check with .isEmpty() for Collection - Fixes incompatible types compilation error in processChunkHoppers and onChunkUnload Co-authored-by: ptthanh02 <73684260+ptthanh02@users.noreply.github.com>
Fixed in b64fc4e. Changed Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot everything seem to be working, can you optimize HopperHandler.transferItems() further for performances |
Performance improvements: - Cache hopper inventory and contents outside loops to avoid repeated getContents() calls - Pre-allocate ArrayList with expected size to reduce resizing - Use itemTransferred flag to exit inner loop early instead of break - Cache currentAmount and maxStackSize to reduce method calls - Separate isSimilar check from amount check for better readability - Update hopperContents array after modification to keep cache synchronized These changes reduce unnecessary object allocations and method calls in the hot path, improving performance especially when processing multiple items per transfer cycle. Co-authored-by: ptthanh02 <73684260+ptthanh02@users.noreply.github.com>
Optimized in 3734aa0. Key improvements:
These changes significantly reduce object allocations and method calls in the transfer hot path. |
|
@copilot But make sure Moved hopper.getInventory().getContents() outside the loop to avoid creating new arrays for every spawner item not exploit able since player can still interact with spawner while transfering items |
Moved getContents() call inside the outer loop (per spawner item) instead of caching it for the entire transfer operation. This prevents exploits where players could interact with the hopper during transfers, as we now get fresh inventory state for each item being transferred. Balance between performance and security: - Still cache hopperInv reference to avoid repeated getInventory() calls - Get fresh contents for each spawner item (not each hopper slot) - Removed stale cache update line that's no longer needed Addresses security concern while maintaining most performance improvements. Co-authored-by: ptthanh02 <73684260+ptthanh02@users.noreply.github.com>
Fixed in 01980fe. Moved |
Fix hopper functionality and refactor for better performance and Folia thread-safety
Summary of Changes
Root Cause
The bug occurred because
startHopperTaskwould return early if a hopper location was already in the activeHoppers map, preventing task replacement whenrestartHopperForSpawnerwas called.Solution
startHopperTaskto atomically replace tasks by callingstopHopperTaskfirstrestartHopperForSpawnerin location-based scheduler for Folia thread-safetysetUpHopperHandlerto prevent resource leaks on reloadtransferItems()method with balanced performance and security approachFiles Modified
HopperHandler.java: Core hopper functionality fixes and performance optimizationsSmartSpawner.java: Cleanup on reloadKey Improvements
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.