Solved Getting balance of player with VaultAPI

Discussion in 'Spigot Plugin Development' started by YunusGG, Jun 19, 2018.

  1. Hey, I want to get player's balance with this code:
    Code (Java):
    //Main class
        public static double getMoney(Player p) {
            double m = ekonomi.getBalance(p);
            return m;
        }

    //Command class
    if(args.length == 0) {
                    if(sender instanceof Player) {
                        Player p = (Player) sender;
                        if(ekonomi.hasAccount(p)) {                      
                            sender.sendMessage(prefix + "§7Money: §6" + Main.getMoney(p));
                            return true;
                        }
                        else {
                            ekonomi.createPlayerAccount(p);
                            return true;
                        }
                    }
                    else {
                        sender.sendMessage(prefix + "§4Only players can use this command.");
                        return true;
                    }
                }
    But I get this error:
    Code (Text):
    org.bukkit.command.CommandException: Unhandled exception executing command 'para' in plugin FFA v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[FFA.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[FFA.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[FFA.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [FFA.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [FFA.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [FFA.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [FFA.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [FFA.jar:git-Spigot-db6de12-18fbb24]
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:?]
        at java.base/java.util.concurrent.FutureTask.run(Unknown Source) [?:?]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [FFA.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [FFA.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [FFA.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [FFA.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [FFA.jar:git-Spigot-db6de12-18fbb24]
        at java.base/java.lang.Thread.run(Unknown Source) [?:?]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
        at commands.Commands.onCommand(Commands.java:49) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[FFA.jar:git-Spigot-db6de12-18fbb24]
        ... 15 more
    Help pls.
     
  2. Theres is an error on line 44 of your command class. Can you show that line please.
     
    • Useful Useful x 1
  3. Umm there is a } in line 44. Here is my command class:
    Code (Java):
    package commands;

    import java.util.Comparator;

    import org.bukkit.Bukkit;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;

    import mainpackage.Main;
    import net.milkbowl.vault.economy.Economy;

    public class Commands implements CommandExecutor {
     
        private Plugin pl = Main.getPlugin(Main.class);
        public String prefix = "§8| §6§lGT §8| ";
        Economy ekonomi = Main.ekonomi;
     
        @Override
        public boolean onCommand(CommandSender sender, Command command, String komut, String[] args) {
            if(komut.equalsIgnoreCase("spawnbelirle")) {
                if(sender instanceof Player) {
                    Player p = (Player) sender;
                    if(p.isOp()) {
                        pl.getConfig().set("Spawn.X", p.getLocation().getX());
                        pl.getConfig().set("Spawn.Y", p.getLocation().getY());
                        pl.getConfig().set("Spawn.Z", p.getLocation().getZ());
                        pl.getConfig().set("Spawn.Yaw", p.getLocation().getYaw());
                        pl.getConfig().set("Spawn.Pitch", p.getLocation().getPitch());
                        p.sendMessage(prefix + "§7Spawn belirlendi!");
                        p.getWorld().strikeLightningEffect(p.getLocation());
                        pl.saveConfig();
                        return true;
                    }
                    else {
                        p.sendMessage(prefix + "§4Hayırdır? Beğenmedin mi spawn'ı?");
                        p.getWorld().playSound(p.getLocation(), Sound.VILLAGER_NO, 50, 1);
                        return true;
                    }
                }
                else {
                    sender.sendMessage(prefix + "§4Bu komudu sadece oyuncular kullanabilir.");
                    return true;
                }
            }   //   Line 44
            if(komut.equalsIgnoreCase("para") || komut.equalsIgnoreCase("money") || komut.equalsIgnoreCase("bal") || komut.equalsIgnoreCase("balance")) {
                if(args[0].equalsIgnoreCase("ekle")) {
                    if(args.length == 3) {
                        Player p = Bukkit.getPlayer(args[1]);
                        int eklenen = Integer.parseInt(args[2]);
                     
                        ekonomi.depositPlayer(p, eklenen);
                        p.sendMessage(prefix + "§6" + sender.getName() + "§7 tarafından hesabına §e" + eklenen + "TL §7eklendi." + ekonomi.getBalance(p));
                        return true;
                    }
                }
                if(args[0].length() == 0) {
                    if(sender instanceof Player) {
                        Player p = (Player) sender;              
                        sender.sendMessage(prefix + "§7Paran: §6" + Main.getMoney(p));
                        return true;
                    }
                    else {
                        sender.sendMessage(prefix + "§4Bunu sadece oyuncular kullanabilir.");
                        return true;
                    }
                }
            }
            return false;
        }
    }
     
     
  4. This is your first problem. You are checking what the first argument is without first checking there is a first argument, just replace that line with this one
    Code (Text):
    if(args.length > 0 && args[0].equalsIgnoreCase("ekle")) {
    This is your second problem. doing args[0] will give you a string, if that strings length is 0 it does not exist what I think you want to do it check if there are no arguments? if so then you should do args.length not args[0].length
     
    • Winner Winner x 1
  5. You are accessing args[0] before checking its length.

    Side note, learn to read stacktraces, and always match the exact code with the stacktraces it's producing. Changing the code makes it harder to debug a stacktrace.
     
  6. Oh, it works now thanks! I'm gonna check how many args there first after this:)