-1

The command /group is supposed to output the group of the given player. The players are saved inside the config.yml. When I execute that command, all I get is an unhandled exception. It doesn't give me anything specific. I don't know, if it is caused by the fact, that I use external classes wrong. I added a simple sendMessage() to prove, that the variable doesn't cause the exception. I am quite new to Java. Maybe, StackOverflow can help.

Stym.java (Main):

import org.bukkit.plugin.java.JavaPlugin;

public class Stym extends JavaPlugin {

    public String necessaryIronPlayers;
    public String necessaryDiamondPlayers;

    @Override
    public void onEnable() {
        getConfig().options().copyDefaults();
        saveConfig();

        necessaryIronPlayers = getConfig().getString("groups.iron");
        necessaryDiamondPlayers = getConfig().getString("groups.diamond");

        Group groupClass = new Group(this);
        getCommand("GROUP").setExecutor(groupClass);

    }

}

Group.java(external command file):

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

public class Group implements CommandExecutor {

    public String ironPlayers;
    public String diamondPlayers;

    Group(Stym stym) {
        ironPlayers = stym.necessaryIronPlayers;
        diamondPlayers = stym.necessaryDiamondPlayers;

    }

    @Override
    public boolean onCommand(CommandSender sender, Command command,
            String label, String[] arguments) {

        //Testing starts

        sender.sendMessage("Player:" + " " + arguments[0]);
        sender.sendMessage("Irons:" + " " + ironPlayers);
        sender.sendMessage("Diamond:" + " " + diamondPlayers);

        //Testing stops

        if(arguments.length == 1) {
            if(!ironPlayers.contains(arguments[0])
                    && !diamondPlayers.contains(arguments[0])) {
                sender.sendMessage(arguments[0] + " " + "is made out of"
                        + " " + "leather!");

            } else if(ironPlayers.contains(arguments[0])
                    && !diamondPlayers.contains(arguments[0])) {
                sender.sendMessage(arguments[0] + " " + "is made out of"
                        + " " + "iron!");

            } else if(!ironPlayers.contains(arguments[0])
                    && diamondPlayers.contains(arguments[0])) {
                sender.sendMessage(arguments[0] + " " + "is made out of"
                        + " " + "diamond!");

            } else {
                sender.sendMessage("Tell Krischon, that he has messed up!");

            }

        } else {
            sender.sendMessage("Wrong usage!");

        }

        return true;

    }

}

plugin.yml:

name: Stym
main: Stym
version: 1.3.3.7
commands:
  group:

config.yml:

groups:
  iron: Player, Another_Player
  diamond: Rich_Player

Console output:

enter image description here

Stacktrace:

[19:46:48] [Server thread/WARN]: java.lang.ArrayIndexOutOfBoundsException: 0
[19:46:48] [Server thread/WARN]:  at Group.onCommand(Group.java:24)
[19:46:48] [Server thread/WARN]:  at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
[19:46:48] [Server thread/WARN]:  at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140)
[19:46:48] [Server thread/WARN]:  at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:620)
[19:46:48] [Server thread/WARN]:  at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:606)
[19:46:48] [Server thread/WARN]:  at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:372)
[19:46:48] [Server thread/WARN]:  at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:336)
[19:46:48] [Server thread/WARN]:  at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629)
[19:46:48] [Server thread/WARN]:  at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537)
[19:46:48] [Server thread/WARN]:  at java.lang.Thread.run(Unknown Source)
Reum12
  • 59
  • 6
  • Could we please see the kind of error you are receiving in the console (including the strack trace etc.)? – Adrian Sohn Nov 07 '15 at 18:23
  • @AdrianSohn Post updated. – Reum12 Nov 07 '15 at 18:49
  • `arguments` is empty in `onCommand` – OneCricketeer Nov 07 '15 at 18:57
  • @cricket_007 I am very confused now. I just moved the "testing"-lines behind the if(arguments.length == 1) line. Then it worked. Now I deleted the "testing"-lines and it won't work again. Could you (or someone else) please post the whole Group.java? – Reum12 Nov 07 '15 at 19:13
  • That doesn't make sense to me if that's the only thing you changed. But if it worked, then why move it back? :) – OneCricketeer Nov 07 '15 at 19:15
  • Actually, no it doesn't work, it just doesn't crash. `arguments` is still empty and the if block wasn't even entered – OneCricketeer Nov 07 '15 at 19:17
  • The only reason your code could throw the `IndexArrayOutOfBoundsException` is because of the line: `sender.sendMessage("Player:" + " " + arguments[0])` because `arguments[0]` might not exist as the `arguments` array might be empty. – Adrian Sohn Nov 07 '15 at 19:36
  • 1
    @cricket_007 Well, I got it running now. I think it actually had something to do with wrong formatting of the plugin.yml, since I didn't really change anything in Group.java. I guess, I'll keep reading my Java book now! :-) – Reum12 Nov 07 '15 at 19:37
  • One quick, last question: What would I need to do, in order to prevent bugs like these, assuming only "Player" is in iron group: _/group Player -> iron, /group Pla -> iron_ – Reum12 Nov 07 '15 at 19:43
  • You should probably store the player names in a different way. First of all you could use unique IDs since names might be changed at a later date. Instead of storing all the names in one string separated by commas you can create lists in YAML files. Then you can store the IDs or names in an actual list instead of checking whether a substring exists in the string containing all the names. Since this is a new question I would create a new one instead of editing this existing question or commenting. – Adrian Sohn Nov 07 '15 at 20:04

1 Answers1

2

Your problem is right here:

sender.sendMessage("Player:" + " " + arguments[0]);

arguments[0] takes the first element in the arguments array. However, since you only typed /group, there are no arguments, so it throws an ArrayIndexOutOfBoundsException: 0 because its trying to access the zero'th element in the array, but it doesn't even exist.

To fix this you need to first check if there are any arguments in the command. You can do this by getting arguments.length and checking if it is greater than 0 or equal to however many arguments you want there to be.

Example:

if(arguments.length > 0){
    sender.sendMessage("Player:" + " " + arguments[0]);
    //Do other stuff
}else{
    sender.sendMessage(ChatColor.RED + "That is not how you run the command!");
}
kmecpp
  • 2,371
  • 1
  • 23
  • 38