Help! I have a problem with Vault!

Discussion in 'Plugin Development' started by odogollie, Sep 14, 2012.

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

    odogollie

    Hello! I am creating a plugin where people can buy and sell items without signs. I am running into the problem that I do not understand. Any help is good help!

    Here is the code:
    Code:
    package co.stock;
     
     
    import java.util.logging.Logger;
     
    import net.milkbowl.vault.chat.Chat;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.permission.Permission;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.*;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class Stock extends JavaPlugin {
       
        public final Logger logger = Logger.getLogger("Minecraft");
       
        public static Permission permission = null;
        public static Economy economy = null;
        public static Chat chat = 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() {
               
            this.logger.info("[StockMarket] Enabling!");
            getConfig().options().copyDefaults(true);
            saveConfig();
           
        }
           
        public void onDisable() {
            this.logger.info("[StockMarket] Disabling!");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
           
            if(commandLabel.equalsIgnoreCase("stock")){
                if (args.length == 0){
                   
                    player.sendMessage(ChatColor.RED + "Usage: /stock [buy|sell|price]");
                   
                }else if(args.length == 2 && args[0].equalsIgnoreCase("price")){
                   
                    String item = args[1].toLowerCase();
                    player.sendMessage(ChatColor.GOLD + "The price of 1 " + item + " is " + getConfig().getString(item));
                   
                }else if(args[0].equalsIgnoreCase("price")){
                   
                    player.sendMessage(ChatColor.RED + "Usage: /stock price [item#]");
                   
                }else if(args.length == 3 && args[0].equalsIgnoreCase("buy")){
                   
                    String item = args[1].toLowerCase();
                    int amount = Integer.parseInt( args[2]);
                    int id = Integer.parseInt( args[1] );
                    PlayerInventory tempInv = player.getInventory();
                    ItemStack tempStack = new ItemStack(id, amount);
                    int pb = (int) Stock.economy.getBalance(player.getName());
                    int total = getConfig().getInt(item);
                    if (pb - total  < 0){
                        player.sendMessage(ChatColor.RED + "You do not have enough money!");
                    }else if(pb - total > 0){
                        tempInv.addItem(tempStack);
                        Stock.economy.bankWithdraw(player.getName(), amount * total);
                    }
                   
                   
                }else if(args[0].equalsIgnoreCase("buy")){
                   
                    player.sendMessage(ChatColor.RED + "Usage: /stock buy [item#]");
                }
            }
            return false;
        }
     
    }
    
    And here is the error I am getting in the console when I type /stock buy 264 1:
    (on a side note: I have the config file with just 264: 100, indicating the item id and the price)
    Code:
    2012-09-14 22:36:10 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'stock' in plugin Stock v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:168)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:492)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:878)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:825)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:807)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:276)
        at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:109)
        at net.minecraft.server.ServerConnection.b(SourceFile:35)
        at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:581)
        at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:212)
        at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
        at co.stock.Stock.onCommand(Stock.java:69)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
        ... 15 more
     
  2. Give me line 69 of your Stock class...
    Also: http://forums.bukkit.org/threads/ho...ubleshoot-your-own-plugins-by-yourself.32457/
     
  3. Offline

    odogollie

    Here is line 69:
    Code:
    int amount = Integer.parseInt( args[2]);
     
  4. Offline

    Squish000

    args[2] is null. Make a check before:

    if(args[2] != null)
    {
    //do stuff
    }
     
  5. Offline

    odogollie

    ok I fixed line 69. now I'm getting an error on line 73.
    Code:
    int pb = (int) Stock.economy.getBalance(player.getName());
    Not sure how you would get that into an integer but I kind of just guessed.
     
  6. Offline

    Sleaker

    First getBalance returns a double, so always casting to an int is a bad idea, and running costs as ints everywhere is not a good idea either. You'll probably want to use doubles for money to stay compatible with economies, otherwise your stuff wont be accurate. Example: what happens if you have an item that costs .1 and the player has .5 money?

    Second, you never setup the economy.
     
Thread Status:
Not open for further replies.

Share This Page