Implement getting the jail nearest to sender of the jail command if no

jail parameter is defined.
This commit is contained in:
graywolf336 2013-12-24 17:51:41 -06:00
parent 64301db196
commit dc2e74a63e
7 changed files with 91 additions and 13 deletions

View File

@ -13,9 +13,10 @@ import com.graywolf336.jail.listeners.PlayerListener;
import com.graywolf336.jail.listeners.PlayerPreventionsListener; import com.graywolf336.jail.listeners.PlayerPreventionsListener;
public class JailMain extends JavaPlugin { public class JailMain extends JavaPlugin {
private CommandHandler cmdHand;
private JailIO io; private JailIO io;
private JailManager jm; private JailManager jm;
private CommandHandler cmdHand; private PrisonerManager pm;
public void onEnable() { public void onEnable() {
loadConfig(); loadConfig();
@ -27,12 +28,13 @@ public class JailMain extends JavaPlugin {
io.loadJails(); io.loadJails();
cmdHand = new CommandHandler(this); cmdHand = new CommandHandler(this);
pm = new PrisonerManager(this);
PluginManager pm = this.getServer().getPluginManager(); PluginManager plm = this.getServer().getPluginManager();
pm.registerEvents(new BlockListener(), this); plm.registerEvents(new BlockListener(), this);
pm.registerEvents(new EntityListener(), this); plm.registerEvents(new EntityListener(), this);
pm.registerEvents(new PlayerListener(this), this); plm.registerEvents(new PlayerListener(this), this);
pm.registerEvents(new PlayerPreventionsListener(this), this); plm.registerEvents(new PlayerPreventionsListener(this), this);
//For the time, we will use: //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) //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); io.saveJail(j);
cmdHand = null; cmdHand = null;
pm = null;
jm = null; jm = null;
io = null; io = null;
} }
@ -88,4 +91,9 @@ public class JailMain extends JavaPlugin {
public JailManager getJailManager() { public JailManager getJailManager() {
return this.jm; return this.jm;
} }
/** Gets the {@link PrisonerManager} instance. */
public PrisonerManager getPrisonerManager() {
return this.pm;
}
} }

View File

@ -3,6 +3,10 @@ package com.graywolf336.jail;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; 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.CreationPlayer;
import com.graywolf336.jail.beans.Jail; import com.graywolf336.jail.beans.Jail;
import com.graywolf336.jail.beans.Prisoner; import com.graywolf336.jail.beans.Prisoner;
@ -79,6 +83,34 @@ public class JailManager {
return this.jails.get(name); 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. * Checks to see if the given name for a {@link Jail} is valid, returns true if it is a valid jail.
* *

View File

@ -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.");
}
}

View File

@ -245,4 +245,17 @@ public class Jail {
return r; 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());
}
} }

View File

@ -35,7 +35,7 @@ public class JailCommand implements Command {
* - If there are any jails. * - If there are any jails.
* - If the command can be parsed correctly. * - 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 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. * - If the prisoner is online or not.
*/ */
public boolean execute(JailManager jm, CommandSender sender, String... args) { public boolean execute(JailManager jm, CommandSender sender, String... args) {
@ -54,19 +54,30 @@ public class JailCommand implements Command {
return true; return true;
} }
//TODO: Add a way to get the nearest jail if they provide no jail //Check the jail params. If it is empty, let's get the default jail
if(jm.getJail(params.jail()) == null) { //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() + "'."); sender.sendMessage(ChatColor.RED + "No jail found by the name of '" + params.jail() + "'.");
return true; return true;
} }
//Trying out the time.
Long time = 10L; Long time = 10L;
try { try {
time = Util.getTime(params.time()); time = Util.getTime(params.time());
}catch(Exception e) { }catch(Exception e) {
try { 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) { }catch(Exception e2) {
sender.sendMessage(ChatColor.RED + "Number format is incorrect."); sender.sendMessage(ChatColor.RED + "Number format is incorrect.");
return true; return true;
@ -74,7 +85,7 @@ public class JailCommand implements Command {
} }
Jail j = jm.getJail(params.jail()); 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()); Player p = jm.getPlugin().getServer().getPlayer(params.player());
//call the event //call the event

View File

@ -2,6 +2,7 @@ package com.graywolf336.jail.enums;
public enum Settings { public enum Settings {
DEBUG("system.debug"), DEBUG("system.debug"),
DEFAULTJAIL("jailing.jail.defaultJail"),
UPDATENOTIFICATIONS("system.updateNotifications"), UPDATENOTIFICATIONS("system.updateNotifications"),
JAILDEFAULTTIME("jailing.jail.defaultTime"); JAILDEFAULTTIME("jailing.jail.defaultTime");

View File

@ -63,7 +63,7 @@ public class TestJailStuff {
assertTrue("Default setting for automatically muting is false.", main.getConfig().getBoolean("jailing.jail.automaticMute")); 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")); 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 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")); 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")); 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")); assertTrue("Default setting for logging to console when someone is jailed is false.", main.getConfig().getBoolean("jailing.jail.logToConsole"));