Skip to content

Commit

Permalink
Adds support for item frames and paintings as entities
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jun 23, 2019
1 parent effd32a commit 230aa62
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>world.bentobox</groupId>
<artifactId>limits</artifactId>
<version>0.2.2</version>
<version>0.2.3-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
4 changes: 3 additions & 1 deletion src/main/java/bentobox/addon/limits/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public Settings(Limits addon) {
for (String key : el.getKeys(false)) {
EntityType type = getType(key);
if (type != null) {
if (!type.isSpawnable() || (LimitPanel.E2M.containsKey(type) && LimitPanel.E2M.get(type) == null)) {
if (!type.equals(EntityType.PAINTING) &&
!type.equals(EntityType.ITEM_FRAME) &&
(!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));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package bentobox.addon.limits.listeners;

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

import bentobox.addon.limits.Limits;
Expand Down Expand Up @@ -86,16 +88,7 @@ public void onCreatureSpawn(final CreatureSpawnEvent e) {
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;
}
}
}
bypass = checkByPass(e.getLocation());
break;
default:
// Other natural reasons
Expand All @@ -106,6 +99,41 @@ public void onCreatureSpawn(final CreatureSpawnEvent e) {

}

private boolean checkByPass(Location l) {
// If someone in that area has the bypass permission, allow the spawning
for (Entity entity : l.getWorld().getNearbyEntities(l, 5, 5, 5)) {
if (entity instanceof Player) {
Player player = (Player)entity;
if (player.isOp() || player.hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(l.getWorld()) + "mod.bypass")) {
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();
boolean bypass = player.isOp() || player.hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(e.getEntity().getWorld()) + "mod.bypass");

addon.getIslands().getIslandAt(e.getEntity().getLocation()).ifPresent(island -> {
// Check if entity can be hung
if (!island.isSpawn() && atLimit(island, bypass, e.getEntity())) {
// Not allowed
e.setCancelled(true);
User.getInstance(player).sendMessage("limits.hit-limit", "[material]",
Util.prettifyText(e.getEntity().getType().toString()),
"[number]", String.valueOf(addon.getSettings().getLimits().get(e.getEntity().getType())));

}
});
}

private void checkLimit(CreatureSpawnEvent e, boolean bypass) {
addon.getIslands().getIslandAt(e.getLocation()).ifPresent(island -> {
// Check if creature is allowed to spawn or not
Expand Down Expand Up @@ -136,9 +164,10 @@ private void checkLimit(CreatureSpawnEvent e, boolean bypass) {
* @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()
long count = ent.getWorld().getEntities().stream()
.filter(e -> e.getType().equals(ent.getType()))
.filter(e -> island.inIslandSpace(e.getLocation())).count();
return addon.getSettings().getLimits().getOrDefault(ent.getType(), -1) <= count;
}
}

Expand Down

0 comments on commit 230aa62

Please sign in to comment.