Throw an event when we change a prisoner's time.

This commit is contained in:
graywolf336 2014-08-19 13:30:46 -05:00
parent 96cb530add
commit 196b7c33ed
10 changed files with 192 additions and 4 deletions

View File

@ -11,6 +11,7 @@ import com.graywolf336.jail.beans.Jail;
import com.graywolf336.jail.beans.Prisoner;
import com.graywolf336.jail.enums.Lang;
import com.graywolf336.jail.enums.Settings;
import com.graywolf336.jail.events.PrisonerTimeChangeEvent;
/**
* Contains all the logic for counting down the time of the prisoners time.
@ -82,8 +83,17 @@ public class JailTimer {
//while the prisoner is offline, then let's do it
if(pl.getConfig().getBoolean(Settings.COUNTDOWNTIMEOFFLINE.getPath())) {
//Set their remaining time but if it is less than zero, set it to zero
p.setRemainingTime(Math.max(0, p.getRemainingTime() - timePassed));
if(p.getRemainingTime() == 0) pl.getPrisonerManager().schedulePrisonerRelease(p);
long before = p.getRemainingTime();
long after = Math.max(0, p.getRemainingTime() - timePassed);
PrisonerTimeChangeEvent event = new PrisonerTimeChangeEvent(j, j.getCellPrisonerIsIn(p.getUUID()), p, player, before, after);
pl.getServer().getPluginManager().callEvent(event);
if(!event.isCancelled()) {
after = event.getTimeAfterChange();
p.setRemainingTime(after);
if(p.getRemainingTime() == 0) pl.getPrisonerManager().schedulePrisonerRelease(p);
}
}
}else {
if(afkTime > 0) {
@ -103,8 +113,17 @@ public class JailTimer {
//The prisoner isn't offline, so let's count down
//Set their remaining time but if it is less than zero, set it to zero
p.setRemainingTime(Math.max(0, p.getRemainingTime() - timePassed));
if(p.getRemainingTime() == 0) pl.getPrisonerManager().schedulePrisonerRelease(p);
long before = p.getRemainingTime();
long after = Math.max(0, p.getRemainingTime() - timePassed);
PrisonerTimeChangeEvent event = new PrisonerTimeChangeEvent(j, j.getCellPrisonerIsIn(p.getUUID()), p, player, before, after);
pl.getServer().getPluginManager().callEvent(event);
if(!event.isCancelled()) {
after = event.getTimeAfterChange();
p.setRemainingTime(after);
if(p.getRemainingTime() == 0) pl.getPrisonerManager().schedulePrisonerRelease(p);
}
}
}
}

View File

@ -65,6 +65,11 @@ public class PrePrisonerJailedByJailStickEvent extends Event implements Cancella
public void setCell(Cell cell) {
this.cell = cell;
}
/** Checks if there is a cell involved. */
public boolean hasCell() {
return this.cell != null;
}
/** Gets the {@link Prisoner} data for this prisoner. */
public Prisoner getPrisoner() {

View File

@ -64,6 +64,11 @@ public class PrePrisonerJailedEvent extends Event implements Cancellable {
public void setCell(Cell cell) {
this.cell = cell;
}
/** Checks if there is a cell involved. */
public boolean hasCell() {
return this.cell != null;
}
/** Gets the {@link Prisoner} data for this prisoner. */
public Prisoner getPrisoner() {

View File

@ -52,6 +52,11 @@ public class PrePrisonerReleasedEvent extends Event {
public Cell getCell() {
return this.cell;
}
/** Checks if there is a cell involved. */
public boolean hasCell() {
return this.cell != null;
}
/** Gets the {@link Prisoner} data for this prisoner. */
public Prisoner getPrisoner() {

View File

@ -60,6 +60,11 @@ public class PrePrisonerTransferredEvent extends Event implements Cancellable {
public Cell getOriginalCell() {
return this.originalCell;
}
/** Checks if there is an original cell involved. */
public boolean hasOriginalCell() {
return this.originalCell != null;
}
/** Gets the {@link Jail} this prisoner is being transferred to. */
public Jail getTargetJail() {
@ -83,6 +88,11 @@ public class PrePrisonerTransferredEvent extends Event implements Cancellable {
if(this.targetJail.isValidCell(this.targetCell.getName())) return this.targetCell;
else return null;
}
/** Checks if there is a target cell involved. */
public boolean hasTargetCell() {
return this.getTargetCell() != null;
}
/**
* Sets the target {@link Cell} this prisoner is being sent to.

View File

@ -55,6 +55,11 @@ public class PrisonerDeathEvent extends Event {
public Cell getCell() {
return this.cell;
}
/** Checks if there is a cell involved. */
public boolean hasCell() {
return this.cell != null;
}
/** Gets the {@link Prisoner}'s data. */
public Prisoner getPrisoner() {

View File

@ -52,6 +52,11 @@ public class PrisonerJailedEvent extends Event {
public Cell getCell() {
return this.cell;
}
/** Checks if there was a cell involved. */
public boolean hasCell() {
return this.cell != null;
}
/** Gets the {@link Prisoner} data for this prisoner. */
public Prisoner getPrisoner() {

View File

@ -52,6 +52,11 @@ public class PrisonerReleasedEvent extends Event {
public Cell getCell() {
return this.cell;
}
/** Checks if there was a cell involved. */
public boolean hasCell() {
return this.cell != null;
}
/** Gets the {@link Prisoner} data for this prisoner. */
public Prisoner getPrisoner() {

View File

@ -0,0 +1,119 @@
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 when a prisoner's time changes.
*
* @author graywolf336
* @since 3.0.0
* @version 1.0.0
*/
public class PrisonerTimeChangeEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private boolean cancelled = false;
private Jail jail;
private Cell cell;
private Prisoner prisoner;
private Player player;
private long before, after;
/**
* Creates a new {@link PrisonerTimeChangeEvent prisoner time change event} for the given prisoner.
*
* @param jail The jail the prisoner is in.
* @param cell The cell the prisoner is in, can be null.
* @param prisoner The prisoner's data.
* @param player The player being jailed.
* @param before the time before it changed
* @param after the time after it changed
*/
public PrisonerTimeChangeEvent(Jail jail, Cell cell, Prisoner prisoner, Player player, long before, long after) {
this.jail = jail;
this.cell = cell;
this.prisoner = prisoner;
this.player = player;
this.before = before;
this.after = after;
}
/** Gets the {@link Jail} this prisoner is in. */
public Jail getJail() {
return this.jail;
}
/** Gets the cell the prisoner is in, can be null. */
public Cell getCell() {
return this.cell;
}
/** Checks if there is a cell involved. */
public boolean hasCell() {
return this.cell != null;
}
/** Gets the {@link Prisoner}'s data. */
public Prisoner getPrisoner() {
return this.prisoner;
}
/** Gets the instance of the player who died. */
public Player getPlayer() {
return this.player;
}
/** Gets the time the prisoner had before it changed. */
public long getTimeBeforeChange() {
return this.before;
}
/** Gets the time the prisoner will have after the change. */
public long getTimeAfterChange() {
return this.after;
}
/** Sets the time the prisoner will have after the change. */
public void setTimeAfterChange(long newtime) {
this.after = Math.max(0, newtime);
}
/**
* Gets the difference the change is making.
*
* <p />
*
* The change can be positive or negative. If it is positive then the
* time after is smaller than it was before. If it is negative then the
* time after is larger than it was before.
*
* @return The difference between the two time changes.
*/
public long getTimeDifference() {
return this.before - this.after;
}
/** 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;
}
public static HandlerList getHandlerList() {
return handlers;
}
public HandlerList getHandlers() {
return handlers;
}
}

View File

@ -54,6 +54,11 @@ public class PrisonerTransferredEvent extends Event {
public Cell getOriginalCell() {
return this.originalCell;
}
/** Checks if there was an original cell involved. */
public boolean hasOriginalCell() {
return this.originalCell != null;
}
/** Gets the {@link Jail} this prisoner is being transferred to. */
public Jail getTargetJail() {
@ -68,6 +73,11 @@ public class PrisonerTransferredEvent extends Event {
if(this.targetJail.isValidCell(this.targetCell.getName())) return this.targetCell;
else return null;
}
/** Checks if there was a target cell involved. */
public boolean hasTargetCell() {
return this.getTargetCell() != null;
}
/** Gets the {@link Prisoner} data for this prisoner. */
public Prisoner getPrisoner() {