Skip to content

Commit

Permalink
Added cancellable to Coop events. For #389
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Dec 7, 2016
1 parent b2e44f8 commit 6ab5169
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 26 deletions.
54 changes: 37 additions & 17 deletions src/com/wasteofplastic/askyblock/CoopPlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())) {
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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());
}
Expand All @@ -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());
}
}

Expand Down Expand Up @@ -288,20 +303,22 @@ public void clearMyInvitedCoops(Player clearer) {
Entry<Location, UUID> 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
}
}
}
Expand All @@ -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);
}
}
Expand All @@ -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;
Expand Down
15 changes: 8 additions & 7 deletions src/com/wasteofplastic/askyblock/commands/IslandCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion src/com/wasteofplastic/askyblock/events/CoopJoinEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import java.util.UUID;

import org.bukkit.event.Cancellable;

import com.wasteofplastic.askyblock.Island;

/**
Expand All @@ -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;


/**
Expand All @@ -52,4 +55,14 @@ public UUID getInviter() {
return inviter;
}

@Override
public boolean isCancelled() {
return cancelled;
}

@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}

}
19 changes: 18 additions & 1 deletion src/com/wasteofplastic/askyblock/events/CoopLeaveEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import java.util.UUID;

import org.bukkit.event.Cancellable;

import com.wasteofplastic.askyblock.Island;


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

}

0 comments on commit 6ab5169

Please sign in to comment.