Skip to content

fix: close open containers on world switch, sync ender chest on close, and use Locale.US in InvEconomy - #68

Merged
tastybento merged 2 commits into
BentoBoxWorld:developfrom
JackStar6677-1:fix/container-close-enderchest-sync-locale
Nov 22, 2026
Merged

tastybento merged 2 commits into
BentoBoxWorld:developfrom
JackStar6677-1:fix/container-close-enderchest-sync-locale

Conversation

@JackStar6677-1

Copy link
Copy Markdown
Contributor

Summary

This PR fixes three issues related to inventory/container consistency and locale-dependent economy formatting:

  1. Close open containers before saving/swapping inventories (PlayerListener)

    • Problem: When a player changes worlds, switches between owned islands, or disconnects while holding a container view open (e.g. chest, ender chest, shulker box), cursor items or open container state can be lost or duplicated across worlds/islands.
    • Fix: Added a safe closeOpenContainer(Player) helper called before storing/switching inventories in onWorldEnter, onIslandEnter, and onPlayerQuit. Personal views (CRAFTING / CREATIVE) are left untouched.
  2. Persist Ender Chest immediately on close (PlayerListener + Store)

    • Problem: Ender Chest snapshots were only persisted on world/island transitions or quit, leaving stored snapshots stale if a server crash, disconnect race, or virtual Ender Chest view occurs after closing the Ender Chest.
    • Fix: Added an InventoryCloseEvent listener (EventPriority.MONITOR) for InventoryType.ENDER_CHEST in managed worlds that calls Store#syncEnderChest(Player, World). Snapshots defensively clone non-null ItemStacks before storing and persisting asynchronously.
  3. Use Locale.US in InvEconomy#format(double)

    • Problem: String.format("%,." + digits + "f %s", ...) relied on the JVM's default locale, causing decimal commas (e.g. 1,00 Dollar instead of 1.00 Dollar) on non-US Linux/European/Latin American locales and failing InvEconomyTest#testFormatSingularAndPlural.
    • Fix: Explicitly pass Locale.US to String.format(...).

Testing

  • Added regression unit tests covering:
    • Closing open containers before storeInventory/storeAndSave on world switch, island switch, and quit (and ignoring CRAFTING / CREATIVE views) in PlayerListenerTest.
    • Syncing Ender Chest on InventoryCloseEvent (and ignoring non-ender-chest views, disabled setting, or unmanaged worlds) in PlayerListenerTest.
    • Defensive cloning and async persistence in Store#syncEnderChest in StoreTest.
    • InvEconomy#format under Locale.FRANCE and es-CL in InvEconomyTest.
    • Added MockBukkit.mock()/unmock() lifecycle in PlayerListenerTest so Util static initialization succeeds regardless of test execution order.
  • Verified locally with mvn -B -T 4 clean test: 137 tests run, 0 failures, 0 errors, 0 skipped.

@tastybento
tastybento self-requested a review November 22, 2026 03:24
@tastybento tastybento self-assigned this Nov 22, 2026

@tastybento tastybento left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: an open Ender Chest during a world change overwrites the new world's Ender Chest

In onPlayerChangedWorld, closeOpenContainer() runs before storeInventory / getInventory:

  1. Player in world A has their Ender Chest open and changes to world B.
  2. PlayerChangedWorldEvent fires — player.getWorld() already returns world B.
  3. closeOpenContainer() closes the Ender Chest → InventoryCloseEvent → onEnderChestClose → syncEnderChest(player, player.getWorld()), i.e. with world B.
  4. With islandsEnderChest=false (the default), syncEnderChest uses getOverworldName(world), so it saves world A's live Ender Chest contents under world B's key.
  5. storeInventory(player, A) correctly saves A's contents under A.
  6. 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 onEnderChestClose return early when it's set. Nothing is lost, since storeInventory saves the Ender Chest immediately afterward.
  • Or have syncEnderChest always resolve the key from currentKey rather 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());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@tastybento
tastybento merged commit b25309e into BentoBoxWorld:develop Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants