From 709a06f330d02b7b428a996f3fc5c8c25edc08a5 Mon Sep 17 00:00:00 2001 From: graywolf336 Date: Wed, 23 Jul 2014 16:39:45 -0500 Subject: [PATCH] Add an API class and also add some testing for it. --- .../com/graywolf336/jail/HandCuffManager.java | 14 ++++ .../java/com/graywolf336/jail/JailMain.java | 7 ++ .../java/com/graywolf336/jail/JailsAPI.java | 51 ++++++++++++++ .../com/graywolf336/jail/PrisonerManager.java | 68 ++++++++++++++++-- .../com/graywolf336/jail/BenchmarkTest.java | 1 + .../com/graywolf336/jail/TestJailAPI.java | 69 +++++++++++++++++++ 6 files changed, 206 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/graywolf336/jail/JailsAPI.java create mode 100644 src/test/java/test/java/com/graywolf336/jail/TestJailAPI.java diff --git a/src/main/java/com/graywolf336/jail/HandCuffManager.java b/src/main/java/com/graywolf336/jail/HandCuffManager.java index 2123f1d..17a0ff2 100644 --- a/src/main/java/com/graywolf336/jail/HandCuffManager.java +++ b/src/main/java/com/graywolf336/jail/HandCuffManager.java @@ -6,6 +6,20 @@ import java.util.UUID; import org.bukkit.Location; /** + * Manages the handcuffing of players. + * + *

+ * + * Provides easy to use methods for adding, + * removing, and checking if someone has + * handcuffs. + * + *

* * @author graywolf336 * @since 2.6.3 diff --git a/src/main/java/com/graywolf336/jail/JailMain.java b/src/main/java/com/graywolf336/jail/JailMain.java index 2476ab1..1f66c5b 100644 --- a/src/main/java/com/graywolf336/jail/JailMain.java +++ b/src/main/java/com/graywolf336/jail/JailMain.java @@ -113,6 +113,7 @@ public class JailMain extends JavaPlugin { reloadJailPayManager(); reloadUpdateCheck(); + new JailsAPI(this); getLogger().info("Completed enablement."); } @@ -300,6 +301,12 @@ public class JailMain extends JavaPlugin { if(inDebug()) getLogger().info("[Debug]: " + message); } + /** + * This method is only for testing, there is no need to use this. + * + * @return the move protection listener + * @deprecated + */ public MoveProtectionListener getPlayerMoveListener() { return this.mpl; } diff --git a/src/main/java/com/graywolf336/jail/JailsAPI.java b/src/main/java/com/graywolf336/jail/JailsAPI.java new file mode 100644 index 0000000..777dfd2 --- /dev/null +++ b/src/main/java/com/graywolf336/jail/JailsAPI.java @@ -0,0 +1,51 @@ +package com.graywolf336.jail; + +/** + * The static api interface for Jail plugin. + * + *

+ * + * If you're looking for non-static methods, please see the + * {@link JailMain} interface. + * + * @author graywolf336 + * @version 3.0.0 + * @since 3.0.0 + */ +public class JailsAPI { + private static JailMain pl; + + public JailsAPI(JailMain plugin) { + pl = plugin; + } + + /** + * The instance of the {@link JailManager} which contains all the jails and in return prisoners. + * + * @return instance of the {@link JailManager} + * @see JailManager + */ + public static JailManager getJailManager() { + return pl.getJailManager(); + } + + /** + * The instance of the {@link PrisonerManager} which handles all the jailing of players. + * + * @return instance of the {@link PrisonerManager} + * @see PrisonerManager + */ + public static PrisonerManager getPrisonerManager() { + return pl.getPrisonerManager(); + } + + /** + * The instance of the {@link HandCuffManager} which handles all the handcuffing of players. + * + * @return instance of the {@link HandCuffManager} + * @see HandCuffManager + */ + public static HandCuffManager getHandCuffManager() { + return pl.getHandCuffManager(); + } +} diff --git a/src/main/java/com/graywolf336/jail/PrisonerManager.java b/src/main/java/com/graywolf336/jail/PrisonerManager.java index c78d41a..3233854 100644 --- a/src/main/java/com/graywolf336/jail/PrisonerManager.java +++ b/src/main/java/com/graywolf336/jail/PrisonerManager.java @@ -22,7 +22,19 @@ import com.graywolf336.jail.events.PrisonerReleasedEvent; import com.graywolf336.jail.events.PrisonerTransferredEvent; /** - * Provides methods, non-statically, that do the preparing of jails and handle all the good stuff like that. + * Provides methods, non-statically, that do the preparing of jails, jailing, etc. + * + *

+ * + *

* * @author graywolf336 * @since 2.x.x @@ -112,6 +124,11 @@ public class PrisonerManager { } } + /** + * Jails the given player, only use when that player has offline data pending. + * + * @param uuid of the player to jail. + */ public void jailPlayer(UUID uuid) { Jail j = pl.getJailManager().getJailPlayerIsIn(uuid); jailPrisoner(j, j.getCellPrisonerIsIn(uuid), pl.getServer().getPlayer(uuid), j.getPrisoner(uuid)); @@ -313,9 +330,10 @@ public class PrisonerManager { } /** - * Schedules a prisoner to be released. + * Schedules a prisoner to be released, this method is to be used async. * * @param prisoner to be released. + * @see {@link #unJail(Jail, Cell, Player, Prisoner, CommandSender)} - If you're wanting to unjail a prisoner. */ public void schedulePrisonerRelease(Prisoner prisoner) { releases.add(prisoner); @@ -481,13 +499,55 @@ public class PrisonerManager { if(sender != null) sender.sendMessage(pl.getJailIO().getLanguageString(LangString.UNJAILSUCCESS, player.getName())); } - /** Forcefully releases a {@link Prisoner prisoner} from {@link Jail}. */ + /** + * Forcefully unjails a {@link Prisoner prisoner} from {@link Jail}. + * + *

+ * + * This method forcefully removes all the references to this prisoner, + * meaning if they're offline the following won't happened: + *

+ * + * But if they're online, it goes through the regular unjailing methods. + * + *

+ * + * @param prisoner to release + * @param sender who is releasing the prisoner, can be null + */ public void forceRelease(Prisoner prisoner, CommandSender sender) { Jail j = pl.getJailManager().getJailPrisonerIsIn(prisoner); forceUnJail(j, j.getCellPrisonerIsIn(prisoner.getUUID()), pl.getServer().getPlayer(prisoner.getUUID()), prisoner, sender); } - /** Forcefully unjails a {@link Prisoner prisoner} from {@link Jail}. */ + /** + * Forcefully unjails a {@link Prisoner prisoner} from {@link Jail}. + * + * + *

+ * + * This method forcefully removes all the references to this prisoner, + * meaning if they're offline the following won't happened: + *

+ * + * But if they're online, it goes through the regular unjailing methods. + * + *

+ * + * @param jail the prisoner is in + * @param cell the prisoner is in, can be null + * @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 + */ public void forceUnJail(Jail jail, Cell cell, Player player, Prisoner prisoner, CommandSender sender) { if(player == null) { //Player is offline, we just forcefully remove them from the database diff --git a/src/test/java/test/java/com/graywolf336/jail/BenchmarkTest.java b/src/test/java/test/java/com/graywolf336/jail/BenchmarkTest.java index e717c9e..8736761 100644 --- a/src/test/java/test/java/com/graywolf336/jail/BenchmarkTest.java +++ b/src/test/java/test/java/com/graywolf336/jail/BenchmarkTest.java @@ -81,6 +81,7 @@ public class BenchmarkTest extends AbstractBenchmark { assertTrue("Prisoner 555 is not jailed", main.getJailManager().isPlayerJailed(use)); } + @SuppressWarnings("deprecation") @BenchmarkOptions(benchmarkRounds = 5000, warmupRounds = 0) @Test public void testPlayerMoveEvent() { diff --git a/src/test/java/test/java/com/graywolf336/jail/TestJailAPI.java b/src/test/java/test/java/com/graywolf336/jail/TestJailAPI.java new file mode 100644 index 0000000..56654d5 --- /dev/null +++ b/src/test/java/test/java/com/graywolf336/jail/TestJailAPI.java @@ -0,0 +1,69 @@ +package test.java.com.graywolf336.jail; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.hamcrest.CoreMatchers.is; + +import java.util.UUID; + +import org.bukkit.Location; +import org.bukkit.plugin.PluginDescriptionFile; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import test.java.com.graywolf336.jail.util.TestInstanceCreator; + +import com.graywolf336.jail.JailMain; +import com.graywolf336.jail.JailsAPI; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ JailMain.class, PluginDescriptionFile.class }) +public class TestJailAPI { + private static TestInstanceCreator creator; + private static JailMain main; + + @BeforeClass + public static void setUp() throws Exception { + creator = new TestInstanceCreator(); + assertNotNull("The instance creator is null.", creator); + assertTrue(creator.setup()); + main = creator.getMain(); + assertNotNull("The JailMain class is null.", main); + } + + @AfterClass + public static void tearDown() throws Exception { + creator.tearDown(); + main = null; + } + + @Test + public void testManagersAreThere() { + assertNotNull(main.getHandCuffManager()); + assertNotNull(main.getJailManager()); + assertNotNull(main.getPrisonerManager()); + } + + @Test + public void testHandCuffManagerAPI() { + UUID id = UUID.randomUUID(); + Location loc = new Location(main.getServer().getWorld("world"), 11.469868464778077, 65.0, -239.27944647045672, Float.valueOf("38.499817"), Float.valueOf("2.0000453")); + assertThat("The HandCuff Managers are different.", JailsAPI.getHandCuffManager(), is(main.getHandCuffManager())); + assertFalse("The test id of someone is already handcuffed.", JailsAPI.getHandCuffManager().isHandCuffed(id)); + + JailsAPI.getHandCuffManager().addHandCuffs(id, loc); + assertTrue(JailsAPI.getHandCuffManager().isHandCuffed(id)); + assertThat(JailsAPI.getHandCuffManager().getLocation(id), is(loc)); + + JailsAPI.getHandCuffManager().removeHandCuffs(id); + assertFalse(JailsAPI.getHandCuffManager().isHandCuffed(id)); + assertNull(JailsAPI.getHandCuffManager().getLocation(id)); + } +}