Register Api Help

Discussion in 'Plugin Development' started by Deathmarine, Nov 3, 2011.

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

    Deathmarine Retired Staff

    Alright so I'm working my first plugin and I'm attempting to use the register api to subtract from an account however I'm getting a null pointer after converting string argument to double. I've used many different ways of converting it however. I believe the issue is from the method I'm trying to use to subtract from the account. Does anyone have a allinone dummy subtract Method that I could possibly try? I'm just in the process of adding features at the moment. I currently have the Method onEnable and Disable. All that abstract is messing with my head.
     
  2. Offline

    Chocanto

    I think it will be easier for us if we have a part of the source code ;)
     
  3. Offline

    randomer679

    Maybe post the error and the method that causes the error?
     
  4. Offline

    Pandemoneus

    I'd rather use the Vault API, it's much easier.
     
    desht likes this.
  5. Offline

    Deathmarine Retired Staff

    I took the advice and switched to vault but I'm having an issue with converting a string argument to a double and not getting a nullpointer. However economy.withdraw(String p, Double amt); is easier then creating a method through register. Just making sense of the abstract coding was irrating.
    CODE:
    //Command Sender
    public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
    String commandName = command.getName().toLowerCase(); String[] trimmedArgs = args;
    if(commandName.equals("fine")){
    return fine(sender, trimmedArgs); //NullPointer
    }}
    private
    boolean fine(CommandSender sender, String[] args){
    boolean auth = false;
    Player player =
    null;
    String admin =
    "server";
    if (sender instanceof Player){
    player = (Player)sender;
    if (Permissions.Security.permission(player, "ultraban.fine")) auth = true;
    admin = player.getName(); }
    else{
    auth =
    true;
    }
    if (!auth){
    sender.sendMessage(ChatColor.
    RED + "You do not have the required permissions.");
    return true;
    }//FUNCTIONING Permissions DON'T Touch
    if (args.length < 1) return false; String p = args[0];
    if(autoComplete) p = expandName(p);
    Player victim =
    this.getServer().getPlayer(p);
    boolean broadcast = true;
    String amt ="0"; //set default string
    if
    (args.length > 1){
    if(args[1].equalsIgnoreCase("-s")){ //Attempt to hide punishments
    String amt = args[2]; broadcast =
    false;

    }
    else{
    String amt = args[1];
    } }
    //Tried all of these
    //Double amt = parseFine(amtd);
    //float amtd = (Float.valueOf(amt) ).floatValue();
    //Double amt = Double.valueOf(amtd);
    //Double amtd = Double.parseDouble(amt);
    //String amt = String.valueOf(amtd);
    if(victim != null){ //cant use(victim.isOnline())null pointer
    if(broadcast = false){ //If silent wake his ass up
    victim.sendMessage(
    "blahblah"); }else{
    this
    .getServer().broadcastMessage("blahblah"));
    }
    economy.withdrawPlayer(p,amtd); //new nullpointer (string, double)
    }
    else{
    sender.sendMessage(ChatColor.
    GRAY + "Player must be online!");
    return true;
    }//something to that extent and yah I write alot of notes, still kinda learning

    Wow formatting fail... Any ideas? the amount or (amt) variables can go either way I tried several different ways and even created a method to Convert and still No dice.. is there a standard way to convert from string to double or am I missing something
    The command goes as /fine {player} {amount}

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  6. Offline

    desht

    Double.parseDouble() should work fine there. Be sure to catchNumberFormatException. What isn't working, exactly?
     
  7. Offline

    Deathmarine Retired Staff

    Well I guess the boiled the problem down to

    economy.withdrawPlayer(String, Double);

    I keep getting a nullpointer off this line, that and the exception from running the command. Now my biggest problem is that Vault (which I haven't fully checked the source) requires the player to be in a string format and the amount to withdraw to be double. Which after further study of the all knowing Oracle site. I'm using the

    double amtd = Double.valueOf(amt.trim());
    I'm also having it catch for NumberFormatExceptions leaving a nice message to type a number next time.

    //Thanks desht forgot about that.

    and even using Math.abs(amtd) for the absolute value of the double.
    So I think it might be with the Player in string format.

    I'm trying to use p.getName() or victim.toString() but I'm not sure how to parse the variable to be a usable format for Vault.
     
  8. Offline

    Pandemoneus

    Your economy is null, that's why you get the NPE.
     
  9. Offline

    Deathmarine Retired Staff

    public static Economy economy;
    (class from vault)
     
  10. Offline

    Pandemoneus

    Your declaration doesn't help, you need to call this method

    Code:java
    1.  
    2. private Boolean setupEconomy()
    3. {
    4. RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
    5. if (economyProvider != null) {
    6. economy = economyProvider.getProvider();
    7. }
    8.  
    9. return (economy != null);
    10. }
    11.  


    somewhere in onEnable() and also add a dependency for Vault.
     
  11. Offline

    Deathmarine Retired Staff

    Gotcha.... booting eclipse and hoping for the best. Pretty sure I was using something to the effect onEnable but I'll most likely need the method. I'll let you know the outcome. I do know its registering iConomy 6.

    Wow..... Pandemoneus worked like a charm. That method was the link that I was missing.
    then a simple.
    if(this.setupEconomy()){
    double amtd = Double.valueOf(amt.trim()); //Amount Trimmed
    economy.withdrawPlayer(victim.toString(), Math.abs(amtd)); //Amount Absolute Value
    }
    and WHAMMY.. Appreciations are in order. Now I'm getting one step closer to completing this thing and releasing.

    Now I must do alot of cleaning. I put way to many Notes.

    Well maybe not like a charm. Still doesn't withdraw the amt however no NPE's I'll figure out the issue... should be simple from here.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
Thread Status:
Not open for further replies.

Share This Page