From e92ec67ec9b8f9d44c82a01af3e5a002b0d79ccd Mon Sep 17 00:00:00 2001 From: graywolf336 Date: Tue, 2 Jun 2015 22:14:06 -0500 Subject: [PATCH] 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. --- .../com/graywolf336/jail/PrisonerManager.java | 49 +++++++++++++++---- .../jail/command/commands/UnJailCommand.java | 4 +- .../command/commands/UnJailForceCommand.java | 4 +- .../AsyncJailingNotSupportedException.java | 21 ++++++++ .../AsyncUnJailingNotSupportedException.java | 21 ++++++++ .../exceptions/CellRequiredException.java | 22 +++++++++ .../exceptions/JailRequiredException.java | 22 +++++++++ .../PrisonerAlreadyJailedException.java | 31 ++++++++++++ .../exceptions/PrisonerRequiredException.java | 22 +++++++++ .../jail/util/TestInstanceCreator.java | 1 + 10 files changed, 185 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/graywolf336/jail/exceptions/AsyncJailingNotSupportedException.java create mode 100644 src/main/java/com/graywolf336/jail/exceptions/AsyncUnJailingNotSupportedException.java create mode 100644 src/main/java/com/graywolf336/jail/exceptions/CellRequiredException.java create mode 100644 src/main/java/com/graywolf336/jail/exceptions/JailRequiredException.java create mode 100644 src/main/java/com/graywolf336/jail/exceptions/PrisonerAlreadyJailedException.java create mode 100644 src/main/java/com/graywolf336/jail/exceptions/PrisonerRequiredException.java diff --git a/src/main/java/com/graywolf336/jail/PrisonerManager.java b/src/main/java/com/graywolf336/jail/PrisonerManager.java index b9d6d8c..20abb2f 100644 --- a/src/main/java/com/graywolf336/jail/PrisonerManager.java +++ b/src/main/java/com/graywolf336/jail/PrisonerManager.java @@ -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,19 +85,28 @@ 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 not 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 not 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, can be null + * @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, can be null + * @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); diff --git a/src/main/java/com/graywolf336/jail/command/commands/UnJailCommand.java b/src/main/java/com/graywolf336/jail/command/commands/UnJailCommand.java index d95448f..8c598b9 100644 --- a/src/main/java/com/graywolf336/jail/command/commands/UnJailCommand.java +++ b/src/main/java/com/graywolf336/jail/command/commands/UnJailCommand.java @@ -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]); diff --git a/src/main/java/com/graywolf336/jail/command/commands/UnJailForceCommand.java b/src/main/java/com/graywolf336/jail/command/commands/UnJailForceCommand.java index 1ffc1bd..463e296 100644 --- a/src/main/java/com/graywolf336/jail/command/commands/UnJailForceCommand.java +++ b/src/main/java/com/graywolf336/jail/command/commands/UnJailForceCommand.java @@ -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); diff --git a/src/main/java/com/graywolf336/jail/exceptions/AsyncJailingNotSupportedException.java b/src/main/java/com/graywolf336/jail/exceptions/AsyncJailingNotSupportedException.java new file mode 100644 index 0000000..1206aa0 --- /dev/null +++ b/src/main/java/com/graywolf336/jail/exceptions/AsyncJailingNotSupportedException.java @@ -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."); + } +} diff --git a/src/main/java/com/graywolf336/jail/exceptions/AsyncUnJailingNotSupportedException.java b/src/main/java/com/graywolf336/jail/exceptions/AsyncUnJailingNotSupportedException.java new file mode 100644 index 0000000..2c9c62c --- /dev/null +++ b/src/main/java/com/graywolf336/jail/exceptions/AsyncUnJailingNotSupportedException.java @@ -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."); + } +} diff --git a/src/main/java/com/graywolf336/jail/exceptions/CellRequiredException.java b/src/main/java/com/graywolf336/jail/exceptions/CellRequiredException.java new file mode 100644 index 0000000..abe1dd8 --- /dev/null +++ b/src/main/java/com/graywolf336/jail/exceptions/CellRequiredException.java @@ -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); + } +} diff --git a/src/main/java/com/graywolf336/jail/exceptions/JailRequiredException.java b/src/main/java/com/graywolf336/jail/exceptions/JailRequiredException.java new file mode 100644 index 0000000..f3c3f54 --- /dev/null +++ b/src/main/java/com/graywolf336/jail/exceptions/JailRequiredException.java @@ -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); + } +} diff --git a/src/main/java/com/graywolf336/jail/exceptions/PrisonerAlreadyJailedException.java b/src/main/java/com/graywolf336/jail/exceptions/PrisonerAlreadyJailedException.java new file mode 100644 index 0000000..2337310 --- /dev/null +++ b/src/main/java/com/graywolf336/jail/exceptions/PrisonerAlreadyJailedException.java @@ -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."); + } +} diff --git a/src/main/java/com/graywolf336/jail/exceptions/PrisonerRequiredException.java b/src/main/java/com/graywolf336/jail/exceptions/PrisonerRequiredException.java new file mode 100644 index 0000000..de2fe1b --- /dev/null +++ b/src/main/java/com/graywolf336/jail/exceptions/PrisonerRequiredException.java @@ -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); + } +} diff --git a/src/test/java/test/java/com/graywolf336/jail/util/TestInstanceCreator.java b/src/test/java/test/java/com/graywolf336/jail/util/TestInstanceCreator.java index 91b2d67..dda0cbd 100644 --- a/src/test/java/test/java/com/graywolf336/jail/util/TestInstanceCreator.java +++ b/src/test/java/test/java/com/graywolf336/jail/util/TestInstanceCreator.java @@ -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);