How do i add an amount of time to a command like /mute?

Discussion in 'Plugin Development' started by Officialjake, Jun 28, 2012.

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

    Officialjake

    I am developing my first plugin and I need help on how to add a time field to a /mute command or any command like that such as freeze. Any help would be greatly appreciated! Thanks!
     
  2. Offline

    EnvisionRed

    Use bukkit scheduler. Take a look at http://wiki.bukkit.org/Scheduler_Programming#Delayed_Tasks, that is probably what you need.

    You probably want to set the player as muted (storing his NAME in a hashmap, storing his actual player instance will cause memory leaks, in a hashmap something like: Map<String, boolean> mutelist = new HashMap<String, boolean>(); )

    Then on the command, store the player.getName() string as the string, and the boolean as true.
    Using bukkit scheduler, after the time specified, have it remove the player from that list.

    In a PlayerChatEvent handler, have it check if the player is in the hashmap, and if so, cancel his chat event.
     
    ferrybig likes this.
  3. Offline

    Sagacious_Zed Bukkit Docs

    You can accomplish the same effect without scheduling, you just need to note when they can talk again, then compare against the current time.
     
  4. its more memory eficent to use an Set<String> instaead of an Map<String,Boolean>, but the others aspects, good tutorial
     
  5. Offline

    Officialjake

    im currently using public ArrayList<String> mute = new ArrayList<String>(); to store the list of muted players.
     
  6. chance it to an set for more speed and less memory, you know, the small details matter
     
  7. Offline

    Sagacious_Zed Bukkit Docs

    Actually according to some IBM presentation a HashSet uses more memory and does less than a HashMap.
    http://t.co/oQIhYWRY

    Officialjake
    If you need to keep a collection of players but don't care about their order a set is the correct data structure.
     
  8. so, most maps use sets as back ground, how can maps be smaller than sets if they use sets?
     
  9. Offline

    aPandaification

    Your argument is invalid Sagacious_Zed posted a very long pdf which probably explains it in which I did not read (I actually agree with you hash sets use less than hash maps)
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    The short version is that a HashSet is implemented with a HashMap. Therefore a HashSet is a HashMap with more data.
    Not all Sets are bigger than Maps. This is a unique situation where a HashSet is bigger than a HashMap.

    EDIT: here is another article this one is shorter http://www.ibm.com/developerworks/java/library/j-codetoheap/index.html the other one came to mind first.
     
  11. my test shows something else
    Code:java
    1. public static void main(String[] args)
    2. {
    3. long memoryFirst = 0L;
    4.  
    5. System.gc();
    6.  
    7. memoryFirst = Runtime.getRuntime().freeMemory();
    8. System.out.println("Testing HashMap first: free mem now: " + Runtime.getRuntime().freeMemory());
    9. HashMap<String, Boolean> map = new HashMap<String, Boolean>(100);
    10.  
    11. System.out.println("Testing HashMap after creation: free mem now: " + Runtime.getRuntime().freeMemory());
    12. for (int i = 0; i < 100; i++)
    13. {
    14. map.put(Integer.toString(i), true);
    15. }
    16.  
    17. System.out.println("Testing HashMap after putting: free mem now: " + Runtime.getRuntime().freeMemory());
    18. System.out.println("usedmem: " + (memoryFirst-Runtime.getRuntime().freeMemory()));
    19.  
    20. map.clear();
    21. map = null;
    22.  
    23. System.gc();
    24.  
    25. memoryFirst = Runtime.getRuntime().freeMemory();
    26. System.out.println("Testing HashSet now: free mem now: " + Runtime.getRuntime().freeMemory());
    27. HashSet<String> set = new HashSet<String>(100);
    28. System.out.println("Testing HashSet after creation: free mem now: " + Runtime.getRuntime().freeMemory());
    29. for (int i = 0; i < 100; i++)
    30. {
    31. set.add(Integer.toString(i));
    32. }
    33.  
    34. System.out.println("Testing HashSet after putting: free mem now: " + Runtime.getRuntime().freeMemory());
    35. System.out.println("usedmem: " + (memoryFirst-Runtime.getRuntime().freeMemory()));
    36.  
    37. set.clear();
    38. set = null;
    39. }

    Code:
    Testing HashMap first: free mem now: 16101464
    Testing HashMap after creation: free mem now: 16009608
    Testing HashMap after putting: free mem now: 16009608
    usedmem: 91856
    Testing HashSet now: free mem now: 16101288
    Testing HashSet after creation: free mem now: 16101288
    Testing HashSet after putting: free mem now: 16101288
    usedmem: 91816
    Sets use 40 bytes less for 100 elements
     
  12. Offline

    Officialjake

    I need to have the command be like /mute <player> <time> with the time being in d,h,m,s format.

    I need to have the command be like /mute <player> <time> with the time being in d,h,m,s format.

    can anyone help?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  13. Offline

    bartboy8

    Well, it would kindof be hard to do it in days in the way I know how to code. I am not experienced at all but you could use a time int
    Code:
    int time = Integer.parseInt(args[1])*20;
    That would make it so that the mute time would be in seconds. Every 20 ticks is 1 second. That is why we have the *20.
     
  14. Offline

    Officialjake

    Thanks yea for a mute command hours and days are impractical.
     
  15. Create a HashMap<String, Long> the string being the name and the long being (system.currentTimeMillis + the time to be muted)
    Then create repeatedsheduleevent and go through the list to see when the currentTimeMillis is larger than the stored value of the player, if so, you can remove that player from the list.

    And the improve performance you should sort the collection and use binary search to lookup names when they try to talk.

    You really shouldn't be worried about memory usage. It would take a lot (10000+) before you'd being use ~1MB of heap space. (based on 2M entries using 200MB heapspace).
     
Thread Status:
Not open for further replies.

Share This Page