No more async jailing, fixes #73 throws Exception

Added some custom exceptions to be thrown when conditions aren't met.
This might break a lot several things, will test after this build
completes but the unit tests all seemed to run fine.
This commit is contained in:
graywolf336 2015-06-02 22:14:06 -05:00
parent cfb62eed13
commit e92ec67ec9
10 changed files with 185 additions and 12 deletions

View File

@ -22,6 +22,11 @@ import com.graywolf336.jail.events.PrePrisonerReleasedEvent;
import com.graywolf336.jail.events.PrisonerJailedEvent;
import com.graywolf336.jail.events.PrisonerReleasedEvent;
import com.graywolf336.jail.events.PrisonerTransferredEvent;
import com.graywolf336.jail.exceptions.AsyncJailingNotSupportedException;
import com.graywolf336.jail.exceptions.AsyncUnJailingNotSupportedException;
import com.graywolf336.jail.exceptions.JailRequiredException;
import com.graywolf336.jail.exceptions.PrisonerAlreadyJailedException;
import com.graywolf336.jail.exceptions.PrisonerRequiredException;
import com.graywolf336.jail.interfaces.ICell;
/**
@ -80,18 +85,27 @@ public class PrisonerManager {
* @param cell The name of the {@link ICell cell} we are sending this prisoner to
* @param player The {@link Player player} we are preparing the jail for.
* @param prisoner The {@link Prisoner prisoner} file.
* @throws Exception if the jail or prisoner are null.
* @throws AsyncJailingNotSupportedException if the method is called from a thread that is <strong>not</strong> the primary one (call it sync).
* @throws JailRequiredException if the jail provided is null.
* @throws PrisonerAlreadyJailedException if the prisoner is already jailed.
* @throws PrisonerRequiredException if the prisoner's data provided is null.
*
*/
public void prepareJail(Jail jail, ICell cell, Player player, Prisoner prisoner) throws Exception {
public void prepareJail(Jail jail, ICell cell, Player player, Prisoner prisoner) throws AsyncJailingNotSupportedException, JailRequiredException, PrisonerAlreadyJailedException, PrisonerRequiredException {
if(!pl.getServer().isPrimaryThread()) throw new AsyncJailingNotSupportedException();
//Do some checks of whether the passed params are null.
if(jail == null)
throw new Exception("The jail can not be null.");
throw new JailRequiredException("jailing a prisoner");
if(cell == null)
cell = new NoCell();
if(prisoner == null)
throw new Exception("Prisoner data can not be null.");
throw new PrisonerRequiredException("jailing a prisoner");
if(this.pl.getJailManager().isPlayerJailed(prisoner.getUUID()))
throw new PrisonerAlreadyJailedException(prisoner.getLastKnownName(), prisoner.getUUID().toString());
//Set whether the prisoner is offline or not.
prisoner.setOfflinePending(player == null);
@ -410,12 +424,17 @@ public class PrisonerManager {
* @param player instance for the prisoner we're unjailing
* @param prisoner data where everything resides
* @param sender The {@link CommandSender} who unjailed this player, can be null.
* @throws Exception
* @throws AsyncUnJailingNotSupportedException when this method is called via a thread that is <strong>not</strong> the primary thread.
* @throws JailRequiredException when the jail provided is null.
* @throws PrisonerRequiredException when the provided prisoner data is null.
*
*/
public void unJail(Jail jail, ICell cell, Player player, Prisoner prisoner, CommandSender sender) throws Exception {
public void unJail(Jail jail, ICell cell, Player player, Prisoner prisoner, CommandSender sender) throws AsyncUnJailingNotSupportedException, JailRequiredException, PrisonerRequiredException {
if(!pl.getServer().isPrimaryThread()) throw new AsyncUnJailingNotSupportedException();
//Do some checks of whether the passed params are null.
if(jail == null)
throw new Exception("The jail can not be null.");
throw new JailRequiredException("unjailing a prisoner");
if(cell instanceof NoCell)
cell = null;
@ -423,7 +442,7 @@ public class PrisonerManager {
cell = null;
if(prisoner == null)
throw new Exception("Prisoner data can not be null.");
throw new PrisonerRequiredException("unjailing a prisoner");
//Throw the custom event which is called before we start releasing them
PrePrisonerReleasedEvent preEvent = new PrePrisonerReleasedEvent(jail, cell, prisoner, player);
@ -557,8 +576,10 @@ public class PrisonerManager {
*
* @param prisoner to release
* @param sender who is releasing the prisoner, <em>can be null</em>
* @throws PrisonerRequiredException when the prisoner data doesn't exist.
* @throws JailRequiredException when the prisoner isn't jailed
*/
public void forceRelease(Prisoner prisoner, CommandSender sender) {
public void forceRelease(Prisoner prisoner, CommandSender sender) throws JailRequiredException, PrisonerRequiredException {
Jail j = pl.getJailManager().getJailPrisonerIsIn(prisoner);
forceUnJail(j, j.getCellPrisonerIsIn(prisoner.getUUID()), pl.getServer().getPlayer(prisoner.getUUID()), prisoner, sender);
}
@ -586,8 +607,16 @@ public class PrisonerManager {
* @param player of the prisoner, if this is null then the player won't be teleported when they come back on.
* @param prisoner to release and remove data
* @param sender who is releasing the prisoner, <em>can be null</em>
* @throws JailRequiredException when the provided jail is null.
* @throws PrisonerRequiredException when the provided prisoner data is null.
*/
public void forceUnJail(Jail jail, Cell cell, Player player, Prisoner prisoner, CommandSender sender) {
public void forceUnJail(Jail jail, Cell cell, Player player, Prisoner prisoner, CommandSender sender) throws JailRequiredException, PrisonerRequiredException {
if(jail == null)
throw new JailRequiredException("jailing a prisoner");
if(prisoner == null)
throw new PrisonerRequiredException("jailing a prisoner");
if(player == null) {
//Player is offline, we just forcefully remove them from the database
pl.getJailIO().removePrisoner(jail, cell, prisoner);

View File

@ -16,6 +16,8 @@ import com.graywolf336.jail.command.Command;
import com.graywolf336.jail.command.CommandInfo;
import com.graywolf336.jail.enums.Lang;
import com.graywolf336.jail.enums.Settings;
import com.graywolf336.jail.exceptions.JailRequiredException;
import com.graywolf336.jail.exceptions.PrisonerRequiredException;
@CommandInfo(
maxArgs = 1,
@ -27,7 +29,7 @@ import com.graywolf336.jail.enums.Settings;
)
public class UnJailCommand implements Command {
public boolean execute(JailManager jm, CommandSender sender, String... args) {
public boolean execute(JailManager jm, CommandSender sender, String... args) throws JailRequiredException, PrisonerRequiredException {
//Check if the player is jailed
if(jm.isPlayerJailedByLastKnownUsername(args[0])) {
Jail j = jm.getJailPlayerIsInByLastKnownName(args[0]);

View File

@ -14,6 +14,8 @@ import com.graywolf336.jail.command.Command;
import com.graywolf336.jail.command.CommandInfo;
import com.graywolf336.jail.enums.Lang;
import com.graywolf336.jail.enums.Settings;
import com.graywolf336.jail.exceptions.JailRequiredException;
import com.graywolf336.jail.exceptions.PrisonerRequiredException;
@CommandInfo(
maxArgs = 1,
@ -25,7 +27,7 @@ import com.graywolf336.jail.enums.Settings;
)
public class UnJailForceCommand implements Command {
public boolean execute(JailManager jm, CommandSender sender, String... args) {
public boolean execute(JailManager jm, CommandSender sender, String... args) throws JailRequiredException, PrisonerRequiredException {
//Check if the player is jailed
if(jm.isPlayerJailedByLastKnownUsername(args[0])) {
jm.getPlugin().getPrisonerManager().forceRelease(jm.getPrisonerByLastKnownName(args[0]), sender);

View File

@ -0,0 +1,21 @@
package com.graywolf336.jail.exceptions;
/**
*
* The exception thrown trying to jail via a thread that is NOT the primary thread.
*
* @author graywolf336
* @since 3.0.0
* @version 1.0.0
*/
public class AsyncJailingNotSupportedException extends Exception {
private static final long serialVersionUID = 2746426914894618352L;
/**
* Creation of an exception from jailing via a thread that is not the primary one.
*
*/
public AsyncJailingNotSupportedException() {
super("Jailing via a thread that is NOT the primary thread is not supported.");
}
}

View File

@ -0,0 +1,21 @@
package com.graywolf336.jail.exceptions;
/**
*
* The exception thrown trying to unjail via a thread that is NOT the primary thread.
*
* @author graywolf336
* @since 3.0.0
* @version 1.0.0
*/
public class AsyncUnJailingNotSupportedException extends Exception {
private static final long serialVersionUID = -1540695375715404835L;
/**
* Creation of an exception from unjailing via a thread that is not the primary one.
*
*/
public AsyncUnJailingNotSupportedException() {
super("Unjailing via a thread that is NOT the primary thread is not supported.");
}
}

View File

@ -0,0 +1,22 @@
package com.graywolf336.jail.exceptions;
/**
*
* The exception thrown when a cell is required but wasn't provided.
*
* @author graywolf336
* @since 3.0.0
* @version 1.0.0
*/
public class CellRequiredException extends Exception {
private static final long serialVersionUID = 6496748770371151376L;
/**
* Creation of a new cell is required exception.
*
* @param action where the jail is required.
*/
public CellRequiredException(String action) {
super("A cell is required (can not be null) for: " + action);
}
}

View File

@ -0,0 +1,22 @@
package com.graywolf336.jail.exceptions;
/**
*
* The exception thrown when a jail is required but wasn't provided.
*
* @author graywolf336
* @since 3.0.0
* @version 1.0.0
*/
public class JailRequiredException extends Exception {
private static final long serialVersionUID = 1046287197309037470L;
/**
* Creation of a new jail is required exception.
*
* @param action where the jail is required.
*/
public JailRequiredException(String action) {
super("A jail is required (can not be null) for: " + action);
}
}

View File

@ -0,0 +1,31 @@
package com.graywolf336.jail.exceptions;
/**
*
* The exception thrown when a prisoner is already jailed.
*
* @author graywolf336
* @since 3.0.0
* @version 1.0.0
*/
public class PrisonerAlreadyJailedException extends Exception {
private static final long serialVersionUID = -5830449694077279409L;
/**
* Creation of a prisoner is already jailed exception.
*
*/
public PrisonerAlreadyJailedException() {
super("A prisoner (no name or uuid provided) is already jailed and can not be jailed twice.");
}
/**
* Creation of a prisoner is already jailed exception.
*
* @param prisonerName the prisoner's name
* @param prisonerUUID the uuid of the prisoner
*/
public PrisonerAlreadyJailedException(String prisonerName, String prisonerUUID) {
super("The prisoner " + prisonerName + " (" + prisonerUUID + ") is already jailed and can not be jailed twice.");
}
}

View File

@ -0,0 +1,22 @@
package com.graywolf336.jail.exceptions;
/**
*
* The exception thrown when a prisoner's data is required but wasn't provided.
*
* @author graywolf336
* @since 3.0.0
* @version 1.0.0
*/
public class PrisonerRequiredException extends Exception {
private static final long serialVersionUID = 5289068334047189357L;
/**
* Creation of a new jail is required exception.
*
* @param action where the jail is required.
*/
public PrisonerRequiredException(String action) {
super("Prisoner data is required (can not be null) for: " + action);
}
}

View File

@ -85,6 +85,7 @@ public class TestInstanceCreator {
when(mockServer.getLogger()).thenReturn(Util.logger);
when(mockServer.getWorldContainer()).thenReturn(worldsDirectory);
when(mockServer.getItemFactory()).thenReturn(CraftItemFactory.instance());
when(mockServer.isPrimaryThread()).thenReturn(true);
mockWorld = MockWorldFactory.makeNewMockWorld("world", Environment.NORMAL, WorldType.NORMAL);