[NOOB HELP] Command not working :(

Discussion in 'Plugin Development' started by samyounes03, Jun 1, 2015.

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

    samyounes03

    Hello,
    I am trying to code an economy plugin for my server using the Vault API. This plugin only has 1 command till now, and this command is: /points (playername) (amount)
    This command is supposed to deposit a specific amount of points to a player. In eclipse, there are no errors, everything looks fine...
    Code:
    package me.samyounes03.Economy;
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import net.milkbowl.vault.economy.*;
    public class Main extends JavaPlugin{
       
        public static Economy economy = null;
    
        private boolean setupEconomy()
        {
            RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
            if (economyProvider != null) {
                economy = economyProvider.getProvider();
            }
    
            return (economy != null);
        }
        public void onEnable() {
            getLogger().info("Plugin enabled");
        }
        public void onDisable() {
            getLogger().info("Plugin disabled");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(sender instanceof Player){
                Player player = (Player) sender;
                int number = Integer.parseInt(args[2]);
                if(cmd.getName().equalsIgnoreCase("points")){
                    economy.bankDeposit(args[1], 1000);
                    player.sendMessage("You sent" + number + "to" + args[1]);
    
                }
            }
            return false;
        }
    }
    
    Please tell me what i did wrong.
    Thank you :)
     
  2. /points <playername> <amount>
    aaaaa^aaaaaaaaaaaaa^aaaaaaaaaaaaaaaa^
    Command aaaaaaaargs[0]aaaaaaaaaaaargs[1]

    Whereever you watched plugin tutorial, please stop it D:
    1. Don't print out when the plugin loads/enables/disables! That just spams the console because Bukkit ALREADY does this for every plugin.
    2. Why don't you let the console run commands? This command doesn't require a player as the CommandSender.
    3. Put number in an try-catch-block, becacuse it will throw an Exception if the argument isn't a number. Like this:
    Code:
    int number = 0;
    try {
    number = Integer.parseInt(args[1]);
    } catch (Exception e) {
    sender.sendMessage("§4Error! §c "+args[1]+" §4isn't a number.");
    return true;
    }
    //Do stuff with the number
    4. Use return true; instead. return false; displays the command usage defined in plugin.yml
    5. Lets say you fixed all the code, and run the command /points FisheyLP 50, you get this message:
    Code:
    You sent50toFisheyLP
    Make whitespaces between (and color codes if you want it to look cool :p)
    Code:
    player.sendMessage("You sent " + number + " points to " + args[0] + ".");
    6. You didn't call the setupEconomy method in onEnable()
     
    Last edited: Jun 1, 2015
    CoolGamerXD likes this.
  3. Offline

    samyounes03

    Thank you a lot for this! Ill try it @FisheyLP
     
  4. Offline

    CoolGamerXD

    @FisheyLP
    Just quick fix in your code :p
    Code:
     catch (NumberFormatException e)
     
    Last edited: Jun 1, 2015
  5. Offline

    samyounes03

    Im kinda a noob... Can anyone give me an example of code of what he asked me to do?

    @CoolGamerXD forgot to tag.

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
  6. Offline

    mine-care

    @samyounes03 umm we dont usually provide code because it is spoon feeding, what @FisheyLP provided is aleady an example of code, If there is any part of the code/post you need further elaboration on, dont hezitate to ask.
     
    samyounes03 and CoolGamerXD like this.
  7. Offline

    samyounes03

    Yes, I understood a part of it, but others like parts 2 3 and 6: i didn't :(
     
  8. Offline

    CoolGamerXD

    You can sure ask... :)
     
    samyounes03 likes this.
  9. Offline

    mine-care

    @samyounes03
    2. In your code you check if sender is a player (line 31) and then execute the command code, FisheyLP sugested you remove that and the casting of sender to Player in (Line 32) and therefore allow the console (That is not a player) to use this command as well.

    3. Line 33, you parse text to number (String to int) so you use Integer.parseInt(String param); This method (parseInt()) will throw an exception (error) if the value provided is not a number, so if you type Integer.parseInt("Hello") this will throw an error to your console. FisheyLP sugested you put a try block to make sure that if args[2] is not a number, you get the exception (error) and handle it properly (Like send a message or something, See example code provided)

    6. You have a method in your code called "setupEconomy()" (lines 15-20) that initializes economy variable (line 13) If you dont call this method then the economy variable is Nothing you can use, in your onEnable() method you need to call setupEconomy() method to prevent errors and achive the connection with the economy plugin.
     
    samyounes03 likes this.
  10. Offline

    samyounes03

    Thank you @CoolGamerXD @mine-care and @FisheyLP i can now code my currency plugin for my minigames server. I got help from this forum and from this video: .

    Thank you a lot everyone :)

    ok @mine-care Thank you :)
     
    Last edited: Jun 1, 2015
  11. Offline

    CoolGamerXD

  12. Offline

    samyounes03

    Ok, so the above problem was solved... but now i have another one.
    Code:
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                    int amount = Integer.parseInt(args[1]);
                    if (cmd.getName().equalsIgnoreCase("paycredits")){
                        econ.depositPlayer(args[0], amount);
                        player.sendMessage(ChatColor.GREEN + "You payed " + args[0] + " "  + args[1] + " credits");
                    }
                    else if (cmd.getName().equalsIgnoreCase("credits")){
                        player.sendMessage("The player samyounes03 has 100 credits!");
                    }
    }
                return false;
            }
    }
    
    "An internal error occured while attempting to perform this command" happens when i type /credits. Help please :)
     
  13. 1. put int amount inside where you check if the command is paycredits.
    2. Inside where you checked if the command is paycredits check if (args.length == 2) { to guarantee, that the command has exactly two arguments.
    3. You didn't read my last post because still many things aren't fixed
     
  14. Offline

    samyounes03

    @FisheyLP I did what made it work. Now the other command was not working :( anyway thanks for the help.n I wont mark this as solved until after i try it.

    @FisheyLP what do you mean in step 1?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  15. If you know basic java you will know how to solve this
     
Thread Status:
Not open for further replies.

Share This Page