Fixes permission limits for new islands, resets, owner change etc.

This commit is contained in:
tastybento 2019-02-09 11:13:20 -08:00
parent 148daf63f3
commit 36ee63a70b
5 changed files with 98 additions and 14 deletions

View File

@ -91,10 +91,20 @@ public class Limits extends Addon {
* @param world - world * @param world - world
* @return game mode name or empty string if none * @return game mode name or empty string if none
*/ */
public String getGameMode(World world) { public String getGameModeName(World world) {
return gameModes.stream().filter(gm -> gm.inWorld(world)).findFirst().map(gm -> gm.getDescription().getName()).orElse(""); return gameModes.stream().filter(gm -> gm.inWorld(world)).findFirst().map(gm -> gm.getDescription().getName()).orElse("");
} }
/**
* Get the name of the game mode for this world
* @param world - world
* @return game mode name or empty string if none
*/
public String getGameModePermPrefix(World world) {
return gameModes.stream().filter(gm -> gm.inWorld(world)).findFirst().map(gm -> gm.getPermissionPrefix()).orElse("");
}
/** /**
* Check if any of the game modes covered have this name * Check if any of the game modes covered have this name
* @param gameMode - name of game mode * @param gameMode - name of game mode

View File

@ -37,7 +37,11 @@ class LimitPanel {
// Get the island for the target // Get the island for the target
Island island = addon.getIslands().getIsland(world, target); Island island = addon.getIslands().getIsland(world, target);
if (island == null) { if (island == null) {
if (user.getUniqueId().equals(target)) {
user.sendMessage("general.errors.no-island");
} else {
user.sendMessage("general.errors.player-has-no-island"); user.sendMessage("general.errors.player-has-no-island");
}
return; return;
} }
IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(island.getUniqueId()); IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(island.getUniqueId());

View File

@ -224,7 +224,7 @@ public class BlockLimitsListener implements Listener {
// Check if on island // Check if on island
return addon.getIslands().getIslandAt(b.getLocation()).map(i -> { return addon.getIslands().getIslandAt(b.getLocation()).map(i -> {
String id = i.getUniqueId(); String id = i.getUniqueId();
String gameMode = addon.getGameMode(b.getWorld()); String gameMode = addon.getGameModeName(b.getWorld());
if (gameMode.isEmpty()) { if (gameMode.isEmpty()) {
// Invalid world // Invalid world
return -1; return -1;

View File

@ -1,9 +1,13 @@
package bentobox.addon.limits.listeners; package bentobox.addon.limits.listeners;
import java.util.Locale; import java.util.Locale;
import java.util.UUID;
import org.apache.commons.lang.math.NumberUtils; import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@ -13,6 +17,10 @@ import org.bukkit.permissions.PermissionAttachmentInfo;
import bentobox.addon.limits.Limits; import bentobox.addon.limits.Limits;
import bentobox.addon.limits.objects.IslandBlockCount; import bentobox.addon.limits.objects.IslandBlockCount;
import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.events.island.IslandEvent.Reason;
import world.bentobox.bentobox.api.events.team.TeamEvent.TeamSetownerEvent;
import world.bentobox.bentobox.database.objects.Island;
/** /**
* Sets block limits based on player permission * Sets block limits based on player permission
@ -27,17 +35,6 @@ public class JoinListener implements Listener {
this.addon = 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 void checkPerms(Player player, String permissionPrefix, String islandId, String gameMode) { private void checkPerms(Player player, String permissionPrefix, String islandId, String gameMode) {
IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(islandId); IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(islandId);
int limit = -1; int limit = -1;
@ -82,4 +79,74 @@ public class JoinListener implements Listener {
addon.logError("Player " + name + " has permission: '" + perm + " but " + error + " Ignoring..."); addon.logError("Player " + name + " has permission: '" + perm + " but " + error + " Ignoring...");
} }
/*
* Event handling
*/
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onNewIsland(IslandEvent e) {
if (!e.getReason().equals(Reason.CREATED)
&& !e.getReason().equals(Reason.RESETTED)
&& !e.getReason().equals(Reason.REGISTERED)) {
return;
}
setOwnerPerms(e.getIsland(), e.getOwner());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onOwnerChange(TeamSetownerEvent e) {
removeOwnerPerms(e.getIsland());
setOwnerPerms(e.getIsland(), e.getNewOwner());
}
@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());
}
});
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onUnregisterIsland(IslandEvent e) {
if (!e.getReason().equals(Reason.UNREGISTERED)) {
return;
}
removeOwnerPerms(e.getIsland());
}
/*
* Utility methods
*/
private void removeOwnerPerms(Island island) {
World world = island.getWorld();
if (addon.inGameModeWorld(world)) {
IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(island.getUniqueId());
if (ibc != null) {
ibc.getBlockLimits().clear();
}
}
}
private void setOwnerPerms(Island island, UUID ownerUUID) {
World world = island.getWorld();
if (addon.inGameModeWorld(world)) {
// Check if owner is online
OfflinePlayer owner = Bukkit.getOfflinePlayer(ownerUUID);
if (owner.isOnline()) {
// Set perm-based limits
String prefix = addon.getGameModePermPrefix(world);
String name = addon.getGameModeName(world);
if (!prefix.isEmpty() && !name.isEmpty()) {
checkPerms(owner.getPlayer(), prefix + "island.limit.", island.getUniqueId(), name);
}
}
}
}
} }

View File

@ -24,6 +24,9 @@ public class IslandBlockCount implements DataObject {
@Expose @Expose
private Map<Material, Integer> blockCount = new HashMap<>(); private Map<Material, Integer> blockCount = new HashMap<>();
/**
* Permission based limits
*/
@Expose @Expose
private Map<Material, Integer> blockLimits = new HashMap<>(); private Map<Material, Integer> blockLimits = new HashMap<>();