@@ -676,4 +676,134 @@ private static void setTotalExperience(final Player player, final int exp)
676676 public void saveOnShutdown () {
677677 Bukkit .getOnlinePlayers ().forEach (p -> this .storeAndSave (p , p .getWorld (), true ));
678678 }
679+
680+ /**
681+ * Compute the storage key for a player and island event world, without using
682+ * the player's current location. Used when the player is not in the target world.
683+ * @param player - player
684+ * @param world - the BentoBox event world
685+ * @param island - the island involved in the event (may be null)
686+ * @return storage key for this world/island combination
687+ */
688+ String getStorageKeyForEvent (Player player , World world , Island island ) {
689+ String overworldName = getOverworldName (world );
690+ if (!addon .getSettings ().isIslandsActive ()) {
691+ return overworldName ;
692+ }
693+ World overworld = Util .getWorld (world );
694+ if (overworld == null ) {
695+ return overworldName ;
696+ }
697+ int count = addon .getIslands ().getNumberOfConcurrentIslands (player .getUniqueId (), overworld );
698+ if (count <= 1 ) {
699+ return overworldName ;
700+ }
701+ // Only use island-specific key if the player owns the island
702+ if (island != null && island .getOwner () != null && island .getOwner ().equals (player .getUniqueId ())) {
703+ return overworldName + "/" + island .getUniqueId ();
704+ }
705+ return overworldName ;
706+ }
707+
708+ /**
709+ * Clears the stored inventory for a BentoBox world when the player is not currently in
710+ * that world. Called when BentoBox fires a {@code PlayerResetInventoryEvent} while the
711+ * player is in a non-BentoBox world so the player's current inventory is not affected.
712+ * @param player - online player
713+ * @param world - the BentoBox world whose stored inventory should be cleared
714+ * @param island - the island involved in the reset (may be null)
715+ */
716+ public void clearStoredInventoryForWorld (Player player , World world , Island island ) {
717+ InventoryStorage store = getInv (player );
718+ String key = getStorageKeyForEvent (player , world , island );
719+ String worldKey = getOverworldName (world );
720+ Settings settings = addon .getSettings ();
721+ if (settings .isInventory ()) {
722+ String k = settings .isIslandsInventory () ? key : worldKey ;
723+ store .setInventory (k , Collections .emptyList ());
724+ }
725+ database .saveObjectAsync (store );
726+ }
727+
728+ /**
729+ * Clears the stored ender chest for a BentoBox world when the player is not currently in
730+ * that world. Called when BentoBox fires a {@code PlayerResetEnderChestEvent} while the
731+ * player is in a non-BentoBox world.
732+ * @param player - online player
733+ * @param world - the BentoBox world whose stored ender chest should be cleared
734+ * @param island - the island involved in the reset (may be null)
735+ */
736+ public void clearStoredEnderChestForWorld (Player player , World world , Island island ) {
737+ InventoryStorage store = getInv (player );
738+ String key = getStorageKeyForEvent (player , world , island );
739+ String worldKey = getOverworldName (world );
740+ Settings settings = addon .getSettings ();
741+ if (settings .isEnderChest ()) {
742+ String k = settings .isIslandsEnderChest () ? key : worldKey ;
743+ store .setEnderChest (k , Collections .emptyList ());
744+ }
745+ database .saveObjectAsync (store );
746+ }
747+
748+ /**
749+ * Zeroes the stored experience for a BentoBox world when the player is not currently in
750+ * that world. Called when BentoBox fires a {@code PlayerResetExpEvent} while the
751+ * player is in a non-BentoBox world.
752+ * @param player - online player
753+ * @param world - the BentoBox world whose stored experience should be zeroed
754+ * @param island - the island involved in the reset (may be null)
755+ */
756+ public void clearStoredExpForWorld (Player player , World world , Island island ) {
757+ InventoryStorage store = getInv (player );
758+ String key = getStorageKeyForEvent (player , world , island );
759+ String worldKey = getOverworldName (world );
760+ Settings settings = addon .getSettings ();
761+ if (settings .isExperience ()) {
762+ String k = settings .isIslandsExperience () ? key : worldKey ;
763+ store .setExp (k , 0 );
764+ }
765+ database .saveObjectAsync (store );
766+ }
767+
768+ /**
769+ * Removes the stored health for a BentoBox world when the player is not currently in
770+ * that world. Called when BentoBox fires a {@code PlayerResetHealthEvent} while the
771+ * player is in a non-BentoBox world. Removing the entry means the player will receive
772+ * maximum health the next time they enter the world.
773+ * @param player - online player
774+ * @param world - the BentoBox world whose stored health should be removed
775+ * @param island - the island involved in the reset (may be null)
776+ */
777+ public void clearStoredHealthForWorld (Player player , World world , Island island ) {
778+ InventoryStorage store = getInv (player );
779+ String key = getStorageKeyForEvent (player , world , island );
780+ String worldKey = getOverworldName (world );
781+ Settings settings = addon .getSettings ();
782+ if (settings .isHealth ()) {
783+ String k = settings .isIslandsHealth () ? key : worldKey ;
784+ store .getHealth ().remove (k );
785+ }
786+ database .saveObjectAsync (store );
787+ }
788+
789+ /**
790+ * Resets the stored food level to full (20) for a BentoBox world when the player is not
791+ * currently in that world. Called when BentoBox fires a {@code PlayerResetHungerEvent}
792+ * while the player is in a non-BentoBox world.
793+ * @param player - online player
794+ * @param world - the BentoBox world whose stored food level should be reset
795+ * @param island - the island involved in the reset (may be null)
796+ */
797+ public void clearStoredFoodForWorld (Player player , World world , Island island ) {
798+ InventoryStorage store = getInv (player );
799+ String key = getStorageKeyForEvent (player , world , island );
800+ String worldKey = getOverworldName (world );
801+ Settings settings = addon .getSettings ();
802+ if (settings .isFood ()) {
803+ String k = settings .isIslandsFood () ? key : worldKey ;
804+ store .setFood (k , 20 );
805+ }
806+ database .saveObjectAsync (store );
807+ }
808+
679809}
0 commit comments