Make the jail stick manager/creation smarter and fix jailing via jail
sticks, as it wasn't working at all.
This commit is contained in:
		| @@ -78,7 +78,6 @@ public class JailMain extends JavaPlugin { | |||||||
| 		cmdHand = new CommandHandler(this); | 		cmdHand = new CommandHandler(this); | ||||||
| 		jh = new JailHandler(this); | 		jh = new JailHandler(this); | ||||||
| 		pm = new PrisonerManager(this); | 		pm = new PrisonerManager(this); | ||||||
| 		jsm = new JailStickManager(this); |  | ||||||
| 		 | 		 | ||||||
| 		PluginManager plm = this.getServer().getPluginManager(); | 		PluginManager plm = this.getServer().getPluginManager(); | ||||||
| 		plm.registerEvents(new BlockListener(this), this); | 		plm.registerEvents(new BlockListener(this), this); | ||||||
| @@ -101,6 +100,10 @@ public class JailMain extends JavaPlugin { | |||||||
| 			plm.registerEvents(this.mpl, this); | 			plm.registerEvents(this.mpl, this); | ||||||
| 		} | 		} | ||||||
| 		 | 		 | ||||||
|  | 		if(getConfig().getBoolean(Settings.JAILSTICKENABLED.getPath())) { | ||||||
|  | 			jsm = new JailStickManager(this); | ||||||
|  | 		} | ||||||
|  | 		 | ||||||
| 		jt = new JailTimer(this); | 		jt = new JailTimer(this); | ||||||
| 		 | 		 | ||||||
| 		reloadJailPayManager(); | 		reloadJailPayManager(); | ||||||
| @@ -188,10 +191,12 @@ public class JailMain extends JavaPlugin { | |||||||
| 	 | 	 | ||||||
| 	/** Reloads the Jail Sticks, so the new ones can be loaded from the config. */ | 	/** Reloads the Jail Sticks, so the new ones can be loaded from the config. */ | ||||||
| 	public void reloadJailSticks() { | 	public void reloadJailSticks() { | ||||||
|  | 		if(getConfig().getBoolean(Settings.JAILSTICKENABLED.getPath())) { | ||||||
| 			this.jsm.removeAllStickUsers(); | 			this.jsm.removeAllStickUsers(); | ||||||
| 			this.jsm = null; | 			this.jsm = null; | ||||||
| 			this.jsm = new JailStickManager(this); | 			this.jsm = new JailStickManager(this); | ||||||
| 		} | 		} | ||||||
|  | 	} | ||||||
| 	 | 	 | ||||||
| 	/**  | 	/**  | ||||||
| 	 * Reloads the {@link JailPayManager}. | 	 * Reloads the {@link JailPayManager}. | ||||||
|   | |||||||
| @@ -101,7 +101,7 @@ public class JailManager { | |||||||
| 	 * @return The {@link Jail} with the given name, if no jail found this <strong>will</strong> return null. | 	 * @return The {@link Jail} with the given name, if no jail found this <strong>will</strong> return null. | ||||||
| 	 */ | 	 */ | ||||||
| 	public Jail getJail(String name) { | 	public Jail getJail(String name) { | ||||||
| 		return this.jails.get(name); | 		return name.isEmpty() ? this.jails.values().iterator().next() : this.jails.get(name); | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	/** | 	/** | ||||||
|   | |||||||
| @@ -2,10 +2,9 @@ package com.graywolf336.jail; | |||||||
|  |  | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| import java.util.HashMap; | import java.util.HashMap; | ||||||
|  | import java.util.UUID; | ||||||
|  |  | ||||||
| import org.bukkit.Material; | import org.bukkit.Material; | ||||||
| import org.bukkit.configuration.file.FileConfiguration; |  | ||||||
| import org.bukkit.entity.Player; |  | ||||||
|  |  | ||||||
| import com.graywolf336.jail.beans.Stick; | import com.graywolf336.jail.beans.Stick; | ||||||
| import com.graywolf336.jail.enums.Settings; | import com.graywolf336.jail.enums.Settings; | ||||||
| @@ -19,27 +18,31 @@ import com.graywolf336.jail.enums.Settings; | |||||||
|  * |  * | ||||||
|  */ |  */ | ||||||
| public class JailStickManager { | public class JailStickManager { | ||||||
| 	private ArrayList<String> stickers; | 	private ArrayList<UUID> stickers; | ||||||
| 	private HashMap<Material, Stick> sticks; | 	private HashMap<Material, Stick> sticks; | ||||||
| 	 | 	 | ||||||
| 	public JailStickManager(JailMain plugin) { | 	public JailStickManager(JailMain plugin) { | ||||||
| 		this.stickers = new ArrayList<String>(); | 		this.stickers = new ArrayList<UUID>(); | ||||||
| 		this.sticks = new HashMap<Material, Stick>(); | 		this.sticks = new HashMap<Material, Stick>(); | ||||||
| 		 | 		 | ||||||
| 		this.loadJailSticks(plugin); | 		this.loadJailSticks(plugin); | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	private void loadJailSticks(JailMain pl) { | 	private void loadJailSticks(JailMain pl) { | ||||||
| 		FileConfiguration config =  pl.getConfig(); |  | ||||||
| 		 |  | ||||||
| 		//item name,time,jail name,reason | 		//item name,time,jail name,reason | ||||||
| 		for(String s : config.getStringList(Settings.JAILSTICKSTICKS.getPath())) { | 		if(pl.getJailManager().getJails().size() == 0) { | ||||||
|  | 			pl.getLogger().warning("Can't have jail sticks without any jails."); | ||||||
|  | 		}else { | ||||||
|  | 			for(String s : pl.getConfig().getStringList(Settings.JAILSTICKSTICKS.getPath())) { | ||||||
| 				pl.debug(s); | 				pl.debug(s); | ||||||
| 				String[] a = s.split(","); | 				String[] a = s.split(","); | ||||||
|  | 				String jail = a[2]; | ||||||
| 				 | 				 | ||||||
| 				//Check if the jail given, if any, exists | 				//Check if the jail given, if any, exists | ||||||
| 			if(!a[2].isEmpty()) { | 				if(jail.isEmpty()) { | ||||||
| 				if(!pl.getJailManager().isValidJail(a[2])) { | 					jail = pl.getJailManager().getJail("").getName(); | ||||||
|  | 				}else { | ||||||
|  | 					if(!pl.getJailManager().isValidJail(jail)) { | ||||||
| 						pl.getLogger().severe(s); | 						pl.getLogger().severe(s); | ||||||
| 						pl.getLogger().severe("The above jail stick configuration is invalid and references a jail that doesn't exist."); | 						pl.getLogger().severe("The above jail stick configuration is invalid and references a jail that doesn't exist."); | ||||||
| 						continue; | 						continue; | ||||||
| @@ -53,8 +56,17 @@ public class JailStickManager { | |||||||
| 					continue; | 					continue; | ||||||
| 				} | 				} | ||||||
| 				 | 				 | ||||||
|  | 				long time = 5; | ||||||
| 				try { | 				try { | ||||||
| 				this.sticks.put(m, new Stick(a[2], a[3], Long.valueOf(a[1]), Double.valueOf(a[4]))); | 					 time = Util.getTime(a[1]); | ||||||
|  | 				} catch (Exception e) { | ||||||
|  | 					pl.getLogger().severe(s); | ||||||
|  | 					pl.getLogger().severe("The time format on the above jail stick configuration is incorrect."); | ||||||
|  | 					continue; | ||||||
|  | 				} | ||||||
|  | 				 | ||||||
|  | 				try { | ||||||
|  | 					this.sticks.put(m, new Stick(jail, a[3], time, Double.valueOf(a[4]))); | ||||||
| 				}catch (Exception e) { | 				}catch (Exception e) { | ||||||
| 					e.printStackTrace(); | 					e.printStackTrace(); | ||||||
| 					pl.getLogger().severe(s); | 					pl.getLogger().severe(s); | ||||||
| @@ -62,6 +74,7 @@ public class JailStickManager { | |||||||
| 					continue; | 					continue; | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
|  | 		} | ||||||
| 		 | 		 | ||||||
| 		int c = sticks.size(); | 		int c = sticks.size(); | ||||||
| 		pl.getLogger().info("Loaded " + c + " jail stick" + (c == 1 ? "" : "s") + "."); | 		pl.getLogger().info("Loaded " + c + " jail stick" + (c == 1 ? "" : "s") + "."); | ||||||
| @@ -83,91 +96,53 @@ public class JailStickManager { | |||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	/** | 	/** | ||||||
| 	 * Adds a player to be using a jail stick, with the player instance. | 	 * Adds a player to be using a jail stick, with the uuid of the player. | ||||||
| 	 *  | 	 *  | ||||||
| 	 * @param player to add | 	 * @param id of the player to add | ||||||
| 	 */ | 	 */ | ||||||
| 	public void addUsingStick(Player player) { | 	public void addUsingStick(UUID id) { | ||||||
| 		this.stickers.add(player.getName()); | 		this.stickers.add(id); | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	/** | 	/** | ||||||
| 	 * Adds a player to be using a jail stick, with their username. | 	 * Removes a player from using a jail stick, with the uuid of the player. | ||||||
| 	 *  | 	 *  | ||||||
| 	 * @param name of the player to add | 	 * @param id of the player to remove using a jail stick | ||||||
| 	 */ | 	 */ | ||||||
| 	public void addUsingStick(String name) { | 	public void removeUsingStick(UUID id) { | ||||||
| 		this.stickers.add(name); | 		this.stickers.remove(id); | ||||||
| 	} |  | ||||||
| 	 |  | ||||||
| 	/** |  | ||||||
| 	 * Removes a player from using a jail stick, with the player instance. |  | ||||||
| 	 *  |  | ||||||
| 	 * @param player to remove using a jail stick |  | ||||||
| 	 */ |  | ||||||
| 	public void removeUsingStick(Player player) { |  | ||||||
| 		this.stickers.remove(player.getName()); |  | ||||||
| 	} |  | ||||||
| 	 |  | ||||||
| 	/** |  | ||||||
| 	 * Removes a player from using a jail stick, with their username. |  | ||||||
| 	 *  |  | ||||||
| 	 * @param name of the player to remove using a jail stick |  | ||||||
| 	 */ |  | ||||||
| 	public void removeUsingStick(String name) { |  | ||||||
| 		this.stickers.remove(name); |  | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	/** | 	/** | ||||||
| 	 * Returns whether or not the player is using a jail stick. | 	 * Returns whether or not the player is using a jail stick. | ||||||
| 	 *  | 	 *  | ||||||
| 	 * @param player to check if using one | 	 * @param id of the player to check if using one | ||||||
| 	 * @return true if the player is using a jail stick, false if not | 	 * @return true if the player is using a jail stick, false if not | ||||||
| 	 */ | 	 */ | ||||||
| 	public boolean isUsingJailStick(Player player) { | 	public boolean isUsingJailStick(UUID id) { | ||||||
| 		return this.stickers.contains(player.getName()); | 		return this.stickers.contains(id); | ||||||
| 	} |  | ||||||
| 	 |  | ||||||
| 	/** |  | ||||||
| 	 * Returns whether or not the player is using a jail stick. |  | ||||||
| 	 *  |  | ||||||
| 	 * @param name of the player to check if using one |  | ||||||
| 	 * @return true if the player is using a jail stick, false if not |  | ||||||
| 	 */ |  | ||||||
| 	public boolean isUsingJailStick(String name) { |  | ||||||
| 		return this.stickers.contains(name); |  | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	/** | 	/** | ||||||
| 	 * Toggles whether the player is using a jail stick, returning the true if enabled false if disabled. | 	 * Toggles whether the player is using a jail stick, returning the true if enabled false if disabled. | ||||||
| 	 *  | 	 *  | ||||||
| 	 * @param player to toggle using a stick | 	 * @param id of the player to toggle using a stick | ||||||
| 	 * @return true if we enabled it, false if we disabled it. | 	 * @return true if we enabled it, false if we disabled it. | ||||||
| 	 */ | 	 */ | ||||||
| 	public boolean toggleUsingStick(Player player) { | 	public boolean toggleUsingStick(UUID id) { | ||||||
| 		return this.toggleUsingStick(player.getName()); | 		if(this.stickers.contains(id)) { | ||||||
| 	} | 			this.stickers.remove(id); | ||||||
| 	 |  | ||||||
| 	/** |  | ||||||
| 	 * Toggles whether the player is using a jail stick, returning the true if enabled false if disabled. |  | ||||||
| 	 *  |  | ||||||
| 	 * @param name of the person to toggle |  | ||||||
| 	 * @return true if we enabled it, false if we disabled it. |  | ||||||
| 	 */ |  | ||||||
| 	public boolean toggleUsingStick(String name) { |  | ||||||
| 		if(this.stickers.contains(name)) { |  | ||||||
| 			this.stickers.remove(name); |  | ||||||
| 			return false; | 			return false; | ||||||
| 		}else { | 		}else { | ||||||
| 			this.stickers.add(name); | 			this.stickers.add(id); | ||||||
| 			return true; | 			return true; | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	/**  Removes all the users currently using the sticks. */ | 	/**  Removes all the users currently using the sticks. */ | ||||||
| 	public void removeAllStickUsers() { | 	public void removeAllStickUsers() { | ||||||
| 		for(String s: stickers) { | 		for(UUID id : stickers) { | ||||||
| 			this.removeUsingStick(s); | 			this.removeUsingStick(id); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
| @@ -27,6 +27,7 @@ import com.graywolf336.jail.command.subcommands.JailPayCommand; | |||||||
| import com.graywolf336.jail.command.subcommands.JailRecordCommand; | import com.graywolf336.jail.command.subcommands.JailRecordCommand; | ||||||
| import com.graywolf336.jail.command.subcommands.JailReloadCommand; | import com.graywolf336.jail.command.subcommands.JailReloadCommand; | ||||||
| import com.graywolf336.jail.command.subcommands.JailStatusCommand; | import com.graywolf336.jail.command.subcommands.JailStatusCommand; | ||||||
|  | import com.graywolf336.jail.command.subcommands.JailStickCommand; | ||||||
| import com.graywolf336.jail.command.subcommands.JailStopCommand; | import com.graywolf336.jail.command.subcommands.JailStopCommand; | ||||||
| import com.graywolf336.jail.command.subcommands.JailTeleInCommand; | import com.graywolf336.jail.command.subcommands.JailTeleInCommand; | ||||||
| import com.graywolf336.jail.command.subcommands.JailTeleOutCommand; | import com.graywolf336.jail.command.subcommands.JailTeleOutCommand; | ||||||
| @@ -190,6 +191,7 @@ public class JailHandler { | |||||||
| 		load(JailRecordCommand.class); | 		load(JailRecordCommand.class); | ||||||
| 		load(JailReloadCommand.class); | 		load(JailReloadCommand.class); | ||||||
| 		load(JailStatusCommand.class); | 		load(JailStatusCommand.class); | ||||||
|  | 		load(JailStickCommand.class); | ||||||
| 		load(JailStopCommand.class); | 		load(JailStopCommand.class); | ||||||
| 		load(JailTeleInCommand.class); | 		load(JailTeleInCommand.class); | ||||||
| 		load(JailTeleOutCommand.class); | 		load(JailTeleOutCommand.class); | ||||||
|   | |||||||
| @@ -1,6 +1,7 @@ | |||||||
| package com.graywolf336.jail.command.subcommands; | package com.graywolf336.jail.command.subcommands; | ||||||
|  |  | ||||||
| import org.bukkit.command.CommandSender; | import org.bukkit.command.CommandSender; | ||||||
|  | import org.bukkit.entity.Player; | ||||||
|  |  | ||||||
| import com.graywolf336.jail.JailManager; | import com.graywolf336.jail.JailManager; | ||||||
| import com.graywolf336.jail.command.Command; | import com.graywolf336.jail.command.Command; | ||||||
| @@ -19,7 +20,7 @@ import com.graywolf336.jail.enums.Settings; | |||||||
| public class JailStickCommand implements Command { | public class JailStickCommand implements Command { | ||||||
| 	public boolean execute(JailManager jm, CommandSender sender, String... args) throws Exception { | 	public boolean execute(JailManager jm, CommandSender sender, String... args) throws Exception { | ||||||
| 		if(jm.getPlugin().getConfig().getBoolean(Settings.JAILSTICKENABLED.getPath())) { | 		if(jm.getPlugin().getConfig().getBoolean(Settings.JAILSTICKENABLED.getPath())) { | ||||||
| 			boolean using = jm.getPlugin().getJailStickManager().toggleUsingStick(sender.getName()); | 			boolean using = jm.getPlugin().getJailStickManager().toggleUsingStick(((Player) sender).getUniqueId()); | ||||||
| 			 | 			 | ||||||
| 			if(using) { | 			if(using) { | ||||||
| 				sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.JAILSTICKENABLED)); | 				sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.JAILSTICKENABLED)); | ||||||
|   | |||||||
| @@ -211,7 +211,7 @@ public class PlayerListener implements Listener { | |||||||
| 		Player attacker = (Player) event.getDamager(); | 		Player attacker = (Player) event.getDamager(); | ||||||
| 		Player player = (Player) event.getEntity(); | 		Player player = (Player) event.getEntity(); | ||||||
| 		 | 		 | ||||||
| 		if(pl.getJailStickManager().isUsingJailStick(attacker)) { | 		if(pl.getJailStickManager().isUsingJailStick(attacker.getUniqueId())) { | ||||||
| 			if(pl.getJailStickManager().isValidStick(attacker.getItemInHand().getType())) { | 			if(pl.getJailStickManager().isValidStick(attacker.getItemInHand().getType())) { | ||||||
| 				if(attacker.hasPermission("jail.usejailstick." + attacker.getItemInHand().getType().toString().toLowerCase())) { | 				if(attacker.hasPermission("jail.usejailstick." + attacker.getItemInHand().getType().toString().toLowerCase())) { | ||||||
| 					//The person the attacker is trying to jail stick is already jailed, don't handle that | 					//The person the attacker is trying to jail stick is already jailed, don't handle that | ||||||
|   | |||||||
| @@ -77,4 +77,4 @@ jailpay: | |||||||
|   priceInfinite: 10000 |   priceInfinite: 10000 | ||||||
| jailstick: | jailstick: | ||||||
|   enabled: true |   enabled: true | ||||||
|   sticks: ["stick,30,,Running away,-1", "blaze_rod,15,,Having too much fun,6"] |   sticks: ["stick,30m,,Running away,-1", "blaze_rod,15m,,Having too much fun,6"] | ||||||
		Reference in New Issue
	
	Block a user
	 graywolf336
					graywolf336