Vault

Vault

7M Downloads

Trouble with VaultAPI

ldfuller01 opened this issue ยท 4 comments

commented

Hey there. I had a question about a part of VaultAPI: economy.getBalance(playername)

I'm assuming that the value of it is null because...

  1. I get an error in my console when I try to call the method in order to print out my balance (however I don't see any net.milkbowl errors in the stack trace, only my own single line of code), and...
  2. When I compare my balance to the price of a sign to teleport with (I'm making a sign teleport plugin), my balance is evidently neither greater than or equal to the price, nor is it less than the price. That would be pretty weird if that value weren't null.

Anyway.... Yeah, this is kind of key to my plugin, so it'd be great if this is resolved.

Thanks.

commented

Hard to debug without stack traces, or code pastes. However, the most common causes of null pointers is due to not initializing the economy variable in your plugin properly, or not actually installing an economy. I can't help anymore until you actually post up the errors and some code.

commented

Well, unless there's a way for you to find out what's going on by the Bukkit stack traces, I only have one single line of code that's going wrong, which is the economy.getBalance(playername) line, which I'm trying to use to print out my balance. I also know that Vault only has one package: net.milkbowl.vault (unless you count that META-INF thing that's automatically generated).

I also tried using iConomy, so thinking that was the error, I installed Essentials to test it further. Neither of them fix the problem.

Otherwise, here's the code that I use in my Main class, and my InteractEvent listener, in which I implement the getBalance method. I'm only giving the hook-to-Vault method and my code in the onEnable that uses it.

Main class:

public boolean economyIsOn = false;

public static Economy economy = null;

private boolean setupEconomy() {

    if (getServer().getPluginManager().getPlugin("Vault") == null) {

        return false;

    }

    RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);

    if (rsp == null) {

        return false;

    }

    economy = rsp.getProvider();

    return economy != null;

}


@Override
public void onEnable() {

    PluginDescriptionFile pdf = this.getDescription();

    logger.info("[" + pdf.getName() + "] " + pdf.getName() + " v" + pdf.getVersion() + " is now enabled.");

    if (!setupEconomy() && getConfig().getBoolean("useEconomy")) {

        economyIsOn = false;

        logger.severe("[" + pdf.getName() +  "] Error setting up economy with Vault! Proceeding without economy!");

    } else if (setupEconomy() && getConfig().getBoolean("useEconomy")) {

        economyIsOn = true;

        logger.info("[" + pdf.getName() + "] Successfully linked to Vault.");

    }

Listener class. Note that the methods you'll see are in another class, and their code is completely functional. This getBalance method is the ONLY thing left to complete my plugin with.

public class SignUsage implements Listener {

public Main M;

private Signs Signs;

private Teleporting Teleporting;

private Permissions Permissions;

public SignUsage(Main plugin) {

    M = plugin;

    Signs = new Signs(plugin);

    Teleporting = new Teleporting(plugin);

    Permissions = new Permissions();

}

private final Economy economy = Main.economy;


@EventHandler
public void onSignUse(PlayerInteractEvent e) {

    Player p = e.getPlayer();

    Block clickedBlock = e.getClickedBlock();

    if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {

        if (clickedBlock.getType() == Material.SIGN
                || clickedBlock.getType() == Material.SIGN_POST
                || clickedBlock.getType() == Material.WALL_SIGN) {

            Sign sign = (Sign) clickedBlock.getState();

            if (Signs.signIsValid(sign)) {

                if (p.hasPermission(Permissions.useSign)) {

                    if (!M.economyIsOn ||
                            (M.economyIsOn && !Signs.signHasPrice(sign))) {

                        Teleporting.useSign(p, sign.getLine(1));

                    }

                    else if (M.economyIsOn && Signs.signHasPrice(sign)) {

                        String pname = p.getName();

                        p.sendMessage(String.valueOf(economy.getBalance(pname)));

                        double price = Signs.getPrice(sign);

                        if (economy.getBalance(pname) >= price) {

                            economy.withdrawPlayer(pname, price);

                            Teleporting.useSign(p, sign.getLine(1));

                        } else if (economy.getBalance(pname) < price) {

                            p.sendMessage(Msgs.NotEnoughCash());

                        }

                    }



                }

            }

        }

    }

}



}
commented

You call setupEconomy twice when you should only be calling it once. You also set a class scoped variable to private Final Economy = Main.economy. This is an extremely bad practice. Reference Main.economy at runtime, or use proper encapsulation. Other than that I need the stack trace to help you out. Stack traces are important because they actually tell you what the problem is.

I don't think you're doing things correctly with your listener class.

commented

Closing this as it's a logic issue, and not related to Vault. If you need further debugging help please use the bukkit forums.