Repository navigation
fix: close open containers on world switch, sync ender chest on close, and use Locale.US in InvEconomy - #68
Conversation
…, and use Locale.US in InvEconomy
tastybento
left a comment
There was a problem hiding this comment.
Bug: an open Ender Chest during a world change overwrites the new world's Ender Chest
In onPlayerChangedWorld, closeOpenContainer() runs before storeInventory / getInventory:
- Player in world A has their Ender Chest open and changes to world B.
PlayerChangedWorldEventfires —player.getWorld()already returns world B.closeOpenContainer()closes the Ender Chest →InventoryCloseEvent→onEnderChestClose→syncEnderChest(player, player.getWorld()), i.e. with world B.- With
islandsEnderChest=false(the default),syncEnderChestusesgetOverworldName(world), so it saves world A's live Ender Chest contents under world B's key. storeInventory(player, A)correctly saves A's contents under A.getInventory(player, B)loads B's Ender Chest from the store (Store.java~L296-298), which now holds A's contents.
Result: the player sees world A's Ender Chest items in world B (effectively duplicating them), and world B's real stored contents are overwritten and lost permanently.
Not affected: islandsEnderChest=true (uses currentKey, which still points at the old key), onIslandEnter (same world), onPlayerQuit.
Suggested fixes (either):
- Suppress the close-sync during a switch: set a per-player "switching" flag around the close/store/load block and have
onEnderChestClosereturn early when it's set. Nothing is lost, sincestoreInventorysaves the Ender Chest immediately afterward. - Or have
syncEnderChestalways resolve the key fromcurrentKeyrather than from the passed-in world.
A test that opens an Ender Chest and then fires PlayerChangedWorldEvent should check that B's stored contents are unchanged and that A's contents land in A's key.
The rest looks good: snapshotEnderChest() cloning, the Locale.US fix in InvEconomy, and closing containers before save/swap are all sensible. Closing containers just needs the guard above.
| if (Util.sameWorld(to, from) || (!addon.getWorlds().contains(from) && !addon.getWorlds().contains(to))) { | ||
| return; | ||
| } | ||
| closeOpenContainer(event.getPlayer()); |
There was a problem hiding this comment.
This runs before the inventory is stored.
…orld change closeOpenContainer() runs inside PlayerChangedWorldEvent, where player.getWorld() already returns the destination world. Closing an open Ender Chest fired onEnderChestClose, which synced the source world's Ender Chest contents into the destination world's slot, so getInventory() then loaded the wrong contents and the destination world's real Ender Chest was lost. Suppress the close-sync while InvSwitcher itself closes the view; every caller stores the Ender Chest immediately afterward. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Summary
This PR fixes three issues related to inventory/container consistency and locale-dependent economy formatting:
Close open containers before saving/swapping inventories (
PlayerListener)closeOpenContainer(Player)helper called before storing/switching inventories inonWorldEnter,onIslandEnter, andonPlayerQuit. Personal views (CRAFTING/CREATIVE) are left untouched.Persist Ender Chest immediately on close (
PlayerListener+Store)InventoryCloseEventlistener (EventPriority.MONITOR) forInventoryType.ENDER_CHESTin managed worlds that callsStore#syncEnderChest(Player, World). Snapshots defensively clone non-nullItemStacks before storing and persisting asynchronously.Use
Locale.USinInvEconomy#format(double)String.format("%,." + digits + "f %s", ...)relied on the JVM's default locale, causing decimal commas (e.g.1,00 Dollarinstead of1.00 Dollar) on non-US Linux/European/Latin American locales and failingInvEconomyTest#testFormatSingularAndPlural.Locale.UStoString.format(...).Testing
storeInventory/storeAndSaveon world switch, island switch, and quit (and ignoringCRAFTING/CREATIVEviews) inPlayerListenerTest.InventoryCloseEvent(and ignoring non-ender-chest views, disabled setting, or unmanaged worlds) inPlayerListenerTest.Store#syncEnderChestinStoreTest.InvEconomy#formatunderLocale.FRANCEandes-CLinInvEconomyTest.MockBukkit.mock()/unmock()lifecycle inPlayerListenerTestsoUtilstatic initialization succeeds regardless of test execution order.mvn -B -T 4 clean test: 137 tests run, 0 failures, 0 errors, 0 skipped.