diff --git a/src/main/java/com/graywolf336/jail/JailTimer.java b/src/main/java/com/graywolf336/jail/JailTimer.java index a4fc64c..67cc8d6 100644 --- a/src/main/java/com/graywolf336/jail/JailTimer.java +++ b/src/main/java/com/graywolf336/jail/JailTimer.java @@ -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); + } } } } diff --git a/src/main/java/com/graywolf336/jail/events/PrePrisonerJailedByJailStickEvent.java b/src/main/java/com/graywolf336/jail/events/PrePrisonerJailedByJailStickEvent.java index 69cf8a1..ffae5af 100644 --- a/src/main/java/com/graywolf336/jail/events/PrePrisonerJailedByJailStickEvent.java +++ b/src/main/java/com/graywolf336/jail/events/PrePrisonerJailedByJailStickEvent.java @@ -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() { diff --git a/src/main/java/com/graywolf336/jail/events/PrePrisonerJailedEvent.java b/src/main/java/com/graywolf336/jail/events/PrePrisonerJailedEvent.java index 2281dd8..83f96db 100644 --- a/src/main/java/com/graywolf336/jail/events/PrePrisonerJailedEvent.java +++ b/src/main/java/com/graywolf336/jail/events/PrePrisonerJailedEvent.java @@ -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() { diff --git a/src/main/java/com/graywolf336/jail/events/PrePrisonerReleasedEvent.java b/src/main/java/com/graywolf336/jail/events/PrePrisonerReleasedEvent.java index 8d3ff76..248ff17 100644 --- a/src/main/java/com/graywolf336/jail/events/PrePrisonerReleasedEvent.java +++ b/src/main/java/com/graywolf336/jail/events/PrePrisonerReleasedEvent.java @@ -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() { diff --git a/src/main/java/com/graywolf336/jail/events/PrePrisonerTransferredEvent.java b/src/main/java/com/graywolf336/jail/events/PrePrisonerTransferredEvent.java index 53c9541..ab2e629 100644 --- a/src/main/java/com/graywolf336/jail/events/PrePrisonerTransferredEvent.java +++ b/src/main/java/com/graywolf336/jail/events/PrePrisonerTransferredEvent.java @@ -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. diff --git a/src/main/java/com/graywolf336/jail/events/PrisonerDeathEvent.java b/src/main/java/com/graywolf336/jail/events/PrisonerDeathEvent.java index 1e327fa..2baf0cf 100644 --- a/src/main/java/com/graywolf336/jail/events/PrisonerDeathEvent.java +++ b/src/main/java/com/graywolf336/jail/events/PrisonerDeathEvent.java @@ -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() { diff --git a/src/main/java/com/graywolf336/jail/events/PrisonerJailedEvent.java b/src/main/java/com/graywolf336/jail/events/PrisonerJailedEvent.java index 920600b..211ee05 100644 --- a/src/main/java/com/graywolf336/jail/events/PrisonerJailedEvent.java +++ b/src/main/java/com/graywolf336/jail/events/PrisonerJailedEvent.java @@ -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() { diff --git a/src/main/java/com/graywolf336/jail/events/PrisonerReleasedEvent.java b/src/main/java/com/graywolf336/jail/events/PrisonerReleasedEvent.java index 17c962f..bed651a 100644 --- a/src/main/java/com/graywolf336/jail/events/PrisonerReleasedEvent.java +++ b/src/main/java/com/graywolf336/jail/events/PrisonerReleasedEvent.java @@ -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() { diff --git a/src/main/java/com/graywolf336/jail/events/PrisonerTimeChangeEvent.java b/src/main/java/com/graywolf336/jail/events/PrisonerTimeChangeEvent.java new file mode 100644 index 0000000..f99726d --- /dev/null +++ b/src/main/java/com/graywolf336/jail/events/PrisonerTimeChangeEvent.java @@ -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. + * + *

+ * + * 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; + } +} diff --git a/src/main/java/com/graywolf336/jail/events/PrisonerTransferredEvent.java b/src/main/java/com/graywolf336/jail/events/PrisonerTransferredEvent.java index 2383db9..b8588ad 100644 --- a/src/main/java/com/graywolf336/jail/events/PrisonerTransferredEvent.java +++ b/src/main/java/com/graywolf336/jail/events/PrisonerTransferredEvent.java @@ -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() {