diff options
author | KHobbits <rob@khobbits.co.uk> | 2011-11-22 04:00:04 +0000 |
---|---|---|
committer | KHobbits <rob@khobbits.co.uk> | 2011-11-22 04:00:04 +0000 |
commit | d59e2834d1d46bef54161e05b13d4d00b1b530c3 (patch) | |
tree | 0f97c13a12d0608f7a3dfa28489be5a7f9f7fd4a | |
parent | a5853baf4cac35c2bb8f373ec0e61b2792531844 (diff) | |
download | Essentials-d59e2834d1d46bef54161e05b13d4d00b1b530c3.tar Essentials-d59e2834d1d46bef54161e05b13d4d00b1b530c3.tar.gz Essentials-d59e2834d1d46bef54161e05b13d4d00b1b530c3.tar.lz Essentials-d59e2834d1d46bef54161e05b13d4d00b1b530c3.tar.xz Essentials-d59e2834d1d46bef54161e05b13d4d00b1b530c3.zip |
Rewriting help, to use new classes.
4 files changed, 179 insertions, 202 deletions
diff --git a/Essentials/src/com/earth2me/essentials/Util.java b/Essentials/src/com/earth2me/essentials/Util.java index 56a8e0334..f73c4daa8 100644 --- a/Essentials/src/com/earth2me/essentials/Util.java +++ b/Essentials/src/com/earth2me/essentials/Util.java @@ -347,6 +347,19 @@ public class Util return Math.round(d * 100.0) / 100.0; } + public static boolean isInt(final String sInt) + { + try + { + Integer.parseInt(sInt); + } + catch (NumberFormatException e) + { + return false; + } + return true; + } + public static String joinList(Object... list) { return joinList(", ", list); diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandhelp.java b/Essentials/src/com/earth2me/essentials/commands/Commandhelp.java index 3ee7d9804..24f463740 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandhelp.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandhelp.java @@ -3,76 +3,45 @@ package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.User; import com.earth2me.essentials.Util; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map.Entry; -import java.util.logging.Level; +import com.earth2me.essentials.textreader.*; import org.bukkit.Server; import org.bukkit.command.CommandSender; -import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.PluginDescriptionFile; -import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.constructor.SafeConstructor; public class Commandhelp extends EssentialsCommand { - private static final String DESCRIPTION = "description"; - private static final String PERMISSION = "permission"; - private static final String PERMISSIONS = "permissions"; - public final Yaml yaml = new Yaml(new SafeConstructor()); - public Commandhelp() { super("help"); } - //TODO: Update to use new text file and command parser classes @Override protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { - int page = 1; - String match = ""; - try + IText output; + String pageStr = args.length > 0 ? args[0] : null; + String chapterPageStr = args.length > 1 ? args[1] : null; + final IText input = new TextInput(user, "help", false, ess); + + if (input.getLines().isEmpty()) { - if (args.length > 0) + if (Util.isInt(pageStr) || pageStr == null) { - match = args[0].toLowerCase(Locale.ENGLISH); - page = Integer.parseInt(args[args.length - 1]); - if (args.length == 1) - { - match = ""; - } + output = new HelpInput(user, "", ess); } - - } - catch (Exception ex) - { - if (args.length == 1) + else { - match = args[0].toLowerCase(Locale.ENGLISH); + output = new HelpInput(user, pageStr, ess); + pageStr = chapterPageStr; } + chapterPageStr = null; } - - final List<String> lines = getHelpLines(user, match); - if (lines.isEmpty()) + else { - throw new Exception(_("noHelpFound")); - } - - final int start = (page - 1) * 9; - final int pages = lines.size() / 9 + (lines.size() % 9 > 0 ? 1 : 0); - - user.sendMessage(_("helpPages", page, pages)); - for (int i = start; i < lines.size() && i < start + 9; i++) - { - user.sendMessage(lines.get(i)); + output = new KeywordReplacer(input, user, ess); } + final TextPager pager = new TextPager(output); + pager.showPage(pageStr, chapterPageStr, user); } @Override @@ -80,158 +49,4 @@ public class Commandhelp extends EssentialsCommand { sender.sendMessage(_("helpConsole")); } - - @SuppressWarnings("CallToThreadDumpStack") - private List<String> getHelpLines(final User user, final String match) throws Exception - { - final List<String> retval = new ArrayList<String>(); - File helpFile = new File(ess.getDataFolder(), "help_" + Util.sanitizeFileName(user.getName()) + ".txt"); - if (!helpFile.exists()) - { - helpFile = new File(ess.getDataFolder(), "help_" + Util.sanitizeFileName(user.getGroup()) + ".txt"); - } - if (!helpFile.exists()) - { - helpFile = new File(ess.getDataFolder(), "help.txt"); - } - if (helpFile.exists()) - { - final BufferedReader bufferedReader = new BufferedReader(new FileReader(helpFile)); - try - { - - while (bufferedReader.ready()) - { - final String line = bufferedReader.readLine(); - if (line == null) - { - break; - } - retval.add(line.replace('&', '§')); - } - } - finally - { - bufferedReader.close(); - } - return retval; - } - - boolean reported = false; - String pluginName = ""; - for (Plugin p : ess.getServer().getPluginManager().getPlugins()) - { - try - { - final PluginDescriptionFile desc = p.getDescription(); - final HashMap<String, HashMap<String, Object>> cmds = (HashMap<String, HashMap<String, Object>>)desc.getCommands(); - pluginName = p.getDescription().getName().toLowerCase(Locale.ENGLISH); - for (Entry<String, HashMap<String, Object>> k : cmds.entrySet()) - { - try - { - if ((!match.equalsIgnoreCase("")) - && (!k.getKey().toLowerCase(Locale.ENGLISH).contains(match)) - && (!(k.getValue().get(DESCRIPTION) instanceof String - && ((String)k.getValue().get(DESCRIPTION)).toLowerCase(Locale.ENGLISH).contains(match))) - && (!pluginName.contains(match))) - { - continue; - } - - if (pluginName.contains("essentials")) - { - final String node = "essentials." + k.getKey(); - if (!ess.getSettings().isCommandDisabled(k.getKey()) && user.isAuthorized(node)) - { - retval.add("§c" + k.getKey() + "§7: " + k.getValue().get(DESCRIPTION)); - } - } - else - { - if (ess.getSettings().showNonEssCommandsInHelp()) - { - final HashMap<String, Object> value = k.getValue(); - if (value.containsKey(PERMISSION) && value.get(PERMISSION) instanceof String && !(value.get(PERMISSION).equals(""))) - { - if (user.isAuthorized((String)value.get(PERMISSION))) - { - retval.add("§c" + k.getKey() + "§7: " + value.get(DESCRIPTION)); - } - } - else if (value.containsKey(PERMISSION) && value.get(PERMISSION) instanceof List && !((List<Object>)value.get(PERMISSION)).isEmpty()) - { - boolean enabled = false; - for (Object o : (List<Object>)value.get(PERMISSION)) - { - if (o instanceof String && user.isAuthorized((String)o)) - { - enabled = true; - break; - } - } - if (enabled) - { - retval.add("§c" + k.getKey() + "§7: " + value.get(DESCRIPTION)); - } - } - else if (value.containsKey(PERMISSIONS) && value.get(PERMISSIONS) instanceof String && !(value.get(PERMISSIONS).equals(""))) - { - if (user.isAuthorized((String)value.get(PERMISSIONS))) - { - retval.add("§c" + k.getKey() + "§7: " + value.get(DESCRIPTION)); - } - } - else if (value.containsKey(PERMISSIONS) && value.get(PERMISSIONS) instanceof List && !((List<Object>)value.get(PERMISSIONS)).isEmpty()) - { - boolean enabled = false; - for (Object o : (List<Object>)value.get(PERMISSIONS)) - { - if (o instanceof String && user.isAuthorized((String)o)) - { - enabled = true; - break; - } - } - if (enabled) - { - retval.add("§c" + k.getKey() + "§7: " + value.get(DESCRIPTION)); - } - } - else if (user.isAuthorized("essentials.help." + pluginName)) - { - retval.add("§c" + k.getKey() + "§7: " + value.get(DESCRIPTION)); - } - else - { - if (!ess.getSettings().hidePermissionlessHelp()) - { - retval.add("§c" + k.getKey() + "§7: " + value.get(DESCRIPTION)); - } - } - } - } - } - catch (NullPointerException ex) - { - continue; - } - } - } - catch (NullPointerException ex) - { - continue; - } - catch (Exception ex) - { - if (!reported) - { - logger.log(Level.WARNING, _("commandHelpFailedForPlugin", pluginName), ex); - } - reported = true; - continue; - } - } - return retval; - } } diff --git a/Essentials/src/com/earth2me/essentials/textreader/HelpInput.java b/Essentials/src/com/earth2me/essentials/textreader/HelpInput.java new file mode 100644 index 000000000..288058591 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/textreader/HelpInput.java @@ -0,0 +1,146 @@ +package com.earth2me.essentials.textreader; + +import static com.earth2me.essentials.I18n._; +import com.earth2me.essentials.IEssentials; +import com.earth2me.essentials.User; +import java.io.IOException; +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginDescriptionFile; + + +public class HelpInput implements IText +{ + private static final String DESCRIPTION = "description"; + private static final String PERMISSION = "permission"; + private static final String PERMISSIONS = "permissions"; + private final transient List<String> lines = new ArrayList<String>(); + private final transient List<String> chapters = new ArrayList<String>(); + private final transient Map<String, Integer> bookmarks = new HashMap<String, Integer>(); + private final static Logger logger = Logger.getLogger("Minecraft"); + + public HelpInput(final User user, final String match, final IEssentials ess) throws IOException + { + boolean reported = false; + String pluginName = ""; + for (Plugin p : ess.getServer().getPluginManager().getPlugins()) + { + try + { + final PluginDescriptionFile desc = p.getDescription(); + final HashMap<String, HashMap<String, Object>> cmds = (HashMap<String, HashMap<String, Object>>)desc.getCommands(); + pluginName = p.getDescription().getName().toLowerCase(Locale.ENGLISH); + for (Map.Entry<String, HashMap<String, Object>> k : cmds.entrySet()) + { + try + { + if ((!match.equalsIgnoreCase("")) + && (!k.getKey().toLowerCase(Locale.ENGLISH).contains(match)) + && (!(k.getValue().get(DESCRIPTION) instanceof String + && ((String)k.getValue().get(DESCRIPTION)).toLowerCase(Locale.ENGLISH).contains(match))) + && (!pluginName.contains(match))) + { + continue; + } + + if (pluginName.contains("essentials")) + { + final String node = "essentials." + k.getKey(); + if (!ess.getSettings().isCommandDisabled(k.getKey()) && user.isAuthorized(node)) + { + lines.add("§c" + k.getKey() + "§7: " + k.getValue().get(DESCRIPTION)); + } + } + else + { + if (ess.getSettings().showNonEssCommandsInHelp()) + { + final HashMap<String, Object> value = k.getValue(); + Object permissions = null; + if (value.containsKey(PERMISSION)) + { + permissions = value.get(PERMISSION); + } + else if (value.containsKey(PERMISSIONS)) + { + permissions = value.get(PERMISSIONS); + } + if (permissions instanceof String && !permissions.equals("")) + { + if (user.isAuthorized((String)permissions)) + { + lines.add("§c" + k.getKey() + "§7: " + value.get(DESCRIPTION)); + } + } + else if (permissions instanceof List && !((List<Object>)permissions).isEmpty()) + { + boolean enabled = false; + for (Object o : (List<Object>)permissions) + { + if (o instanceof String && user.isAuthorized((String)o)) + { + enabled = true; + break; + } + } + if (enabled) + { + lines.add("§c" + k.getKey() + "§7: " + value.get(DESCRIPTION)); + } + } + else if (user.isAuthorized("essentials.help." + pluginName)) + { + lines.add("§c" + k.getKey() + "§7: " + value.get(DESCRIPTION)); + } + else + { + if (!ess.getSettings().hidePermissionlessHelp()) + { + lines.add("§c" + k.getKey() + "§7: " + value.get(DESCRIPTION)); + } + } + } + } + } + catch (NullPointerException ex) + { + continue; + } + } + } + catch (NullPointerException ex) + { + continue; + } + catch (Exception ex) + { + if (!reported) + { + logger.log(Level.WARNING, _("commandHelpFailedForPlugin", pluginName), ex); + } + reported = true; + continue; + } + } + } + + @Override + public List<String> getLines() + { + return lines; + } + + @Override + public List<String> getChapters() + { + return chapters; + } + + @Override + public Map<String, Integer> getBookmarks() + { + return bookmarks; + } +} diff --git a/Essentials/src/com/earth2me/essentials/textreader/TextPager.java b/Essentials/src/com/earth2me/essentials/textreader/TextPager.java index 0a33e537a..06fd8763f 100644 --- a/Essentials/src/com/earth2me/essentials/textreader/TextPager.java +++ b/Essentials/src/com/earth2me/essentials/textreader/TextPager.java @@ -40,6 +40,9 @@ public class TextPager { page = 1; } + if (page < 1) { + page = 1; + } int start = (page - 1) * 9; if (showHeader) |