Skip to content

Commit

Permalink
Adds event that fires when player joins and before perm check
Browse files Browse the repository at this point in the history
Cancel the event to prevent perm settings of limits.
  • Loading branch information
tastybento committed Jun 3, 2020
1 parent caf7664 commit ad18695
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private Map<Material, Integer> loadLimits(ConfigurationSection cs) {
* Save the count database completely
*/
public void save() {
islandCountMap.values().forEach(handler::saveObject);
islandCountMap.values().forEach(handler::saveObjectAsync);
}

// Player-related events
Expand Down Expand Up @@ -349,7 +349,7 @@ private int process(Block b, boolean add, Material changeTo) {
}
}
if (saveMap.get(id) > CHANGE_LIMIT) {
handler.saveObject(islandCountMap.get(id));
handler.saveObjectAsync(islandCountMap.get(id));
saveMap.remove(id);
}
return -1;
Expand Down Expand Up @@ -428,7 +428,7 @@ public void onIslandDelete(IslandDeleteEvent e) {
*/
public void setIsland(String islandId, IslandBlockCount ibc) {
islandCountMap.put(islandId, ibc);
handler.saveObject(ibc);
handler.saveObjectAsync(ibc);
}

/**
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/world/bentobox/limits/listeners/JoinListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;

/**
Expand All @@ -40,6 +41,18 @@ public JoinListener(Limits addon) {

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();
Expand Down Expand Up @@ -127,7 +140,7 @@ 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();
String islandId = Objects.requireNonNull(addon.getIslands().getIsland(gm.getOverWorld(), e.getPlayer().getUniqueId())).getUniqueId();
checkPerms(e.getPlayer(), gm.getPermissionPrefix() + "island.limit.", islandId, gm.getDescription().getName());
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/addon.yml
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit ad18695

Please sign in to comment.