From 620c57cdc929589868b8a8461db74db3fd02430f Mon Sep 17 00:00:00 2001 From: graywolf336 Date: Sat, 15 Feb 2014 00:52:25 -0600 Subject: [PATCH] Add a PrePrisonerTransferredEvent #9. --- .../subcommands/JailTransferCommand.java | 20 ++- .../graywolf336/jail/enums/LangString.java | 2 + .../events/PrePrisonerTransferredEvent.java | 151 ++++++++++++++++++ src/main/resources/en.yml | 1 + 4 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/graywolf336/jail/events/PrePrisonerTransferredEvent.java diff --git a/src/main/java/com/graywolf336/jail/command/subcommands/JailTransferCommand.java b/src/main/java/com/graywolf336/jail/command/subcommands/JailTransferCommand.java index bc5ae74..8e418ef 100644 --- a/src/main/java/com/graywolf336/jail/command/subcommands/JailTransferCommand.java +++ b/src/main/java/com/graywolf336/jail/command/subcommands/JailTransferCommand.java @@ -14,6 +14,7 @@ import com.graywolf336.jail.command.Command; import com.graywolf336.jail.command.CommandInfo; import com.graywolf336.jail.command.commands.jewels.Transfer; import com.graywolf336.jail.enums.LangString; +import com.graywolf336.jail.events.PrePrisonerTransferredEvent; import com.lexicalscope.jewel.cli.ArgumentValidationException; import com.lexicalscope.jewel.cli.CliFactory; @@ -100,7 +101,24 @@ public class JailTransferCommand implements Command { } } - jm.getPlugin().debug("Sending the transferring off, jail and cell check all came out clean."); + jm.getPlugin().debug("Calling the PrePrisonerTransferredEvent, jail and cell check all came out clean."); + + //Throw the custom event before transferring them, allowing another plugin to cancel it. + PrePrisonerTransferredEvent event = new PrePrisonerTransferredEvent(jm.getJailPlayerIsIn(params.getPlayer()), + jm.getJailPlayerIsIn(params.getPlayer()).getCellPrisonerIsIn(params.getPlayer()), + target, targetCell, jm.getPrisoner(params.getPlayer()), jm.getPlugin().getServer().getPlayer(params.getPlayer()), sender.getName()); + jm.getPlugin().getServer().getPluginManager().callEvent(event); + + if(event.isCancelled()) { + if(event.getCancelledMessage().isEmpty()) { + //The plugin didn't provide a cancel message/reason so send the default one + sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.TRANSFERCANCELLEDBYANOTHERPLUGIN, params.getPlayer())); + }else { + sender.sendMessage(event.getCancelledMessage()); + } + + return true; + } //Start the transferring of the prisoner jm.getPlugin().getPrisonerManager().transferPrisoner(jm.getJailPlayerIsIn(params.getPlayer()), diff --git a/src/main/java/com/graywolf336/jail/enums/LangString.java b/src/main/java/com/graywolf336/jail/enums/LangString.java index 5092b08..321fa3b 100644 --- a/src/main/java/com/graywolf336/jail/enums/LangString.java +++ b/src/main/java/com/graywolf336/jail/enums/LangString.java @@ -78,6 +78,8 @@ public enum LangString { TELEOUT ("jailing"), /** The message sent to the sender when they transfer all a jail's prisoners to another jail. */ TRANSFERALLCOMPLETE ("jailing"), + /** The message sent when another plugin cancels the transferring but doesn't provide a reason why. */ + TRANSFERCANCELLEDBYANOTHERPLUGIN ("jailing"), /** The message sent to the sender when they transfer someone to a jail and a cell. */ TRANSFERCOMPLETECELL ("jailing"), /** The message sent to the sender when they transfer someone to a jail. */ diff --git a/src/main/java/com/graywolf336/jail/events/PrePrisonerTransferredEvent.java b/src/main/java/com/graywolf336/jail/events/PrePrisonerTransferredEvent.java new file mode 100644 index 0000000..9d19461 --- /dev/null +++ b/src/main/java/com/graywolf336/jail/events/PrePrisonerTransferredEvent.java @@ -0,0 +1,151 @@ +package com.graywolf336.jail.events; + +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +import com.graywolf336.jail.beans.Cell; +import com.graywolf336.jail.beans.Jail; +import com.graywolf336.jail.beans.Prisoner; + +/** + * Event thrown before we transfer a prisoner, both offline and online prisoner. + * + *

+ * + * This event is called right before we actually transfer a prisoner, and is cancellable, whether the prisoner is offline or online, getPlayer() will always return null if isOnline() return false. + * + * @author graywolf336 + * @since 3.0.0 + * @version 1.0.0 + */ +public class PrePrisonerTransferredEvent extends Event implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled = false; + private Jail originalJail, targetJail; + private Cell originalCell, targetCell; + private Prisoner prisoner; + private Player player; + private String transferor, cancelMsg; + + /** + * Creates a new {@link PrePrisonerTransferredEvent prisoner transferred event} for the given player before they get transferred to their jail and cell. + * + * @param jail The jail the prisoner will be jailed at. + * @param cell The cell we're going to be sending the prisoner to, can be null. + * @param prisoner The prisoner data. + * @param player The player being jailed. + * @param online Whether the player is online or not. + * @param jailer The name of what jailed this prisoner. + */ + public PrePrisonerTransferredEvent(Jail originalJail, Cell originalCell, Jail targetJail, Cell targetCell, Prisoner prisoner, Player player, String transferor) { + this.originalJail = originalJail; + this.originalCell = originalCell; + this.targetJail = targetJail; + this.targetCell = targetCell; + this.prisoner = prisoner; + this.player = player; + this.transferor = transferor; + this.cancelMsg = ""; + } + + /** Gets the {@link Jail} this prisoner is coming from. */ + public Jail getOriginalJail() { + return this.originalJail; + } + + /** Gets the {@link Cell} this prisoner is coming from, can be null. */ + public Cell getOriginalCell() { + return this.originalCell; + } + + /** Gets the {@link Jail} this prisoner is being transferred to. */ + public Jail getTargetJail() { + return this.targetJail; + } + + /** + * Sets the target jail where this prisoner is being sent to. + * + * @param jail The {@link Jail} this prisoner should be sent to instead of what it was. + */ + public void setTargetJail(Jail jail) { + this.targetJail = jail; + } + + /** Gets the {@link Cell} this prisoner is being sent to, can be null. + * + * Will return null if the cell is not in the targetJail. + */ + public Cell getTargetCell() { + if(this.targetJail.isValidCell(this.targetCell.getName())) return this.targetCell; + else return null; + } + + /** + * Sets the target {@link Cell} this prisoner is being sent to. + * + * @param cell The {@link Cell} this prisoner should be sent to instead of what it was. + */ + public void setTargetCell(Cell cell) { + this.targetCell = cell; + } + + /** Gets the {@link Prisoner} data for this prisoner. */ + public Prisoner getPrisoner() { + return this.prisoner; + } + + /** Gets the instance of the player being transferred but will return null if {@link #isOnline()} returns false. */ + public Player getPlayer() { + return this.player; + } + + /** Gets whether the prisoner being transferred is online or not. */ + public boolean isOnline() { + return player == null; + } + + /** Gets the name of what is transferring this prisoner. */ + public String getTransferor() { + return this.transferor; + } + + /** + * Sets the prisoner whom the data should say jailed this prisoner. + * + * @param jailer The name to put who is the jailer for this prisoner. + */ + public void setTransferor(String transferor) { + this.transferor = transferor; + } + + /** Checks whether this event is cancelled or not. */ + public boolean isCancelled() { + return this.cancelled; + } + + /** Sets whether this event should be cancelled. */ + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + /** Returns the cancelled message. */ + public String getCancelledMessage() { + return this.cancelMsg; + } + + /** Sets the cancelled message. */ + public void setCancelledMessage(String msg) { + this.cancelMsg = msg; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + public HandlerList getHandlers() { + return handlers; + } +} diff --git a/src/main/resources/en.yml b/src/main/resources/en.yml index 29f6f01..0dc9181 100644 --- a/src/main/resources/en.yml +++ b/src/main/resources/en.yml @@ -61,6 +61,7 @@ language: telein: "&9Teleported %0% to %1%'s teleport in location." teleout: "&9Teleported %0% to %1%'s teleport out location." transferallcomplete: '&2Successfully transferred all the prisoners from %0% to %1%.' + transfercancelledbyanotherplugin: '&cTransferring %0% was cancelled by another plugin and they did not leave you a message.' transfercompletecell: '&2Successfully transferred %0% to %1% in the cell %2%.' transfercompletenocell: '&2Successfully transferred %0% to %1%.' transferred: '&9You have been transferred to %0%.'