Skip to content

Commit 8f1f995

Browse files
committed
Move to spawn generator
1 parent 5832843 commit 8f1f995

3 files changed

Lines changed: 53 additions & 54 deletions

File tree

Common/src/main/java/systems/kscott/randomspawnplus/spawn/SpawnData.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package systems.kscott.randomspawnplus.spawn;
22

3+
import com.tcoded.folialib.wrapper.task.WrappedTask;
34
import it.unimi.dsi.fastutil.longs.LongArrayList;
45
import org.bukkit.Material;
56

7+
import java.util.ArrayList;
8+
import java.util.List;
69
import java.util.Set;
710

811
public class SpawnData {
@@ -14,6 +17,8 @@ public class SpawnData {
1417
// Unsafe blocks
1518
private static Set<Material> unsafeBlocks;
1619

20+
public static final List<String> cachedSpawns = new ArrayList<>();
21+
public static WrappedTask cacheSpawnTask;
1722

1823
// Check whether need to use method, or public field to access directly.
1924
// Or move to interface as default?

Common/src/main/java/systems/kscott/randomspawnplus/spawn/SpawnFinder.java

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020

2121
public 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) {

Common/src/main/java/systems/kscott/randomspawnplus/spawn/SpawnGenerator.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package systems.kscott.randomspawnplus.spawn;
22

33
import it.unimi.dsi.fastutil.longs.LongArrayList;
4+
import systems.kscott.randomspawnplus.RandomSpawnPlus;
45
import systems.kscott.randomspawnplus.config.Config;
6+
import systems.kscott.randomspawnplus.util.Locations;
57
import systems.kscott.randomspawnplus.util.PlatformUtil;
68
import org.bukkit.Bukkit;
9+
import org.bukkit.Location;
710
import org.bukkit.Material;
811
import org.bukkit.World;
912

13+
import java.util.ArrayList;
1014
import java.util.HashSet;
1115
import java.util.List;
1216
import java.util.concurrent.CompletableFuture;
@@ -44,13 +48,51 @@ private static void initSpawnChunks() {
4448
CompletableFuture<LongArrayList> prepareChunksTask = PlatformUtil.getPlatform().collectNonGeneratedChunksAsync(spawnLevel, minX, minZ, maxX, maxZ);
4549

4650
prepareChunksTask.thenAccept($ -> {
51+
initSpawnPoints();
4752
// TODO: change to logger or remove
4853
System.out.println("Prepare chunks took " + (System.currentTimeMillis() - start) + "ms");
4954
SpawnData.finalizeSpawnChunksGeneration();
5055
});
5156
}
5257

5358
private static void initSpawnPoints() {
59+
List<String> locationStrings = Config.getSpawnStorage().get().getStringList("spawns");
5460

61+
SpawnData.cachedSpawns.addAll(locationStrings);
62+
63+
int missingLocations = Config.getGlobalConfig().spawnCacheCount - locationStrings.size();
64+
65+
if (missingLocations <= 0) {
66+
return;
67+
}
68+
69+
List<String> newLocations = new ArrayList<>();
70+
71+
Bukkit.getLogger().info("Caching " + missingLocations + " spawns.");
72+
for (int i = 0; i <= missingLocations; i++) {
73+
RandomSpawnPlus.getInstance().foliaLib.getScheduler().runLater(() -> {
74+
Location location = null;
75+
boolean valid = false;
76+
77+
while (!valid) {
78+
location = SpawnFinder.getCandidateLocation();
79+
valid = SpawnFinder.checkSpawn(location);
80+
}
81+
82+
newLocations.add(Locations.serializeString(location));
83+
}, 1);
84+
}
85+
86+
SpawnData.cacheSpawnTask = RandomSpawnPlus.getInstance().foliaLib.getScheduler().runTimer(() -> {
87+
// Wait for all spawns to be cached
88+
if (newLocations.size() > missingLocations) {
89+
SpawnData.cachedSpawns.addAll(newLocations);
90+
// Save spawns to file
91+
Config.getSpawnStorage().get().set("spawns", SpawnData.cachedSpawns);
92+
RandomSpawnPlus.getInstance().saveConfig();
93+
94+
RandomSpawnPlus.getInstance().foliaLib.getScheduler().cancelTask(SpawnData.cacheSpawnTask);
95+
}
96+
}, 10, 10);
5597
}
5698
}

0 commit comments

Comments
 (0)