Solved Issues with static referencing

Discussion in 'Plugin Development' started by 615283, Feb 20, 2018.

Thread Status:
Not open for further replies.
  1. I have the code below in my listener class and am trying to use the method in the class MainClass called logUUID(). But, I get an error on the line that is MainClass.logUUID(player); stating that it Cannot make a static reference to the non-static method logUUID(Player) from the type MainClass. Any ideas why, I'm totally confused here.

    Code:
    package com.georlegacy.general.betterwarnings;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    public class JoinListener implements Listener {
       
        @EventHandler
        public void playerJoinEvent(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            MainClass.logUUID(player);
        }
    }
    
    Code:
        //method to log player uuid to config file
        public void logUUID(Player player) {
            String pname = player.getName();
            UUID puuid = player.getUniqueId();
            String path = "UUIDs." + pname.toLowerCase();
            getLogger().info("INFO: Player " + pname + " joined with UUID " + puuid + "!");
            if (!(getConfig().contains(path))) {
                getLogger().info("INFO: " + pname + " is joining for first time! Logging UUID to config file!");
                getConfig().set(path, puuid);
            }
        }
     
  2. Offline

    timtower Administrator Administrator Moderator

    @615283 You don't have an instance of MainClass in the listener.
     
  3. So I just have to put in a line that says MainClass MainClass;?
     
  4. Offline

    timtower Administrator Administrator Moderator

    And fill it using the constructor, variable should start with lowercase though according to java conventions.
     
  5. Changed to lowercase variable, what did you mean by fill it using the constructor?
     
  6. Offline

    timtower Administrator Administrator Moderator

    It needs a value, value needs to come from somewhere, constructor is generally used for setting variables.
     
  7. So do I need to add more code than this:
    Code:
    package com.georlegacy.general.betterwarnings;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    public class JoinListener implements Listener {
       
        MainClass mainClass;
       
        @EventHandler
        public void playerJoinEvent(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            mainClass.logUUID(player);
        }
    }
    
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. How do I add a constructor?
     
  10. Offline

    timtower Administrator Administrator Moderator

  11. Any chance you could tell me how I would do it in my case?
     
  12. Offline

    timtower Administrator Administrator Moderator

    @615283 No, this is part of basic Java in my opinion. And in my opinion you shouldn't be working with Bukkit if you can't deal with basic Java.
     
  13. Ok thanks for your help so far though marking as solved.
     
Thread Status:
Not open for further replies.

Share This Page