Skip to content

Instantly share code, notes, and snippets.

@aikar
Created January 29, 2014 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aikar/8695871 to your computer and use it in GitHub Desktop.
Save aikar/8695871 to your computer and use it in GitHub Desktop.
public static boolean attemptSpawn(Entity entity) {
Location loc = entity.getLocation();
long now = System.currentTimeMillis();
if (now - lastSpawnAttempt >= 200) {
lastSpawnAttempt = now;
final MetaApi.MetaMap worldMetaMap = MetaApi.getWorldMetaMap(entity.getWorld());
for (CustomMob customMob : spawnableList) {
String metaKey = "survivalRespawn_" + customMob.customType;
final Object o = worldMetaMap.get(metaKey);
Long nextSpawn = o instanceof Long ? (Long) o : now;
if (customMob.spawnRate == 0 || nextSpawn > now) {
continue;
}
if (!customMob.canSpawn(loc)) {
continue;
}
float randChance = Util.RANDOM.nextFloat() * 100;
if (randChance > customMob.spawnRate) {
continue;
}
boolean tooClose = false;
for (Entity playerCheck : entity.getNearbyEntities(customMob.minPlayerSpawn,
256,
customMob.minPlayerSpawn)) {
if (playerCheck instanceof Player) {
tooClose = true;
break;
}
if (playerCheck.getType() == customMob.baseType) {
if (getType(playerCheck) == customMob) {
tooClose = true;
break;
}
}
}
if (tooClose) {
continue;
}
if (Wastelands.isWastelands(entity.getWorld())) {
if (Wastelands.isSpawnVicinity(entity.getLocation(), 64)) {
continue;
}
}
// Finally can spawn
nextSpawn = now + Util.rand((int) (customMob.respawnRate * 0.9), (int) (customMob.respawnRate * 1.1));
worldMetaMap.put(metaKey, nextSpawn);
customMob.spawn(loc);
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment