From 6ab51695f2da38d8434a2a478e15399398d44cac Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 6 Dec 2016 17:59:50 -0800 Subject: [PATCH] Added cancellable to Coop events. For #389 --- .../wasteofplastic/askyblock/CoopPlay.java | 54 +++++++++++++------ .../askyblock/commands/IslandCmd.java | 15 +++--- .../askyblock/events/CoopJoinEvent.java | 15 +++++- .../askyblock/events/CoopLeaveEvent.java | 19 ++++++- 4 files changed, 77 insertions(+), 26 deletions(-) diff --git a/src/com/wasteofplastic/askyblock/CoopPlay.java b/src/com/wasteofplastic/askyblock/CoopPlay.java index a634ff5b1..5f64150ab 100644 --- a/src/com/wasteofplastic/askyblock/CoopPlay.java +++ b/src/com/wasteofplastic/askyblock/CoopPlay.java @@ -66,8 +66,9 @@ private CoopPlay(ASkyBlock plugin) { * * @param requester * @param newPlayer + * @return true if successful, otherwise false */ - public void addCoopPlayer(Player requester, Player newPlayer) { + public boolean addCoopPlayer(Player requester, Player newPlayer) { // plugin.getLogger().info("DEBUG: adding coop player"); // Find out which island this coop player is being requested to join Location islandLoc = null; @@ -97,6 +98,12 @@ public void addCoopPlayer(Player requester, Player newPlayer) { islandLoc = plugin.getPlayers().getIslandLocation(requester.getUniqueId()); } Island coopIsland = plugin.getGrid().getIslandAt(islandLoc); + // Fire event and check if it is cancelled + final CoopJoinEvent event = new CoopJoinEvent(newPlayer.getUniqueId(), coopIsland, requester.getUniqueId()); + plugin.getServer().getPluginManager().callEvent(event); + if (event.isCancelled()) { + return false; + } // Add the coop to the list. If the location already exists then the new // requester will replace the old if (coopPlayers.containsKey(newPlayer.getUniqueId())) { @@ -109,9 +116,7 @@ public void addCoopPlayer(Player requester, Player newPlayer) { loc.put(coopIsland.getCenter(), requester.getUniqueId()); coopPlayers.put(newPlayer.getUniqueId(), loc); } - // Fire event - final CoopJoinEvent event = new CoopJoinEvent(newPlayer.getUniqueId(), coopIsland, requester.getUniqueId()); - plugin.getServer().getPluginManager().callEvent(event); + return true; } /** @@ -173,6 +178,7 @@ public void clearAllIslandCoops(UUID player) { // Fire event final CoopLeaveEvent event = new CoopLeaveEvent(player, inviter, island); plugin.getServer().getPluginManager().callEvent(event); + // Cannot be cancelled } coopPlayer.remove(island.getCenter()); } @@ -189,13 +195,22 @@ public void clearMyCoops(Player player) { Island coopIsland = plugin.getGrid().getIsland(player.getUniqueId()); if (coopPlayers.get(player.getUniqueId()) != null) { //plugin.getLogger().info("DEBUG: " + player.getName() + " is a member of a coop"); + boolean notCancelled = false; for (UUID inviter : coopPlayers.get(player.getUniqueId()).values()) { // Fire event //plugin.getLogger().info("DEBUG: removing invite from " + plugin.getServer().getPlayer(inviter).getName()); final CoopLeaveEvent event = new CoopLeaveEvent(player.getUniqueId(), inviter, coopIsland); plugin.getServer().getPluginManager().callEvent(event); + if (!event.isCancelled()) { + coopPlayers.get(player.getUniqueId()).remove(inviter); + } else { + notCancelled = true; + } + } + // If the event was never cancelled, then delete the entry fully just in case. May not be needed. + if (notCancelled) { + coopPlayers.remove(player.getUniqueId()); } - coopPlayers.remove(player.getUniqueId()); } } @@ -288,20 +303,22 @@ public void clearMyInvitedCoops(Player clearer) { Entry entry = en.next(); // Check if this invite was sent by clearer if (entry.getValue().equals(clearer.getUniqueId())) { - // Yes, so get the invitee (target) - Player target = plugin.getServer().getPlayer(playerUUID); - if (target != null) { - target.sendMessage(ChatColor.RED + plugin.myLocale(playerUUID).coopRemoved.replace("[name]", clearer.getDisplayName())); - } else { - plugin.getMessages().setMessage(playerUUID, ChatColor.RED + plugin.myLocale(playerUUID).coopRemoved.replace("[name]", clearer.getDisplayName())); - } // Fire event final CoopLeaveEvent event = new CoopLeaveEvent(playerUUID, clearer.getUniqueId(), coopIsland); plugin.getServer().getPluginManager().callEvent(event); - // Mark them as no longer on a coop island - // setOnCoopIsland(players, null); - // Remove this entry - en.remove(); + if (!event.isCancelled()) { + // Yes, so get the invitee (target) + Player target = plugin.getServer().getPlayer(playerUUID); + if (target != null) { + target.sendMessage(ChatColor.RED + plugin.myLocale(playerUUID).coopRemoved.replace("[name]", clearer.getDisplayName())); + } else { + plugin.getMessages().setMessage(playerUUID, ChatColor.RED + plugin.myLocale(playerUUID).coopRemoved.replace("[name]", clearer.getDisplayName())); + } + // Mark them as no longer on a coop island + // setOnCoopIsland(players, null); + // Remove this entry + en.remove(); + } // else do not remove } } } @@ -322,6 +339,7 @@ public void clearAllIslandCoops(Location island) { // Fire event final CoopLeaveEvent event = new CoopLeaveEvent(coopPlayer.get(island), coopIsland.getOwner(), coopIsland); plugin.getServer().getPluginManager().callEvent(event); + // Cannot be cancelled coopPlayer.remove(island); } } @@ -345,10 +363,12 @@ public boolean removeCoopPlayer(Player requester, UUID targetPlayerUUID) { if (coopPlayers.containsKey(targetPlayerUUID)) { Island coopIsland = plugin.getGrid().getIsland(requester.getUniqueId()); if (coopIsland != null) { - removed = coopPlayers.get(targetPlayerUUID).remove(coopIsland.getCenter()) != null ? true: false; // Fire event final CoopLeaveEvent event = new CoopLeaveEvent(targetPlayerUUID, requester.getUniqueId(), coopIsland); plugin.getServer().getPluginManager().callEvent(event); + if (!event.isCancelled()) { + removed = coopPlayers.get(targetPlayerUUID).remove(coopIsland.getCenter()) != null ? true: false; + } } } return removed; diff --git a/src/com/wasteofplastic/askyblock/commands/IslandCmd.java b/src/com/wasteofplastic/askyblock/commands/IslandCmd.java index eed59f81a..96b6bf1ed 100644 --- a/src/com/wasteofplastic/askyblock/commands/IslandCmd.java +++ b/src/com/wasteofplastic/askyblock/commands/IslandCmd.java @@ -518,7 +518,7 @@ public boolean addPlayertoTeam(final UUID playerUUID, final UUID teamLeader) { // Set the player's team giving the team leader's name and the team's // island // location - if (!plugin.getPlayers().setJoinTeam(playerUUID, teamLeader, plugin.getPlayers().getIslandLocation(teamLeader)) { + if (!plugin.getPlayers().setJoinTeam(playerUUID, teamLeader, plugin.getPlayers().getIslandLocation(teamLeader))) { return false; } // If the player's name and the team leader are NOT the same when this @@ -1948,7 +1948,7 @@ public void run() { // Log the location that this player left so they // cannot join again before the cool down ends plugin.getPlayers().startInviteCoolDownTimer(playerUUID, plugin.getPlayers().getTeamIslandLocation(teamLeader)); - + // Remove any warps plugin.getWarpSignsListener().removeWarp(playerUUID); player.sendMessage(ChatColor.YELLOW + plugin.myLocale(player.getUniqueId()).leaveyouHaveLeftTheIsland); @@ -2543,10 +2543,11 @@ public void run() { } } // Add target to coop list - CoopPlay.getInstance().addCoopPlayer(player, target); - // Tell everyone what happened - player.sendMessage(ChatColor.GREEN + plugin.myLocale(player.getUniqueId()).coopSuccess.replace("[name]", target.getDisplayName())); - target.sendMessage(ChatColor.GREEN + plugin.myLocale(targetPlayerUUID).coopMadeYouCoop.replace("[name]", player.getDisplayName())); + if (CoopPlay.getInstance().addCoopPlayer(player, target)) { + // Tell everyone what happened + player.sendMessage(ChatColor.GREEN + plugin.myLocale(player.getUniqueId()).coopSuccess.replace("[name]", target.getDisplayName())); + target.sendMessage(ChatColor.GREEN + plugin.myLocale(targetPlayerUUID).coopMadeYouCoop.replace("[name]", player.getDisplayName())); + } // else fail silently return true; } else if (split[0].equalsIgnoreCase("expel")) { if (!VaultHelper.checkPerm(player, Settings.PERMPREFIX + "island.expel")) { @@ -2943,7 +2944,7 @@ public void run() { } player.sendMessage(ChatColor.GREEN + plugin.myLocale(player.getUniqueId()).makeLeadernameIsNowTheOwner.replace("[name]", plugin.getPlayers().getName(targetPlayer))); - + // plugin.getLogger().info("DEBUG: " + // plugin.getPlayers().getIslandLevel(teamLeader)); // Transfer the data from the old leader to the diff --git a/src/com/wasteofplastic/askyblock/events/CoopJoinEvent.java b/src/com/wasteofplastic/askyblock/events/CoopJoinEvent.java index 69b61134f..6faacd7ea 100644 --- a/src/com/wasteofplastic/askyblock/events/CoopJoinEvent.java +++ b/src/com/wasteofplastic/askyblock/events/CoopJoinEvent.java @@ -19,6 +19,8 @@ import java.util.UUID; +import org.bukkit.event.Cancellable; + import com.wasteofplastic.askyblock.Island; /** @@ -27,9 +29,10 @@ * @author tastybento * */ -public class CoopJoinEvent extends ASkyBlockEvent { +public class CoopJoinEvent extends ASkyBlockEvent implements Cancellable { private final UUID inviter; + private boolean cancelled; /** @@ -52,4 +55,14 @@ public UUID getInviter() { return inviter; } + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + } diff --git a/src/com/wasteofplastic/askyblock/events/CoopLeaveEvent.java b/src/com/wasteofplastic/askyblock/events/CoopLeaveEvent.java index 92b3020b1..d0b071b75 100644 --- a/src/com/wasteofplastic/askyblock/events/CoopLeaveEvent.java +++ b/src/com/wasteofplastic/askyblock/events/CoopLeaveEvent.java @@ -19,6 +19,8 @@ import java.util.UUID; +import org.bukkit.event.Cancellable; + import com.wasteofplastic.askyblock.Island; @@ -27,10 +29,13 @@ * @author tastybento * */ -public class CoopLeaveEvent extends ASkyBlockEvent { +public class CoopLeaveEvent extends ASkyBlockEvent implements Cancellable { private final UUID expeller; + private boolean cancelled; /** + * Note that not all coop leaving events can be cancelled because they could be due to bigger events than + * coop, e.g., an island being reset. * @param expelledPlayer * @param expellingPlayer * @param island @@ -49,5 +54,17 @@ public UUID getExpeller() { return expeller; } + @Override + public boolean isCancelled() { + return cancelled; + } + + /* (non-Javadoc) + * @see org.bukkit.event.Cancellable#setCancelled(boolean) + */ + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } }