Solved "/cmd <name>" then choose punishment in GUI

Discussion in 'Plugin Development' started by yPedx, Mar 21, 2017.

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


    Hello :D

    I'm VERY curious how people do this.. Let me explain a little more detailed; I'm trying to make a punishGUI, basically you do /punish <name> and then select a punishment in a GUI. But I have no idea how to make the commands inside the GUI affect that specific player..
  2. Offline


    Hmm, well to achieve this, you obviously need an inventory. You would type /punish player123 and then have that open up your custom gui, I would recommend using a custom InventoryHolder to save the players name. THEN, you listen on the InventoryClickEvent for clicks and run the command based on the item.
  3. Offline


    1. When a user specifies which player to "punish" (either through another UI or through a command), store the name or uuid of the player. Best place where to store them is in a hashmap.
    2. After that, open the GUI with all the options on how to punish them.
    3. When an option is selected, get the player from the hashmap and punish them.
  4. Offline


    The only thing I don't know is, how to store the player's name.

    I'm not that experienced with coding :p But I'll try search up how to make and use hashmaps.
  5. Offline


    You need a static variable and a setter if they are in separate classes :p
  6. Offline


    Don't recommend the static modifier. It does not have to be used in any situation.
    mine-care likes this.
  7. Offline


    @Zombie_Striker there it has to in some cases (constant fields across objects) but when it is used, it needs some special care. That is, release all objects pointed-at by static pointers (assign the pointers to null) so that garbage collection can occur.

    Still this statement remains vaigue and -by me- incorrect because it as it stands it defeats Object orientation to some extent... Rather than using static to comunicate between Classes how about using object pointers and references to interact with Objects. :p
  8. Offline


  9. Offline


    I read something that said static is for getting variables from other classes. What exactly is this and final used for?

    Sent from my iPhone using Tapatalk
  10. Offline


    @mehboss Constructor can do the same. Static is just an easier way for access to variables and methods.
    But it's a pain for the memory. And bukkit has already too much of static methods.

    Static should be used only for keep a variable safe from instance of the class.
  11. Offline


    HashMap<UUID, Integer> playerScores = new HashMap<UUID, Integer>();
    Is it possible to change this to capture name? If I want to use it in a /ban cmd, does the UUID work? (AdvancedBan) Would it be;
    HashMap<NAME, Integer> playerName= new HashMap<NAME, Integer>();
    I literally have no idea how to do this..

    ! Before you comment "learn Java" or something.. I'm trying.

    EDIT: Wait, I think I've miss understood xD
    Last edited: Mar 22, 2017
  12. Offline


    If you want to store names, then you should replace UUID/NAME with "String" (Since names are strings). The thing is, players can re-name themselves, so using UUIDs are recommended.
  13. Offline


    And you store UUID's with;
    HashMapString playerUUID = new HashMapString();
    ? xD

    Last edited: Mar 23, 2017
  14. Offline


    Well static is indeed for accessing CLASS members. Non static however is declaring variables belonging to the perticular Object and not the class.
    Final is a modifier that for variables means that they will only be assigned one value in code and they wont be modifyable after that, for methods it means that they can't be overwritten by a subclass and for classes it means that they can't be superclasses (i.e. a class may not extend a final class)

    static final variables are called constants because they belong to the Class and have a specific value that cant be changed during runtime :)
  15. @yPedx Use a UUID, you can easily get the UUID from a name with Bukkit#getPlayer or if they're offline Bukkit#getOfflinePlayer and then just do getUniqueId()
    It is recommended as @Zombie_Striker said.
  16. Offline


    The point is, I don't know how to make that code xD :

    I found a page about, but I'm still scratching my head.
  17. Offline


    To create a hashmap, use
    HashMap<KEY,VALUE> hashmap = new HashMap<>();
    1. KEY is the class type of what you want the key to be (In this example, use UUID)
    2. Value is the class type of the variable. This will be an Integer.
  18. Offline


    Thank you both! I'll try a bit more and see, I'll come back and tell if I got it to work or not.
Thread Status:
Not open for further replies.

Share This Page