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-09-07 03:30:05 +02:00
|
|
|
import java.util.UUID;
|
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;
|
2020-09-20 18:07:35 +02:00
|
|
|
import org.bukkit.Tag;
|
2019-11-17 01:23:37 +01:00
|
|
|
import org.bukkit.World;
|
2020-09-12 03:43:36 +02:00
|
|
|
import org.bukkit.block.Block;
|
2020-09-07 03:30:05 +02:00
|
|
|
import org.bukkit.block.BlockFace;
|
2021-12-22 21:16:02 +01:00
|
|
|
import org.bukkit.entity.Animals;
|
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;
|
2020-09-11 04:19:49 +02:00
|
|
|
import org.bukkit.entity.LivingEntity;
|
2019-04-23 00:50:15 +02:00
|
|
|
import org.bukkit.entity.Player;
|
2020-09-11 04:19:49 +02:00
|
|
|
import org.bukkit.event.Cancellable;
|
2019-04-23 00:50:15 +02:00
|
|
|
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;
|
2021-12-22 18:19:00 +01:00
|
|
|
import org.bukkit.event.entity.EntityBreedEvent;
|
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-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;
|
2020-09-07 03:30:05 +02:00
|
|
|
private final List<UUID> justSpawned = new ArrayList<>();
|
2021-12-22 18:19:00 +01:00
|
|
|
private static final List<BlockFace> CARDINALS = List.of(BlockFace.UP, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST, BlockFace.DOWN);
|
2019-04-23 00:50:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2021-02-22 04:02:50 +01:00
|
|
|
if (!addon.inGameModeWorld(e.getVehicle().getWorld())) {
|
2019-04-23 00:50:15 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-12-22 18:19:00 +01:00
|
|
|
// Debounce
|
2020-09-07 03:30:05 +02:00
|
|
|
if (justSpawned.contains(e.getVehicle().getUniqueId())) {
|
|
|
|
justSpawned.remove(e.getVehicle().getUniqueId());
|
|
|
|
return;
|
|
|
|
}
|
2021-12-22 18:19:00 +01:00
|
|
|
// Check island
|
|
|
|
addon.getIslands().getProtectedIslandAt(e.getVehicle().getLocation())
|
|
|
|
// Ignore spawn
|
|
|
|
.filter(i -> !i.isSpawn())
|
|
|
|
.ifPresent(island -> {
|
|
|
|
// Check if the player is at the limit
|
|
|
|
AtLimitResult res = atLimit(island, e.getVehicle());
|
|
|
|
if (res.hit()) {
|
|
|
|
e.setCancelled(true);
|
|
|
|
this.tellPlayers(e.getVehicle().getLocation(), e.getVehicle(), SpawnReason.MOUNT, res);
|
2019-04-23 00:50:15 +02:00
|
|
|
}
|
2021-12-22 18:19:00 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
|
|
|
public void onBreed(final EntityBreedEvent e) {
|
2021-12-22 22:43:45 +01:00
|
|
|
if (addon.inGameModeWorld(e.getEntity().getWorld())
|
|
|
|
&& e.getBreeder() != null
|
|
|
|
&& e.getBreeder() instanceof Player p
|
|
|
|
&& !(p.isOp() || p.hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(e.getEntity().getWorld()) + MOD_BYPASS))
|
|
|
|
&& !checkLimit(e, e.getEntity(), SpawnReason.BREEDING, false)
|
|
|
|
&& e.getFather() instanceof Animals f && e.getMother() instanceof Animals m) {
|
|
|
|
f.setLoveModeTicks(0);
|
|
|
|
m.setLoveModeTicks(0);
|
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
|
2021-02-22 04:02:50 +01:00
|
|
|
if (!addon.inGameModeWorld(e.getLocation().getWorld())) {
|
2019-04-23 00:50:15 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-09-07 03:30:05 +02:00
|
|
|
if (justSpawned.contains(e.getEntity().getUniqueId())) {
|
|
|
|
justSpawned.remove(e.getEntity().getUniqueId());
|
|
|
|
return;
|
|
|
|
}
|
2021-12-22 22:43:45 +01:00
|
|
|
if (e.getSpawnReason().equals(SpawnReason.SHOULDER_ENTITY) || e.getSpawnReason().equals(SpawnReason.BREEDING)) {
|
2020-09-11 04:19:49 +02:00
|
|
|
// Special case - do nothing - jumping around spawns parrots as they drop off player's shoulder
|
2021-12-22 22:43:45 +01:00
|
|
|
// Ignore breeding because it's handled in the EntityBreedEvent listener
|
2020-09-11 04:19:49 +02:00
|
|
|
return;
|
2019-04-23 00:50:15 +02:00
|
|
|
}
|
2020-09-11 04:19:49 +02:00
|
|
|
// Some checks can be done async, some not
|
2021-12-22 18:19:00 +01:00
|
|
|
if (e.getSpawnReason().equals(SpawnReason.BUILD_SNOWMAN) || e.getSpawnReason().equals(SpawnReason.BUILD_IRONGOLEM)) {
|
|
|
|
checkLimit(e, e.getEntity(), e.getSpawnReason(), addon.getSettings().isAsyncGolums());
|
|
|
|
} else {
|
2020-09-20 18:07:35 +02:00
|
|
|
// Check limit sync
|
2021-12-22 18:19:00 +01:00
|
|
|
checkLimit(e, e.getEntity(), e.getSpawnReason(), false);
|
2020-09-11 04:19:49 +02:00
|
|
|
}
|
2020-09-12 03:43:36 +02:00
|
|
|
|
2019-04-23 00:50:15 +02:00
|
|
|
}
|
|
|
|
|
2019-06-23 08:41:30 +02:00
|
|
|
/**
|
|
|
|
* handles paintings and item frames
|
|
|
|
* @param e - event
|
|
|
|
*/
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
|
|
|
public void onBlock(HangingPlaceEvent e) {
|
2021-02-22 04:02:50 +01:00
|
|
|
if (!addon.inGameModeWorld(e.getBlock().getWorld())) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-23 08:41:30 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-11 04:19:49 +02:00
|
|
|
/**
|
|
|
|
* Check if a creature is allowed to spawn or not
|
|
|
|
* @param e - CreatureSpawnEvent
|
2020-09-18 03:54:02 +02:00
|
|
|
* @param async - true if check can be done async, false if not
|
2021-12-22 21:16:02 +01:00
|
|
|
* @return true if allowed or asycn, false if not.
|
2020-09-11 04:19:49 +02:00
|
|
|
*/
|
2021-12-22 21:16:02 +01:00
|
|
|
private boolean checkLimit(Cancellable c, LivingEntity e, SpawnReason reason, boolean async) {
|
2020-09-07 03:30:05 +02:00
|
|
|
Location l = e.getLocation();
|
2020-09-11 04:19:49 +02:00
|
|
|
if (async) {
|
|
|
|
c.setCancelled(true);
|
|
|
|
}
|
2021-12-22 21:16:02 +01:00
|
|
|
return processIsland(c, e, l, reason, async);
|
2020-09-11 04:19:49 +02:00
|
|
|
}
|
|
|
|
|
2021-12-22 21:16:02 +01:00
|
|
|
private boolean processIsland(Cancellable c, LivingEntity e, Location l, SpawnReason reason, boolean async) {
|
|
|
|
if (addon.getIslands().getIslandAt(e.getLocation()).isEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Island island = addon.getIslands().getIslandAt(e.getLocation()).get();
|
|
|
|
// Check if creature is allowed to spawn or not
|
|
|
|
AtLimitResult res = atLimit(island, e);
|
|
|
|
if (island.isSpawn() || !res.hit()) {
|
|
|
|
// Allowed
|
|
|
|
if (async) {
|
2021-12-22 23:37:10 +01:00
|
|
|
Bukkit.getScheduler().runTask(BentoBox.getInstance(), () -> l.getWorld().spawn(l, e.getClass(), entity -> preSpawn(entity, reason, l)));
|
2021-12-22 21:16:02 +01:00
|
|
|
} // else do nothing
|
|
|
|
} else {
|
|
|
|
if (async) {
|
|
|
|
e.remove();
|
2020-09-07 03:30:05 +02:00
|
|
|
} else {
|
2021-12-22 21:16:02 +01:00
|
|
|
c.setCancelled(true);
|
2020-07-26 23:35:05 +02:00
|
|
|
}
|
2021-12-22 21:16:02 +01:00
|
|
|
// If the reason is anything but because of a spawner then tell players within range
|
|
|
|
tellPlayers(l, e, reason, res);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2019-04-23 00:50:15 +02:00
|
|
|
}
|
|
|
|
|
2020-09-11 04:19:49 +02:00
|
|
|
private void preSpawn(Entity entity, SpawnReason reason, Location l) {
|
|
|
|
justSpawned.add(entity.getUniqueId());
|
|
|
|
// Check for entities that need cleanup
|
|
|
|
switch (reason) {
|
2021-12-22 23:37:10 +01:00
|
|
|
case BUILD_IRONGOLEM -> detectIronGolem(l);
|
|
|
|
case BUILD_SNOWMAN -> detectSnowman(l);
|
|
|
|
case BUILD_WITHER -> {
|
|
|
|
detectWither(l);
|
|
|
|
// Create explosion
|
|
|
|
l.getWorld().createExplosion(l, 7F, true, true, entity);
|
|
|
|
}
|
|
|
|
default -> {
|
|
|
|
}
|
2020-09-11 04:19:49 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-26 23:24:03 +02:00
|
|
|
|
2020-09-12 03:43:36 +02:00
|
|
|
private void detectIronGolem(Location l) {
|
2020-09-20 18:07:35 +02:00
|
|
|
Block legs = l.getBlock();
|
2020-09-12 03:43:36 +02:00
|
|
|
// Erase legs
|
2020-09-20 18:07:35 +02:00
|
|
|
addon.getBlockLimitListener().removeBlock(legs);
|
|
|
|
legs.setType(Material.AIR);
|
2020-09-12 03:43:36 +02:00
|
|
|
// Look around for possible constructions
|
|
|
|
for (BlockFace bf : CARDINALS) {
|
2020-09-20 18:07:35 +02:00
|
|
|
Block body = legs.getRelative(bf);
|
2020-09-12 03:43:36 +02:00
|
|
|
if (body.getType().equals(Material.IRON_BLOCK)) {
|
|
|
|
// Check for head
|
|
|
|
Block head = body.getRelative(bf);
|
|
|
|
if (head.getType().equals(Material.CARVED_PUMPKIN)) {
|
|
|
|
// Check for arms the rule is that they must be opposite and have nothing "beneath" them
|
|
|
|
for (BlockFace bf2 : CARDINALS) {
|
|
|
|
Block arm1 = body.getRelative(bf2);
|
|
|
|
Block arm2 = body.getRelative(bf2.getOppositeFace());
|
|
|
|
if (arm1.getType() == Material.IRON_BLOCK && arm2.getType() == Material.IRON_BLOCK
|
|
|
|
&& arm1.getRelative(bf.getOppositeFace()).isEmpty()
|
|
|
|
&& arm2.getRelative(bf.getOppositeFace()).isEmpty()) {
|
|
|
|
// Erase!
|
2020-09-20 18:07:35 +02:00
|
|
|
addon.getBlockLimitListener().removeBlock(body);
|
|
|
|
addon.getBlockLimitListener().removeBlock(arm1);
|
|
|
|
addon.getBlockLimitListener().removeBlock(arm2);
|
|
|
|
addon.getBlockLimitListener().removeBlock(head);
|
2020-09-12 03:43:36 +02:00
|
|
|
body.setType(Material.AIR);
|
|
|
|
arm1.setType(Material.AIR);
|
|
|
|
arm2.setType(Material.AIR);
|
|
|
|
head.setType(Material.AIR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void detectSnowman(Location l) {
|
2020-09-20 18:07:35 +02:00
|
|
|
Block legs = l.getBlock();
|
2020-09-12 03:43:36 +02:00
|
|
|
// Erase legs
|
2020-09-20 18:07:35 +02:00
|
|
|
addon.getBlockLimitListener().removeBlock(legs);
|
|
|
|
legs.setType(Material.AIR);
|
2020-09-12 03:43:36 +02:00
|
|
|
// Look around for possible constructions
|
|
|
|
for (BlockFace bf : CARDINALS) {
|
2020-09-20 18:07:35 +02:00
|
|
|
Block body = legs.getRelative(bf);
|
2020-09-12 03:43:36 +02:00
|
|
|
if (body.getType().equals(Material.SNOW_BLOCK)) {
|
|
|
|
// Check for head
|
|
|
|
Block head = body.getRelative(bf);
|
|
|
|
if (head.getType().equals(Material.CARVED_PUMPKIN)) {
|
|
|
|
// Erase
|
2020-09-20 18:07:35 +02:00
|
|
|
addon.getBlockLimitListener().removeBlock(body);
|
|
|
|
addon.getBlockLimitListener().removeBlock(head);
|
|
|
|
|
2020-09-12 03:43:36 +02:00
|
|
|
body.setType(Material.AIR);
|
|
|
|
head.setType(Material.AIR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void detectWither(Location l) {
|
2020-09-20 18:07:35 +02:00
|
|
|
Block legs = l.getBlock();
|
2020-09-12 03:43:36 +02:00
|
|
|
// Erase legs
|
2020-09-20 18:07:35 +02:00
|
|
|
addon.getBlockLimitListener().removeBlock(legs);
|
|
|
|
legs.setType(Material.AIR);
|
2020-09-12 03:43:36 +02:00
|
|
|
// Look around for possible constructions
|
|
|
|
for (BlockFace bf : CARDINALS) {
|
2020-09-20 18:07:35 +02:00
|
|
|
Block body = legs.getRelative(bf);
|
|
|
|
if (isWither(body)) {
|
2020-09-12 03:43:36 +02:00
|
|
|
// Check for head
|
|
|
|
Block head = body.getRelative(bf);
|
|
|
|
if (head.getType().equals(Material.WITHER_SKELETON_SKULL) || head.getType().equals(Material.WITHER_SKELETON_WALL_SKULL)) {
|
|
|
|
// Check for arms the rule is that they must be opposite and have nothing "beneath" them
|
|
|
|
for (BlockFace bf2 : CARDINALS) {
|
|
|
|
Block arm1 = body.getRelative(bf2);
|
|
|
|
Block arm2 = body.getRelative(bf2.getOppositeFace());
|
|
|
|
Block head2 = arm1.getRelative(bf);
|
|
|
|
Block head3 = arm2.getRelative(bf);
|
2020-09-20 18:07:35 +02:00
|
|
|
if (isWither(arm1)
|
|
|
|
&& isWither(arm2)
|
2020-09-12 03:43:36 +02:00
|
|
|
&& arm1.getRelative(bf.getOppositeFace()).isEmpty()
|
|
|
|
&& arm2.getRelative(bf.getOppositeFace()).isEmpty()
|
|
|
|
&& (head2.getType().equals(Material.WITHER_SKELETON_SKULL) || head2.getType().equals(Material.WITHER_SKELETON_WALL_SKULL))
|
|
|
|
&& (head3.getType().equals(Material.WITHER_SKELETON_SKULL) || head3.getType().equals(Material.WITHER_SKELETON_WALL_SKULL))
|
|
|
|
) {
|
|
|
|
// Erase!
|
2020-09-20 18:07:35 +02:00
|
|
|
addon.getBlockLimitListener().removeBlock(body);
|
|
|
|
addon.getBlockLimitListener().removeBlock(arm1);
|
|
|
|
addon.getBlockLimitListener().removeBlock(arm2);
|
|
|
|
addon.getBlockLimitListener().removeBlock(head);
|
|
|
|
addon.getBlockLimitListener().removeBlock(head2);
|
|
|
|
addon.getBlockLimitListener().removeBlock(head3);
|
2020-09-12 03:43:36 +02:00
|
|
|
body.setType(Material.AIR);
|
|
|
|
arm1.setType(Material.AIR);
|
|
|
|
arm2.setType(Material.AIR);
|
|
|
|
head.setType(Material.AIR);
|
|
|
|
head2.setType(Material.AIR);
|
|
|
|
head3.setType(Material.AIR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-20 18:07:35 +02:00
|
|
|
private boolean isWither(Block body) {
|
|
|
|
if (Util.getMinecraftVersion() < 16) {
|
|
|
|
return body.getType().equals(Material.SOUL_SAND);
|
|
|
|
}
|
|
|
|
return Tag.WITHER_SUMMON_BASE_BLOCKS.isTagged(body.getType());
|
|
|
|
}
|
|
|
|
|
2021-12-22 18:19:00 +01:00
|
|
|
/**
|
|
|
|
* Tell players within a 5 x 5 x 5 radius that the spawning was denied. Informing happens 1 tick after event
|
|
|
|
* @param l location
|
|
|
|
* @param entity entity spawned
|
|
|
|
* @param reason reason - some reasons are not reported
|
|
|
|
* @param res at limit result
|
|
|
|
*/
|
|
|
|
private void tellPlayers(Location l, Entity entity, SpawnReason reason, AtLimitResult res) {
|
|
|
|
if (reason.equals(SpawnReason.SPAWNER) || reason.equals(SpawnReason.NATURAL)
|
|
|
|
|| reason.equals(SpawnReason.INFECTION) || reason.equals(SpawnReason.NETHER_PORTAL)
|
|
|
|
|| reason.equals(SpawnReason.REINFORCEMENTS) || reason.equals(SpawnReason.SLIME_SPLIT)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
World w = l.getWorld();
|
|
|
|
if (w == null) return;
|
|
|
|
Bukkit.getScheduler().runTask(addon.getPlugin(), () -> {
|
|
|
|
for (Entity ent : w.getNearbyEntities(l, 5, 5, 5)) {
|
|
|
|
if (ent instanceof Player p) {
|
|
|
|
p.updateInventory();
|
|
|
|
if (res.getTypelimit() != null) {
|
|
|
|
User.getInstance(p).notify("entity-limits.hit-limit", "[entity]",
|
|
|
|
Util.prettifyText(entity.getType().toString()),
|
|
|
|
TextVariables.NUMBER, String.valueOf(res.getTypelimit().getValue()));
|
|
|
|
} else {
|
|
|
|
User.getInstance(p).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()));
|
2020-07-25 17:15:55 +02:00
|
|
|
}
|
|
|
|
}
|
2021-12-22 18:19:00 +01:00
|
|
|
}
|
|
|
|
});
|
2020-07-25 17:15:55 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
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-09-28 01:54:00 +02:00
|
|
|
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();
|
2021-09-07 02:11:02 +02:00
|
|
|
|
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-09-30 23:46:07 +02:00
|
|
|
int count = (int) ent.getWorld().getEntities().stream()
|
|
|
|
.filter(e -> e.getType().equals(ent.getType()))
|
2020-07-25 17:15:55 +02:00
|
|
|
.filter(e -> island.inIslandSpace(e.getLocation()))
|
|
|
|
.count();
|
2021-09-07 02:11:02 +02:00
|
|
|
if (count >= limitAmount) {
|
2020-04-12 14:39:19 +02:00
|
|
|
return new AtLimitResult(ent.getType(), limitAmount);
|
2021-09-07 02:11:02 +02:00
|
|
|
}
|
2020-04-12 14:39:19 +02:00
|
|
|
}
|
2020-11-24 15:43:37 +01:00
|
|
|
// Merge in any permission-based limits
|
|
|
|
if (addon.getBlockLimitListener().getIsland(island.getUniqueId()) != null) {
|
2021-12-22 23:37:10 +01:00
|
|
|
Map<String, EntityGroup> groupbyname = groupsLimits.keySet().stream().collect(Collectors.toMap(EntityGroup::getName, e -> e));
|
2020-11-24 15:43:37 +01:00
|
|
|
addon.getBlockLimitListener().getIsland(island.getUniqueId()).getEntityGroupLimits().entrySet().stream()
|
2020-12-22 18:40:09 +01:00
|
|
|
.filter(e -> groupbyname.containsKey(e.getKey()))
|
|
|
|
.forEach(e -> groupsLimits.put(groupbyname.get(e.getKey()), e.getValue()));
|
2020-11-24 15:43:37 +01: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-09-07 03:30:05 +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
|
|
|
|
2021-12-22 23:37:10 +01:00
|
|
|
static class AtLimitResult {
|
2020-04-11 20:59:54 +02:00
|
|
|
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-09-07 03:30:05 +02:00
|
|
|
/**
|
|
|
|
* @return true if at limit
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|