-
-
Save aikar/38ab07fb016ec368f366 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static boolean attemptSpawn(Entity entity) { | |
Location loc = entity.getLocation(); | |
long now = System.currentTimeMillis(); | |
if (now - lastSpawnAttempt > 5000) { | |
lastSpawnAttempt = now; | |
for (CustomMob customMob : spawnableList) { | |
float randChance = Util.RANDOM.nextFloat() * 100; | |
if (customMob.spawnRate == 0 || | |
now - customMob.lastSpawn < customMob.respawnRate) { | |
continue; | |
} | |
if (randChance > customMob.spawnRate || !customMob.canSpawn(loc)) { | |
continue; | |
} | |
boolean tooClose = false; | |
for (Entity playerCheck : entity.getNearbyEntities(customMob.minPlayerSpawn, | |
256, | |
customMob.minPlayerSpawn)) { | |
if (playerCheck instanceof Player) { | |
tooClose = true; | |
break; | |
} | |
} | |
if (!tooClose) { | |
continue; | |
} | |
if (Wastelands.isWastelands(entity.getWorld())) { | |
if (Wastelands.isSpawnVicinity(entity.getLocation(), 64)) { | |
continue; | |
} | |
} | |
// Finally can spawn | |
customMob.lastSpawn = now; | |
customMob.spawn(loc); | |
return true; | |
} | |
} | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@EventHandler(ignoreCancelled = true) | |
public void onCreatureSpawn(CreatureSpawnEvent event) { | |
Location loc = event.getLocation(); | |
if (Util.isTownWorld(loc.getWorld()) || event.getSpawnReason() != CreatureSpawnEvent.SpawnReason.NATURAL) { | |
return; | |
} | |
if (event.getEntity() instanceof Monster) { | |
if (CustomMob.attemptSpawn(event.getEntity())) { | |
event.setCancelled(true); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why not just use
Math.random()
for random number generation?You could move the
randChance
variable down below the firstif
statement. Probably doesn't make much difference, but if the firstif
statement evaluates to true, then the loop continues and it's a wasted calculation.Is
if (!tooClose)
correct? Shouldn't it be reversed? o.O