2019-12-16 16:47:51 +01:00
|
|
|
package world.bentobox.limits.listeners;
|
2019-04-23 00:50:15 +02:00
|
|
|
|
2019-11-17 01:23:37 +01:00
|
|
|
import java.util.Objects;
|
|
|
|
|
2019-06-23 08:41:30 +02:00
|
|
|
import org.bukkit.Location;
|
2019-11-17 01:23:37 +01:00
|
|
|
import org.bukkit.World;
|
2019-04-23 00:50:15 +02:00
|
|
|
import org.bukkit.entity.Entity;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.Listener;
|
|
|
|
import org.bukkit.event.entity.CreatureSpawnEvent;
|
|
|
|
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
|
2019-06-23 08:41:30 +02:00
|
|
|
import org.bukkit.event.hanging.HangingPlaceEvent;
|
2019-04-23 00:50:15 +02:00
|
|
|
import org.bukkit.event.vehicle.VehicleCreateEvent;
|
|
|
|
|
2019-11-08 21:35:47 +01:00
|
|
|
import world.bentobox.bentobox.api.localization.TextVariables;
|
2019-04-23 00:50:15 +02:00
|
|
|
import world.bentobox.bentobox.api.user.User;
|
|
|
|
import world.bentobox.bentobox.database.objects.Island;
|
|
|
|
import world.bentobox.bentobox.util.Util;
|
2019-12-16 16:47:51 +01:00
|
|
|
import world.bentobox.limits.Limits;
|
2019-04-23 00:50:15 +02:00
|
|
|
|
|
|
|
public class EntityLimitListener implements Listener {
|
2019-11-08 21:35:47 +01:00
|
|
|
private static final String MOD_BYPASS = "mod.bypass";
|
2019-04-23 00:50:15 +02:00
|
|
|
private final Limits addon;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles entity and natural limitations
|
|
|
|
* @param addon - Limits object
|
|
|
|
*/
|
|
|
|
public EntityLimitListener(Limits addon) {
|
|
|
|
this.addon = addon;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles minecart placing
|
|
|
|
* @param e - event
|
|
|
|
*/
|
|
|
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
|
|
|
public void onMinecart(VehicleCreateEvent e) {
|
|
|
|
// Return if not in a known world
|
|
|
|
if (!addon.getPlugin().getIWM().inWorld(e.getVehicle().getWorld())) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-11 01:08:21 +01:00
|
|
|
// If someone in that area has the bypass permission, allow the spawning
|
|
|
|
for (Entity entity : Objects.requireNonNull(e.getVehicle().getLocation().getWorld()).getNearbyEntities(e.getVehicle().getLocation(), 5, 5, 5)) {
|
|
|
|
if (entity instanceof Player) {
|
|
|
|
Player player = (Player)entity;
|
|
|
|
boolean bypass = (player.isOp() || player.hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(e.getVehicle().getWorld()) + MOD_BYPASS));
|
|
|
|
// Check island
|
|
|
|
addon.getIslands().getProtectedIslandAt(e.getVehicle().getLocation()).ifPresent(island -> {
|
|
|
|
// Ignore spawn
|
|
|
|
if (island.isSpawn()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check if the player is at the limit
|
|
|
|
if (!bypass && atLimit(island, e.getVehicle())) {
|
|
|
|
e.setCancelled(true);
|
|
|
|
for (Entity ent : e.getVehicle().getLocation().getWorld().getNearbyEntities(e.getVehicle().getLocation(), 5, 5, 5)) {
|
|
|
|
if (ent instanceof Player) {
|
|
|
|
((Player) ent).updateInventory();
|
|
|
|
User.getInstance(ent).sendMessage("entity-limits.hit-limit", "[entity]",
|
|
|
|
Util.prettifyText(e.getVehicle().getType().toString())
|
|
|
|
, TextVariables.NUMBER, String.valueOf(addon.getSettings().getLimits().get(e.getVehicle().getType())));
|
2019-04-23 00:50:15 +02:00
|
|
|
}
|
|
|
|
}
|
2020-01-11 01:08:21 +01:00
|
|
|
}
|
|
|
|
});
|
2019-04-23 00:50:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
|
|
|
public void onCreatureSpawn(final CreatureSpawnEvent e) {
|
|
|
|
// Return if not in a known world
|
|
|
|
if (!addon.getPlugin().getIWM().inWorld(e.getLocation())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
boolean bypass = false;
|
|
|
|
// Check why it was spawned
|
|
|
|
switch (e.getSpawnReason()) {
|
|
|
|
// These reasons are due to a player being involved (usually) so there may be a bypass
|
|
|
|
case BREEDING:
|
|
|
|
case BUILD_IRONGOLEM:
|
|
|
|
case BUILD_SNOWMAN:
|
|
|
|
case BUILD_WITHER:
|
|
|
|
case CURED:
|
|
|
|
case EGG:
|
|
|
|
case SPAWNER_EGG:
|
2019-06-23 08:41:30 +02:00
|
|
|
bypass = checkByPass(e.getLocation());
|
2019-04-23 00:50:15 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Other natural reasons
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Tag the entity with the island spawn location
|
|
|
|
checkLimit(e, bypass);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-23 08:41:30 +02:00
|
|
|
private boolean checkByPass(Location l) {
|
|
|
|
// If someone in that area has the bypass permission, allow the spawning
|
2019-11-17 01:23:37 +01:00
|
|
|
for (Entity entity : Objects.requireNonNull(l.getWorld()).getNearbyEntities(l, 5, 5, 5)) {
|
2019-06-23 08:41:30 +02:00
|
|
|
if (entity instanceof Player) {
|
|
|
|
Player player = (Player)entity;
|
2019-11-08 21:35:47 +01:00
|
|
|
if (player.isOp() || player.hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(l.getWorld()) + MOD_BYPASS)) {
|
2019-06-23 08:41:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* handles paintings and item frames
|
|
|
|
* @param e - event
|
|
|
|
*/
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
|
|
|
public void onBlock(HangingPlaceEvent e) {
|
|
|
|
Player player = e.getPlayer();
|
2019-11-17 01:23:37 +01:00
|
|
|
if (player == null) return;
|
2019-06-23 08:41:30 +02:00
|
|
|
addon.getIslands().getIslandAt(e.getEntity().getLocation()).ifPresent(island -> {
|
2019-11-17 01:23:37 +01:00
|
|
|
boolean bypass = Objects.requireNonNull(player).isOp() || player.hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(e.getEntity().getWorld()) + MOD_BYPASS);
|
2019-06-23 08:41:30 +02:00
|
|
|
// Check if entity can be hung
|
2019-11-08 21:35:47 +01:00
|
|
|
if (!bypass && !island.isSpawn() && atLimit(island, e.getEntity())) {
|
2019-06-23 08:41:30 +02:00
|
|
|
// Not allowed
|
|
|
|
e.setCancelled(true);
|
2019-08-08 06:20:08 +02:00
|
|
|
User.getInstance(player).sendMessage("block-limits.hit-limit", "[material]",
|
2019-06-23 08:41:30 +02:00
|
|
|
Util.prettifyText(e.getEntity().getType().toString()),
|
2019-11-08 21:35:47 +01:00
|
|
|
TextVariables.NUMBER, String.valueOf(addon.getSettings().getLimits().getOrDefault(e.getEntity().getType(), -1)));
|
2019-06-23 08:41:30 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-23 00:50:15 +02:00
|
|
|
private void checkLimit(CreatureSpawnEvent e, boolean bypass) {
|
|
|
|
addon.getIslands().getIslandAt(e.getLocation()).ifPresent(island -> {
|
|
|
|
// Check if creature is allowed to spawn or not
|
2019-11-08 21:35:47 +01:00
|
|
|
if (!bypass && !island.isSpawn() && atLimit(island, e.getEntity())) {
|
2019-04-23 00:50:15 +02:00
|
|
|
// Not allowed
|
|
|
|
e.setCancelled(true);
|
|
|
|
// If the reason is anything but because of a spawner then tell players within range
|
2019-06-27 17:34:01 +02:00
|
|
|
if (!e.getSpawnReason().equals(SpawnReason.SPAWNER) && !e.getSpawnReason().equals(SpawnReason.NATURAL) && !e.getSpawnReason().equals(SpawnReason.INFECTION) && !e.getSpawnReason().equals(SpawnReason.NETHER_PORTAL) && !e.getSpawnReason().equals(SpawnReason.REINFORCEMENTS) && !e.getSpawnReason().equals(SpawnReason.SLIME_SPLIT)) {
|
2019-11-17 01:23:37 +01:00
|
|
|
World w = e.getLocation().getWorld();
|
|
|
|
if (w == null) return;
|
|
|
|
for (Entity ent : w.getNearbyEntities(e.getLocation(), 5, 5, 5)) {
|
2019-04-23 00:50:15 +02:00
|
|
|
if (ent instanceof Player) {
|
2019-06-16 14:30:27 +02:00
|
|
|
User.getInstance(ent).sendMessage("entity-limits.hit-limit", "[entity]",
|
2019-04-23 00:50:15 +02:00
|
|
|
Util.prettifyText(e.getEntityType().toString()),
|
2019-11-08 21:35:47 +01:00
|
|
|
TextVariables.NUMBER, String.valueOf(addon.getSettings().getLimits().get(e.getEntityType())));
|
2019-04-23 00:50:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if new entities can be added to island
|
|
|
|
* @param island - island
|
|
|
|
* @param ent - the entity
|
|
|
|
* @return true if at the limit, false if not
|
|
|
|
*/
|
2019-11-08 21:35:47 +01:00
|
|
|
private boolean atLimit(Island island, Entity ent) {
|
2019-06-23 08:41:30 +02:00
|
|
|
long count = ent.getWorld().getEntities().stream()
|
2019-04-23 00:50:15 +02:00
|
|
|
.filter(e -> e.getType().equals(ent.getType()))
|
|
|
|
.filter(e -> island.inIslandSpace(e.getLocation())).count();
|
2020-01-09 23:42:37 +01:00
|
|
|
// Check island settings first
|
2020-01-11 18:38:45 +01:00
|
|
|
int limitAmount = -1;
|
|
|
|
if (addon.getBlockLimitListener().getIsland(island.getUniqueId()) != null) {
|
|
|
|
limitAmount = addon.getBlockLimitListener().getIsland(island.getUniqueId()).getEntityLimit(ent.getType());
|
|
|
|
}
|
2020-01-09 23:42:37 +01:00
|
|
|
// If no island settings then try global settings
|
|
|
|
if (limitAmount < 0 && addon.getSettings().getLimits().containsKey(ent.getType())) {
|
|
|
|
limitAmount = addon.getSettings().getLimits().get(ent.getType());
|
|
|
|
}
|
2020-01-11 18:38:45 +01:00
|
|
|
if (limitAmount == -1) return false;
|
2020-01-09 23:42:37 +01:00
|
|
|
return count >= limitAmount;
|
2019-04-23 00:50:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|