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-09-12 03:43:36 +02:00
|
|
|
import java.util.Collections;
|
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;
|
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;
|
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<>();
|
2020-09-12 03:43:36 +02:00
|
|
|
private static final List<BlockFace> CARDINALS;
|
|
|
|
static {
|
|
|
|
List<BlockFace> cardinals = new ArrayList<>();
|
|
|
|
cardinals.add(BlockFace.UP);
|
|
|
|
cardinals.add(BlockFace.NORTH);
|
|
|
|
cardinals.add(BlockFace.SOUTH);
|
|
|
|
cardinals.add(BlockFace.EAST);
|
|
|
|
cardinals.add(BlockFace.WEST);
|
|
|
|
cardinals.add(BlockFace.DOWN);
|
|
|
|
CARDINALS = Collections.unmodifiableList(cardinals);
|
|
|
|
}
|
2019-04-23 00:50:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles entity and natural limitations
|
|
|
|
* @param addon - Limits object
|
|
|
|
*/
|
|
|
|
public EntityLimitListener(Limits addon) {
|
|
|
|
this.addon = addon;
|
2020-09-07 03:30:05 +02:00
|
|
|
justSpawned.clear();
|
2019-04-23 00:50:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-09-07 03:30:05 +02:00
|
|
|
if (justSpawned.contains(e.getVehicle().getUniqueId())) {
|
|
|
|
justSpawned.remove(e.getVehicle().getUniqueId());
|
|
|
|
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;
|
|
|
|
}
|
2020-09-07 03:30:05 +02:00
|
|
|
if (justSpawned.contains(e.getEntity().getUniqueId())) {
|
|
|
|
justSpawned.remove(e.getEntity().getUniqueId());
|
|
|
|
return;
|
|
|
|
}
|
2019-04-23 00:50:15 +02:00
|
|
|
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;
|
2020-09-11 04:19:49 +02:00
|
|
|
case SHOULDER_ENTITY:
|
|
|
|
// Special case - do nothing - jumping around spawns parrots as they drop off player's shoulder
|
|
|
|
return;
|
2019-04-23 00:50:15 +02:00
|
|
|
default:
|
|
|
|
// Other natural reasons
|
|
|
|
break;
|
|
|
|
}
|
2020-09-11 04:19:49 +02:00
|
|
|
// Some checks can be done async, some not
|
|
|
|
switch (e.getSpawnReason()) {
|
2020-09-20 18:07:35 +02:00
|
|
|
case BUILD_WITHER:
|
|
|
|
case BUILD_SNOWMAN:
|
|
|
|
case BUILD_IRONGOLEM:
|
|
|
|
checkLimit(e, e.getEntity(), e.getSpawnReason(), bypass, true);
|
2020-09-11 04:19:49 +02:00
|
|
|
default:
|
2020-09-20 18:07:35 +02:00
|
|
|
// Check limit sync
|
2020-09-18 03:54:02 +02:00
|
|
|
checkLimit(e, e.getEntity(), e.getSpawnReason(), bypass, false);
|
2020-09-11 04:19:49 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
2020-09-12 03:43:36 +02:00
|
|
|
|
2019-04-23 00:50:15 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-11 04:19:49 +02:00
|
|
|
/**
|
|
|
|
* Check if a creature is allowed to spawn or not
|
|
|
|
* @param e - CreatureSpawnEvent
|
|
|
|
* @param bypass - true if the player involved can bypass the checks
|
2020-09-18 03:54:02 +02:00
|
|
|
* @param async - true if check can be done async, false if not
|
2020-09-11 04:19:49 +02:00
|
|
|
*/
|
|
|
|
private void checkLimit(Cancellable c, LivingEntity e, SpawnReason reason, boolean bypass, 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);
|
|
|
|
Bukkit.getScheduler().runTaskAsynchronously(BentoBox.getInstance(), () -> processIsland(c, e, l, reason, bypass, async));
|
|
|
|
} else {
|
|
|
|
processIsland(c, e, l, reason, bypass, async);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void processIsland(Cancellable c, LivingEntity e, Location l, SpawnReason reason, boolean bypass, boolean async) {
|
2020-07-26 23:35:05 +02:00
|
|
|
addon.getIslands().getIslandAt(e.getLocation()).ifPresent(island -> {
|
|
|
|
// Check if creature is allowed to spawn or not
|
2020-09-11 04:19:49 +02:00
|
|
|
AtLimitResult res = atLimit(island, e);
|
2020-09-07 03:30:05 +02:00
|
|
|
|
|
|
|
if (bypass || island.isSpawn() || !res.hit()) {
|
|
|
|
// Allowed
|
2020-09-11 04:19:49 +02:00
|
|
|
if (async) {
|
|
|
|
Bukkit.getScheduler().runTask(BentoBox.getInstance(), () -> {
|
|
|
|
l.getWorld().spawn(l, e.getClass(), entity -> preSpawn(entity, reason, l));
|
2020-09-07 03:30:05 +02:00
|
|
|
});
|
2020-09-11 04:19:49 +02:00
|
|
|
} // else do nothing
|
2020-09-07 03:30:05 +02:00
|
|
|
} else {
|
2020-09-11 04:19:49 +02:00
|
|
|
if (async) {
|
|
|
|
e.remove();
|
|
|
|
} else {
|
|
|
|
c.setCancelled(true);
|
|
|
|
}
|
2020-09-07 03:30:05 +02:00
|
|
|
// If the reason is anything but because of a spawner then tell players within range
|
2020-09-11 04:19:49 +02:00
|
|
|
tellPlayers(c, l, e, reason, res);
|
2020-07-26 23:35:05 +02:00
|
|
|
}
|
2019-04-23 00:50:15 +02:00
|
|
|
|
2020-09-11 04:19:49 +02:00
|
|
|
});
|
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) {
|
|
|
|
case BUILD_IRONGOLEM:
|
2020-09-12 03:43:36 +02:00
|
|
|
detectIronGolem(l);
|
2020-09-11 04:19:49 +02:00
|
|
|
break;
|
|
|
|
case BUILD_SNOWMAN:
|
2020-09-12 03:43:36 +02:00
|
|
|
detectSnowman(l);
|
2020-09-11 04:19:49 +02:00
|
|
|
break;
|
|
|
|
case BUILD_WITHER:
|
2020-09-12 03:43:36 +02:00
|
|
|
detectWither(l);
|
2020-09-11 04:19:49 +02:00
|
|
|
// Create explosion
|
|
|
|
l.getWorld().createExplosion(l, 7F, true, true, entity);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
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());
|
|
|
|
}
|
|
|
|
|
2020-09-11 04:19:49 +02:00
|
|
|
private void tellPlayers(Cancellable e, Location l, LivingEntity 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)) {
|
|
|
|
World w = l.getWorld();
|
2020-07-25 17:15:55 +02:00
|
|
|
if (w == null) return;
|
2020-09-07 03:30:05 +02:00
|
|
|
Bukkit.getScheduler().runTask(addon.getPlugin(), () -> {
|
2020-09-11 04:19:49 +02:00
|
|
|
for (Entity ent : w.getNearbyEntities(l, 5, 5, 5)) {
|
2020-09-07 03:30:05 +02:00
|
|
|
if (ent instanceof Player) {
|
|
|
|
if (res.getTypelimit() != null) {
|
|
|
|
User.getInstance(ent).notify("entity-limits.hit-limit", "[entity]",
|
2020-09-11 04:19:49 +02:00
|
|
|
Util.prettifyText(entity.getType().toString()),
|
2020-09-07 03:30:05 +02:00
|
|
|
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()));
|
|
|
|
}
|
2020-07-25 17:15:55 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-07 03:30:05 +02: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();
|
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-09-07 03:30:05 +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-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
|
|
|
|
2020-09-28 01:54:00 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|