2019-12-16 16:47:51 +01:00
|
|
|
package world.bentobox.limits.listeners;
|
2019-04-23 00:50:15 +02:00
|
|
|
|
2020-04-11 20:59:54 +02:00
|
|
|
import java.util.AbstractMap;
|
2020-04-10 15:01:37 +02:00
|
|
|
import java.util.ArrayList;
|
2020-04-11 13:53:28 +02:00
|
|
|
import java.util.HashMap;
|
2020-04-10 15:01:37 +02:00
|
|
|
import java.util.List;
|
2020-04-11 13:53:28 +02:00
|
|
|
import java.util.Map;
|
2019-11-17 01:23:37 +01:00
|
|
|
import java.util.Objects;
|
2020-04-11 13:53:28 +02:00
|
|
|
import java.util.stream.Collectors;
|
2019-11-17 01:23:37 +01:00
|
|
|
|
2020-07-25 17:15:55 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2019-06-23 08:41:30 +02:00
|
|
|
import org.bukkit.Location;
|
2020-07-26 23:24:03 +02:00
|
|
|
import org.bukkit.Material;
|
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;
|
2020-04-11 20:59:54 +02:00
|
|
|
import org.bukkit.entity.EntityType;
|
2019-04-23 00:50:15 +02:00
|
|
|
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;
|
2020-07-26 23:24:03 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2019-04-23 00:50:15 +02:00
|
|
|
|
2020-07-25 17:15:55 +02:00
|
|
|
import world.bentobox.bentobox.BentoBox;
|
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;
|
2020-04-10 15:01:37 +02:00
|
|
|
import world.bentobox.limits.Settings;
|
2020-04-11 20:59:54 +02:00
|
|
|
import world.bentobox.limits.Settings.EntityGroup;
|
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
|
2020-04-11 20:59:54 +02:00
|
|
|
AtLimitResult res;
|
|
|
|
if (!bypass && (res = atLimit(island, e.getVehicle())).hit()) {
|
2020-01-11 01:08:21 +01:00
|
|
|
e.setCancelled(true);
|
|
|
|
for (Entity ent : e.getVehicle().getLocation().getWorld().getNearbyEntities(e.getVehicle().getLocation(), 5, 5, 5)) {
|
|
|
|
if (ent instanceof Player) {
|
|
|
|
((Player) ent).updateInventory();
|
2020-04-11 20:59:54 +02:00
|
|
|
if (res.getTypelimit() != null) {
|
|
|
|
User.getInstance(ent).notify("entity-limits.hit-limit", "[entity]",
|
|
|
|
Util.prettifyText(e.getVehicle().getType().toString()),
|
|
|
|
TextVariables.NUMBER, String.valueOf(res.getTypelimit().getValue()));
|
|
|
|
} else {
|
|
|
|
User.getInstance(ent).notify("entity-limits.hit-limit", "[entity]",
|
|
|
|
res.getGrouplimit().getKey().getName() + " (" + res.getGrouplimit().getKey().getTypes().stream().map(x -> Util.prettifyText(x.toString())).collect(Collectors.joining(", ")) + ")",
|
|
|
|
TextVariables.NUMBER, String.valueOf(res.getGrouplimit().getValue()));
|
|
|
|
}
|
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
|
2020-04-11 20:59:54 +02:00
|
|
|
AtLimitResult res;
|
|
|
|
if (!bypass && !island.isSpawn() && (res = atLimit(island, e.getEntity())).hit()) {
|
2019-06-23 08:41:30 +02:00
|
|
|
// Not allowed
|
|
|
|
e.setCancelled(true);
|
2020-04-11 20:59:54 +02:00
|
|
|
if (res.getTypelimit() != null) {
|
|
|
|
User.getInstance(player).notify("block-limits.hit-limit", "[material]",
|
|
|
|
Util.prettifyText(e.getEntity().getType().toString()),
|
|
|
|
TextVariables.NUMBER, String.valueOf(res.getTypelimit().getValue()));
|
|
|
|
} else {
|
|
|
|
User.getInstance(player).notify("block-limits.hit-limit", "[material]",
|
|
|
|
res.getGrouplimit().getKey().getName() + " (" + res.getGrouplimit().getKey().getTypes().stream().map(x -> Util.prettifyText(x.toString())).collect(Collectors.joining(", ")) + ")",
|
|
|
|
TextVariables.NUMBER, String.valueOf(res.getGrouplimit().getValue()));
|
|
|
|
}
|
2019-06-23 08:41:30 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-23 00:50:15 +02:00
|
|
|
private void checkLimit(CreatureSpawnEvent e, boolean bypass) {
|
2020-07-26 23:35:05 +02:00
|
|
|
Bukkit.getScheduler().runTaskAsynchronously(BentoBox.getInstance(), () ->
|
|
|
|
addon.getIslands().getIslandAt(e.getLocation()).ifPresent(island -> {
|
|
|
|
// Check if creature is allowed to spawn or not
|
|
|
|
AtLimitResult res;
|
|
|
|
if (!bypass && !island.isSpawn() && (res = atLimit(island, e.getEntity())).hit()) {
|
|
|
|
// Not allowed
|
|
|
|
Bukkit.getScheduler().runTask(BentoBox.getInstance(), () -> {
|
|
|
|
e.getEntity().remove();
|
|
|
|
// If the entity was build, drop the building materials
|
|
|
|
Bukkit.getScheduler().runTask(addon.getPlugin(), () -> replaceEntity(e));
|
|
|
|
// If the reason is anything but because of a spawner then tell players within range
|
|
|
|
tellPlayers(e, res);
|
|
|
|
});
|
|
|
|
}
|
2019-04-23 00:50:15 +02:00
|
|
|
|
2020-07-26 23:35:05 +02:00
|
|
|
}));
|
2019-04-23 00:50:15 +02:00
|
|
|
}
|
|
|
|
|
2020-07-26 23:24:03 +02:00
|
|
|
private void replaceEntity(CreatureSpawnEvent e) {
|
|
|
|
World world = e.getEntity().getWorld();
|
|
|
|
Location l = e.getLocation();
|
|
|
|
switch (e.getSpawnReason()) {
|
|
|
|
case BUILD_IRONGOLEM:
|
|
|
|
world.dropItem(l, new ItemStack(Material.IRON_BLOCK,3));
|
|
|
|
world.dropItem(l, new ItemStack(Material.CARVED_PUMPKIN));
|
|
|
|
break;
|
|
|
|
case BUILD_SNOWMAN:
|
|
|
|
world.dropItem(l, new ItemStack(Material.SNOW_BLOCK,2));
|
|
|
|
world.dropItem(l, new ItemStack(Material.CARVED_PUMPKIN));
|
|
|
|
l.getBlock().setType(Material.AIR);
|
|
|
|
break;
|
|
|
|
case BUILD_WITHER:
|
|
|
|
world.dropItem(l, new ItemStack(Material.SOUL_SAND,3));
|
|
|
|
world.dropItem(l, new ItemStack(Material.WITHER_SKELETON_SKULL, 3));
|
|
|
|
break;
|
|
|
|
case CURED:
|
|
|
|
world.spawnEntity(e.getLocation(), EntityType.ZOMBIE_VILLAGER);
|
|
|
|
break;
|
|
|
|
case DISPENSE_EGG:
|
|
|
|
break;
|
|
|
|
case EGG:
|
|
|
|
world.dropItem(l, new ItemStack(Material.EGG));
|
|
|
|
break;
|
|
|
|
case SPAWNER_EGG:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-25 17:15:55 +02:00
|
|
|
private void tellPlayers(CreatureSpawnEvent e, AtLimitResult res) {
|
|
|
|
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)) {
|
|
|
|
World w = e.getLocation().getWorld();
|
|
|
|
if (w == null) return;
|
|
|
|
for (Entity ent : w.getNearbyEntities(e.getLocation(), 5, 5, 5)) {
|
|
|
|
if (ent instanceof Player) {
|
|
|
|
if (res.getTypelimit() != null) {
|
|
|
|
User.getInstance(ent).notify("entity-limits.hit-limit", "[entity]",
|
|
|
|
Util.prettifyText(e.getEntityType().toString()),
|
|
|
|
TextVariables.NUMBER, String.valueOf(res.getTypelimit().getValue()));
|
|
|
|
} else {
|
|
|
|
User.getInstance(ent).notify("entity-limits.hit-limit", "[entity]",
|
|
|
|
res.getGrouplimit().getKey().getName() + " (" + res.getGrouplimit().getKey().getTypes().stream().map(x -> Util.prettifyText(x.toString())).collect(Collectors.joining(", ")) + ")",
|
|
|
|
TextVariables.NUMBER, String.valueOf(res.getGrouplimit().getValue()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2020-04-11 20:59:54 +02:00
|
|
|
private AtLimitResult atLimit(Island island, Entity ent) {
|
2020-01-09 23:42:37 +01:00
|
|
|
// Check island settings first
|
2020-01-11 18:38:45 +01:00
|
|
|
int limitAmount = -1;
|
2020-04-11 13:53:28 +02:00
|
|
|
Map<Settings.EntityGroup, Integer> groupsLimits = new HashMap<>();
|
2020-01-11 18:38:45 +01:00
|
|
|
if (addon.getBlockLimitListener().getIsland(island.getUniqueId()) != null) {
|
|
|
|
limitAmount = addon.getBlockLimitListener().getIsland(island.getUniqueId()).getEntityLimit(ent.getType());
|
2020-06-27 01:22:18 +02:00
|
|
|
List<Settings.EntityGroup> groupdefs = addon.getSettings().getGroupLimits().getOrDefault(ent.getType(), new ArrayList<>());
|
2020-04-11 13:53:28 +02:00
|
|
|
groupdefs.forEach(def -> {
|
|
|
|
int limit = addon.getBlockLimitListener().getIsland(island.getUniqueId()).getEntityGroupLimit(def.getName());
|
|
|
|
if (limit >= 0)
|
|
|
|
groupsLimits.put(def, limit);
|
|
|
|
});
|
2020-01-11 18:38:45 +01:00
|
|
|
}
|
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-04-11 13:53:28 +02:00
|
|
|
if (addon.getSettings().getGroupLimits().containsKey(ent.getType())) {
|
|
|
|
addon.getSettings().getGroupLimits().getOrDefault(ent.getType(), new ArrayList<>()).stream()
|
2020-06-27 01:22:18 +02:00
|
|
|
.filter(group -> !groupsLimits.containsKey(group) || groupsLimits.get(group) > group.getLimit())
|
|
|
|
.forEach(group -> groupsLimits.put(group, group.getLimit()));
|
2020-04-10 15:01:37 +02:00
|
|
|
}
|
2020-04-11 20:59:54 +02:00
|
|
|
if (limitAmount < 0 && groupsLimits.isEmpty()) return new AtLimitResult();
|
2020-03-26 05:00:37 +01:00
|
|
|
// We have to count the entities
|
2020-04-12 14:39:19 +02:00
|
|
|
if (limitAmount >= 0)
|
|
|
|
{
|
2020-07-25 17:15:55 +02:00
|
|
|
int count = (int) ent.getWorld().getEntitiesByClasses(ent.getClass()).stream()
|
|
|
|
.filter(e -> island.inIslandSpace(e.getLocation()))
|
|
|
|
.count();
|
2020-07-26 23:24:03 +02:00
|
|
|
if (count > limitAmount)
|
2020-04-12 14:39:19 +02:00
|
|
|
return new AtLimitResult(ent.getType(), limitAmount);
|
|
|
|
}
|
2020-06-27 01:22:18 +02:00
|
|
|
|
2020-04-10 15:01:37 +02:00
|
|
|
// Now do the group limits
|
2020-04-11 13:53:28 +02:00
|
|
|
for (Map.Entry<Settings.EntityGroup, Integer> group : groupsLimits.entrySet()) { //do not use lambda
|
2020-04-12 13:38:21 +02:00
|
|
|
if (group.getValue() < 0)
|
|
|
|
continue;
|
2020-04-12 14:39:19 +02:00
|
|
|
int count = (int) ent.getWorld().getEntities().stream()
|
2020-04-11 13:53:28 +02:00
|
|
|
.filter(e -> group.getKey().contains(e.getType()))
|
2020-04-10 15:01:37 +02:00
|
|
|
.filter(e -> island.inIslandSpace(e.getLocation())).count();
|
2020-07-26 23:24:03 +02:00
|
|
|
if (count > group.getValue())
|
2020-04-11 20:59:54 +02:00
|
|
|
return new AtLimitResult(group.getKey(), group.getValue());
|
|
|
|
}
|
|
|
|
return new AtLimitResult();
|
|
|
|
}
|
2020-06-27 01:22:18 +02:00
|
|
|
|
2020-04-11 20:59:54 +02:00
|
|
|
private class AtLimitResult {
|
|
|
|
private Map.Entry<EntityType, Integer> typelimit;
|
|
|
|
private Map.Entry<EntityGroup, Integer> grouplimit;
|
|
|
|
|
|
|
|
public AtLimitResult() {}
|
2020-06-27 01:22:18 +02:00
|
|
|
|
2020-04-11 20:59:54 +02:00
|
|
|
public AtLimitResult(EntityType type, int limit) {
|
|
|
|
typelimit = new AbstractMap.SimpleEntry<>(type, limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
public AtLimitResult(EntityGroup type, int limit) {
|
|
|
|
grouplimit = new AbstractMap.SimpleEntry<>(type, limit);
|
|
|
|
}
|
2020-06-27 01:22:18 +02:00
|
|
|
|
2020-04-11 20:59:54 +02:00
|
|
|
public boolean hit() {
|
|
|
|
return typelimit != null || grouplimit != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map.Entry<EntityType, Integer> getTypelimit() {
|
|
|
|
return typelimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map.Entry<EntityGroup, Integer> getGrouplimit() {
|
|
|
|
return grouplimit;
|
2020-04-10 15:01:37 +02:00
|
|
|
}
|
2019-04-23 00:50:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|