Add some settings management and also parse the time format.

This allows for: 30seconds, 10minutes, 3hours, or 2days
This commit is contained in:
graywolf336 2013-12-24 12:28:40 -06:00
parent 4a45229bc5
commit 0d79569c74
3 changed files with 63 additions and 4 deletions

View File

@ -1,7 +1,11 @@
package com.graywolf336.jail; package com.graywolf336.jail;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -9,6 +13,7 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
public class Util { public class Util {
private final static Pattern DURATION_PATTERN = Pattern.compile("^(\\d+)\\s*(sec(?:ond?)?s?|(min(?:ute)?s?|h(?:ours?)?|d(?:ays?)?$", Pattern.CASE_INSENSITIVE);
/** Checks if the first {@link Vector} is inside the other two. */ /** Checks if the first {@link Vector} is inside the other two. */
public static boolean isInsideAB(Vector point, Vector first, Vector second) { public static boolean isInsideAB(Vector point, Vector first, Vector second) {
@ -59,4 +64,31 @@ public class Util {
return wand; return wand;
} }
public static Long getTime(String time) throws Exception {
Long t = 10L;
Matcher match = DURATION_PATTERN.matcher(time);
if (match.matches()) {
String units = match.group(2);
if ("seconds".equals(units) || "second".equals(units) || "s".equals(units))
t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.SECONDS);
if ("minutes".equals(units) || "minute".equals(units) || "mins".equals(units) || "min".equals(units))
t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.MINUTES);
else if ("hours".equals(units) || "hour".equals(units) || "h".equals(units))
t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.HOURS);
else if ("days".equals(units) || "day".equals(units) || "d".equals(units))
t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.DAYS);
Bukkit.getLogger().info(units);
}else {
throw new Exception("Invalid format.");
}
return t;
}
/*
*/
} }

View File

@ -9,12 +9,14 @@ import org.bukkit.entity.Player;
import com.beust.jcommander.JCommander; import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException; import com.beust.jcommander.ParameterException;
import com.graywolf336.jail.JailManager; import com.graywolf336.jail.JailManager;
import com.graywolf336.jail.Util;
import com.graywolf336.jail.beans.Jail; import com.graywolf336.jail.beans.Jail;
import com.graywolf336.jail.beans.Prisoner; import com.graywolf336.jail.beans.Prisoner;
import com.graywolf336.jail.command.Command; import com.graywolf336.jail.command.Command;
import com.graywolf336.jail.command.CommandInfo; import com.graywolf336.jail.command.CommandInfo;
import com.graywolf336.jail.command.parameters.JailParameters; import com.graywolf336.jail.command.parameters.JailParameters;
import com.graywolf336.jail.enums.LangString; import com.graywolf336.jail.enums.LangString;
import com.graywolf336.jail.enums.Settings;
import com.graywolf336.jail.events.PrisonerJailedEvent; import com.graywolf336.jail.events.PrisonerJailedEvent;
@CommandInfo( @CommandInfo(
@ -61,11 +63,15 @@ public class JailCommand implements Command {
Long time = 10L; Long time = 10L;
try { try {
time = Long.parseLong(params.time()); time = Util.getTime(params.time());
}catch(NumberFormatException e) { }catch(Exception e) {
try {
time = Util.getTime(jm.getPlugin().getConfig().getString(Settings.JAILDEFAULTTIME.getPath()));
}catch(Exception e2) {
sender.sendMessage(ChatColor.RED + "Number format is incorrect."); sender.sendMessage(ChatColor.RED + "Number format is incorrect.");
return true; return true;
} }
}
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(), TimeUnit.MILLISECONDS.convert(time, TimeUnit.MINUTES));

View File

@ -0,0 +1,21 @@
package com.graywolf336.jail.enums;
public enum Settings {
DEBUG("system.debug"),
UPDATENOTIFICATIONS("system.updateNotifications"),
JAILDEFAULTTIME("jailing.jail.defaultTime");
private String path;
private Settings(String path) {
this.path = path;
}
/**
* Gets the path this setting is in config.
* @return The path where this setting resides in the config.
*/
public String getPath() {
return this.path;
}
}