Skip to content

Commit

Permalink
Adds permissions to limits
Browse files Browse the repository at this point in the history
#5

Also adds gamemodes to config.
  • Loading branch information
tastybento committed Feb 9, 2019
1 parent 3f120a4 commit df5b3a9
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 32 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.0.3-SNAPSHOT</version>
<version>0.1.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
55 changes: 44 additions & 11 deletions src/main/java/bentobox/addon/limits/Limits.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.bukkit.World;

import bentobox.addon.limits.listeners.BlockLimitsListener;
import bentobox.addon.limits.listeners.EntityLimitsListener;
import bentobox.addon.limits.listeners.JoinListener;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.addons.GameModeAddon;

Expand All @@ -19,15 +19,11 @@
public class Limits extends Addon {

private Settings settings;
private EntityLimitsListener listener;
private List<World> worlds;
private List<GameModeAddon> gameModes;
private BlockLimitsListener blockLimitListener;

@Override
public void onDisable(){
if (listener != null) {
worlds.forEach(listener::disable);
}
if (blockLimitListener != null) {
blockLimitListener.save();
}
Expand All @@ -40,16 +36,14 @@ public void onEnable() {
// Load settings
settings = new Settings(this);
// Register worlds from GameModes
worlds = getPlugin().getAddonsManager().getGameModeAddons().stream()
gameModes = getPlugin().getAddonsManager().getGameModeAddons().stream()
.filter(gm -> settings.getGameModes().contains(gm.getDescription().getName()))
.map(GameModeAddon::getOverWorld)
.collect(Collectors.toList());
worlds.forEach(w -> log("Limits will apply to " + w.getName()));
gameModes.forEach(w -> log("Limits will apply to " + w.getDescription().getName()));
// Register listener
//listener = new EntityLimitsListener(this);
//registerListener(listener);
blockLimitListener = new BlockLimitsListener(this);
registerListener(blockLimitListener);
registerListener(new JoinListener(this));
// Done
}

Expand All @@ -60,5 +54,44 @@ public Settings getSettings() {
return settings;
}

/**
* @return the gameModes
*/
public List<GameModeAddon> getGameModes() {
return gameModes;
}

/**
* @return the blockLimitListener
*/
public BlockLimitsListener getBlockLimitListener() {
return blockLimitListener;
}

/**
* Checks if this world is covered by the activated game modes
* @param world - world
* @return true or false
*/
public boolean inGameModeWorld(World world) {
return gameModes.stream().anyMatch(gm -> gm.inWorld(world));
}

/**
* Get the name of the game mode for this world
* @param world - world
* @return game mode name or empty string if none
*/
public String getGameMode(World world) {
return gameModes.stream().filter(gm -> gm.inWorld(world)).findFirst().map(gm -> gm.getDescription().getName()).orElse("");
}

/**
* Check if any of the game modes covered have this name
* @param gameMode - name of game mode
* @return true or false
*/
public boolean isCoveredGameMode(String gameMode) {
return gameModes.stream().anyMatch(gm -> gm.getDescription().getName().equals(gameMode));
}
}
2 changes: 1 addition & 1 deletion src/main/java/bentobox/addon/limits/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Settings {
public Settings(Limits addon) {

// GameModes
gameModes = addon.getConfig().getStringList("game-modes");
gameModes = addon.getConfig().getStringList("gamemodes");

ConfigurationSection el = addon.getConfig().getConfigurationSection("entitylimits");
if (el != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
*/
package bentobox.addon.limits.listeners;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bukkit.Bukkit;
Expand Down Expand Up @@ -47,6 +50,7 @@ public class BlockLimitsListener implements Listener {
* Save every 10 blocks of change
*/
private static final Integer CHANGE_LIMIT = 9;
private static final List<Material> DO_NOT_COUNT = Arrays.asList(Material.LAVA, Material.WATER, Material.AIR, Material.FIRE, Material.END_PORTAL, Material.NETHER_PORTAL);
private Limits addon;
private Map<String, IslandBlockCount> countMap = new HashMap<>();
private Map<String, Integer> saveMap = new HashMap<>();
Expand All @@ -57,7 +61,18 @@ public class BlockLimitsListener implements Listener {
public BlockLimitsListener(Limits addon) {
this.addon = addon;
handler = new Database<>(addon, IslandBlockCount.class);
handler.loadObjects().forEach(ibc -> countMap.put(ibc.getUniqueId(), ibc));
List<String> toBeDeleted = new ArrayList<>();
handler.loadObjects().forEach(ibc -> {
// Clean up
if (addon.isCoveredGameMode(ibc.getGameMode())) {
ibc.getBlockCount().keySet().removeIf(DO_NOT_COUNT::contains);
// Store
countMap.put(ibc.getUniqueId(), ibc);
} else {
toBeDeleted.add(ibc.getUniqueId());
}
});
toBeDeleted.forEach(handler::deleteID);
loadAllLimits();
}

Expand All @@ -73,12 +88,11 @@ private void loadAllLimits() {
}

// Load specific worlds

if (addon.getConfig().isConfigurationSection("worlds")) {
ConfigurationSection worlds = addon.getConfig().getConfigurationSection("worlds");
for (String worldName : worlds.getKeys(false)) {
World world = Bukkit.getWorld(worldName);
if (world != null && addon.getPlugin().getIWM().inWorld(world)) {
if (world != null && addon.inGameModeWorld(world)) {
addon.log("Loading limits for " + world.getName());
limitMap.putIfAbsent(world, new HashMap<>());
ConfigurationSection matsConfig = worlds.getConfigurationSection(worldName);
Expand All @@ -98,7 +112,7 @@ private Map<Material, Integer> loadLimits(ConfigurationSection cs) {
Map<Material, Integer> mats = new HashMap<>();
for (String material : cs.getKeys(false)) {
Material mat = Material.getMaterial(material);
if (mat != null && mat.isBlock()) {
if (mat != null && mat.isBlock() && !DO_NOT_COUNT.contains(mat)) {
mats.put(mat, cs.getInt(material));
addon.log("Limit " + mat + " to " + cs.getInt(material));
} else {
Expand Down Expand Up @@ -203,10 +217,18 @@ private int process(Block b, boolean add) {
* @return limit amount if over limit, or -1 if no limitation
*/
private int process(Block b, boolean add, Material changeTo) {
if (DO_NOT_COUNT.contains(b.getType()) || !addon.inGameModeWorld(b.getWorld())) {
return -1;
}
// Check if on island
return addon.getIslands().getIslandAt(b.getLocation()).map(i -> {
String id = i.getUniqueId();
countMap.putIfAbsent(id, new IslandBlockCount(id));
String gameMode = addon.getGameMode(b.getWorld());
if (gameMode.isEmpty()) {
// Invalid world
return -1;
}
countMap.putIfAbsent(id, new IslandBlockCount(id, gameMode));
saveMap.putIfAbsent(id, 0);
if (add) {
// Check limit
Expand All @@ -219,7 +241,7 @@ private int process(Block b, boolean add, Material changeTo) {
} else {
if (countMap.containsKey(id)) {
// Check for changes
if (!changeTo.equals(b.getType()) && changeTo.isBlock()) {
if (!changeTo.equals(b.getType()) && changeTo.isBlock() && !DO_NOT_COUNT.contains(changeTo)) {
// Check limit
int limit = checkLimit(b.getWorld(), changeTo, id);
if (limit > -1) {
Expand Down Expand Up @@ -247,22 +269,18 @@ private int process(Block b, boolean add, Material changeTo) {
* @return limit amount if at limit
*/
private int checkLimit(World w, Material m, String id) {
// Check specific world first
// Check island limits
IslandBlockCount island = countMap.get(id);
if (island.isBlockLimited(m)) {
return island.isAtLimit(m) ? island.getBlockLimit(m) : -1;
}
// Check specific world limits
if (limitMap.containsKey(w) && limitMap.get(w).containsKey(m)) {
// Material is overridden in world
if (countMap.get(id).isAtLimit(m, limitMap.get(w).get(m))) {
return limitMap.get(w).get(m);
} else {
// No limit
return -1;
}
return island.isAtLimit(m, limitMap.get(w).get(m)) ? limitMap.get(w).get(m) : -1;
}
// Check default limit map
if (defaultLimitMap.containsKey(m)
&& countMap
.get(id)
.isAtLimit(m,
defaultLimitMap.get(m))) {
if (defaultLimitMap.containsKey(m) && island.isAtLimit(m, defaultLimitMap.get(m))) {
return defaultLimitMap.get(m);
}
// No limit
Expand All @@ -282,4 +300,24 @@ public void onIslandDelete(IslandDeleteEvent e) {
}
}


/**
* Set the island block count values
* @param islandId - island unique id
* @param ibc - island block count
*/
public void setIsland(String islandId, IslandBlockCount ibc) {
countMap.put(islandId, ibc);
handler.saveObject(ibc);
}

/**
* Get the island block count
* @param islandId - island unique id
* @return island block count or null if there is none yet
*/
public IslandBlockCount getIsland(String islandId) {
return countMap.get(islandId);
}

}
86 changes: 86 additions & 0 deletions src/main/java/bentobox/addon/limits/listeners/JoinListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package bentobox.addon.limits.listeners;

import java.util.Locale;

import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.permissions.PermissionAttachmentInfo;

import bentobox.addon.limits.Limits;
import bentobox.addon.limits.objects.IslandBlockCount;

/**
* Sets block limits based on player permission
* @author tastybento
*
*/
public class JoinListener implements Listener {

private Limits addon;

public JoinListener(Limits addon) {
this.addon = addon;
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onPlayerJoin(PlayerJoinEvent e) {
// Check if player has any islands in the game modes
addon.getGameModes().forEach(gm -> {
if (addon.getIslands().hasIsland(gm.getOverWorld(), e.getPlayer().getUniqueId())) {
String islandId = addon.getIslands().getIsland(gm.getOverWorld(), e.getPlayer().getUniqueId()).getUniqueId();
checkPerms(e.getPlayer(), gm.getPermissionPrefix() + "island.limit.", islandId, gm.getDescription().getName());
}
});
}

private boolean checkPerms(Player player, String permissionPrefix, String islandId, String gameMode) {
IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(islandId);
int limit = -1;
for (PermissionAttachmentInfo perms : player.getEffectivePermissions()) {
if (perms.getPermission().startsWith(permissionPrefix)) {
// Get the Material
String[] split = perms.getPermission().split("\\.");
if (split.length != 5) {
logError(player.getName(), perms.getPermission(), "format must be " + permissionPrefix + "MATERIAL.NUMBER");
return false;
}
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 false;
}
// Get the max value should there be more than one
if (perms.getPermission().contains(permissionPrefix + ".*")) {
logError(player.getName(), perms.getPermission(), "wildcards are not allowed");
return false;
}
if (!NumberUtils.isDigits(split[4])) {
logError(player.getName(), perms.getPermission(), "the last part MUST be a number!");
} else {
limit = Math.max(limit, Integer.valueOf(split[4]));
// Set the limit
if (ibc == null) {
ibc = new IslandBlockCount(islandId, gameMode);
}
ibc.setBlockLimit(m, limit);
}
}
}
// If any changes have been made then store it
if (ibc != null) {
addon.getBlockLimitListener().setIsland(islandId, ibc);
}
return true;

}

private void logError(String name, String perm, String error) {
addon.logError("Player " + name + " has permission: '" + perm + " but " + error + " Ignoring...");
}

}

0 comments on commit df5b3a9

Please sign in to comment.