WIP - adding island-level entity limits

This commit is contained in:
tastybento 2019-09-23 13:43:42 -07:00
parent aab601ff9f
commit 30814bdb16
5 changed files with 77 additions and 107 deletions

View File

@ -41,7 +41,7 @@ public class Settings {
limits.entrySet().stream().map(e -> "Limit " + e.getKey().toString() + " to " + e.getValue()).forEach(addon::log);
}
private EntityType getType(String key) {
public static EntityType getType(String key) {
return Arrays.stream(EntityType.values()).filter(v -> v.name().equalsIgnoreCase(key)).findFirst().orElse(null);
}

View File

@ -20,6 +20,7 @@ public class EntityLimitListener implements Listener {
private final Limits addon;
/**
* TODO: Add island-level limits
* Handles entity and natural limitations
* @param addon - Limits object
*/

View File

@ -8,6 +8,7 @@ import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -16,6 +17,8 @@ import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.permissions.PermissionAttachmentInfo;
import bentobox.addon.limits.Limits;
import bentobox.addon.limits.Settings;
import bentobox.addon.limits.commands.LimitPanel;
import bentobox.addon.limits.objects.IslandBlockCount;
import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.events.island.IslandEvent.Reason;
@ -37,33 +40,52 @@ public class JoinListener implements Listener {
private void checkPerms(Player player, String permissionPrefix, String islandId, String gameMode) {
IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(islandId);
if (ibc == null) {
ibc = new IslandBlockCount(islandId, gameMode);
}
for (PermissionAttachmentInfo perms : player.getEffectivePermissions()) {
if (perms.getPermission().startsWith(permissionPrefix)) {
// No wildcards
if (perms.getPermission().contains(permissionPrefix + "*")) {
logError(player.getName(), perms.getPermission(), "wildcards are not allowed.");
logError(player.getName(), perms.getPermission(), "Wildcards are not allowed.");
return;
}
// Get the Material
String[] split = perms.getPermission().split("\\.");
if (split.length != 5) {
logError(player.getName(), perms.getPermission(), "format must be '" + permissionPrefix + "MATERIAL.NUMBER'");
logError(player.getName(), perms.getPermission(), "format must be '" + permissionPrefix + "MATERIAL/ENTITY-TYPE.NUMBER'");
return;
}
Material m = Material.getMaterial(split[3].toUpperCase(Locale.ENGLISH));
if (m == null) {
logError(player.getName(), perms.getPermission(), split[3].toUpperCase(Locale.ENGLISH) + " is not a valid material.");
return;
}
// Get the max value should there be more than one
// Get the value
if (!NumberUtils.isDigits(split[4])) {
logError(player.getName(), perms.getPermission(), "the last part MUST be a number!");
} else {
// Set the limit
if (ibc == null) {
ibc = new IslandBlockCount(islandId, gameMode);
return;
}
try {
int limit = Integer.valueOf(split[4]);
String key = split[3].toUpperCase(Locale.ENGLISH);
Material m = Material.getMaterial(key);
if (m == null) {
EntityType type = Settings.getType(key);
if (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 {
ibc.setEntityLimit(type, Math.max(ibc.getEntityLimit(type), limit));
return;
}
} else {
logError(player.getName(), perms.getPermission(), split[3].toUpperCase(Locale.ENGLISH) + " is not a valid material or entity type.");
return;
}
}
ibc.setBlockLimit(m, Math.max(ibc.getBlockLimit(m), Integer.valueOf(split[4])));
// Set the limit
ibc.setBlockLimit(m, Math.max(ibc.getBlockLimit(m), limit));
} catch (Exception e) {
logError(player.getName(), perms.getPermission(), "the last part MUST be a number!");
return;
}
}
}
@ -74,6 +96,7 @@ public class JoinListener implements Listener {
}
private void logError(String name, String perm, String error) {
addon.logError("Player " + name + " has permission: '" + perm + "' but " + error + " Ignoring...");
}

View File

@ -1,91 +0,0 @@
package bentobox.addon.limits.objects;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import com.google.gson.annotations.Expose;
import world.bentobox.bentobox.database.objects.DataObject;
/**
* @author tastybento
*
*/
public class EntityLimitsDO implements DataObject {
@Expose
private String uniqueId = "";
@Expose
private Map<UUID, String> spawnLoc = new HashMap<>();
public EntityLimitsDO() {}
public EntityLimitsDO(String uniqueId) {
this.uniqueId = uniqueId;
}
/* (non-Javadoc)
* @see world.bentobox.bentobox.database.objects.DataObject#getUniqueId()
*/
@Override
public String getUniqueId() {
return uniqueId;
}
/* (non-Javadoc)
* @see world.bentobox.bentobox.database.objects.DataObject#setUniqueId(java.lang.String)
*/
@Override
public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}
/**
* @return the spawnLoc
*/
public Map<UUID, String> getSpawnLoc() {
return spawnLoc;
}
/**
* @param spawnLoc the spawnLoc to set
*/
public void setSpawnLoc(Map<UUID, String> spawnLoc) {
this.spawnLoc = spawnLoc;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((uniqueId == null) ? 0 : uniqueId.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof EntityLimitsDO)) {
return false;
}
EntityLimitsDO other = (EntityLimitsDO) obj;
if (uniqueId == null) {
return other.uniqueId == null;
} else return uniqueId.equals(other.uniqueId);
}
}

View File

@ -4,6 +4,7 @@ import java.util.HashMap;
import java.util.Map;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import com.google.gson.annotations.Expose;
@ -23,12 +24,15 @@ public class IslandBlockCount implements DataObject {
@Expose
private Map<Material, Integer> blockCount = new HashMap<>();
/**
* Permission based limits
*/
@Expose
private Map<Material, Integer> blockLimits = new HashMap<>();
@Expose
private Map<EntityType, Integer> entityLimits = new HashMap<>();
// Required for YAML database
public IslandBlockCount() {}
@ -158,4 +162,37 @@ public class IslandBlockCount implements DataObject {
public void setGameMode(String gameMode) {
this.gameMode = gameMode;
}
}
/**
* Get the entity limit for this island
* @param e - entity type
* @return limit or -1 for unlimited
*/
public Integer getEntityLimit(EntityType e) {
return entityLimits.getOrDefault(e, -1);
}
/**
* Set an entity limit for this island
* @param e - entity type
* @param limit - limit
*/
public void setEntityLimit(EntityType e, int limit) {
entityLimits.put(e, limit);
}
/**
* @return the entityLimits
*/
public Map<EntityType, Integer> getEntityLimits() {
return entityLimits;
}
/**
* @param entityLimits the entityLimits to set
*/
public void setEntityLimits(Map<EntityType, Integer> entityLimits) {
this.entityLimits = entityLimits;
}
}