11package systems .kscott .randomspawnplus .spawn ;
22
3- import com .cryptomorin .xseries .XMaterial ;
43import java .util .concurrent .ThreadLocalRandom ;
54import org .bukkit .Bukkit ;
65import org .bukkit .Location ;
109import org .bukkit .configuration .file .FileConfiguration ;
1110import systems .kscott .randomspawnplus .RandomSpawnPlus ;
1211import systems .kscott .randomspawnplus .events .SpawnCheckEvent ;
13- import systems .kscott .randomspawnplus .exceptions .FinderTimedOutException ;
1412import systems .kscott .randomspawnplus .util .Chat ;
1513import systems .kscott .randomspawnplus .util .Numbers ;
1614
@@ -22,24 +20,20 @@ public class SpawnFinder {
2220 public static SpawnFinder INSTANCE ;
2321 public RandomSpawnPlus plugin ;
2422 public FileConfiguration config ;
25- ArrayList <Material > safeBlocks ;
23+ ArrayList <Material > unsafeBlocks ;
2624
2725 public SpawnFinder (RandomSpawnPlus plugin ) {
2826 this .plugin = plugin ;
2927 this .config = plugin .getConfig ();
3028
3129 /* Setup safeblocks */
32- List <String > safeBlockStrings ;
33- safeBlockStrings = config .getStringList ("safe -blocks" );
30+ List <String > unsafeBlockStrings ;
31+ unsafeBlockStrings = config .getStringList ("unsafe -blocks" );
3432
35- safeBlocks = new ArrayList <>();
36- for (String string : safeBlockStrings ) {
37- safeBlocks .add (Material .matchMaterial (string ));
33+ unsafeBlocks = new ArrayList <>();
34+ for (String string : unsafeBlockStrings ) {
35+ unsafeBlocks .add (Material .matchMaterial (string ));
3836 }
39-
40- safeBlocks .add (XMaterial .AIR .parseMaterial ());
41- safeBlocks .add (XMaterial .VOID_AIR .parseMaterial ());
42- safeBlocks .add (XMaterial .CAVE_AIR .parseMaterial ());
4337 }
4438
4539 public static void initialize (RandomSpawnPlus plugin ) {
@@ -93,22 +87,14 @@ public Location getCandidateLocation() {
9387 maxZ = region .getMaxZ ();
9488 }
9589
96- boolean debugMode = config .getBoolean ("debug-mode" );
97- if (debugMode ) {
98- System .out .println (minX );
99- System .out .println (minZ );
100- System .out .println (maxX );
101- System .out .println (maxZ );
102- }
103-
10490 int candidateX = Numbers .getRandomNumberInRange (minX , maxX );
10591 int candidateZ = Numbers .getRandomNumberInRange (minZ , maxZ );
10692 int candidateY = getHighestY (world , candidateX , candidateZ );
10793
10894 return new Location (world , candidateX , candidateY , candidateZ );
10995 }
11096
111- private Location getValidLocation (boolean useSpawnCaching ) throws FinderTimedOutException {
97+ private Location getValidLocation (boolean useSpawnCaching ) throws Exception {
11298 boolean useCache = config .getBoolean ("enable-spawn-cacher" );
11399
114100 boolean valid = false ;
@@ -118,7 +104,7 @@ private Location getValidLocation(boolean useSpawnCaching) throws FinderTimedOut
118104 int tries = 0 ;
119105 while (!valid ) {
120106 if (tries >= 30 ) {
121- throw new FinderTimedOutException ();
107+ throw new Exception ();
122108 }
123109 if (SpawnCacher .getInstance ().getCachedSpawns ().isEmpty ()) {
124110 plugin .getLogger ().severe (Chat .get ("no-spawns-cached" ));
@@ -135,29 +121,27 @@ private Location getValidLocation(boolean useSpawnCaching) throws FinderTimedOut
135121 }
136122 tries = tries + 1 ;
137123 }
138-
124+ if ( location == null ) return null ;
139125 return location ;
140126 }
141127
142- public Location findSpawn (boolean useSpawnCaching ) throws FinderTimedOutException {
128+ public Location findSpawn (boolean useSpawnCaching ) throws Exception {
143129
144130 Location location = getValidLocation (useSpawnCaching );
131+ if (location == null ) return null ;
145132
146- boolean debugMode = config .getBoolean ("debug-mode" );
147- if (debugMode ) {
133+ if (config .getBoolean ("debug-mode" )) {
148134 Location locClone = location .clone ();
149- plugin . getLogger (). info (locClone .getBlock ().getType (). toString ());
150- plugin . getLogger (). info (locClone .add (0 , 1 , 0 ).getBlock ().getType (). toString ());
151- plugin . getLogger (). info (locClone .add (0 , 1 , 0 ).getBlock ().getType (). toString ());
152- plugin . getLogger (). info ("Spawned at " + location .getBlockX () + ", " + location .getBlockY () + ", " + location .getBlockZ ());
135+ System . out . println (locClone .getBlock ().getType ());
136+ System . out . println (locClone .add (0 , 1 , 0 ).getBlock ().getType ());
137+ System . out . println (locClone .add (0 , 1 , 0 ).getBlock ().getType ());
138+ System . out . println ("Spawned at " + location .getBlockX () + ", " + location .getBlockY () + ", " + location .getBlockZ ());
153139 }
154140 return location .add (0 , 1 , 0 );
155141 }
156142
157143 public boolean checkSpawn (Location location ) {
158- if (location == null ) {
159- return false ;
160- }
144+ if (location == null ) return false ;
161145
162146 boolean blockWaterSpawns = config .getBoolean ("block-water-spawns" );
163147 boolean blockLavaSpawns = config .getBoolean ("block-lava-spawns" );
@@ -173,23 +157,16 @@ public boolean checkSpawn(Location location) {
173157
174158 Location locClone = location .clone ();
175159
176- if (locClone == null ) {
177- return false ;
178- }
179160 // 89apt89 start - Fix Paper method use
180- if (!location .getChunk ().isLoaded ()) {
181- location .getChunk ().load ();
161+ if (!location .getChunk ().isLoaded () || ! location . getChunk (). isGenerated () ) {
162+ location .getChunk ().load (true );
182163 }
183164 // 89apt89 end
184165
185166 Block block0 = locClone .getBlock ();
186167 Block block1 = locClone .add (0 , 1 , 0 ).getBlock ();
187168 Block block2 = locClone .add (0 , 1 , 0 ).getBlock ();
188169
189- if (block0 == null || block1 == null || block2 == null ) {
190- return false ;
191- }
192-
193170 SpawnCheckEvent spawnCheckEvent = new SpawnCheckEvent (location );
194171
195172 Bukkit .getServer ().getPluginManager ().callEvent (spawnCheckEvent );
@@ -198,7 +175,7 @@ public boolean checkSpawn(Location location) {
198175
199176 if (!isValid ) {
200177 if (debugMode ) {
201- plugin . getLogger (). info ("Invalid spawn: " + spawnCheckEvent .getValidReason ());
178+ System . out . println ("Invalid spawn: " + spawnCheckEvent .getValidReason ());
202179 }
203180 }
204181
@@ -211,31 +188,31 @@ public boolean checkSpawn(Location location) {
211188 }
212189 }
213190
214- if (block0 .isEmpty ()) {
191+ if (block0 .getType (). isAir ()) {
215192 if (debugMode ) {
216- plugin . getLogger (). info ("Invalid spawn: block0 isAir" );
193+ System . out . println ("Invalid spawn: block0 isAir" );
217194 }
218195 isValid = false ;
219196 }
220197
221- if (!block1 .isEmpty () || !block2 .isEmpty ()) {
198+ if (!block1 .getType (). isAir () || !block2 .getType (). isAir ()) {
222199 if (debugMode ) {
223- plugin . getLogger (). info ("Invalid spawn: block1 or block2 !isAir" );
200+ System . out . println ("Invalid spawn: block1 or block2 !isAir" );
224201 }
225202 isValid = false ;
226203 }
227204
228- if (! safeBlocks .contains (block1 .getType ())) {
205+ if (unsafeBlocks .contains (block1 .getType ())) {
229206 if (debugMode ) {
230- plugin . getLogger (). info ("Invalid spawn: " + block1 .getType () + " is not a safe block!" );
207+ System . out . println ("Invalid spawn: " + block1 .getType () + " is not a safe block!" );
231208 }
232209 isValid = false ;
233210 }
234211
235212 if (blockWaterSpawns ) {
236213 if (block0 .getType () == Material .WATER ) {
237214 if (debugMode ) {
238- plugin . getLogger (). info ("Invalid spawn: blockWaterSpawns" );
215+ System . out . println ("Invalid spawn: blockWaterSpawns" );
239216 }
240217 isValid = false ;
241218 }
@@ -244,7 +221,7 @@ public boolean checkSpawn(Location location) {
244221 if (blockLavaSpawns ) {
245222 if (block0 .getType () == Material .LAVA ) {
246223 if (debugMode ) {
247- plugin . getLogger (). info ("Invalid spawn: blockLavaSpawns" );
224+ System . out . println ("Invalid spawn: blockLavaSpawns" );
248225 }
249226 isValid = false ;
250227 }
@@ -254,12 +231,11 @@ public boolean checkSpawn(Location location) {
254231 }
255232
256233 public int getHighestY (World world , int x , int z ) {
257- boolean debugMode = config .getBoolean ("debug-mode" );
258234 int i = world .getMaxHeight ();
259235 while (i > world .getMinHeight ()) {
260236 if (!(new Location (world , x , i , z ).getBlock ()).isEmpty ()) {
261- if (debugMode ) {
262- plugin . getLogger (). info ( Integer . toString ( i ) );
237+ if (config . getBoolean ( "debug-mode" ) ) {
238+ System . out . println ( i );
263239 }
264240 return i ;
265241 }
0 commit comments