Solved How to set a variable for a specific player

Discussion in 'Plugin Development' started by KarimAKL, Mar 20, 2018.

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

    KarimAKL

    I want to set a variable for a specific player so i can check later if they have that variable. What i want is to have a command that has 1 arg and if that arg is set it'll check if the player in arg 1 has a variable (the variable needs to be diffrent for everyone) and if that variable is set to false it'll set it to true and do some stuff same for true to false (like a toggle for the player) so my question is, how would i do this?
    EDIT: Yes i know this would be easy to make in skript but i wanna learn how to do it like this.
     
  2. Online

    timtower Moderator Moderator

  3. Offline

    KarimAKL

    Uhm, and how would i do it? ._.
     
  4. Online

    timtower Moderator Moderator

    @KarimAKL You make a hashmap uuid, whatevertypeofvariableyouwant.
    You put something in it with the onCommand.
    You read something from it in the event or where you might want to use it.
     
  5. Offline

    Horsey

    You should use a WeakHashMap<Player, YourData> so that you don't hold on to players after they leave.
     
    RcExtract likes this.
  6. Offline

    KarimAKL

    Sorry, i'm new to this hashmap thing so i don't know how to do all of that. :/
     
  7. Online

    timtower Moderator Moderator

  8. Offline

    KarimAKL

    I'm sorry but i still don't know what to do, i got a little information out of that but still not enough to know what i need to do. :/
     
  9. Online

    timtower Moderator Moderator

  10. Offline

    KarimAKL

    Thanks, i'll try reading that again when i'm at my main computer (the one that has eclipse downloaded) again. :D
     
  11. Offline

    KarimAKL

    Okay, i'm back at my main computer and i just tried reading the thread you sent a link to and watched some tutorials about hashmaps but i'm stuck at what i need to write at these parts:
    Code:
    public final static HashMap<Something, Something> whatevername = new HashMap<Something, Something>();
    
    And
    Code:
    whatevername.put(Something, Something);
    
    The parts are what i need to write after put at whatevername.put and at HashMap<Something, Something>? What i want is to have a toggle for the player in arg 1 with '/whatever <player>' and btw the player needs to be online for it to work(is what i want), what would i do?
     
  12. Online

    timtower Moderator Moderator

    @KarimAKL Please. never use public static. You don't need it.
    Something1 is the key, Something2 is the value.
    In your case I would suggest UUID, String
     
  13. Offline

    KarimAKL

    UUID as string or UUID and string? Also is there somewhere i can read/see all the diffrent keys/values i can choose from?
    EDIT: Oh, and then 'public final' only?
     
  14. Online

    timtower Moderator Moderator

    @KarimAKL It is a key value, so UUID and string (assuming that you want to save a string)
    The types are flexible, you can use almost any class you can find.
    And private final please with a getter if needed.
     
  15. Offline

    KarimAKL

    Okay i will do that then, thanks. Getter? What do you mean?
     
  16. Online

    timtower Moderator Moderator

  17. Offline

    KarimAKL

    Okay i think i know how to do that now, thanks. But what was it that you meant? I mean why should i use a getter?
     
  18. Online

    timtower Moderator Moderator

    @KarimAKL you only need it if you want to access the hadhmap from a different class
     
  19. Offline

    KarimAKL

    Well i need to access it from my event class after i set it with the command so i guess i would need it, right? Anyway, so i would do this for the hashmap?:
    Code:
    private final HashMap<String, Integer> whatevername = new HashMap<String, Integer>();
    
    And then in my command:
    Code:
    whatevername.put(how_i_would_get_the_UUID, What_should_i_do_here?)
    
    Is that right?
     
  20. Online

    timtower Moderator Moderator

    @KarimAKL Use an UUID, not String
    And the UUID is the player UUID
    The value is whatever you want to store.
     
  21. Offline

    KarimAKL

    So i should do this then?:
    Code:
    private final HashMap<UUID, Integer> toggle = new HashMap<UUID, Integer>();
    
    Whatever i want to store? What is it that i would want to store if i want to make a command that has 1 arg and that 1 arg needs to be an online player and then set that player's "value?" to true/false(if that is possible, if not then just 0 and 1, if that isn't possible either then just whatever i would do instead) and get that in the event class?
     
  22. Online

    timtower Moderator Moderator

    @KarimAKL For that purpose a list would be better, then you can check if the list contains the player or not.

    Try to explain your end goal, there might be a better way for you.
     
  23. Offline

    KarimAKL

    Explain my end goal? Like what i want the command/plugin to do?
     
  24. Online

    timtower Moderator Moderator

    Yes
     
  25. Offline

    KarimAKL

    Well i want to create a command with the name '/hacker' and then have it have 1 arg (like /hacker <player>) and that should be a toggle that tags them as a hacker and then you can type it again to untag them if you accidently wrote the wrong name or something and then if the player is tagged as a hacker i want to have another class with the EntityDamageByEntityEvent and then check if they are tagged and if they are i want them to get banned the next time they hit someone. That's pretty much it. Then i'll create a config where you can choose if you want them to get killed before they are banned and stuff like that. That's pretty much what i wanna create.
     
  26. Online

    timtower Moderator Moderator

    @KarimAKL Then a list which contains the hackers UUID is the best option for you.
     
  27. Offline

    KarimAKL

    How would i do that?
     
  28. Online

    timtower Moderator Moderator

  29. Offline

    KarimAKL

    Sorry but where should i put this?
     
  30. Online

    timtower Moderator Moderator

    @KarimAKL It is a replacement for the hashmap
     
Thread Status:
Not open for further replies.

Share This Page