Skip to content

Commit

Permalink
Fixes permission limits for new islands, resets, owner change etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Feb 9, 2019
1 parent 148daf6 commit 36ee63a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 14 deletions.
12 changes: 11 additions & 1 deletion src/main/java/bentobox/addon/limits/Limits.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,20 @@ public boolean inGameModeWorld(World world) {
* @param world - world
* @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("");
}

/**
* 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
* @param gameMode - name of game mode
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/bentobox/addon/limits/commands/LimitPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ public void showLimits(World world, User user, UUID target) {
// Get the island for the target
Island island = addon.getIslands().getIsland(world, target);
if (island == null) {
user.sendMessage("general.errors.player-has-no-island");
if (user.getUniqueId().equals(target)) {
user.sendMessage("general.errors.no-island");
} else {
user.sendMessage("general.errors.player-has-no-island");
}
return;
}
IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(island.getUniqueId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ private int process(Block b, boolean add, Material changeTo) {
// Check if on island
return addon.getIslands().getIslandAt(b.getLocation()).map(i -> {
String id = i.getUniqueId();
String gameMode = addon.getGameMode(b.getWorld());
String gameMode = addon.getGameModeName(b.getWorld());
if (gameMode.isEmpty()) {
// Invalid world
return -1;
Expand Down
89 changes: 78 additions & 11 deletions src/main/java/bentobox/addon/limits/listeners/JoinListener.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package bentobox.addon.limits.listeners;

import java.util.Locale;
import java.util.UUID;

import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand All @@ -13,6 +17,10 @@

import bentobox.addon.limits.Limits;
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
Expand All @@ -27,17 +35,6 @@ 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 void checkPerms(Player player, String permissionPrefix, String islandId, String gameMode) {
IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(islandId);
int limit = -1;
Expand Down Expand Up @@ -82,4 +79,74 @@ private void logError(String name, String perm, String error) {
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);
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class IslandBlockCount implements DataObject {
@Expose
private Map<Material, Integer> blockCount = new HashMap<>();

/**
* Permission based limits
*/
@Expose
private Map<Material, Integer> blockLimits = new HashMap<>();

Expand Down

0 comments on commit 36ee63a

Please sign in to comment.