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 see, then how would i do it? :eek:
     
  2. Offline

    timtower Administrator Administrator Moderator

    @KarimAKL You add the UUID to the list if they are marked as hacker, the contains function is now your boolean check.
     
  3. Offline

    KarimAKL

    Okay, but these lists are new to me aswell so i don't know how to do that. :/
     
  4. Offline

    KarimAKL

    bump, how would i do this?
     
  5. @KarimAKL
    List<UUID> hackerList = new ArrayList<UUID>();

    to add players you do
    hackerList.add(<whatever player>.getUniqueId());

    to check if a player is in that list do
    if(hackerList.contains(<whatever player>.getUniqueId())){}
    and that returns true if the player is in the list and false if they aren't

    hopefully you understand now, if you're still unsure maybe watch some youtube videos on lists and arrays
     
  6. Offline

    KarimAKL

    @Blackwing_Forged Okay thanks so much, also could you answer two more question for me?: What would i write in the "<whatever player>" for it to be a target player(like arg 1 in the command) and how would i check if arg 1 in the command is an online player?
    EDIT: Would it be possible to get the "hackerList" in another class or should i do something for that? right now i have "private final List<UUID> whatever = new ArrayList<UUID>();" should i change that to "public final List<UUID> whatever= new ArrayList<UUID>();" or something to be able to use it in another class?
     
    Last edited by a moderator: Mar 30, 2018
  7. @KarimAKL
    You can do a loop of all the online players and check if their name matches the arg1
    for(Player p : Bukkit.getOnlinePlayers())
    and then inside of that do
    if(p.getName().equals(args1)

    and yes you should do a public list to use in other classes, so if the class the list is in is called ListClass and you want to use it in OtherClass you would put in OtherClass
    ListClass listClass = new ListClass();
    List<UUID> list = listClass.hackerList;
     
  8. Offline

    KarimAKL

    Okay but then how would i get the player in arg 1? And how would i check if arg 1 is an online player?
    EDIT: I just tried "if (hackerList.contains(<whatever player>.getUniqueId())) {" but i got an error
     
    Last edited by a moderator: Mar 30, 2018
  9. @KarimAKL
    what error
    and i told you how to check if its an online player
     
  10. Offline

    KarimAKL

    @Blackwing_Forged Oh sorry i was probably too tired, anyway the error is:
    - Syntax error on token "<", @ expected
    - Syntax error on token ".", delete this token
    - The method getUniqueId() is undefined for the type
    ClassName
    - HackerList cannot be resolved
    - player cannot be resolved to a variable
    - Syntax error, type annotations are illegal here
    And i had to write it all instead of it coming with diffrent things i could write.
    I think this is because i don't have the
    "ListClass listClass = new ListClass();
    List<UUID> list = listClass.hackerList;"
    Like you said but when i try doing this:
    Code:
    NameOfClassWithList whatever = new NameOfClassWithList();
    
    List<UUID> list = whatever.hackerList;
    
    It comes with this error:
    "The constructor NameOfClassWithList() is undefined"

    So what should i do and what did i do wrong?
     
  11. Offline

    KarimAKL

    @Blackwing_Forged
    Code:
    public class ClassName implements Listener {
      
        NameOfClassWithList whatever = new NameOfClassWithList();
      
        List<UUID> list = whatever.HackerList;
      
        private Main plugin;
      
        public ClassName(Main plugin) {
            this.plugin = plugin;
          
            Bukkit.getPluginManager().registerEvents(this, plugin);
        }
      
        @EventHandler
        public void whatevereventname(EntityDamageByEntityEvent e) {
            if (e.getEntity() instanceof Player && e.getDamager() instanceof Player) {
                Player attacker = (Player) e.getDamager();
                Player victim = (Player) e.getEntity();
              
               //Code goes here (i just haven't wrote any because i need to get this done first)
            }
        }
    
    }
    
    
    This is my event class and here is the part with the list in my command class:
    Code:
    public final List<UUID> HackerList = new ArrayList<UUID>();
    
     
  12. @KarimAKL
    i meant your NameOfClassWithList
     
  13. Offline

    KarimAKL

    @Blackwing_Forged The rest doesn't have anything to do with the list but i can send it anyway, here you go:
    Code:
    public class ClassName implements CommandExecutor {
       
        public final List<UUID> HackerList = new ArrayList<UUID>();
       
        private Main plugin;
       
        public ClassName(Main plugin) {
            this.plugin = plugin;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("whatevercommandname")) {
                if (plugin.getConfig().getBoolean("whateverconfigbooleanname")) {
                    if (args.length == 1) {
                        if (sender.hasPermission("whateverpermission")) {
                           
                        }
                    }
                }
            }
            return false;
        }
    
    }
    
    
    
    This is the rest of the second line i sent you before
     
  14. @KarimAKL
    The class name should be ClassName not NameOfClassWithList
    and when you do ClassName class = new ClassName(); inside those parenthesis you need to add a plugin because thats what your constructor in that class has
     
  15. Offline

    KarimAKL

    That's my bad, the names of the classes is the same i just wrote "ClassName" in there instead of "NameOfClassWithList" but how do i "add a plugin"?
     
  16. Offline

    timtower Administrator Administrator Moderator

  17. Offline

    KarimAKL

    Okay, i'll do that next. :p And where should i put that new ClassName(this)?
     
  18. Offline

    timtower Administrator Administrator Moderator

    Where you make an instance of it...
     
  19. @timtower
    I was just using those class names as a clear example
     
  20. Offline

    KarimAKL

    @timtower Like this?:
    Code:
    CommandHacker CommandClass = new CommandHacker(this);
    
    List<UUID> list = CommandClass.HackerList;
    
     
  21. @KarimAKL
    You should start your variables with lowercase like commandClass
    just good practice, but yes like that
     
  22. Offline

    timtower Administrator Administrator Moderator

    @KarimAKL Why not start with a single class?
     
  23. Offline

    KarimAKL

    @Blackwing_Forged I just changed from CommandClass to commandClass and HackerList to hackerlist but still the same result.

    @timtower Well i could do that but i wanna learn how to do it with multiple classes. :/
     
  24. Offline

    timtower Administrator Administrator Moderator

    @KarimAKL I get that but currently it looks like you are trying to learn the basics of Java through Bukkit.
    That is not the way to do it.
     
  25. Offline

    KarimAKL

    @timtower Then where can i learn the basics of Java? Thank you for the help anyway. :D
    I'll try using 1 class until i get better, if using 1 class works for me i'll change this to solved.
     
    Last edited by a moderator: Mar 31, 2018
  26. Offline

    timtower Administrator Administrator Moderator

  27. Offline

    KarimAKL

    @timtower Okay thanks. :D Btw i found out that i can do this:
    Code:
    public static final List<UUID> ListName = new ArrayList<UUID>();
    
    And then in another class:
    Code:
    if (NameOfClassWithList.ListName.contains(player)) {
    
    It doesn't come with any errors but i haven't tried it in-game yet, anyway just wanted to tell you.

    Also i tried with one class but it comes with an error when i try doing this:
    Code:
    public class ClassName implements Listener implements CommandExecutor {
    
    Another thing: How would i add the UUID of a player to the List? Like this:
    Code:
    ListName.add(WhatShouldITypeHereToGetTheUUID?);
    
     
  28. @KarimAKL
    You just do a comma, not implements twice, so implements CommandExecutor, Listener

    and to get the UUID you need a Player variable then do player.getUniqueId()
     
  29. Offline

    KarimAKL

    @Blackwing_Forged I see, i tried implements Listener, implements CommandExecutor but not without the second implements, anyway thanks. :D And i want arg 1's UUID so how would i do that?
     
Thread Status:
Not open for further replies.

Share This Page