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:
graywolf336 2014-07-15 01:05:46 -05:00
parent d034a0f1ed
commit 8a7a9cefef
7 changed files with 77 additions and 94 deletions

View File

@ -78,7 +78,6 @@ public class JailMain extends JavaPlugin {
cmdHand = new CommandHandler(this);
jh = new JailHandler(this);
pm = new PrisonerManager(this);
jsm = new JailStickManager(this);
PluginManager plm = this.getServer().getPluginManager();
plm.registerEvents(new BlockListener(this), this);
@ -101,6 +100,10 @@ public class JailMain extends JavaPlugin {
plm.registerEvents(this.mpl, this);
}
if(getConfig().getBoolean(Settings.JAILSTICKENABLED.getPath())) {
jsm = new JailStickManager(this);
}
jt = new JailTimer(this);
reloadJailPayManager();
@ -188,10 +191,12 @@ public class JailMain extends JavaPlugin {
/** Reloads the Jail Sticks, so the new ones can be loaded from the config. */
public void reloadJailSticks() {
if(getConfig().getBoolean(Settings.JAILSTICKENABLED.getPath())) {
this.jsm.removeAllStickUsers();
this.jsm = null;
this.jsm = new JailStickManager(this);
}
}
/**
* Reloads the {@link JailPayManager}.

View File

@ -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.
*/
public Jail getJail(String name) {
return this.jails.get(name);
return name.isEmpty() ? this.jails.values().iterator().next() : this.jails.get(name);
}
/**

View File

@ -2,10 +2,9 @@ package com.graywolf336.jail;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
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.enums.Settings;
@ -19,27 +18,31 @@ import com.graywolf336.jail.enums.Settings;
*
*/
public class JailStickManager {
private ArrayList<String> stickers;
private ArrayList<UUID> stickers;
private HashMap<Material, Stick> sticks;
public JailStickManager(JailMain plugin) {
this.stickers = new ArrayList<String>();
this.stickers = new ArrayList<UUID>();
this.sticks = new HashMap<Material, Stick>();
this.loadJailSticks(plugin);
}
private void loadJailSticks(JailMain pl) {
FileConfiguration config = pl.getConfig();
//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);
String[] a = s.split(",");
String jail = a[2];
//Check if the jail given, if any, exists
if(!a[2].isEmpty()) {
if(!pl.getJailManager().isValidJail(a[2])) {
if(jail.isEmpty()) {
jail = pl.getJailManager().getJail("").getName();
}else {
if(!pl.getJailManager().isValidJail(jail)) {
pl.getLogger().severe(s);
pl.getLogger().severe("The above jail stick configuration is invalid and references a jail that doesn't exist.");
continue;
@ -53,8 +56,17 @@ public class JailStickManager {
continue;
}
long time = 5;
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) {
e.printStackTrace();
pl.getLogger().severe(s);
@ -62,6 +74,7 @@ public class JailStickManager {
continue;
}
}
}
int c = sticks.size();
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) {
this.stickers.add(player.getName());
public void addUsingStick(UUID id) {
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) {
this.stickers.add(name);
}
/**
* 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);
public void removeUsingStick(UUID id) {
this.stickers.remove(id);
}
/**
* 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
*/
public boolean isUsingJailStick(Player player) {
return this.stickers.contains(player.getName());
}
/**
* 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);
public boolean isUsingJailStick(UUID id) {
return this.stickers.contains(id);
}
/**
* 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.
*/
public boolean toggleUsingStick(Player player) {
return this.toggleUsingStick(player.getName());
}
/**
* 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);
public boolean toggleUsingStick(UUID id) {
if(this.stickers.contains(id)) {
this.stickers.remove(id);
return false;
}else {
this.stickers.add(name);
this.stickers.add(id);
return true;
}
}
/** Removes all the users currently using the sticks. */
public void removeAllStickUsers() {
for(String s: stickers) {
this.removeUsingStick(s);
for(UUID id : stickers) {
this.removeUsingStick(id);
}
}
}

View File

@ -27,6 +27,7 @@ import com.graywolf336.jail.command.subcommands.JailPayCommand;
import com.graywolf336.jail.command.subcommands.JailRecordCommand;
import com.graywolf336.jail.command.subcommands.JailReloadCommand;
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.JailTeleInCommand;
import com.graywolf336.jail.command.subcommands.JailTeleOutCommand;
@ -190,6 +191,7 @@ public class JailHandler {
load(JailRecordCommand.class);
load(JailReloadCommand.class);
load(JailStatusCommand.class);
load(JailStickCommand.class);
load(JailStopCommand.class);
load(JailTeleInCommand.class);
load(JailTeleOutCommand.class);

View File

@ -1,6 +1,7 @@
package com.graywolf336.jail.command.subcommands;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.graywolf336.jail.JailManager;
import com.graywolf336.jail.command.Command;
@ -19,7 +20,7 @@ import com.graywolf336.jail.enums.Settings;
public class JailStickCommand implements Command {
public boolean execute(JailManager jm, CommandSender sender, String... args) throws Exception {
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) {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.JAILSTICKENABLED));

View File

@ -211,7 +211,7 @@ public class PlayerListener implements Listener {
Player attacker = (Player) event.getDamager();
Player player = (Player) event.getEntity();
if(pl.getJailStickManager().isUsingJailStick(attacker)) {
if(pl.getJailStickManager().isUsingJailStick(attacker.getUniqueId())) {
if(pl.getJailStickManager().isValidStick(attacker.getItemInHand().getType())) {
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

View File

@ -77,4 +77,4 @@ jailpay:
priceInfinite: 10000
jailstick:
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"]