2020
2121public class SpawnFinder {
2222
23- private static final List <String > cachedSpawns = new ArrayList <>();
24- private WrappedTask cacheSpawnTask ;
25-
2623 public static Location getRandomSpawn () throws Exception {
2724 boolean valid = false ;
2825 Location location = null ;
@@ -33,11 +30,11 @@ public static Location getRandomSpawn() throws Exception {
3330 if (tries >= 30 ) {
3431 throw new Exception ();
3532 }
36- if (cachedSpawns .isEmpty ()) {
33+ if (SpawnData . cachedSpawns .isEmpty ()) {
3734 String msg = Config .getLangConfig ().noSpawnFound ;
3835 RandomSpawnPlus .getInstance ().getLogger ().severe (msg );
3936 }
40- if (!cachedSpawns .isEmpty ()) {
37+ if (!SpawnData . cachedSpawns .isEmpty ()) {
4138 location = getRandomSpawn2 ();
4239 } else {
4340 location = getCandidateLocation ();
@@ -166,59 +163,14 @@ public static boolean checkSpawn(Location location) {
166163 return isValid ;
167164 }
168165
169- private void cacheSpawns () {
170- List <String > locationStrings = Config .getSpawnStorage ().get ().getStringList ("spawns" );
171-
172- cachedSpawns .addAll (locationStrings );
173-
174- int missingLocations = Config .getGlobalConfig ().spawnCacheCount - locationStrings .size ();
175-
176- if (missingLocations <= 0 ) {
177- return ;
178- }
179-
180- List <String > newLocations = new ArrayList <>();
181-
182- Bukkit .getLogger ().info ("Caching " + missingLocations + " spawns." );
183- for (int i = 0 ; i <= missingLocations ; i ++) {
184- RandomSpawnPlus .getInstance ().foliaLib .getScheduler ().runLater (() -> {
185- Location location = null ;
186- boolean valid = false ;
187-
188- while (!valid ) {
189- location = getCandidateLocation ();
190- valid = checkSpawn (location );
191- }
192-
193- newLocations .add (Locations .serializeString (location ));
194- }, 1 );
195- }
196-
197- cacheSpawnTask = RandomSpawnPlus .getInstance ().foliaLib .getScheduler ().runTimer (() -> {
198- // Wait for all spawns to be cached
199- if (newLocations .size () <= missingLocations ) {
200- if (RandomSpawnPlus .getInstance ().getConfig ().getBoolean ("debug-mode" )) {
201- System .out .println (newLocations .size () + ", " + missingLocations );
202- }
203- } else {
204- cachedSpawns .addAll (newLocations );
205- // Save spawns to file
206- Config .getSpawnStorage ().get ().set ("spawns" , cachedSpawns );
207- RandomSpawnPlus .getInstance ().saveConfig ();
208-
209- RandomSpawnPlus .getInstance ().foliaLib .getScheduler ().cancelTask (cacheSpawnTask );
210- }
211- }, 10 , 10 );
212- }
213-
214166 public static Location getRandomSpawn2 () {
215- int element = ThreadLocalRandom .current ().nextInt (cachedSpawns .size ());
216- return Locations .deserializeLocationString (cachedSpawns .get (element ));
167+ int element = ThreadLocalRandom .current ().nextInt (SpawnData . cachedSpawns .size ());
168+ return Locations .deserializeLocationString (SpawnData . cachedSpawns .get (element ));
217169 }
218170
219171 public static void deleteSpawn (Location location ) {
220- cachedSpawns .removeIf (locationString -> Locations .serializeString (location ).equals (locationString ));
221- Config .getSpawnStorage ().get ().set ("spawns" , cachedSpawns );
172+ SpawnData . cachedSpawns .removeIf (locationString -> Locations .serializeString (location ).equals (locationString ));
173+ Config .getSpawnStorage ().get ().set ("spawns" , SpawnData . cachedSpawns );
222174 try {
223175 Config .getSpawnStorage ().saveConfig ();
224176 } catch (IOException e ) {
0 commit comments