Solved Number from config

Discussion in 'Plugin Development' started by plisov, May 30, 2016.

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

    plisov

    Hi,
    I have been trying to develop a plugin where if you type /points it tells you the amount of points you have and if you type /points (playersname) it tells you their number of points. I got the first part working but I can't seem to get the second part to work. Have a look.
    Code:
            if (cmd.getName().equalsIgnoreCase("points")) {
                Double playerBalance = Main.getBalance(player.getName());
                if(args.length == 0) {
                    player.sendMessage(prefix + ChatColor.GREEN + " Your Points: " + playerBalance);
                    return true;
                } else if(args.length == 1) {
                player.sendMessage(args[1] + "" + ChatColor.GREEN + " has " + getConfig().getInt("balance") + " points.");
                }
                }  else {
                    return false;
                }
    Now, when I place
    Code:
    getConfig().getInt("balance.(someone's name")
    it works great! it shows me that specific person's number of points whether args[1] equals to that name or not. But when I type
    Code:
    getConfig().getInt("balance" + args[1])
    it give me An internal error occurred while attempting to perform this command. The console give random gibberish basically saying "Unhandled exception executing the command 'points' in plugin Points v1.0

    Any help is greatly appreciated.
    plisov
     
  2. Offline

    ticklemypp

    try changing this

    Code:
    getConfig().getInt("balance" + args[1])
    to

    Code:
    getConfig().getInt("balance." + args[1])
     
  3. Offline

    I Al Istannen

    @plisov
    Also rember that you need the INDEX in the array, starting with 0! You check if args.length == 1 (1 element) and then try to access the elemt with index 1, which is the second. Change that to 0.

    Playerbalance can be a "double", it probably doesn't need to be a "Double". Always work with primitives if possible (faster, less memory, etc.).

    And you code would benefit from pressing CTRL+A and then CTRL+SHIFT+F to format it.

    You should also post that "random gibberish", as reading Stacktraces (how they are called) is a vital part of programming.
     
  4. Offline

    plisov

    Here is the "random gibberish"
    Code:
    [10:26:19 INFO]: HardestBunny issued server command: /points HardestBunny
    [10:26:19 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'points' in plugin Poin
    ts v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-latest.jar:git
    -Spigot-8a048fe-71e5248]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-latest
    .jar:git-Spigot-8a048fe-71e5248]
            at org.bukkit.craftbukkit.v1_9_R2.CraftServer.dispatchCommand(CraftServer.java:646) ~[spig
    ot-latest.jar:git-Spigot-8a048fe-71e5248]
            at net.minecraft.server.v1_9_R2.PlayerConnection.handleCommand(PlayerConnection.java:1349)
    [spigot-latest.jar:git-Spigot-8a048fe-71e5248]
            at net.minecraft.server.v1_9_R2.PlayerConnection.a(PlayerConnection.java:1184) [spigot-lat
    est.jar:git-Spigot-8a048fe-71e5248]
            at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-lates
    t.jar:git-Spigot-8a048fe-71e5248]
            at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-latest
    .jar:git-Spigot-8a048fe-71e5248]
            at net.minecraft.server.v1_9_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-latest.
    jar:git-Spigot-8a048fe-71e5248]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_73]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_73]
            at net.minecraft.server.v1_9_R2.SystemUtils.a(SourceFile:45) [spigot-latest.jar:git-Spigot
    -8a048fe-71e5248]
            at net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:726) [spigot-latest
    .jar:git-Spigot-8a048fe-71e5248]
            at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:399) [spigot-latest
    .jar:git-Spigot-8a048fe-71e5248]
            at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [spigot-latest
    .jar:git-Spigot-8a048fe-71e5248]
            at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [spigot-late
    st.jar:git-Spigot-8a048fe-71e5248]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_73]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
            at me.plisov.points.Enable.onCommand(Enable.java:189) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-latest.jar:git
    -Spigot-8a048fe-71e5248]
            ... 15 more
    I've already tried that. Still no luck. :(
     
    Last edited: May 30, 2016
  5. Offline

    I Al Istannen

    @plisov
    No random gibberish, just the description of your problem:
    Now have a look at this line and check if you ever address the index "1" of an array, that doesn't have enough elements.

    Probably this:
     
    Last edited: May 30, 2016
  6. Offline

    jsutaria

    @plisov The first args is args[0] so just change
    getConfig().getInt("balance" + args[1]);
    to
    getConfig().getInt("balance." + args[0])
     
  7. Offline

    plisov

    No. I'm trying to get the players name not the /points
     
  8. Offline

    EmeraldRailsMC

    args[0] is the first argument, not the command name.
    In other words, the args array doesn't contain the command name. That's in alias/command#getName()
     
  9. Offline

    plisov

    Thanks so much! I've fixed it. :)
     
Thread Status:
Not open for further replies.

Share This Page