Calling a function in another class, erroring?

Discussion in 'Spigot Plugin Development' started by Moderance, May 21, 2016.

  1. So I have a main class, and another class I am working on, here's the other one:

    Code (Text):
                            for (Player checkPlayer : Bukkit.getServer().getOnlinePlayers()) {
                                boolean Checking = new Main.checkUserdata(checkPlayer);
                                if (Checking == true) {
                                    //Todo: checkPlayer.sendMessage("");
                                }
                            }
    So it checks to see if the player has the 'SpyEnabled' setting in their config set to true, here's the main:

    Code (Text):
    public boolean checkUserdata(Player checkPlayer) throws IOException {
            if (checkPlayer.hasPermission("spy")) {
                File file = new File("plugins//test//players" + checkPlayer.getName() + ".yml");
                //If it cannot find the userdata file.
                if (!file.exists()) {
                    file.createNewFile();
                    YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
                    config.set("SpyEnabled", "true");
                    config.save(file);
                    return true;
                } else {
                    YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
                    if (config.getString("SpyEnabled").equals("true")) {
                        return true;
                    } else {
                        return false;
                    }
                }
            } else {
                return false;
            }
        }
    So the 'Main' checks to see if they have the permission of 'spy' then if they have their own .yml file if not it creates one, then checks to see if it has 'SpyEnabled', and if not to create it, then return true to say it's enabled, otherwise if they already have one then it will return the value, I am doing this because I want my admins to be able to 'spy' on certain commands which they can disable/enable with a command which will just change the value of the SpyEnable in their .yml file.

    The error:

    So back to the other class this is the line which gets the error:

    Code (Text):
    boolean Checking = new Main.checkUserdata(checkPlayer);
    "Main.checkUserdata cannot be resolved to a type".
     
  2. Solved!

    Also, what is wrong with this function, it doesn't seem to create a directory when it doesn't exist I had to manually make the folders:

    And it's also called everytime onEnabled():

    Code (Text):
        public static void checkServerFolder() {
            File file = new File("plugins//Server//players");
            //If it cannot find the userdata file.
            if (!file.isDirectory()) {
                file.mkdir();
            }
        }
     
  3. First, use camelCase for the name. The variable "Checking " is not proper java naming conventions.
     
  4. I previously mentioned that I solved it, I used this:

    if (Main.checkUserdata(checkPlayer)) {
     
  5. Not solved, just making things static is not the right way to go (that's not OOP). And as I can see in your original question you tried to make a new instance of your Main class which you should not do either. There is already an instance of your Main class created by the server.

    You should use dependency injection instead. Just pass 'this' in the Main class to the constructor of another class.

    I think you should use mkdirs() instead of mkdir().

    Edit: You should also use a single / instead of // in your path. It's safe for all platforms in Java.
     
    #5 stonar96, May 21, 2016
    Last edited: May 21, 2016