Adds event that fires when player joins and before perm check

Cancel the event to prevent perm settings of limits.
This commit is contained in:
tastybento 2020-06-02 17:54:54 -07:00
parent caf766403c
commit ad186958cc
4 changed files with 128 additions and 5 deletions

View File

@ -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;
}
}

View File

@ -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);
}
/**

View File

@ -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());
}
});

View File

@ -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