Solved Help with calling an int function in the main plugin class

Discussion in 'Plugin Development' started by Bbop537, Aug 3, 2020.

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

    Bbop537

    Hello, I can't seem to figure out how to call a function(in another class) that returns an integer(how many coins the player has). I have tried to search for multiple answers but to no avail, I haven't found any.
     
    Last edited: Aug 3, 2020
  2. Offline

    timtower Administrator Administrator Moderator

    @Bbop537 Have the instance of the class in a field, call the method on it.
     
  3. Offline

    Bbop537

  4. Offline

    timtower Administrator Administrator Moderator

    Depends on what you have.
    Please post the class that has the method in it using [code]<code here>[/code]
     
  5. Offline

    Bbop537

    Here you go:
    Code:
    public int getCoins(Player player) {
            String uuid = player.getUniqueId().toString();
            File userdata = new File(this.getServer().getPluginManager().getPlugin("Marketplace").getDataFolder(), File.separator + "PlayerDatabase");
            File f = new File(userdata, File.separator + uuid + ".yml");
            FileConfiguration playerData = YamlConfiguration.loadConfiguration(f);
            if(f.exists()) {
                try {
                    playerData.load(f);
                    int coins = (int)playerData.get("coins");
                    return coins;
                }
                catch(Exception e) {
                    Bukkit.broadcastMessage("Error getCoins: "+e.toString());
                }
            }
            setCoins(player,0);
            return 0;
    }
    
     
  6. Offline

    timtower Administrator Administrator Moderator

    @Bbop537 You called new classname somewhere, did you save that result?
     
  7. Offline

    Bbop537

    Yes:
    Code:
    Main main = new Main();
    int coins = main.getCoins(player);
    
    And that returns a NullPointerException
     
  8. Offline

    timtower Administrator Administrator Moderator

    That is something else than not knowing how to do it.
    Print the full error, see what is broken.
    And you can use getInt instead of casting.
     
  9. Offline

    Bbop537

    @timtower Okay, I figured out how to fix it. Thank you.
     
Thread Status:
Not open for further replies.

Share This Page