1111
1212import java .util .ArrayList ;
1313import java .util .List ;
14+ import java .util .concurrent .CopyOnWriteArrayList ;
1415
1516public class DataSave extends BukkitRunnable {
1617
@@ -19,18 +20,22 @@ public class DataSave extends BukkitRunnable {
1920 public static int autosave = 3 ;
2021 final public static String dataVersion = "1.2" ;
2122 public static DataSave running ;
23+ public static List <World > unloadingWorlds = new CopyOnWriteArrayList <>();
2224
2325 public ReadOldData read ;
24- private long time ;
26+ private final long time ;
27+ private final List <World > loadedWorlds ;
2528 public boolean collected = false ;
2629
2730 // Not Thread-Safe! Needs to be run in main thread but uses async Read/Write
2831 public DataSave (ReadOldData read ) {
2932 this .read = read ;
3033 time = System .currentTimeMillis ();
34+ loadedWorlds = P .p .getServer ().getWorlds ();
3135 }
3236
3337
38+ // Running in Main Thread
3439 @ Override
3540 public void run () {
3641 try {
@@ -107,6 +112,20 @@ public void run() {
107112
108113 collected = true ;
109114
115+ if (!unloadingWorlds .isEmpty ()) {
116+ try {
117+ for (World world : unloadingWorlds ) {
118+ // In the very most cases, it is just one world, so just looping like this is fine
119+ Barrel .onUnload (world );
120+ BCauldron .onUnload (world );
121+ Wakeup .onUnload (world );
122+ }
123+ } catch (Exception e ) {
124+ e .printStackTrace ();
125+ }
126+ unloadingWorlds .clear ();
127+ }
128+
110129 P .p .debugLog ("saving: " + ((System .nanoTime () - saveTime ) / 1000000.0 ) + "ms" );
111130
112131 if (P .p .isEnabled ()) {
@@ -121,6 +140,22 @@ public void run() {
121140 }
122141 }
123142
143+ public void saveWorldNames (FileConfiguration root , ConfigurationSection old ) {
144+ if (old != null ) {
145+ root .set ("Worlds" , old );
146+ }
147+ for (World world : loadedWorlds ) {
148+ String worldName = world .getName ();
149+ if (worldName .startsWith ("DXL_" )) {
150+ worldName = BUtil .getDxlName (worldName );
151+ root .set ("Worlds." + worldName , 0 );
152+ } else {
153+ worldName = world .getUID ().toString ();
154+ root .set ("Worlds." + worldName , world .getName ());
155+ }
156+ }
157+ }
158+
124159 // Finish the collection of data immediately
125160 public void now () {
126161 if (!read .done ) {
@@ -164,20 +199,4 @@ public static void autoSave() {
164199 lastSave ++;
165200 }
166201 }
167-
168- public static void saveWorldNames (FileConfiguration root , ConfigurationSection old ) {
169- if (old != null ) {
170- root .set ("Worlds" , old );
171- }
172- for (World world : P .p .getServer ().getWorlds ()) {
173- String worldName = world .getName ();
174- if (worldName .startsWith ("DXL_" )) {
175- worldName = BUtil .getDxlName (worldName );
176- root .set ("Worlds." + worldName , 0 );
177- } else {
178- worldName = world .getUID ().toString ();
179- root .set ("Worlds." + worldName , world .getName ());
180- }
181- }
182- }
183202}
0 commit comments