From ad186958ccd0b2ee34c97260589bf176716809db Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 2 Jun 2020 17:54:54 -0700 Subject: [PATCH] Adds event that fires when player joins and before perm check Cancel the event to prevent perm settings of limits. --- .../events/LimitsJoinPermCheckEvent.java | 110 ++++++++++++++++++ .../limits/listeners/BlockLimitsListener.java | 6 +- .../limits/listeners/JoinListener.java | 15 ++- src/main/resources/addon.yml | 2 +- 4 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 src/main/java/world/bentobox/limits/events/LimitsJoinPermCheckEvent.java diff --git a/src/main/java/world/bentobox/limits/events/LimitsJoinPermCheckEvent.java b/src/main/java/world/bentobox/limits/events/LimitsJoinPermCheckEvent.java new file mode 100644 index 0000000..6d18b91 --- /dev/null +++ b/src/main/java/world/bentobox/limits/events/LimitsJoinPermCheckEvent.java @@ -0,0 +1,110 @@ +package world.bentobox.limits.events; + +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; + +import world.bentobox.bentobox.api.events.BentoBoxEvent; +import world.bentobox.limits.objects.IslandBlockCount; + +/** + * Fired when a player joins the server and before limit settings for their island are changed based + * on the player's permissions. If cancelled, no limit settings will be made. + * @author tastybento + * + */ +public class LimitsJoinPermCheckEvent extends BentoBoxEvent implements Cancellable { + + private final Player player; + private final String islandId; + private IslandBlockCount ibc; + private boolean cancel; + private boolean ignorePerms; + + /** + * Fired when a player joins the server and before limit settings for their island are changed based + * on the player's permissions. If cancelled, no limit settings will be made. + * @param player - player joining + * @param islandId - the unique island id. + * @param ibc - IslandBlockCount object for this island + */ + public LimitsJoinPermCheckEvent(@NonNull Player player, @NonNull String islandId, @Nullable IslandBlockCount ibc) { + super(); + this.player = player; + this.islandId = islandId; + this.ibc = ibc; + } + + + /** + * Get the player joining + * @return the player + */ + @NonNull + public Player getPlayer() { + return player; + } + + + /** + * Get the unique island id. Use the islands manager to obtain the island + * @return the islandId + */ + @NonNull + public String getIslandId() { + return islandId; + } + + + /** + * Get the island block count + * @return the ibc + */ + @Nullable + public IslandBlockCount getIbc() { + return ibc; + } + + + /** + * Set the island block count to a specific setting + * @param ibc the ibc to set + */ + public void setIbc(@Nullable IslandBlockCount ibc) { + this.ibc = ibc; + } + + + @Override + public boolean isCancelled() { + return cancel; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancel = cancel; + + } + + + /** + * Check if player's perms should be considered or not + * @return the ignorePerms + */ + public boolean isIgnorePerms() { + return ignorePerms; + } + + + /** + * Ignore player's perms. This differs to canceling the event in that the IslandBlockCount will be used if given via + * {@link setIbc(IslandBlockCount ibc)} + * @param ignorePerms the ignorePerms to set + */ + public void setIgnorePerms(boolean ignorePerms) { + this.ignorePerms = ignorePerms; + } + + +} diff --git a/src/main/java/world/bentobox/limits/listeners/BlockLimitsListener.java b/src/main/java/world/bentobox/limits/listeners/BlockLimitsListener.java index db28154..837ef38 100644 --- a/src/main/java/world/bentobox/limits/listeners/BlockLimitsListener.java +++ b/src/main/java/world/bentobox/limits/listeners/BlockLimitsListener.java @@ -146,7 +146,7 @@ public class BlockLimitsListener implements Listener { * Save the count database completely */ public void save() { - islandCountMap.values().forEach(handler::saveObject); + islandCountMap.values().forEach(handler::saveObjectAsync); } // Player-related events @@ -349,7 +349,7 @@ public class BlockLimitsListener implements Listener { } } if (saveMap.get(id) > CHANGE_LIMIT) { - handler.saveObject(islandCountMap.get(id)); + handler.saveObjectAsync(islandCountMap.get(id)); saveMap.remove(id); } return -1; @@ -428,7 +428,7 @@ public class BlockLimitsListener implements Listener { */ public void setIsland(String islandId, IslandBlockCount ibc) { islandCountMap.put(islandId, ibc); - handler.saveObject(ibc); + handler.saveObjectAsync(ibc); } /** diff --git a/src/main/java/world/bentobox/limits/listeners/JoinListener.java b/src/main/java/world/bentobox/limits/listeners/JoinListener.java index 612a5de..1e66fb5 100644 --- a/src/main/java/world/bentobox/limits/listeners/JoinListener.java +++ b/src/main/java/world/bentobox/limits/listeners/JoinListener.java @@ -23,6 +23,7 @@ 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; import world.bentobox.limits.Limits; +import world.bentobox.limits.events.LimitsJoinPermCheckEvent; import world.bentobox.limits.objects.IslandBlockCount; /** @@ -40,6 +41,18 @@ public class JoinListener implements Listener { private void checkPerms(Player player, String permissionPrefix, String islandId, String gameMode) { IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(islandId); + // Fire event, so other addons can cancel this permissions change + LimitsJoinPermCheckEvent e = new LimitsJoinPermCheckEvent(player, gameMode, ibc); + Bukkit.getPluginManager().callEvent(e); + if (e.isCancelled()) return; + // Get ibc from event if it has changed + ibc = e.getIbc(); + // If perms should be ignored, but the IBC given in the event used, then set it and return + if (e.isIgnorePerms() && ibc != null) { + addon.getBlockLimitListener().setIsland(islandId, ibc); + return; + } + // Check permissions if (ibc != null) { // Clear permission limits ibc.getEntityLimits().clear(); @@ -127,7 +140,7 @@ public class JoinListener implements Listener { // 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(); + String islandId = Objects.requireNonNull(addon.getIslands().getIsland(gm.getOverWorld(), e.getPlayer().getUniqueId())).getUniqueId(); checkPerms(e.getPlayer(), gm.getPermissionPrefix() + "island.limit.", islandId, gm.getDescription().getName()); } }); diff --git a/src/main/resources/addon.yml b/src/main/resources/addon.yml index f22eff4..322e347 100755 --- a/src/main/resources/addon.yml +++ b/src/main/resources/addon.yml @@ -1,7 +1,7 @@ name: Limits main: world.bentobox.limits.Limits version: ${version}${build.number} -api-version: 1.12 +api-version: 1.13 authors: tastybento