From 67281efac1ad2955ba6640b046a389319c1a6071 Mon Sep 17 00:00:00 2001 From: snowleo Date: Wed, 14 Mar 2012 03:36:13 +0100 Subject: The textreader classes are utils, so they should be under the utils package --- .../essentials/commands/Commandbalancetop.java | 4 +- .../earth2me/essentials/commands/Commandhelp.java | 6 +- .../earth2me/essentials/commands/Commandinfo.java | 8 +- .../earth2me/essentials/commands/Commandmotd.java | 8 +- .../earth2me/essentials/commands/Commandrules.java | 8 +- .../listener/EssentialsPlayerListener.java | 8 +- .../earth2me/essentials/settings/SpawnsHolder.java | 8 +- .../essentials/textreader/ArrayListInput.java | 31 ---- .../earth2me/essentials/textreader/HelpInput.java | 152 ----------------- .../com/earth2me/essentials/textreader/IText.java | 14 -- .../essentials/textreader/KeywordReplacer.java | 156 ------------------ .../essentials/textreader/SimpleTextInput.java | 35 ---- .../essentials/textreader/SimpleTextPager.java | 31 ---- .../earth2me/essentials/textreader/TextInput.java | 147 ----------------- .../earth2me/essentials/textreader/TextPager.java | 182 --------------------- .../utils/textreader/ArrayListInput.java | 31 ++++ .../essentials/utils/textreader/HelpInput.java | 152 +++++++++++++++++ .../essentials/utils/textreader/IText.java | 14 ++ .../utils/textreader/KeywordReplacer.java | 156 ++++++++++++++++++ .../utils/textreader/SimpleTextInput.java | 35 ++++ .../utils/textreader/SimpleTextPager.java | 31 ++++ .../essentials/utils/textreader/TextInput.java | 147 +++++++++++++++++ .../essentials/utils/textreader/TextPager.java | 182 +++++++++++++++++++++ 23 files changed, 775 insertions(+), 771 deletions(-) delete mode 100644 Essentials/src/com/earth2me/essentials/textreader/ArrayListInput.java delete mode 100644 Essentials/src/com/earth2me/essentials/textreader/HelpInput.java delete mode 100644 Essentials/src/com/earth2me/essentials/textreader/IText.java delete mode 100644 Essentials/src/com/earth2me/essentials/textreader/KeywordReplacer.java delete mode 100644 Essentials/src/com/earth2me/essentials/textreader/SimpleTextInput.java delete mode 100644 Essentials/src/com/earth2me/essentials/textreader/SimpleTextPager.java delete mode 100644 Essentials/src/com/earth2me/essentials/textreader/TextInput.java delete mode 100644 Essentials/src/com/earth2me/essentials/textreader/TextPager.java create mode 100644 Essentials/src/com/earth2me/essentials/utils/textreader/ArrayListInput.java create mode 100644 Essentials/src/com/earth2me/essentials/utils/textreader/HelpInput.java create mode 100644 Essentials/src/com/earth2me/essentials/utils/textreader/IText.java create mode 100644 Essentials/src/com/earth2me/essentials/utils/textreader/KeywordReplacer.java create mode 100644 Essentials/src/com/earth2me/essentials/utils/textreader/SimpleTextInput.java create mode 100644 Essentials/src/com/earth2me/essentials/utils/textreader/SimpleTextPager.java create mode 100644 Essentials/src/com/earth2me/essentials/utils/textreader/TextInput.java create mode 100644 Essentials/src/com/earth2me/essentials/utils/textreader/TextPager.java diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandbalancetop.java b/Essentials/src/com/earth2me/essentials/commands/Commandbalancetop.java index be1a4976c..1508a134f 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandbalancetop.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandbalancetop.java @@ -3,8 +3,8 @@ package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.utils.Util; import com.earth2me.essentials.api.IUser; -import com.earth2me.essentials.textreader.ArrayListInput; -import com.earth2me.essentials.textreader.TextPager; +import com.earth2me.essentials.utils.textreader.ArrayListInput; +import com.earth2me.essentials.utils.textreader.TextPager; import java.text.DateFormat; import java.util.*; import java.util.Map.Entry; diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandhelp.java b/Essentials/src/com/earth2me/essentials/commands/Commandhelp.java index 512c8fcb7..40b99f06f 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandhelp.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandhelp.java @@ -1,9 +1,13 @@ package com.earth2me.essentials.commands; +import com.earth2me.essentials.utils.textreader.TextInput; +import com.earth2me.essentials.utils.textreader.IText; +import com.earth2me.essentials.utils.textreader.TextPager; +import com.earth2me.essentials.utils.textreader.HelpInput; +import com.earth2me.essentials.utils.textreader.KeywordReplacer; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.utils.Util; import com.earth2me.essentials.api.IUser; -import com.earth2me.essentials.textreader.*; import org.bukkit.command.CommandSender; diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandinfo.java b/Essentials/src/com/earth2me/essentials/commands/Commandinfo.java index d694c9fa4..924e0a74a 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandinfo.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandinfo.java @@ -1,9 +1,9 @@ package com.earth2me.essentials.commands; -import com.earth2me.essentials.textreader.IText; -import com.earth2me.essentials.textreader.KeywordReplacer; -import com.earth2me.essentials.textreader.TextInput; -import com.earth2me.essentials.textreader.TextPager; +import com.earth2me.essentials.utils.textreader.IText; +import com.earth2me.essentials.utils.textreader.KeywordReplacer; +import com.earth2me.essentials.utils.textreader.TextInput; +import com.earth2me.essentials.utils.textreader.TextPager; import org.bukkit.command.CommandSender; diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandmotd.java b/Essentials/src/com/earth2me/essentials/commands/Commandmotd.java index 3531d0708..c1d1e92bf 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandmotd.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandmotd.java @@ -1,9 +1,9 @@ package com.earth2me.essentials.commands; -import com.earth2me.essentials.textreader.IText; -import com.earth2me.essentials.textreader.KeywordReplacer; -import com.earth2me.essentials.textreader.TextInput; -import com.earth2me.essentials.textreader.TextPager; +import com.earth2me.essentials.utils.textreader.IText; +import com.earth2me.essentials.utils.textreader.KeywordReplacer; +import com.earth2me.essentials.utils.textreader.TextInput; +import com.earth2me.essentials.utils.textreader.TextPager; import org.bukkit.command.CommandSender; diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandrules.java b/Essentials/src/com/earth2me/essentials/commands/Commandrules.java index 5445b9bc6..da0a3eaec 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandrules.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandrules.java @@ -1,9 +1,9 @@ package com.earth2me.essentials.commands; -import com.earth2me.essentials.textreader.IText; -import com.earth2me.essentials.textreader.KeywordReplacer; -import com.earth2me.essentials.textreader.TextInput; -import com.earth2me.essentials.textreader.TextPager; +import com.earth2me.essentials.utils.textreader.IText; +import com.earth2me.essentials.utils.textreader.KeywordReplacer; +import com.earth2me.essentials.utils.textreader.TextInput; +import com.earth2me.essentials.utils.textreader.TextPager; import org.bukkit.command.CommandSender; diff --git a/Essentials/src/com/earth2me/essentials/listener/EssentialsPlayerListener.java b/Essentials/src/com/earth2me/essentials/listener/EssentialsPlayerListener.java index 0040dd50e..978794b75 100644 --- a/Essentials/src/com/earth2me/essentials/listener/EssentialsPlayerListener.java +++ b/Essentials/src/com/earth2me/essentials/listener/EssentialsPlayerListener.java @@ -5,10 +5,10 @@ import com.earth2me.essentials.api.IEssentials; import com.earth2me.essentials.api.ISettings; import com.earth2me.essentials.api.IUser; import com.earth2me.essentials.permissions.Permissions; -import com.earth2me.essentials.textreader.IText; -import com.earth2me.essentials.textreader.KeywordReplacer; -import com.earth2me.essentials.textreader.TextInput; -import com.earth2me.essentials.textreader.TextPager; +import com.earth2me.essentials.utils.textreader.IText; +import com.earth2me.essentials.utils.textreader.KeywordReplacer; +import com.earth2me.essentials.utils.textreader.TextInput; +import com.earth2me.essentials.utils.textreader.TextPager; import com.earth2me.essentials.user.UserData.TimestampType; import com.earth2me.essentials.utils.LocationUtil; import java.io.IOException; diff --git a/Essentials/src/com/earth2me/essentials/settings/SpawnsHolder.java b/Essentials/src/com/earth2me/essentials/settings/SpawnsHolder.java index eb648097d..19c9198da 100644 --- a/Essentials/src/com/earth2me/essentials/settings/SpawnsHolder.java +++ b/Essentials/src/com/earth2me/essentials/settings/SpawnsHolder.java @@ -7,10 +7,10 @@ import com.earth2me.essentials.api.ISettings; import com.earth2me.essentials.api.IUser; import com.earth2me.essentials.storage.AsyncStorageObjectHolder; import com.earth2me.essentials.storage.Location.WorldNotLoadedException; -import com.earth2me.essentials.textreader.IText; -import com.earth2me.essentials.textreader.KeywordReplacer; -import com.earth2me.essentials.textreader.SimpleTextInput; -import com.earth2me.essentials.textreader.SimpleTextPager; +import com.earth2me.essentials.utils.textreader.IText; +import com.earth2me.essentials.utils.textreader.KeywordReplacer; +import com.earth2me.essentials.utils.textreader.SimpleTextInput; +import com.earth2me.essentials.utils.textreader.SimpleTextPager; import java.io.File; import java.util.HashMap; import java.util.Locale; diff --git a/Essentials/src/com/earth2me/essentials/textreader/ArrayListInput.java b/Essentials/src/com/earth2me/essentials/textreader/ArrayListInput.java deleted file mode 100644 index 0da83f3b4..000000000 --- a/Essentials/src/com/earth2me/essentials/textreader/ArrayListInput.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.earth2me.essentials.textreader; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; - - -public class ArrayListInput implements IText -{ - private final transient List lines = new ArrayList(); - - @Override - public List getLines() - { - return lines; - } - - @Override - public List getChapters() - { - return Collections.emptyList(); - } - - @Override - public Map getBookmarks() - { - return Collections.emptyMap(); - } - -} diff --git a/Essentials/src/com/earth2me/essentials/textreader/HelpInput.java b/Essentials/src/com/earth2me/essentials/textreader/HelpInput.java deleted file mode 100644 index f455800fb..000000000 --- a/Essentials/src/com/earth2me/essentials/textreader/HelpInput.java +++ /dev/null @@ -1,152 +0,0 @@ -package com.earth2me.essentials.textreader; - -import static com.earth2me.essentials.I18n._; -import com.earth2me.essentials.api.IEssentials; -import com.earth2me.essentials.api.ISettings; -import com.earth2me.essentials.api.IUser; -import com.earth2me.essentials.permissions.HelpPermissions; -import java.io.IOException; -import java.util.*; -import java.util.logging.Level; -import java.util.logging.Logger; -import lombok.Cleanup; -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 lines = new ArrayList(); - private final transient List chapters = new ArrayList(); - private final transient Map bookmarks = new HashMap(); - private final static Logger logger = Logger.getLogger("Minecraft"); - - public HelpInput(final IUser user, final String match, final IEssentials ess) throws IOException - { - @Cleanup - final ISettings settings = ess.getSettings(); - settings.acquireReadLock(); - boolean reported = false; - String pluginName = ""; - for (Plugin p : ess.getServer().getPluginManager().getPlugins()) - { - try - { - final PluginDescriptionFile desc = p.getDescription(); - final Map> cmds = desc.getCommands(); - pluginName = p.getDescription().getName().toLowerCase(Locale.ENGLISH); - for (Map.Entry> 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 (!settings.getData().getCommands().isDisabled(k.getKey()) && user.hasPermission(node)) - { - lines.add(_("helpLine", k.getKey(), k.getValue().get(DESCRIPTION))); - } - } - else - { - if (settings.getData().getCommands().getHelp().isShowNonEssCommandsInHelp()) - { - final Map value = k.getValue(); - Object permissions = null; - if (value.containsKey(PERMISSION)) - { - permissions = value.get(PERMISSION); - } - else if (value.containsKey(PERMISSIONS)) - { - permissions = value.get(PERMISSIONS); - } - if (HelpPermissions.getPermission(pluginName).isAuthorized(user)) - { - lines.add(_("helpLine", k.getKey(), value.get(DESCRIPTION))); - } - else if (permissions instanceof List && !((List)permissions).isEmpty()) - { - boolean enabled = false; - for (Object o : (List)permissions) - { - if (o instanceof String && user.hasPermission(o.toString())) - { - enabled = true; - break; - } - } - if (enabled) - { - lines.add(_("helpLine", k.getKey(), value.get(DESCRIPTION))); - } - } - else if (permissions instanceof String && !"".equals(permissions)) - { - if (user.hasPermission(permissions.toString())) - { - lines.add(_("helpLine", k.getKey(), value.get(DESCRIPTION))); - } - } - else - { - if (!settings.getData().getCommands().getHelp().isHidePermissionlessCommands()) - { - lines.add(_("helpLine", k.getKey(), 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 getLines() - { - return lines; - } - - @Override - public List getChapters() - { - return chapters; - } - - @Override - public Map getBookmarks() - { - return bookmarks; - } -} diff --git a/Essentials/src/com/earth2me/essentials/textreader/IText.java b/Essentials/src/com/earth2me/essentials/textreader/IText.java deleted file mode 100644 index 851119701..000000000 --- a/Essentials/src/com/earth2me/essentials/textreader/IText.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.earth2me.essentials.textreader; - -import java.util.List; -import java.util.Map; - - -public interface IText -{ - List getLines(); - - List getChapters(); - - Map getBookmarks(); -} diff --git a/Essentials/src/com/earth2me/essentials/textreader/KeywordReplacer.java b/Essentials/src/com/earth2me/essentials/textreader/KeywordReplacer.java deleted file mode 100644 index 75289d617..000000000 --- a/Essentials/src/com/earth2me/essentials/textreader/KeywordReplacer.java +++ /dev/null @@ -1,156 +0,0 @@ -package com.earth2me.essentials.textreader; - -import com.earth2me.essentials.utils.DescParseTickFormat; -import com.earth2me.essentials.api.IEssentials; -import com.earth2me.essentials.api.IUser; -import java.text.DateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Map; -import lombok.Cleanup; -import org.bukkit.World; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; - - -public class KeywordReplacer implements IText -{ - private final transient IText input; - private final transient List replaced; - private final transient IEssentials ess; - - public KeywordReplacer(final IText input, final CommandSender sender, final IEssentials ess) - { - this.input = input; - this.replaced = new ArrayList(this.input.getLines().size()); - this.ess = ess; - replaceKeywords(sender); - } - - private void replaceKeywords(final CommandSender sender) - { - String displayName, ipAddress, balance, mails, world; - String worlds, online, unique, playerlist, date, time; - String worldTime12, worldTime24, worldDate, plugins; - String userName, address, version; - if (sender instanceof Player) - { - @Cleanup - final IUser user = ess.getUser((Player)sender); - user.acquireReadLock(); - displayName = user.getDisplayName(); - userName = user.getName(); - ipAddress = user.getAddress().getAddress().toString(); - address = user.getAddress().toString(); - balance = Double.toString(user.getMoney()); - mails = Integer.toString(user.getData().getMails() == null ? 0 : user.getData().getMails().size()); - world = user.getLocation().getWorld().getName(); - worldTime12 = DescParseTickFormat.format12(user.getWorld().getTime()); - worldTime24 = DescParseTickFormat.format24(user.getWorld().getTime()); - worldDate = DateFormat.getDateInstance(DateFormat.MEDIUM, ess.getI18n().getCurrentLocale()).format(DescParseTickFormat.ticksToDate(user.getWorld().getFullTime())); - } - else - { - displayName = ipAddress = balance = mails = world = worldTime12 = worldTime24 = worldDate = ""; - } - - int playerHidden = 0; - for (Player p : ess.getServer().getOnlinePlayers()) - { - if (ess.getUser(p).isHidden()) - { - playerHidden++; - } - } - online = Integer.toString(ess.getServer().getOnlinePlayers().length - playerHidden); - unique = Integer.toString(ess.getUserMap().getUniqueUsers()); - - final StringBuilder worldsBuilder = new StringBuilder(); - for (World w : ess.getServer().getWorlds()) - { - if (worldsBuilder.length() > 0) - { - worldsBuilder.append(", "); - } - worldsBuilder.append(w.getName()); - } - worlds = worldsBuilder.toString(); - - final StringBuilder playerlistBuilder = new StringBuilder(); - for (Player p : ess.getServer().getOnlinePlayers()) - { - if (ess.getUser(p).isHidden()) - { - continue; - } - if (playerlistBuilder.length() > 0) - { - playerlistBuilder.append(", "); - } - playerlistBuilder.append(p.getDisplayName()); - } - playerlist = playerlistBuilder.toString(); - - final StringBuilder pluginlistBuilder = new StringBuilder(); - for (Plugin p : ess.getServer().getPluginManager().getPlugins()) - { - if (pluginlistBuilder.length() > 0) - { - pluginlistBuilder.append(", "); - } - pluginlistBuilder.append(p.getDescription().getName()); - } - plugins = pluginlistBuilder.toString(); - - date = DateFormat.getDateInstance(DateFormat.MEDIUM, ess.getI18n().getCurrentLocale()).format(new Date()); - time = DateFormat.getTimeInstance(DateFormat.MEDIUM, ess.getI18n().getCurrentLocale()).format(new Date()); - - version = ess.getServer().getVersion(); - - for (int i = 0; i < input.getLines().size(); i++) - { - String line = input.getLines().get(i); - - line = line.replace("{PLAYER}", displayName); - line = line.replace("{DISPLAYNAME}", displayName); - line = line.replace("{USERNAME}", displayName); - line = line.replace("{IP}", ipAddress); - line = line.replace("{ADDRESS}", ipAddress); - line = line.replace("{BALANCE}", balance); - line = line.replace("{MAILS}", mails); - line = line.replace("{WORLD}", world); - line = line.replace("{ONLINE}", online); - line = line.replace("{UNIQUE}", unique); - line = line.replace("{WORLDS}", worlds); - line = line.replace("{PLAYERLIST}", playerlist); - line = line.replace("{TIME}", time); - line = line.replace("{DATE}", date); - line = line.replace("{WORLDTIME12}", worldTime12); - line = line.replace("{WORLDTIME24}", worldTime24); - line = line.replace("{WORLDDATE}", worldDate); - line = line.replace("{PLUGINS}", plugins); - line = line.replace("{VERSION}", version); - replaced.add(line); - } - } - - @Override - public List getLines() - { - return replaced; - } - - @Override - public List getChapters() - { - return input.getChapters(); - } - - @Override - public Map getBookmarks() - { - return input.getBookmarks(); - } -} diff --git a/Essentials/src/com/earth2me/essentials/textreader/SimpleTextInput.java b/Essentials/src/com/earth2me/essentials/textreader/SimpleTextInput.java deleted file mode 100644 index a9f9e1480..000000000 --- a/Essentials/src/com/earth2me/essentials/textreader/SimpleTextInput.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.earth2me.essentials.textreader; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; - - -public class SimpleTextInput implements IText -{ - private final transient List lines = new ArrayList(); - - public SimpleTextInput (final String input) { - lines.add(input); - } - - @Override - public List getLines() - { - return lines; - } - - @Override - public List getChapters() - { - return Collections.emptyList(); - } - - @Override - public Map getBookmarks() - { - return Collections.emptyMap(); - } - -} diff --git a/Essentials/src/com/earth2me/essentials/textreader/SimpleTextPager.java b/Essentials/src/com/earth2me/essentials/textreader/SimpleTextPager.java deleted file mode 100644 index ea1b787d5..000000000 --- a/Essentials/src/com/earth2me/essentials/textreader/SimpleTextPager.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.earth2me.essentials.textreader; - -import org.bukkit.command.CommandSender; - - -public class SimpleTextPager -{ - private final transient IText text; - - public SimpleTextPager(final IText text) - { - this.text = text; - } - - public void showPage(final CommandSender sender) - { - for (String line : text.getLines()) - { - sender.sendMessage(line); - } - } - - public String getString(int line) - { - if (text.getLines().size() < line) - { - return null; - } - return text.getLines().get(line); - } -} diff --git a/Essentials/src/com/earth2me/essentials/textreader/TextInput.java b/Essentials/src/com/earth2me/essentials/textreader/TextInput.java deleted file mode 100644 index 96a4533fa..000000000 --- a/Essentials/src/com/earth2me/essentials/textreader/TextInput.java +++ /dev/null @@ -1,147 +0,0 @@ -package com.earth2me.essentials.textreader; - -import com.earth2me.essentials.utils.Util; -import com.earth2me.essentials.api.IEssentials; -import com.earth2me.essentials.api.IUser; -import com.earth2me.essentials.api.InvalidNameException; -import java.io.*; -import java.lang.ref.SoftReference; -import java.util.*; -import java.util.logging.Level; -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - - -public class TextInput implements IText -{ - private final transient List lines; - private final transient List chapters; - private final transient Map bookmarks; - private final transient long lastChange; - private final static HashMap> cache = new HashMap>(); - - public TextInput(final CommandSender sender, final String filename, final boolean createFile, final IEssentials ess) throws IOException - { - - File file = null; - if (sender instanceof Player) - { - try - { - final IUser user = ess.getUser((Player)sender); - file = new File(ess.getDataFolder(), filename + "_" + Util.sanitizeFileName(user.getName()) + ".txt"); - if (!file.exists()) - { - file = new File(ess.getDataFolder(), filename + "_" + Util.sanitizeFileName(ess.getRanks().getMainGroup(user)) + ".txt"); - } - } - catch (InvalidNameException ex) - { - Bukkit.getLogger().log(Level.WARNING, ex.getMessage(), ex); - } - } - if (file == null || !file.exists()) - { - file = new File(ess.getDataFolder(), filename + ".txt"); - } - if (file.exists()) - { - lastChange = file.lastModified(); - boolean readFromfile; - synchronized (cache) - { - final SoftReference inputRef = cache.get(file.getName()); - TextInput input; - if (inputRef == null || (input = inputRef.get()) == null || input.lastChange < lastChange) - { - lines = new ArrayList(); - chapters = new ArrayList(); - bookmarks = new HashMap(); - cache.put(file.getName(), new SoftReference(this)); - readFromfile = true; - } - else - { - lines = Collections.unmodifiableList(input.getLines()); - chapters = Collections.unmodifiableList(input.getChapters()); - bookmarks = Collections.unmodifiableMap(input.getBookmarks()); - readFromfile = false; - } - } - if (readFromfile) - { - final BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); - try - { - int lineNumber = 0; - while (bufferedReader.ready()) - { - final String line = bufferedReader.readLine(); - if (line == null) - { - break; - } - if (line.length() > 0 && line.charAt(0) == '#') - { - bookmarks.put(line.substring(1).toLowerCase(Locale.ENGLISH).replaceAll("&[0-9a-f]", ""), lineNumber); - chapters.add(line.substring(1).replace('&', '�').replace("�", "&")); - } - lines.add(line.replace('&', '�').replace("�", "&")); - lineNumber++; - } - } - finally - { - bufferedReader.close(); - } - } - } - else - { - lastChange = 0; - lines = Collections.emptyList(); - chapters = Collections.emptyList(); - bookmarks = Collections.emptyMap(); - if (createFile) - { - final InputStream input = ess.getResource(filename + ".txt"); - final OutputStream output = new FileOutputStream(file); - try - { - final byte[] buffer = new byte[1024]; - int length = input.read(buffer); - while (length > 0) - { - output.write(buffer, 0, length); - length = input.read(buffer); - } - } - finally - { - output.close(); - input.close(); - } - throw new FileNotFoundException("File " + filename + ".txt does not exist. Creating one for you."); - } - } - } - - @Override - public List getLines() - { - return lines; - } - - @Override - public List getChapters() - { - return chapters; - } - - @Override - public Map getBookmarks() - { - return bookmarks; - } -} diff --git a/Essentials/src/com/earth2me/essentials/textreader/TextPager.java b/Essentials/src/com/earth2me/essentials/textreader/TextPager.java deleted file mode 100644 index c23df734d..000000000 --- a/Essentials/src/com/earth2me/essentials/textreader/TextPager.java +++ /dev/null @@ -1,182 +0,0 @@ -package com.earth2me.essentials.textreader; - -import static com.earth2me.essentials.I18n._; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import org.bukkit.command.CommandSender; - - -public class TextPager -{ - private final transient IText text; - private final transient boolean onePage; - - public TextPager(final IText text) - { - this(text, false); - } - - public TextPager(final IText text, final boolean onePage) - { - this.text = text; - this.onePage = onePage; - } - - public void showPage(final String pageStr, final String chapterPageStr, final String commandName, final CommandSender sender) - { - List lines = text.getLines(); - List chapters = text.getChapters(); - Map bookmarks = text.getBookmarks(); - - if (bookmarks.isEmpty()) - { - int page = 1; - try - { - page = Integer.parseInt(pageStr); - } - catch (Exception ex) - { - page = 1; - } - if (page < 1) - { - page = 1; - } - - final int start = onePage ? 0 : (page - 1) * 9; - final int pages = lines.size() / 9 + (lines.size() % 9 > 0 ? 1 : 0); - if (!onePage) - { - sender.sendMessage(_("infoPages", page, pages)); - } - for (int i = start; i < lines.size() && i < start + (onePage ? 20 : 9); i++) - { - sender.sendMessage(lines.get(i)); - } - if (!onePage && page < pages) - { - sender.sendMessage(_("readNextPage", commandName, page + 1)); - } - return; - } - - if (pageStr == null || pageStr.isEmpty() || pageStr.matches("[0-9]+")) - { - if (lines.get(0).startsWith("#")) - { - if (onePage) - { - return; - } - sender.sendMessage(_("infoChapter")); - final StringBuilder sb = new StringBuilder(); - boolean first = true; - for (String string : chapters) - { - if (!first) - { - sb.append(", "); - } - first = false; - sb.append(string); - } - sender.sendMessage(sb.toString()); - return; - } - else - { - int page = 1; - try - { - page = Integer.parseInt(pageStr); - } - catch (Exception ex) - { - page = 1; - } - if (page < 1) - { - page = 1; - } - - int start = onePage ? 0 : (page - 1) * 9; - int end; - for (end = 0; end < lines.size(); end++) - { - String line = lines.get(end); - if (line.startsWith("#")) - { - break; - } - } - - int pages = end / 9 + (end % 9 > 0 ? 1 : 0); - if (!onePage) - { - - sender.sendMessage(_("infoPages", page, pages)); - } - for (int i = start; i < end && i < start + (onePage ? 20 : 9); i++) - { - sender.sendMessage(lines.get(i)); - } - if (!onePage && page < pages) - { - sender.sendMessage(_("readNextPage", commandName, page + 1)); - } - return; - } - } - - int chapterpage = 0; - if (chapterPageStr != null) - { - try - { - chapterpage = Integer.parseInt(chapterPageStr) - 1; - } - catch (Exception ex) - { - chapterpage = 0; - } - if (chapterpage < 0) - { - chapterpage = 0; - } - } - - if (!bookmarks.containsKey(pageStr.toLowerCase(Locale.ENGLISH))) - { - sender.sendMessage(_("infoUnknownChapter")); - return; - } - final int chapterstart = bookmarks.get(pageStr.toLowerCase(Locale.ENGLISH)) + 1; - int chapterend; - for (chapterend = chapterstart; chapterend < lines.size(); chapterend++) - { - final String line = lines.get(chapterend); - if (line.length() > 0 && line.charAt(0) == '#') - { - break; - } - } - final int start = chapterstart + (onePage ? 0 : chapterpage * 9); - - final int page = chapterpage + 1; - final int pages = (chapterend - chapterstart) / 9 + ((chapterend - chapterstart) % 9 > 0 ? 1 : 0); - if (!onePage) - { - sender.sendMessage(_("infoChapterPages", pageStr, page, pages)); - } - for (int i = start; i < chapterend && i < start + (onePage ? 20 : 9); i++) - { - sender.sendMessage(lines.get(i)); - } - if (!onePage && page < pages) - { - sender.sendMessage(_("readNextPage", commandName, pageStr + " " + (page + 1))); - } - } -} diff --git a/Essentials/src/com/earth2me/essentials/utils/textreader/ArrayListInput.java b/Essentials/src/com/earth2me/essentials/utils/textreader/ArrayListInput.java new file mode 100644 index 000000000..5943387a5 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/utils/textreader/ArrayListInput.java @@ -0,0 +1,31 @@ +package com.earth2me.essentials.utils.textreader; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + + +public class ArrayListInput implements IText +{ + private final transient List lines = new ArrayList(); + + @Override + public List getLines() + { + return lines; + } + + @Override + public List getChapters() + { + return Collections.emptyList(); + } + + @Override + public Map getBookmarks() + { + return Collections.emptyMap(); + } + +} diff --git a/Essentials/src/com/earth2me/essentials/utils/textreader/HelpInput.java b/Essentials/src/com/earth2me/essentials/utils/textreader/HelpInput.java new file mode 100644 index 000000000..67b946469 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/utils/textreader/HelpInput.java @@ -0,0 +1,152 @@ +package com.earth2me.essentials.utils.textreader; + +import static com.earth2me.essentials.I18n._; +import com.earth2me.essentials.api.IEssentials; +import com.earth2me.essentials.api.ISettings; +import com.earth2me.essentials.api.IUser; +import com.earth2me.essentials.permissions.HelpPermissions; +import java.io.IOException; +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; +import lombok.Cleanup; +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 lines = new ArrayList(); + private final transient List chapters = new ArrayList(); + private final transient Map bookmarks = new HashMap(); + private final static Logger logger = Logger.getLogger("Minecraft"); + + public HelpInput(final IUser user, final String match, final IEssentials ess) throws IOException + { + @Cleanup + final ISettings settings = ess.getSettings(); + settings.acquireReadLock(); + boolean reported = false; + String pluginName = ""; + for (Plugin p : ess.getServer().getPluginManager().getPlugins()) + { + try + { + final PluginDescriptionFile desc = p.getDescription(); + final Map> cmds = desc.getCommands(); + pluginName = p.getDescription().getName().toLowerCase(Locale.ENGLISH); + for (Map.Entry> 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 (!settings.getData().getCommands().isDisabled(k.getKey()) && user.hasPermission(node)) + { + lines.add(_("helpLine", k.getKey(), k.getValue().get(DESCRIPTION))); + } + } + else + { + if (settings.getData().getCommands().getHelp().isShowNonEssCommandsInHelp()) + { + final Map value = k.getValue(); + Object permissions = null; + if (value.containsKey(PERMISSION)) + { + permissions = value.get(PERMISSION); + } + else if (value.containsKey(PERMISSIONS)) + { + permissions = value.get(PERMISSIONS); + } + if (HelpPermissions.getPermission(pluginName).isAuthorized(user)) + { + lines.add(_("helpLine", k.getKey(), value.get(DESCRIPTION))); + } + else if (permissions instanceof List && !((List)permissions).isEmpty()) + { + boolean enabled = false; + for (Object o : (List)permissions) + { + if (o instanceof String && user.hasPermission(o.toString())) + { + enabled = true; + break; + } + } + if (enabled) + { + lines.add(_("helpLine", k.getKey(), value.get(DESCRIPTION))); + } + } + else if (permissions instanceof String && !"".equals(permissions)) + { + if (user.hasPermission(permissions.toString())) + { + lines.add(_("helpLine", k.getKey(), value.get(DESCRIPTION))); + } + } + else + { + if (!settings.getData().getCommands().getHelp().isHidePermissionlessCommands()) + { + lines.add(_("helpLine", k.getKey(), 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 getLines() + { + return lines; + } + + @Override + public List getChapters() + { + return chapters; + } + + @Override + public Map getBookmarks() + { + return bookmarks; + } +} diff --git a/Essentials/src/com/earth2me/essentials/utils/textreader/IText.java b/Essentials/src/com/earth2me/essentials/utils/textreader/IText.java new file mode 100644 index 000000000..c70554b28 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/utils/textreader/IText.java @@ -0,0 +1,14 @@ +package com.earth2me.essentials.utils.textreader; + +import java.util.List; +import java.util.Map; + + +public interface IText +{ + List getLines(); + + List getChapters(); + + Map getBookmarks(); +} diff --git a/Essentials/src/com/earth2me/essentials/utils/textreader/KeywordReplacer.java b/Essentials/src/com/earth2me/essentials/utils/textreader/KeywordReplacer.java new file mode 100644 index 000000000..92c7c8a0b --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/utils/textreader/KeywordReplacer.java @@ -0,0 +1,156 @@ +package com.earth2me.essentials.utils.textreader; + +import com.earth2me.essentials.utils.DescParseTickFormat; +import com.earth2me.essentials.api.IEssentials; +import com.earth2me.essentials.api.IUser; +import java.text.DateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; +import lombok.Cleanup; +import org.bukkit.World; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + + +public class KeywordReplacer implements IText +{ + private final transient IText input; + private final transient List replaced; + private final transient IEssentials ess; + + public KeywordReplacer(final IText input, final CommandSender sender, final IEssentials ess) + { + this.input = input; + this.replaced = new ArrayList(this.input.getLines().size()); + this.ess = ess; + replaceKeywords(sender); + } + + private void replaceKeywords(final CommandSender sender) + { + String displayName, ipAddress, balance, mails, world; + String worlds, online, unique, playerlist, date, time; + String worldTime12, worldTime24, worldDate, plugins; + String userName, address, version; + if (sender instanceof Player) + { + @Cleanup + final IUser user = ess.getUser((Player)sender); + user.acquireReadLock(); + displayName = user.getDisplayName(); + userName = user.getName(); + ipAddress = user.getAddress().getAddress().toString(); + address = user.getAddress().toString(); + balance = Double.toString(user.getMoney()); + mails = Integer.toString(user.getData().getMails() == null ? 0 : user.getData().getMails().size()); + world = user.getLocation().getWorld().getName(); + worldTime12 = DescParseTickFormat.format12(user.getWorld().getTime()); + worldTime24 = DescParseTickFormat.format24(user.getWorld().getTime()); + worldDate = DateFormat.getDateInstance(DateFormat.MEDIUM, ess.getI18n().getCurrentLocale()).format(DescParseTickFormat.ticksToDate(user.getWorld().getFullTime())); + } + else + { + displayName = ipAddress = balance = mails = world = worldTime12 = worldTime24 = worldDate = ""; + } + + int playerHidden = 0; + for (Player p : ess.getServer().getOnlinePlayers()) + { + if (ess.getUser(p).isHidden()) + { + playerHidden++; + } + } + online = Integer.toString(ess.getServer().getOnlinePlayers().length - playerHidden); + unique = Integer.toString(ess.getUserMap().getUniqueUsers()); + + final StringBuilder worldsBuilder = new StringBuilder(); + for (World w : ess.getServer().getWorlds()) + { + if (worldsBuilder.length() > 0) + { + worldsBuilder.append(", "); + } + worldsBuilder.append(w.getName()); + } + worlds = worldsBuilder.toString(); + + final StringBuilder playerlistBuilder = new StringBuilder(); + for (Player p : ess.getServer().getOnlinePlayers()) + { + if (ess.getUser(p).isHidden()) + { + continue; + } + if (playerlistBuilder.length() > 0) + { + playerlistBuilder.append(", "); + } + playerlistBuilder.append(p.getDisplayName()); + } + playerlist = playerlistBuilder.toString(); + + final StringBuilder pluginlistBuilder = new StringBuilder(); + for (Plugin p : ess.getServer().getPluginManager().getPlugins()) + { + if (pluginlistBuilder.length() > 0) + { + pluginlistBuilder.append(", "); + } + pluginlistBuilder.append(p.getDescription().getName()); + } + plugins = pluginlistBuilder.toString(); + + date = DateFormat.getDateInstance(DateFormat.MEDIUM, ess.getI18n().getCurrentLocale()).format(new Date()); + time = DateFormat.getTimeInstance(DateFormat.MEDIUM, ess.getI18n().getCurrentLocale()).format(new Date()); + + version = ess.getServer().getVersion(); + + for (int i = 0; i < input.getLines().size(); i++) + { + String line = input.getLines().get(i); + + line = line.replace("{PLAYER}", displayName); + line = line.replace("{DISPLAYNAME}", displayName); + line = line.replace("{USERNAME}", displayName); + line = line.replace("{IP}", ipAddress); + line = line.replace("{ADDRESS}", ipAddress); + line = line.replace("{BALANCE}", balance); + line = line.replace("{MAILS}", mails); + line = line.replace("{WORLD}", world); + line = line.replace("{ONLINE}", online); + line = line.replace("{UNIQUE}", unique); + line = line.replace("{WORLDS}", worlds); + line = line.replace("{PLAYERLIST}", playerlist); + line = line.replace("{TIME}", time); + line = line.replace("{DATE}", date); + line = line.replace("{WORLDTIME12}", worldTime12); + line = line.replace("{WORLDTIME24}", worldTime24); + line = line.replace("{WORLDDATE}", worldDate); + line = line.replace("{PLUGINS}", plugins); + line = line.replace("{VERSION}", version); + replaced.add(line); + } + } + + @Override + public List getLines() + { + return replaced; + } + + @Override + public List getChapters() + { + return input.getChapters(); + } + + @Override + public Map getBookmarks() + { + return input.getBookmarks(); + } +} diff --git a/Essentials/src/com/earth2me/essentials/utils/textreader/SimpleTextInput.java b/Essentials/src/com/earth2me/essentials/utils/textreader/SimpleTextInput.java new file mode 100644 index 000000000..4a94126d3 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/utils/textreader/SimpleTextInput.java @@ -0,0 +1,35 @@ +package com.earth2me.essentials.utils.textreader; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + + +public class SimpleTextInput implements IText +{ + private final transient List lines = new ArrayList(); + + public SimpleTextInput (final String input) { + lines.add(input); + } + + @Override + public List getLines() + { + return lines; + } + + @Override + public List getChapters() + { + return Collections.emptyList(); + } + + @Override + public Map getBookmarks() + { + return Collections.emptyMap(); + } + +} diff --git a/Essentials/src/com/earth2me/essentials/utils/textreader/SimpleTextPager.java b/Essentials/src/com/earth2me/essentials/utils/textreader/SimpleTextPager.java new file mode 100644 index 000000000..3c9e9cb55 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/utils/textreader/SimpleTextPager.java @@ -0,0 +1,31 @@ +package com.earth2me.essentials.utils.textreader; + +import org.bukkit.command.CommandSender; + + +public class SimpleTextPager +{ + private final transient IText text; + + public SimpleTextPager(final IText text) + { + this.text = text; + } + + public void showPage(final CommandSender sender) + { + for (String line : text.getLines()) + { + sender.sendMessage(line); + } + } + + public String getString(int line) + { + if (text.getLines().size() < line) + { + return null; + } + return text.getLines().get(line); + } +} diff --git a/Essentials/src/com/earth2me/essentials/utils/textreader/TextInput.java b/Essentials/src/com/earth2me/essentials/utils/textreader/TextInput.java new file mode 100644 index 000000000..369c0e0e1 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/utils/textreader/TextInput.java @@ -0,0 +1,147 @@ +package com.earth2me.essentials.utils.textreader; + +import com.earth2me.essentials.utils.Util; +import com.earth2me.essentials.api.IEssentials; +import com.earth2me.essentials.api.IUser; +import com.earth2me.essentials.api.InvalidNameException; +import java.io.*; +import java.lang.ref.SoftReference; +import java.util.*; +import java.util.logging.Level; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + + +public class TextInput implements IText +{ + private final transient List lines; + private final transient List chapters; + private final transient Map bookmarks; + private final transient long lastChange; + private final static HashMap> cache = new HashMap>(); + + public TextInput(final CommandSender sender, final String filename, final boolean createFile, final IEssentials ess) throws IOException + { + + File file = null; + if (sender instanceof Player) + { + try + { + final IUser user = ess.getUser((Player)sender); + file = new File(ess.getDataFolder(), filename + "_" + Util.sanitizeFileName(user.getName()) + ".txt"); + if (!file.exists()) + { + file = new File(ess.getDataFolder(), filename + "_" + Util.sanitizeFileName(ess.getRanks().getMainGroup(user)) + ".txt"); + } + } + catch (InvalidNameException ex) + { + Bukkit.getLogger().log(Level.WARNING, ex.getMessage(), ex); + } + } + if (file == null || !file.exists()) + { + file = new File(ess.getDataFolder(), filename + ".txt"); + } + if (file.exists()) + { + lastChange = file.lastModified(); + boolean readFromfile; + synchronized (cache) + { + final SoftReference inputRef = cache.get(file.getName()); + TextInput input; + if (inputRef == null || (input = inputRef.get()) == null || input.lastChange < lastChange) + { + lines = new ArrayList(); + chapters = new ArrayList(); + bookmarks = new HashMap(); + cache.put(file.getName(), new SoftReference(this)); + readFromfile = true; + } + else + { + lines = Collections.unmodifiableList(input.getLines()); + chapters = Collections.unmodifiableList(input.getChapters()); + bookmarks = Collections.unmodifiableMap(input.getBookmarks()); + readFromfile = false; + } + } + if (readFromfile) + { + final BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); + try + { + int lineNumber = 0; + while (bufferedReader.ready()) + { + final String line = bufferedReader.readLine(); + if (line == null) + { + break; + } + if (line.length() > 0 && line.charAt(0) == '#') + { + bookmarks.put(line.substring(1).toLowerCase(Locale.ENGLISH).replaceAll("&[0-9a-f]", ""), lineNumber); + chapters.add(line.substring(1).replace('&', '�').replace("�", "&")); + } + lines.add(line.replace('&', '�').replace("�", "&")); + lineNumber++; + } + } + finally + { + bufferedReader.close(); + } + } + } + else + { + lastChange = 0; + lines = Collections.emptyList(); + chapters = Collections.emptyList(); + bookmarks = Collections.emptyMap(); + if (createFile) + { + final InputStream input = ess.getResource(filename + ".txt"); + final OutputStream output = new FileOutputStream(file); + try + { + final byte[] buffer = new byte[1024]; + int length = input.read(buffer); + while (length > 0) + { + output.write(buffer, 0, length); + length = input.read(buffer); + } + } + finally + { + output.close(); + input.close(); + } + throw new FileNotFoundException("File " + filename + ".txt does not exist. Creating one for you."); + } + } + } + + @Override + public List getLines() + { + return lines; + } + + @Override + public List getChapters() + { + return chapters; + } + + @Override + public Map getBookmarks() + { + return bookmarks; + } +} diff --git a/Essentials/src/com/earth2me/essentials/utils/textreader/TextPager.java b/Essentials/src/com/earth2me/essentials/utils/textreader/TextPager.java new file mode 100644 index 000000000..70f68222b --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/utils/textreader/TextPager.java @@ -0,0 +1,182 @@ +package com.earth2me.essentials.utils.textreader; + +import static com.earth2me.essentials.I18n._; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import org.bukkit.command.CommandSender; + + +public class TextPager +{ + private final transient IText text; + private final transient boolean onePage; + + public TextPager(final IText text) + { + this(text, false); + } + + public TextPager(final IText text, final boolean onePage) + { + this.text = text; + this.onePage = onePage; + } + + public void showPage(final String pageStr, final String chapterPageStr, final String commandName, final CommandSender sender) + { + List lines = text.getLines(); + List chapters = text.getChapters(); + Map bookmarks = text.getBookmarks(); + + if (bookmarks.isEmpty()) + { + int page = 1; + try + { + page = Integer.parseInt(pageStr); + } + catch (Exception ex) + { + page = 1; + } + if (page < 1) + { + page = 1; + } + + final int start = onePage ? 0 : (page - 1) * 9; + final int pages = lines.size() / 9 + (lines.size() % 9 > 0 ? 1 : 0); + if (!onePage) + { + sender.sendMessage(_("infoPages", page, pages)); + } + for (int i = start; i < lines.size() && i < start + (onePage ? 20 : 9); i++) + { + sender.sendMessage(lines.get(i)); + } + if (!onePage && page < pages) + { + sender.sendMessage(_("readNextPage", commandName, page + 1)); + } + return; + } + + if (pageStr == null || pageStr.isEmpty() || pageStr.matches("[0-9]+")) + { + if (lines.get(0).startsWith("#")) + { + if (onePage) + { + return; + } + sender.sendMessage(_("infoChapter")); + final StringBuilder sb = new StringBuilder(); + boolean first = true; + for (String string : chapters) + { + if (!first) + { + sb.append(", "); + } + first = false; + sb.append(string); + } + sender.sendMessage(sb.toString()); + return; + } + else + { + int page = 1; + try + { + page = Integer.parseInt(pageStr); + } + catch (Exception ex) + { + page = 1; + } + if (page < 1) + { + page = 1; + } + + int start = onePage ? 0 : (page - 1) * 9; + int end; + for (end = 0; end < lines.size(); end++) + { + String line = lines.get(end); + if (line.startsWith("#")) + { + break; + } + } + + int pages = end / 9 + (end % 9 > 0 ? 1 : 0); + if (!onePage) + { + + sender.sendMessage(_("infoPages", page, pages)); + } + for (int i = start; i < end && i < start + (onePage ? 20 : 9); i++) + { + sender.sendMessage(lines.get(i)); + } + if (!onePage && page < pages) + { + sender.sendMessage(_("readNextPage", commandName, page + 1)); + } + return; + } + } + + int chapterpage = 0; + if (chapterPageStr != null) + { + try + { + chapterpage = Integer.parseInt(chapterPageStr) - 1; + } + catch (Exception ex) + { + chapterpage = 0; + } + if (chapterpage < 0) + { + chapterpage = 0; + } + } + + if (!bookmarks.containsKey(pageStr.toLowerCase(Locale.ENGLISH))) + { + sender.sendMessage(_("infoUnknownChapter")); + return; + } + final int chapterstart = bookmarks.get(pageStr.toLowerCase(Locale.ENGLISH)) + 1; + int chapterend; + for (chapterend = chapterstart; chapterend < lines.size(); chapterend++) + { + final String line = lines.get(chapterend); + if (line.length() > 0 && line.charAt(0) == '#') + { + break; + } + } + final int start = chapterstart + (onePage ? 0 : chapterpage * 9); + + final int page = chapterpage + 1; + final int pages = (chapterend - chapterstart) / 9 + ((chapterend - chapterstart) % 9 > 0 ? 1 : 0); + if (!onePage) + { + sender.sendMessage(_("infoChapterPages", pageStr, page, pages)); + } + for (int i = start; i < chapterend && i < start + (onePage ? 20 : 9); i++) + { + sender.sendMessage(lines.get(i)); + } + if (!onePage && page < pages) + { + sender.sendMessage(_("readNextPage", commandName, pageStr + " " + (page + 1))); + } + } +} -- cgit v1.2.3