Add some settings management and also parse the time format.
This allows for: 30seconds, 10minutes, 3hours, or 2days
This commit is contained in:
parent
4a45229bc5
commit
0d79569c74
@ -1,7 +1,11 @@
|
||||
package com.graywolf336.jail;
|
||||
|
||||
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.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@ -9,6 +13,7 @@ import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
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. */
|
||||
public static boolean isInsideAB(Vector point, Vector first, Vector second) {
|
||||
@ -59,4 +64,31 @@ public class Util {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
*/
|
||||
}
|
||||
|
@ -9,12 +9,14 @@ import org.bukkit.entity.Player;
|
||||
import com.beust.jcommander.JCommander;
|
||||
import com.beust.jcommander.ParameterException;
|
||||
import com.graywolf336.jail.JailManager;
|
||||
import com.graywolf336.jail.Util;
|
||||
import com.graywolf336.jail.beans.Jail;
|
||||
import com.graywolf336.jail.beans.Prisoner;
|
||||
import com.graywolf336.jail.command.Command;
|
||||
import com.graywolf336.jail.command.CommandInfo;
|
||||
import com.graywolf336.jail.command.parameters.JailParameters;
|
||||
import com.graywolf336.jail.enums.LangString;
|
||||
import com.graywolf336.jail.enums.Settings;
|
||||
import com.graywolf336.jail.events.PrisonerJailedEvent;
|
||||
|
||||
@CommandInfo(
|
||||
@ -61,10 +63,14 @@ public class JailCommand implements Command {
|
||||
Long time = 10L;
|
||||
|
||||
try {
|
||||
time = Long.parseLong(params.time());
|
||||
}catch(NumberFormatException e) {
|
||||
sender.sendMessage(ChatColor.RED + "Number format is incorrect.");
|
||||
return true;
|
||||
time = Util.getTime(params.time());
|
||||
}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.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Jail j = jm.getJail(params.jail());
|
||||
|
21
src/main/java/com/graywolf336/jail/enums/Settings.java
Normal file
21
src/main/java/com/graywolf336/jail/enums/Settings.java
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user