lexi-lambda / SimpleJail

Simplistic Jail plugin for Bukkit
http://dev.bukkit.org/bukkit-plugins/simplejail/
5 stars 3 forks source link

Add jail reasons, change /jailtime to /jailinfo for listing all info about jailed players. #4

Closed lexi-lambda closed 9 years ago

lexi-lambda commented 12 years ago

Will add jail reasons along with other metadata to be stored, including who jailed the player, when, remaining jail time, etc.

Will be implemented as a JailInfo object, will be passed around in API. Additional properties can be registered by plugins in a LinkedHashMap.

Mitsugaru commented 12 years ago

Just out of curosity, thought I'd pop by and check on the repository... Saw you posted some issues and came across this.

Dunno how far you've gotten along with this, but in any case thought I'd try and give back to the original project. :P

So, here's a modified copy of how I deal with paginated entries.

/**
* This will probably go into your CommandExecutor class
*/
//Class variables
private final Map<String, Integer> historyPage = new HashMap<String, Integer>();
private final Map<String, String> historyCache = new HashMap<String, String>();

/**
* Somewhere, someone issued a command to look up the history for a player, with a given player name
* we validate the name. If valid, throw the name into history cache with key of the command sender name 
* and target name as value. Then, we can call listHistory(sender.getName(), 0);
*
* To skip pages, parse the given integer value, subtract 1 due to arrays starting at 0, put it into history page,
* and then call listHistory(sender.getName(), 0);
*/

/**
* pageAdjust of 0 shows current page. Use 1 and -1 to incremently show next and previous page with whatever
* command you wish.
*
* Also, you're going to have to change the "config.limit" to whatever you're going to use. Its basically the number of
* entries that are seen per page. I allow users to set it in the config (within reasonable bounds). 10 works fairly well...
* although not sure what my default actually is. I just make sure they don't do anything weird, like a negative value
* or something crazy high, anything more than a full chat block (which I think is 16). But that's all handled in the
* config class anyways.
*/
private void listHistory(CommandSender sender, int pageAdjust)
    {
        /**
        * You can handle this however you want. Also, you can use any object you want.
        */
        final List<String> list = plugin.getPlayerHistory(
                historyCache.get(sender.getName()););
        if (list.isEmpty())
        {
            sender.sendMessage(ChatColor.RED + KarmicJail.prefix
                    + " No history for " + ChatColor.AQUA + name);
            historyCache.remove(sender.getName());
            return;
        }
        if (!historyPage.containsKey(sender.getName()))
        {
            historyPage.put(sender.getName(), 0);
        }
        else
        {
            if (pageAdjust != 0)
            {
                int adj = page.get(sender.getName()).intValue() + pageAdjust;
                page.put(sender.getName(), adj);
            }
        }
        /**
        * Edit this to the correct array type if necessary
        */
        final String[] array = list.toArray(new String[0]);
        boolean valid = true;
        // Caluclate amount of pages
        int num = array.length / 8;
        double rem = (double) array.length % (double) config.limit;
        if (rem != 0)
        {
            num++;
        }
        if (historyPage.get(sender.getName()).intValue() < 0)
        {
            // They tried to use /ks prev when they're on page 0
            sender.sendMessage(ChatColor.YELLOW + KarmicJail.prefix
                    + " Page does not exist");
            // reset their current page back to 0
            historyPage.put(sender.getName(), 0);
            valid = false;
        }
        else if ((historyPage.get(sender.getName()).intValue()) * config.limit > array.length)
        {
            // They tried to use /ks next at the end of the list
            sender.sendMessage(ChatColor.YELLOW + KarmicJail.prefix
                    + " Page does not exist");
            // Revert to last page
            historyPage.put(sender.getName(), num - 1);
            valid = false;
        }
        if (valid)
        {
            // Header with amount of pages
            sender.sendMessage(ChatColor.BLUE + "===" + ChatColor.AQUA + name
                    + ChatColor.BLUE + "===" + ChatColor.GRAY + "Page: "
                    + ((historyPage.get(sender.getName()).intValue()) + 1)
                    + ChatColor.BLUE + " of " + ChatColor.GRAY + num
                    + ChatColor.BLUE + "===");
            // list
            for (int i = ((historyPage.get(sender.getName()).intValue()) * config.limit); i < ((historyPage
                    .get(sender.getName()).intValue()) * config.limit)
                    + config.limit; i++)
            {
                // Don't try to pull something beyond the bounds
                if (i < array.length)
                {
                    /**
                    * This was assuming that the List of strings. You just handle your
                    * object parsing and output however you need to here.
                    */
                    sender.sendMessage(plugin.colorizeText(array[i]));
                }
                else
                {
                    break;
                }
            }
        }
    }
lexi-lambda commented 9 years ago

This is no longer maintained.