Storing commands done, then getting them back as an int?

Discussion in 'Plugin Development' started by MrMag518, Feb 23, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    MrMag518

    Ok, the title may been a little fuzzy, but what I was wondering was if it is possible, or more, how to store commands in a hashmap, hashset, arraylist or something familiar.
    Then if you for an example do "/check /lol" to check how many times the command /lol have been done in the server, you'd get something like "the command /lol have been done __ times".

    So I've done something like
    Code:java
    1. public Set<String> command = new HashSet<String>();

    Then in the PlayerCommandPreprocessEvent I've done something like
    Code:java
    1. Player player = event.getPlayer();
    2. String command = event.getMessage();
    3.  
    4. plugin.command.add(command);
    5. player.sendMessage(ChatColor.GOLD + event.getMessage() + " have been typed "+ plugin.command.size() + " times.");

    This is just some test code, but the first problem I get is that it'll display how much "/" have been typed, not how many times "/lol" have been typed or, "/smile".. (It does not register the command separately, only when the slash is typed.)
    The second problem is that it does not register every command either, it just suddenly stops rising the size.
    The third known problem is that when the plugin is reloaded or unloaded someway, it will reset the amount.

    Yes, my knowledge isn’t on the top right now, and I've not tested to much either, but I'm kind of lost.
    I've tried to explain my best here, hope you can help me someway :)
     
  2. Offline

    dark navi

    Create a HashMap<String, int>, where string is the command and int is the number of times used. Simply increment the value every time the command is handled.
     
  3. Offline

    MrMag518

    Code:java
    1. public Map<String, int> command = new HashMap<String, int>();

    This returns the error:
    Code:
    unexpected type
    required: reference
    found: int
    I do not seem to find any sulution on how to make a HashMap using int, because of that error, any fix?

    EDIT: Sorry, found the problem, You have to use "Integer" instead of "int".
     
  4. It's called auto-boxing. Generics don't allow primitive types (like int, byte, float, double, short, long, char), only classes. So every primitive type has auto-boxed equivalents, which are Objects so they can be used as generics.
    The one for int is called "Integer" (exactly like that).

    Reference: http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html
     
    MrMag518 likes this.
  5. Offline

    MrMag518

    Sorry if I'm wrong, but
    Code:java
    1. @EventHandler
    2. public static void main(PlayerChatEvent event) {
    3. String[] args = null;
    4. Map<String, Integer> m = new TreeMap<String, Integer>();
    5. Player player = event.getPlayer();
    6. if (event.getMessage().startsWith("/")) {
    7. for (String word : args) {
    8. Integer freq = m.get(word);
    9. m.put(word, (freq == null ? 1 : freq + 1));
    10. }
    11. System.out.println(m);
    12. player.sendMessage(ChatColor.GOLD + event.getMessage() + "have been done "+ m + "times.");
    13. }
    14. }
    Doesn't return the player or the console any message, when a command is done :/

    The event has been registered too.
     
  6. You are not supposed to just copy and paste the example code from the linked page, it is not at all related to bukkit, just an informational page about Java auto-boxing ...
    Stick to PlayerCommandPreprocessEvent, you don't want to capture all chat messages, only commands.

    The HashMap (which shouldn't be a TreeMap) may not be a local variable in your event handler, it's supposed to be an attribute of your class. "public static void main(String[] args)" is the method head for the entry point of a Java program, why would you use that as an event listener ...
     
  7. Offline

    dark navi

    What about placing the incrementing code in the onCommand function?
     
  8. I suppose his goal is to get statistics for all commands, not just the ones of his plugin. onCommand is only called for commands that you registered in your plugin.yml.
     
    dark navi likes this.
  9. Offline

    dark navi

    Thanks for the info :)
     
Thread Status:
Not open for further replies.

Share This Page