Help with token plugin

Discussion in 'Plugin Development' started by TheRealItsMike, Aug 14, 2016.

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

    TheRealItsMike

    Hi i'm trying to create a plugin that gives you tokens(custom currency) when you kill a player.
    but it's not that i'm having issues with. I'm trying to create a command (/tokens add) i have the code working so it gives the player the tokens but i want it so when a user does /tokens add it displays the correct usage (e.g. "Usage: /tokens add <name> <amount>" but i have been getting internal error when attempting to preform the command.

    Link to my current code: http://pastebin.com/6mULk2Nk

    Thanks for reading.

    If you can fix my code can you please put the entire code (with fix) in a pastebin and send me the link as i am a visual learner and will find it easier to understand and learn from. Thanks.

    What i need help with: I need to make it so if a user doesn't specify a player(args[1]) or amount(args[2]) it displays the message "Usage: /tokens add <player> <name>".

    Solution found!
    Code:java
    1.  
    2. if(args[0].equalsIgnoreCase("add")){
    3. if(args.length > 1){
    4. Player t = Bukkit.getPlayer(args[1]);
    5. if(t == null){
    6. p.sendMessage("§3[§9Tokens§3]§f " + "Player can't be found!");
    7. return true;
    8. }
    9. int value = Integer.parseInt(args[2]);
    10. getConfig().set(t.getName() + ".money", getConfig().getInt(t.getName() + ".money") + value);
    11. saveConfig();
    12. p.sendMessage("§3[§9Tokens§3]§f " + value + " tokens have been given to " + t.getName());
    13. } else {
    14. p.sendMessage("Usage: /tokens add <name> <amount>");
    15. }
    16.  
    17. }
    18.  

    What i was doing wrong:
    Correct code:
    Code:java
    1.  
    2. if(args.length > //Arg number(e.g. 0,1,2,3,4){
    3. //code
    4. }
    5.  

    Incorrect code:
    Code:java
    1.  
    2. if(args[1].length() == 0){
    3. //code
    4. }
    5.  
     
    Last edited: Aug 15, 2016
  2. Online

    timtower Administrator Administrator Moderator

    @TheRealItsMike
    1. Don't cast before you are sure that it is a player.
    2. My guess is the Integer.parseInt, throws errors when it gets strings.
    3. You assume that the player has a score already, even in the join...
     
    mine-care likes this.
Thread Status:
Not open for further replies.

Share This Page