2019-02-03 10:22:44 +01:00
|
|
|
package world.bentobox.bentobox.listeners.flags.settings;
|
2018-02-02 05:30:57 +01:00
|
|
|
|
2019-07-05 01:54:05 +02:00
|
|
|
import java.util.Optional;
|
|
|
|
|
2020-06-21 17:47:01 +02:00
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.entity.Entity;
|
2019-03-09 21:21:46 +01:00
|
|
|
import org.bukkit.entity.PufferFish;
|
2018-02-02 05:30:57 +01:00
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.entity.CreatureSpawnEvent;
|
2019-07-05 01:54:05 +02:00
|
|
|
|
2020-06-21 17:47:01 +02:00
|
|
|
import world.bentobox.bentobox.api.flags.Flag;
|
2018-11-04 16:40:20 +01:00
|
|
|
import world.bentobox.bentobox.api.flags.FlagListener;
|
2018-07-31 18:03:32 +02:00
|
|
|
import world.bentobox.bentobox.database.objects.Island;
|
|
|
|
import world.bentobox.bentobox.lists.Flags;
|
2019-03-09 21:21:46 +01:00
|
|
|
import world.bentobox.bentobox.util.Util;
|
|
|
|
|
2018-02-02 05:30:57 +01:00
|
|
|
/**
|
|
|
|
* Handles natural mob spawning.
|
|
|
|
* @author tastybento
|
|
|
|
*/
|
2018-11-04 16:40:20 +01:00
|
|
|
public class MobSpawnListener extends FlagListener {
|
2018-02-02 05:30:57 +01:00
|
|
|
|
2022-01-22 21:16:27 +01:00
|
|
|
/**
|
|
|
|
* Prevents mobs spawning naturally
|
|
|
|
*
|
|
|
|
* @param e - event
|
|
|
|
*/
|
|
|
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
|
|
|
public void onMobSpawnEvent(CreatureSpawnEvent e) {
|
|
|
|
onMobSpawn(e);
|
|
|
|
}
|
2018-02-02 05:30:57 +01:00
|
|
|
/**
|
|
|
|
* Prevents mobs spawning naturally
|
|
|
|
*
|
2018-02-18 02:01:25 +01:00
|
|
|
* @param e - event
|
2018-02-20 07:32:00 +01:00
|
|
|
* @return true if cancelled
|
2018-02-02 05:30:57 +01:00
|
|
|
*/
|
|
|
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
2022-01-22 21:16:27 +01:00
|
|
|
boolean onMobSpawn(CreatureSpawnEvent e) {
|
2019-10-30 22:16:47 +01:00
|
|
|
// If not in the right world, or spawning is not natural return
|
2019-03-24 15:47:25 +01:00
|
|
|
if (!getIWM().inWorld(e.getEntity().getLocation())) {
|
2022-01-22 21:16:27 +01:00
|
|
|
return false;
|
2018-02-02 05:30:57 +01:00
|
|
|
}
|
2019-10-30 22:16:47 +01:00
|
|
|
switch (e.getSpawnReason()) {
|
2019-10-31 02:56:12 +01:00
|
|
|
// Natural
|
2019-10-30 22:16:47 +01:00
|
|
|
case DEFAULT:
|
|
|
|
case DROWNED:
|
|
|
|
case JOCKEY:
|
|
|
|
case LIGHTNING:
|
|
|
|
case MOUNT:
|
|
|
|
case NATURAL:
|
|
|
|
case NETHER_PORTAL:
|
|
|
|
case OCELOT_BABY:
|
|
|
|
case PATROL:
|
|
|
|
case RAID:
|
|
|
|
case REINFORCEMENTS:
|
|
|
|
case SILVERFISH_BLOCK:
|
2021-10-04 05:33:50 +02:00
|
|
|
//case SLIME_SPLIT: messes with slimes from spawners, slime must have previously existed to create another
|
2019-10-30 22:16:47 +01:00
|
|
|
case TRAP:
|
|
|
|
case VILLAGE_DEFENSE:
|
|
|
|
case VILLAGE_INVASION:
|
2020-06-21 17:47:01 +02:00
|
|
|
boolean cancelNatural = shouldCancel(e.getEntity(), e.getLocation(), Flags.ANIMAL_NATURAL_SPAWN, Flags.MONSTER_NATURAL_SPAWN);
|
|
|
|
e.setCancelled(cancelNatural);
|
2022-01-22 21:16:27 +01:00
|
|
|
return cancelNatural;
|
2021-01-09 05:05:23 +01:00
|
|
|
// Spawners
|
2020-06-21 17:47:01 +02:00
|
|
|
case SPAWNER:
|
|
|
|
boolean cancelSpawners = shouldCancel(e.getEntity(), e.getLocation(), Flags.ANIMAL_SPAWNERS_SPAWN, Flags.MONSTER_SPAWNERS_SPAWN);
|
|
|
|
e.setCancelled(cancelSpawners);
|
2022-01-22 21:16:27 +01:00
|
|
|
return cancelSpawners;
|
2019-10-30 22:16:47 +01:00
|
|
|
default:
|
2022-01-22 21:16:27 +01:00
|
|
|
return false;
|
2018-02-02 05:30:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 17:47:01 +02:00
|
|
|
private boolean shouldCancel(Entity entity, Location loc, Flag animalSpawnFlag, Flag monsterSpawnFlag) {
|
|
|
|
Optional<Island> island = getIslands().getIslandAt(loc);
|
|
|
|
if (Util.isHostileEntity(entity) && !(entity instanceof PufferFish)) {
|
2021-01-09 05:05:23 +01:00
|
|
|
return island.map(i -> !i.isAllowed(monsterSpawnFlag)).orElseGet(() -> !monsterSpawnFlag.isSetForWorld(entity.getWorld()));
|
2020-06-21 17:47:01 +02:00
|
|
|
} else if (Util.isPassiveEntity(entity) || entity instanceof PufferFish) {
|
2021-01-09 05:05:23 +01:00
|
|
|
return island.map(i -> !i.isAllowed(animalSpawnFlag)).orElseGet(() -> !animalSpawnFlag.isSetForWorld(entity.getWorld()));
|
2020-06-21 17:47:01 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-02 05:30:57 +01:00
|
|
|
}
|