From dc2e74a63e430e5ce8a1a5d2c5bf330376f84352 Mon Sep 17 00:00:00 2001 From: graywolf336 Date: Tue, 24 Dec 2013 17:51:41 -0600 Subject: [PATCH] Implement getting the jail nearest to sender of the jail command if no jail parameter is defined. --- .../java/com/graywolf336/jail/JailMain.java | 20 ++++++++---- .../com/graywolf336/jail/JailManager.java | 32 +++++++++++++++++++ .../com/graywolf336/jail/PrisonerManager.java | 13 ++++++++ .../java/com/graywolf336/jail/beans/Jail.java | 13 ++++++++ .../jail/command/commands/JailCommand.java | 23 +++++++++---- .../com/graywolf336/jail/enums/Settings.java | 1 + .../com/graywolf336/jail/TestJailStuff.java | 2 +- 7 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/graywolf336/jail/PrisonerManager.java diff --git a/src/main/java/com/graywolf336/jail/JailMain.java b/src/main/java/com/graywolf336/jail/JailMain.java index fbb7809..049cfc0 100644 --- a/src/main/java/com/graywolf336/jail/JailMain.java +++ b/src/main/java/com/graywolf336/jail/JailMain.java @@ -13,9 +13,10 @@ import com.graywolf336.jail.listeners.PlayerListener; import com.graywolf336.jail.listeners.PlayerPreventionsListener; public class JailMain extends JavaPlugin { + private CommandHandler cmdHand; private JailIO io; private JailManager jm; - private CommandHandler cmdHand; + private PrisonerManager pm; public void onEnable() { loadConfig(); @@ -27,12 +28,13 @@ public class JailMain extends JavaPlugin { io.loadJails(); cmdHand = new CommandHandler(this); + pm = new PrisonerManager(this); - PluginManager pm = this.getServer().getPluginManager(); - pm.registerEvents(new BlockListener(), this); - pm.registerEvents(new EntityListener(), this); - pm.registerEvents(new PlayerListener(this), this); - pm.registerEvents(new PlayerPreventionsListener(this), this); + PluginManager plm = this.getServer().getPluginManager(); + plm.registerEvents(new BlockListener(), this); + plm.registerEvents(new EntityListener(), this); + plm.registerEvents(new PlayerListener(this), this); + plm.registerEvents(new PlayerPreventionsListener(this), this); //For the time, we will use: //http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html#convert(long, java.util.concurrent.TimeUnit) @@ -44,6 +46,7 @@ public class JailMain extends JavaPlugin { io.saveJail(j); cmdHand = null; + pm = null; jm = null; io = null; } @@ -88,4 +91,9 @@ public class JailMain extends JavaPlugin { public JailManager getJailManager() { return this.jm; } + + /** Gets the {@link PrisonerManager} instance. */ + public PrisonerManager getPrisonerManager() { + return this.pm; + } } diff --git a/src/main/java/com/graywolf336/jail/JailManager.java b/src/main/java/com/graywolf336/jail/JailManager.java index 5364941..5834098 100644 --- a/src/main/java/com/graywolf336/jail/JailManager.java +++ b/src/main/java/com/graywolf336/jail/JailManager.java @@ -3,6 +3,10 @@ package com.graywolf336.jail; import java.util.HashMap; import java.util.HashSet; +import org.bukkit.Location; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + import com.graywolf336.jail.beans.CreationPlayer; import com.graywolf336.jail.beans.Jail; import com.graywolf336.jail.beans.Prisoner; @@ -79,6 +83,34 @@ public class JailManager { return this.jails.get(name); } + /** + * Gets the nearest {@link Jail} to the player, if the sender is a player or else it will get the first jail defined. + * + * @param sender The sender who we are looking around. + * @return The nearest {@link Jail} to the sender if it is a player or else the first jail defined. + */ + public Jail getNearestJail(CommandSender sender) { + if(sender instanceof Player) { + Location loc = ((Player) sender).getLocation(); + + Jail j = null; + double len = -1; + + for(Jail jail : jails.values()) { + double clen = jail.getDistance(loc); + + if (clen < len || len == -1) { + len = clen; + j = jail; + } + } + + return (j == null ? jails.values().iterator().next() : j); + }else { + return jails.values().iterator().next(); + } + } + /** * Checks to see if the given name for a {@link Jail} is valid, returns true if it is a valid jail. * diff --git a/src/main/java/com/graywolf336/jail/PrisonerManager.java b/src/main/java/com/graywolf336/jail/PrisonerManager.java new file mode 100644 index 0000000..189e1eb --- /dev/null +++ b/src/main/java/com/graywolf336/jail/PrisonerManager.java @@ -0,0 +1,13 @@ +package com.graywolf336.jail; + +public class PrisonerManager { + private JailMain pl; + + public PrisonerManager(JailMain plugin) { + this.pl = plugin; + } + + public void prepareSomething() { + pl.getLogger().info("Preparing something."); + } +} diff --git a/src/main/java/com/graywolf336/jail/beans/Jail.java b/src/main/java/com/graywolf336/jail/beans/Jail.java index 7df1d92..ae310e0 100644 --- a/src/main/java/com/graywolf336/jail/beans/Jail.java +++ b/src/main/java/com/graywolf336/jail/beans/Jail.java @@ -245,4 +245,17 @@ public class Jail { return r; } + + /** + * Returns the squared distance between teleport location of this jail + * and specified location in blocks. If locations are not in same world, + * distance cannot be calculated and it will return Integer.MAX_VALUE. + * + * @param loc The location to check + * @return Distance between the location provided and the teleport in location. + */ + public double getDistance(Location loc) { + if (loc.getWorld().getName().equalsIgnoreCase(getTeleportIn().getWorld().getName())) return (double) Integer.MAX_VALUE; + else return loc.distance(getTeleportIn()); + } } diff --git a/src/main/java/com/graywolf336/jail/command/commands/JailCommand.java b/src/main/java/com/graywolf336/jail/command/commands/JailCommand.java index bd744ec..588a795 100644 --- a/src/main/java/com/graywolf336/jail/command/commands/JailCommand.java +++ b/src/main/java/com/graywolf336/jail/command/commands/JailCommand.java @@ -35,7 +35,7 @@ public class JailCommand implements Command { * - If there are any jails. * - If the command can be parsed correctly. * - If the specified jail is null (TODO: if no jail given then find one close to them) - * - If the given time can be parsed correctly, defaults to 10 minutes at the moment TODO + * - If the given time can be parsed correctly, defaults to what is defined in the config * - If the prisoner is online or not. */ public boolean execute(JailManager jm, CommandSender sender, String... args) { @@ -54,19 +54,30 @@ public class JailCommand implements Command { return true; } - //TODO: Add a way to get the nearest jail if they provide no jail - if(jm.getJail(params.jail()) == null) { + //Check the jail params. If it is empty, let's get the default jail + //from the config. If that is nearest, let's make a call to getting the nearest jail to + //the sender but otherwise if it isn't nearest then let's set it to the default jail + //which is defined in the config. + if(params.jail().isEmpty()) { + String dJail = jm.getPlugin().getConfig().getString(Settings.DEFAULTJAIL.getPath()); + + if(dJail.equalsIgnoreCase("nearest")) { + params.setJail(jm.getNearestJail(sender).getName()); + }else { + params.setJail(jm.getPlugin().getConfig().getString(Settings.DEFAULTJAIL.getPath())); + } + }else if(jm.getJail(params.jail()) == null) { sender.sendMessage(ChatColor.RED + "No jail found by the name of '" + params.jail() + "'."); return true; } + //Trying out the time. Long time = 10L; - try { time = Util.getTime(params.time()); }catch(Exception e) { try { - time = Util.getTime(jm.getPlugin().getConfig().getString(Settings.JAILDEFAULTTIME.getPath())); + time = Util.getTime(jm.getPlugin().getConfig().getString(Settings.JAILDEFAULTTIME.getPath(), "10m")); }catch(Exception e2) { sender.sendMessage(ChatColor.RED + "Number format is incorrect."); return true; @@ -74,7 +85,7 @@ public class JailCommand implements Command { } Jail j = jm.getJail(params.jail()); - Prisoner pris = new Prisoner(params.player(), params.muted(), TimeUnit.MILLISECONDS.convert(time, TimeUnit.MINUTES)); + Prisoner pris = new Prisoner(params.player(), params.muted(), time); Player p = jm.getPlugin().getServer().getPlayer(params.player()); //call the event diff --git a/src/main/java/com/graywolf336/jail/enums/Settings.java b/src/main/java/com/graywolf336/jail/enums/Settings.java index 2834a12..19937d5 100644 --- a/src/main/java/com/graywolf336/jail/enums/Settings.java +++ b/src/main/java/com/graywolf336/jail/enums/Settings.java @@ -2,6 +2,7 @@ package com.graywolf336.jail.enums; public enum Settings { DEBUG("system.debug"), + DEFAULTJAIL("jailing.jail.defaultJail"), UPDATENOTIFICATIONS("system.updateNotifications"), JAILDEFAULTTIME("jailing.jail.defaultTime"); diff --git a/src/test/java/test/java/com/graywolf336/jail/TestJailStuff.java b/src/test/java/test/java/com/graywolf336/jail/TestJailStuff.java index 13d7bba..8f99b20 100644 --- a/src/test/java/test/java/com/graywolf336/jail/TestJailStuff.java +++ b/src/test/java/test/java/com/graywolf336/jail/TestJailStuff.java @@ -63,7 +63,7 @@ public class TestJailStuff { assertTrue("Default setting for automatically muting is false.", main.getConfig().getBoolean("jailing.jail.automaticMute")); assertFalse("Default setting for broadcasting a jailing is true.", main.getConfig().getBoolean("jailing.jail.broadcastJailing")); assertEquals("Default setting for commands contains information.", 0, main.getConfig().getList("jailing.jail.commands").size()); - assertEquals("Default setting for default jail is not nearest but something else.", "nearest", main.getConfig().getString("jailing.jail.defaultJail")); + assertEquals("Default setting for default jail is not 'nearest' but something else.", "nearest", main.getConfig().getString("jailing.jail.defaultJail")); assertEquals("Default setting for time is not 30 minutes.", "30m", main.getConfig().getString("jailing.jail.defaultTime")); assertFalse("Default setting for deleting inventory is true.", main.getConfig().getBoolean("jailing.jail.deleteInventory")); assertTrue("Default setting for logging to console when someone is jailed is false.", main.getConfig().getBoolean("jailing.jail.logToConsole"));