Let's use the PrisonerJailedEvent in the jailing of a player.

This commit is contained in:
graywolf336 2013-12-19 16:13:41 -06:00
parent 92c19fd05f
commit 1a04ecf409
2 changed files with 152 additions and 20 deletions

View File

@ -9,10 +9,12 @@ import org.bukkit.entity.Player;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
import com.graywolf336.jail.JailManager;
import com.graywolf336.jail.beans.Jail;
import com.graywolf336.jail.beans.Prisoner;
import com.graywolf336.jail.command.Command;
import com.graywolf336.jail.command.CommandInfo;
import com.graywolf336.jail.command.parameters.JailParameters;
import com.graywolf336.jail.events.PrisonerJailedEvent;
@CommandInfo(
maxArgs = -1,
@ -23,7 +25,16 @@ import com.graywolf336.jail.command.parameters.JailParameters;
usage = "/jail [-p name] (-t time) (-j JailName) (-c CellName) (-m Muted) (-r A reason for jailing)"
)
public class JailCommand implements Command {
/*
* Executes the command. Checks the following:
*
* - If there are any jails.
* - If the command can be parsed correctly.
* - If the specified jail is null (TODO: if no jail given then find one close to them)
* - If the given time can be parsed correctly, defaults to 10 minutes at the moment TODO
* - If the prisoner is online or not.
*/
public boolean execute(JailManager jm, CommandSender sender, String... args) {
if(jm.getJails().size() == 0) {
@ -40,6 +51,12 @@ public class JailCommand implements Command {
return true;
}
//TODO: Add a way to get the nearest jail if they provide no jail
if(jm.getJail(params.jail()) == null) {
sender.sendMessage(ChatColor.RED + "No jail found by the name of '" + params.jail() + "'.");
return true;
}
Long time = 10L;
try {
@ -49,17 +66,38 @@ public class JailCommand implements Command {
return true;
}
Player p = jm.getPlugin().getServer().getPlayer(params.player());
Jail j = jm.getJail(params.jail());
Prisoner pris = new Prisoner(params.player(), params.muted(), TimeUnit.MILLISECONDS.convert(time, TimeUnit.MINUTES));
Player p = jm.getPlugin().getServer().getPlayer(params.player());
//call the event
PrisonerJailedEvent event = new PrisonerJailedEvent(j, pris, p, p == null, sender.getName());
jm.getPlugin().getServer().getPluginManager().callEvent(event);
//check if the event is cancelled
if(event.isCancelled()) {
if(event.getCancelledMessage().isEmpty())
sender.sendMessage(ChatColor.RED + "Jailing " + params.player() + " was cancelled by another plugin and they didn't leave you a message.");
else
sender.sendMessage(event.getCancelledMessage());
return true;
}
//recall data from the event
j = event.getJail();
pris = event.getPrisoner();
p = event.getPlayer();
String jailer = event.getJailer();
//Player is not online
if(p == null) {
sender.sendMessage(pris.getName() + " is offline and will be jailed for " + pris.getRemainingTime() + " milliseconds in the jail " + params.jail() + "in the cell " + params.cell() + " and will be muted: " + pris.isMuted() + ".");
sender.sendMessage(pris.getName() + " will be jailed for " + TimeUnit.MINUTES.convert(pris.getRemainingTime(), TimeUnit.MILLISECONDS) + " minutes.");
sender.sendMessage(pris.getName() + " is offline and will be jailed for " + pris.getRemainingTime() + " milliseconds in the jail " + params.jail() + " in the cell " + params.cell() + " and will be muted: " + pris.isMuted() + ".");
sender.sendMessage(pris.getName() + " will be jailed for " + TimeUnit.MINUTES.convert(pris.getRemainingTime(), TimeUnit.MILLISECONDS) + " minutes by " + jailer + ".");
}else {
//Player *is* online
sender.sendMessage(pris.getName() + " is online and will be jailed for " + pris.getRemainingTime() + " milliseconds in the jail " + params.jail() + "in the cell " + params.cell() + " and will be muted: " + pris.isMuted() + ".");
sender.sendMessage(pris.getName() + " will be jailed for " + TimeUnit.MINUTES.convert(pris.getRemainingTime(), TimeUnit.MILLISECONDS) + " minutes.");
sender.sendMessage(pris.getName() + " is online and will be jailed for " + pris.getRemainingTime() + " milliseconds in the jail " + params.jail() + " in the cell " + params.cell() + " and will be muted: " + pris.isMuted() + ".");
sender.sendMessage(pris.getName() + " will be jailed for " + TimeUnit.MINUTES.convert(pris.getRemainingTime(), TimeUnit.MILLISECONDS) + " minutes by " + jailer + ".");
}
return true;

View File

@ -1,14 +1,108 @@
package com.graywolf336.jail.events;
/**
* Event thrown when a player is fixing to be jailed, both offline and online players.
*
* This event is called right before we actually jail a player, and is cancellable, whether the player is offline or online, getPlayer() will always return null if isOnline() return false.
*
* @author graywolf336
* @since 3.0.0
* @version 0.0.0
*/
public class PrisonerJailedEvent {
}
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.Jail;
import com.graywolf336.jail.beans.Prisoner;
/**
* Event thrown when a player is fixing to be jailed, both offline and online players.
*
* This event is called right before we actually jail a player, and is cancellable, whether the player 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 PrisonerJailedEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private boolean cancelled;
private boolean online;
private Jail jail;
private Prisoner prisoner;
private Player player;
private String jailer, cancelMsg;
/**
* Creates a new {@link PrisonerJailedEvent prisoner jailed event} for the given player.
*
* @param jail The jail the prisoner will be jailed at.
* @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 PrisonerJailedEvent(Jail jail, Prisoner prisoner, Player player, boolean online, String jailer) {
this.jail = jail;
this.prisoner = prisoner;
this.player = player;
this.online = online;
this.jailer = jailer;
cancelMsg = "";
}
/** Gets the {@link Jail} this prisoner is being sent to. */
public Jail getJail() {
return this.jail;
}
/** Gets the {@link Prisoner} data for this prisoner. */
public Prisoner getPrisoner() {
return this.prisoner;
}
/** Gets the instance of the player being jailed <strong>but will return null if {@link #isOnline()} returns false</strong>. */
public Player getPlayer() {
return this.player;
}
/** Gets whether the prisoner being jailed is online or not. */
public boolean isOnline() {
return this.online;
}
/** Gets the jailer who jailed this prisoner. */
public String getJailer() {
return this.jailer;
}
/**
* 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 setJailer(String jailer) {
this.jailer = jailer;
}
/** 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;
}
}