Implement getting the jail nearest to sender of the jail command if no
jail parameter is defined.
This commit is contained in:
		@@ -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;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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.
 | 
			
		||||
	 * 
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										13
									
								
								src/main/java/com/graywolf336/jail/PrisonerManager.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/main/java/com/graywolf336/jail/PrisonerManager.java
									
									
									
									
									
										Normal 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.");
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -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());
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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");
 | 
			
		||||
	
 | 
			
		||||
 
 | 
			
		||||
@@ -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"));
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user