How do I make a togglable command?

Discussion in 'Spigot Plugin Development' started by Creatos137, May 5, 2017.

  1. Hey,

    I was wondering how to create a togglable command for example: /nightvision

    (if its not enabled) when using it you enable nightvision
    (if its enabled) when using it you disable nightvision

    Thank you for your time.
    - Creatos
  2. You save the players (players' UUID) who currently have it enabled. I believe in this case you should save them in a HashSet.
    Sets have a "contains" method, so you know whether the specific player has it enabled at that moment.
  3. Have a List with all the player names/UUIDs of those who have the thing toggled on. Then in your command use:
    Code (Text):
    If (Toggled#contains(player name/UUID){
    Toggled.remove (player name/UUID);
    //do what needs to be done if toggled off

    }else {
    Toggled.add (Player name/UUID);
    //do what needs to be done when toggled on

  4. ArrayList, why to use a hashset? If the player is not in the arraylist, add it and execute the script for enabling him whatever you want
    If the player is, remove him and execute the script for disabling him whatever you want

    A List is not recomended, is an old version of ArrayList :p
    • Funny Funny x 1
  5. Because you don't need to have the players to be ordered, therefore a set is enough, and is faster.

    List is an interface, arraylist implemts it. I'm pretty sure you are totally wrong in you reply.
  6. No its not. Please take time to learn the language. List is a interface , it is implemented by ArrayList. Same goes for Maps and hashmaps. Map is a interface and hashmap implements it. If you would like to test this, try the following,

    Code (Text):
    List<String> learnJava = new ArrayList<String>;
    List refers to all objects which implement List since all of them are basically coming under List. Again, List is not a older version of ArrayList.

    Refer to THIS if you would like.
  7. Mas


    You're better of to disable night vision on whether the player currently has it as an active effect. Loop through Player#getActivePotionEffects and see if PotionEffect#getType is NIGHT_VISION (or whatever bukkit calls it).
  8. make a boolean for each player uuid in a config, try avoid using lots of ram to make other larger plugins make use of the ram
  9. Ram usage for such a small thing isnt gonna be much at all. :/
  10. (stacking) just negates your statement
  11. In this case the RAM usage is so minimal that any other approach is probably worse.
    If you save the data in a (bukkit) config file, it is cached in memory, so you gain nothing at all.
    If you use a filewriters-filereaders, it will slow down the server a lot.
    The best approach is a HashSet.
  12. What about using player.setMetadata()?

    you could just set a flag on them, it doesnt save during restarts (and im not sure about relogs) but i dont think that'd matter much for a nightvision command?

    Or does setmeta data have a big downside?
  13. Okay, my bad... I made a mistake and I was thinking in ArrayList and HashMap.

    Oh, also... I don't need that reference, I prefer using the API :)
    • Funny Funny x 1
  14. This too sounds like a worse way to store data than using a HashSet (or HashMap in case you need more than contains/not contains).
  15. But for toggeling a command that is exactly all you need.
    When they execute command check if metadata is set. if so unset and turn off "feature" else set metadata and turn on.
    You can even store more data gainst the player setMetadata("pluginname.nightvision",data) and retrieve it directly off of the player entity.

    But yeh the typical hasmap soloution is probably preferable, i was more just asking if metadata has any major downsides as a method of storing data against players in general.