Args help

Discussion in 'Plugin Development' started by Jmcm71, Oct 3, 2013.

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

    Jmcm71

    Ok im trying to get the number the player types in when typing /tax [number], and store it in a int, thanks in advance
     
  2. Jmcm71
    You should take a look at this. http://wiki.bukkit.org/Plugin_Tutorial#Commands
    What you want to do is use the String array variable you defined in the command constructor (often named 'args').

    Check if args.length == 1, then parse the integer from args[0].
    Code:
    int amount = 0;
     
    if(cmd.getName().equalsIgnoreCase("tax")) {
        if(args.length == 1) {
            try {
                amount = Integer.parseInt(args[0]);
            } catch(NumberFormatException ex) {
                /* tell them that args[0] must be an int */
            }
        }
    }
     
  3. Offline

    GaaTavares

    Not sure:
    Code:java
    1. Integer.parseInt(args[0]);
    2.  
     
  4. Offline

    Jmcm71

    wouldnt it be Integer.parseInt(args[1]); because thats what im trying to get
     
  5. Jmcm71
    0 is always the first element in an array, so if you want to define the amount in the first argument of the command, then use args[0].
     
  6. Offline

    KanTe

    Args [0] returns the first arg because java starts an array with 0
     
  7. Offline

    Jmcm71

    Assist KanTe ok thanks just making sure, i have this args thing done, but getting all the players in the server and charging them with vault is not wanting to work for me
    Code:
    package me.josiah.civiltaxes;
     
    import java.util.logging.Logger;
     
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class civiltaxes extends JavaPlugin{
     
        public Logger logger = Logger.getLogger("Minecraft");
        public static Economy econ = null;
     
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info("[" + pdfFile.getName() + "]" + " "+ "Has been Disabled!");
        }
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info("[" + pdfFile.getName() + "]" + " " + "Version "+ pdfFile.getVersion() + " " + "Has been Enabled!");
            if (!setupEconomy() ) {
                logger.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
        }
     
        private boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player player = (Player) sender;
            Player targetplayers = this.getServer().getOnlinePlayers();
            if(commandLabel.equalsIgnoreCase("tax")){
                if(args.length == 0){
                    player.sendMessage(ChatColor.RED + "Invailed args use /tax [number]");
                }
                if(args.length == 1){
                int tax = 0;
                try {
                    tax = Integer.parseInt(args[0]);
                } catch(NumberFormatException ex) {
                    player.sendMessage(ChatColor.RED + "You must type in a number example: /tax 20");
                }
                EconomyResponse r = econ.withdrawPlayer(targetplayers, tax);
                if(r.transactionSuccess()) {
                    player.sendMessage(ChatColor.GREEN + "You were taxed " + tax + " dallors.");
                } else {
                    player.sendMessage("An error occured while trying to tax you.");
                }
            }
        }
            return false;
    }
    }
    Can you tell me what i did wrong?
     
Thread Status:
Not open for further replies.

Share This Page