Skip to content

Commit

Permalink
Adds entity limits
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Apr 22, 2019
1 parent 5326f90 commit ffeb4a7
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 260 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>world.bentobox</groupId>
<artifactId>limits</artifactId>
<version>0.1.1-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
<name>addon-limits</name>
<description>An add-on for BentoBox that limits blocks and entities on islands.</description>
<url>https://github.com/BentoBoxWorld/addon-level</url>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/bentobox/addon/limits/Limits.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import bentobox.addon.limits.commands.AdminCommand;
import bentobox.addon.limits.commands.PlayerCommand;
import bentobox.addon.limits.listeners.BlockLimitsListener;
import bentobox.addon.limits.listeners.EntityLimitListener;
import bentobox.addon.limits.listeners.JoinListener;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.addons.GameModeAddon;
Expand Down Expand Up @@ -53,6 +54,7 @@ public void onEnable() {
blockLimitListener = new BlockLimitsListener(this);
registerListener(blockLimitListener);
registerListener(new JoinListener(this));
registerListener(new EntityLimitListener(this));
// Done
}

Expand Down
12 changes: 11 additions & 1 deletion src/main/java/bentobox/addon/limits/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.EntityType;

import bentobox.addon.limits.commands.LimitPanel;

public class Settings {

private final Map<EntityType, Integer> limits = new HashMap<>();
Expand All @@ -23,10 +25,18 @@ public Settings(Limits addon) {
for (String key : el.getKeys(false)) {
EntityType type = getType(key);
if (type != null) {
limits.put(type, el.getInt(key, 0));
if (!type.isSpawnable() || (LimitPanel.E2M.containsKey(type) && LimitPanel.E2M.get(type) == null)) {
addon.logError("Entity type: " + key + " is not supported - skipping...");
} else {
limits.put(type, el.getInt(key, 0));
}
} else {
addon.logError("Unknown entity type: " + key + " - skipping...");
}
}
}
addon.log("Entity limits:");
limits.entrySet().stream().map(e -> "Limit " + e.getKey().toString() + " to " + e.getValue()).forEach(addon::log);
}

private EntityType getType(String key) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package bentobox.addon.limits.commands;

import bentobox.addon.limits.Limits;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import bentobox.addon.limits.Limits;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/bentobox/addon/limits/commands/LimitPanel.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package bentobox.addon.limits.commands;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.EntityType;

import bentobox.addon.limits.Limits;
import bentobox.addon.limits.objects.IslandBlockCount;
Expand All @@ -24,6 +26,53 @@
public class LimitPanel {

private final Limits addon;
public final static Map<EntityType, Material> E2M = new HashMap<>();
static {
E2M.put(EntityType.PIG_ZOMBIE, Material.ZOMBIE_PIGMAN_SPAWN_EGG);
E2M.put(EntityType.SNOWMAN, Material.SNOW_BLOCK);
E2M.put(EntityType.IRON_GOLEM, Material.IRON_BLOCK);
E2M.put(EntityType.ILLUSIONER, Material.VILLAGER_SPAWN_EGG);
E2M.put(EntityType.WITHER, Material.WITHER_SKELETON_SKULL);
E2M.put(EntityType.BOAT, Material.OAK_BOAT);
E2M.put(EntityType.ARMOR_STAND, Material.ARMOR_STAND);

// Minecarts
E2M.put(EntityType.MINECART_TNT, Material.TNT_MINECART);
E2M.put(EntityType.MINECART_CHEST, Material.CHEST_MINECART);
E2M.put(EntityType.MINECART_COMMAND, Material.COMMAND_BLOCK_MINECART);
E2M.put(EntityType.MINECART_FURNACE, Material.FURNACE_MINECART);
E2M.put(EntityType.MINECART_HOPPER, Material.HOPPER_MINECART);
E2M.put(EntityType.MINECART_MOB_SPAWNER, Material.MINECART);
E2M.put(EntityType.MINECART_TNT, Material.TNT_MINECART);
// Disallowed
E2M.put(EntityType.PRIMED_TNT, null);
E2M.put(EntityType.EVOKER_FANGS, null);
E2M.put(EntityType.LLAMA_SPIT, null);
E2M.put(EntityType.DRAGON_FIREBALL, null);
E2M.put(EntityType.AREA_EFFECT_CLOUD, null);
E2M.put(EntityType.ENDER_SIGNAL, null);
E2M.put(EntityType.SMALL_FIREBALL, null);
E2M.put(EntityType.DRAGON_FIREBALL, null);
E2M.put(EntityType.FIREBALL, null);
E2M.put(EntityType.THROWN_EXP_BOTTLE, null);
E2M.put(EntityType.EXPERIENCE_ORB, null);
E2M.put(EntityType.SHULKER_BULLET, null);
E2M.put(EntityType.WITHER_SKULL, null);
E2M.put(EntityType.TRIDENT, null);
E2M.put(EntityType.ARROW, null);
E2M.put(EntityType.SPECTRAL_ARROW, null);
E2M.put(EntityType.TIPPED_ARROW, null);
E2M.put(EntityType.SNOWBALL, null);
E2M.put(EntityType.EGG, null);
E2M.put(EntityType.LEASH_HITCH, null);
E2M.put(EntityType.ITEM_FRAME, null);
E2M.put(EntityType.PAINTING, null);
E2M.put(EntityType.GIANT, null);
E2M.put(EntityType.ENDER_CRYSTAL, null);
E2M.put(EntityType.ENDER_PEARL, null);
E2M.put(EntityType.ENDER_DRAGON, null);

}

/**
* @param addon - limit addon
Expand Down Expand Up @@ -67,7 +116,36 @@ public void showLimits(World world, User user, UUID target) {
"[limit]", String.valueOf(en.getValue())));
pb.item(pib.build());
}
addon.getSettings().getLimits().forEach((k,v) -> {
PanelItemBuilder pib = new PanelItemBuilder();
pib.name(Util.prettifyText(k.toString()));
Material m = Material.BARRIER;
try {
if (E2M.containsKey(k)) {
m = E2M.get(k);
} else if (k.isAlive()) {
m = Material.valueOf(k.toString() + "_SPAWN_EGG");
} else {
m = Material.valueOf(k.toString());
}
} catch (Exception e) {
m = Material.BARRIER;
}
pib.icon(m);
long count = getCount(island, k);
String color = count >= v ? user.getTranslation("island.limits.max-color") : user.getTranslation("island.limits.regular-color");
pib.description(color
+ user.getTranslation("island.limits.block-limit-syntax",
TextVariables.NUMBER, String.valueOf(count),
"[limit]", String.valueOf(v)));
pb.item(pib.build());
});
pb.build();
}

private long getCount(Island island, EntityType ent) {
return island.getWorld().getEntities().stream()
.filter(e -> e.getType().equals(ent))
.filter(e -> island.inIslandSpace(e.getLocation())).count();
}
}
145 changes: 145 additions & 0 deletions src/main/java/bentobox/addon/limits/listeners/EntityLimitListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package bentobox.addon.limits.listeners;

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;
import org.bukkit.event.vehicle.VehicleCreateEvent;

import bentobox.addon.limits.Limits;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;

public class EntityLimitListener implements Listener {
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;
}
if (addon.getSettings().getLimits().containsKey(e.getVehicle().getType())) {
// If someone in that area has the bypass permission, allow the spawning
for (Entity entity : 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 (atLimit(island, bypass, 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("limits.hit-limit", "[material]",
Util.prettifyText(e.getVehicle().getType().toString())
,"[number]", String.valueOf(addon.getSettings().getLimits().get(e.getVehicle().getType())));
}
}
}
});
}
}
}
}

@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;
}
if (!addon.getSettings().getLimits().containsKey(e.getEntityType())) {
// Unknown entity limit or unlimited
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:
// If someone in that area has the bypass permission, allow the spawning
for (Entity entity : e.getLocation().getWorld().getNearbyEntities(e.getLocation(), 5, 5, 5)) {
if (entity instanceof Player) {
Player player = (Player)entity;
if (player.isOp() || player.hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(e.getEntity().getWorld()) + "mod.bypass")) {
bypass = true;
break;
}
}
}
break;
default:
// Other natural reasons
break;
}
// Tag the entity with the island spawn location
checkLimit(e, bypass);

}

private void checkLimit(CreatureSpawnEvent e, boolean bypass) {
addon.getIslands().getIslandAt(e.getLocation()).ifPresent(island -> {
// Check if creature is allowed to spawn or not
if (!island.isSpawn() && atLimit(island, bypass, e.getEntity())) {
// Not allowed
e.setCancelled(true);
// If the reason is anything but because of a spawner then tell players within range
if (!e.getSpawnReason().equals(SpawnReason.SPAWNER)) {
for (Entity ent : e.getLocation().getWorld().getNearbyEntities(e.getLocation(), 5, 5, 5)) {
if (ent instanceof Player) {
User.getInstance(ent).sendMessage("limits.hit-limit", "[material]",
Util.prettifyText(e.getEntityType().toString()),
"[number]", String.valueOf(addon.getSettings().getLimits().get(e.getEntityType())));
}
}
}

}
});

}

/**
* Checks if new entities can be added to island
* @param island - island
* @param bypass - true if this is being done by a player with authorization to bypass limits
* @param ent - the entity
* @return true if at the limit, false if not
*/
private boolean atLimit(Island island, boolean bypass, Entity ent) {
return addon.getSettings().getLimits().getOrDefault(ent.getType(), -1) <= ent.getWorld().getEntities().stream()
.filter(e -> e.getType().equals(ent.getType()))
.filter(e -> island.inIslandSpace(e.getLocation())).count();
}
}


0 comments on commit ffeb4a7

Please sign in to comment.