STOCK MARKET TWEAKS (NeoForge 1.21.1)
Summary:
Removes StockMarket's world-load stall, speeds up its UI and background ticks in large modpacks, and adds Shift + Right-Click JEI market creation — without changing trading behaviour.
======================================================================
ABOUT
======================================================================
Stock Market Tweaks is a lightweight companion mod for KROIA's StockMarket and BankSystem (NeoForge 1.21.1). It fixes the multi-minute "Loading world" freeze caused by StockMarket's market-preset setup in large modpacks, smooths out recurring server-tick spikes as your economy grows, eliminates search/sort freezes in StockMarket's management UI, and lets admins create markets directly from JEI.
No game rules, prices, or trading mechanics are altered. Nothing in the original jars is patched or overwritten. Every tweak falls back cleanly to StockMarket's original code if a target changes, and every feature can be toggled individually in config/stockmarkettweaks.json.
======================================================================
WHY IT HELPS
======================================================================
In large modpacks, StockMarket pre-registers every market preset on world load by asking BankSystem to resolve each preset against every registered item in the pack. BankSystem answers with a linear scan, and for component-bearing items (enchanted books, potions, tipped arrows, modded NBT/component items) it falls back to a second full scan that NBT-serialises every registered stack. With hundreds of presets and tens of thousands of items, that turns into minutes of server-thread stall.
Stock Market Tweaks replaces that lookup with an O(1) identity index and builds/registers presets in parallel.
======================================================================
FEATURES
======================================================================
World Load & Startup:
- O(1) ItemID identity index over BankSystem's registry (plain items keyed by Item, component items keyed with the same NBT encoding BankSystem's fallback uses), eliminating O(M^2) scans during world load and initial item registration.
- Parallel preset ItemStack construction with a single batched registration call (preserving exact registration order and assigned ItemIDs).
- Startup phase timing logs (DataManager.load, preset pre-registration, total StockMarket setup) plus an in-game /stockmarkettweaks command (OP level 2) showing live index statistics.
Server Runtime Performance:
- Caches ServerMarketManager's available market-ID list (rebuilt only when markets are created, deleted, or loaded).
- Returns a shared empty list for markets without resting limit orders instead of allocating a new list on every read.
- Spreads TargetPriceBot's 5-tick PID step across ticks so all markets no longer spike on the same tick.
- Spreads StockMarket 2.2.0's 1-second virtual-depth memory fade step across ticks (automatic no-op on 2.1.x).
- Skips StockMarket 2.2.0's 2-second placeholder refresh when neither TAB nor PlaceholderAPI is installed (configurable: auto, always, never).
- Makes the per-viewer market price stream poll interval configurable (default 200 ms instead of 100 ms; set 0 for original behaviour).
- Adds one-time, additive SQLite indexes on OrderHistory (user/account + time) and MarketPrice (time) for faster history and 24-hour stats queries.
Client UI & Quality of Life:
- Create Markets Directly from JEI (Optional): Admins can Shift + Right-Click any hovered item in JEI's ingredient list or bookmarks to create a StockMarket immediately without opening StockMarket's search picker. Keeps normal Right-Click ("Show Uses") intact, shows a Shift-hover tooltip status (Create StockMarket / Market already exists), prints status messages to chat so you can create multiple markets in a row, and respects all server-side admin/permission checks. Zero dependency on JEI (silently skipped if JEI is not installed).
- Eliminates the item picker sort freeze when opening Create Market, Management, or Market Selector screens by caching each item's tag sort key once.
- Replaces the item picker search filter with a cached filter so typing no longer rebuilds full tooltips for every item in the pack on every keystroke.
- Debounces preset and picker search rebuilds by 150 ms while typing, while keeping non-search actions (category switches, Select All/Deselect All, preset edits) instant.
- Caches tooltip-based search display text while StockMarket screens are open (auto-clearing on language or world/server changes) and reuses Create Markets' "already exists" set while the market list is unchanged.
======================================================================
REQUIREMENTS
======================================================================
- Minecraft 1.21.1
- NeoForge 21.1.x
- StockMarket 1.21.1-2.0.0+ (verified against both 2.1.1 and 2.2.0)
- BankSystem 1.21.1-2.0.0+ (includes embedded ModUtilities)
- Architectury API (required by StockMarket)
- JEI (optional - enables Shift + Right-Click market creation)
Works on existing worlds with no migration required. Can be installed on both server and client (server-side optimisations work on dedicated servers; UI and JEI features apply on clients).
======================================================================
CONFIGURATION
======================================================================
Generated automatically at config/stockmarkettweaks.json on first launch (and automatically updated with new defaults on upgrades):
- enableFastItemIdIndex (default: true) - Master switch for the O(1) ItemID index.
- safeComponentScanOnMiss (default: true) - Runs BankSystem's cheap component scan on an index miss as a safety net for exotic components.
- enableParallelPresetRegistration (default: true) - Parallel preset stack build + single bulk registration.
- presetRegistrationThreads (default: 0) - Worker thread count (0 = auto, half of CPU cores, clamped 2-8).
- logStartupTimings (default: true) - Logs phase timings and ItemID index stats on startup.
- maxIndexedItemIds (default: 200000) - Memory guard cap before falling back to linear lookup.
- cacheMarketIdList (default: true) - Caches ServerMarketManager's available market-ID snapshot.
- emptyOrderBookFastPath (default: true) - Returns a shared empty list when an orderbook has no limit orders.
- placeholderMode (default: "auto") - "auto" skips refresh without TAB/PlaceholderAPI; "always" keeps StockMarket default; "never" disables it.
- staggerTargetPriceBot (default: true) - Spreads TargetPriceBot's 5-tick cadence across ticks.
- staggerDepthFade (default: true) - Spreads 2.2.0 virtual-depth memory fade across the 1-second cycle.
- priceStreamIntervalMs (default: 200) - Per-viewer price stream poll interval in ms (0 = StockMarket's 100 ms; minimum 50).
- searchDebounceMs (default: 150) - Search typing debounce delay in ms (0 = rebuild on every character).
- cacheSearchDisplayText (default: true) - Caches tooltip search text while StockMarket screens are open.
- fastItemPickerSearch (default: true) - Uses cached search filter and cached tag sorter in the item picker.
- cacheExistingMarketsMs (default: 2000) - Reuses Create Markets' "already exists" set (invalidates immediately when markets change).
- jeiShiftRightClickCreateMarket (default: true) - Enables Shift + Right-Click in JEI to create a market.
- createHistoryIndexes (default: true) - Adds covering SQLite indexes for OrderHistory and MarketPrice queries.
========================================================================
COMMANDS
========================================================================
/stockmarkettweaks (requires permission level 2)
Prints live ItemID index statistics (entry counts, hit rate, rebuilds, invalidations, scan fallbacks).
======================================================================
NOTES FOR SERVER ADMINS
======================================================================
- Safe to add or remove on existing worlds at any time.
- Recommended for any modpack with more than a few thousand item types or a large number of active markets.
- If a future StockMarket or BankSystem update changes an internal target, the affected tweak disables itself with a log message instead of crashing.

