ArrayOutOfBoundsException

Discussion in 'Plugin Development' started by gamingmind99, Feb 26, 2017.

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

    gamingmind99

    grr this username is so cringe. Haven't been on this account in YEARS. Anyway-

    Hello! I'm quite new to plugin development-- under a week, actually. I keep on running into a problem with a command called setspawn. You can probably guess what it does.

    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class primaryCommands extends JavaPlugin{
        public Permission sSpawn = new Permission("teleport.setSpawn");
        public Permission Spawn = new Permission("teleport.spawn");
        public Permission gto = new Permission("teleport.goto");
        public Permission bring = new Permission("teleport.bring");
        public void onEnable(){
            this.saveConfig();
            getConfig().options().copyDefaults(true);
            Bukkit.getServer().getLogger().info("TeleportCommands by SeductiveWalrus has been enabled");
        }
     
        public void onDisable(){
            saveConfig();
            Bukkit.getServer().getLogger().info("TeleportCommands has been disabled");
        }
     
        public boolean onCommand(CommandSender sender, Command command, String alias, String[] args){
         
            FileConfiguration config = this.getConfig();
         
            Player t = Bukkit.getPlayerExact(args[0]);
            Player p = (Player) sender;
            if(alias.equalsIgnoreCase("goto") && sender instanceof Player){
            if(!p.hasPermission("teleport.goto")){
                p.sendMessage(ChatColor.GRAY + "Command access denied");
            }else{
                if(args.length == 0) {
                    p.sendMessage(ChatColor.GRAY + "Invalid arguments. Specify an online player");
                }else{
                    if(t != null) {
                        Location tLoc = t.getLocation();
                        p.teleport(tLoc);
                        t.sendMessage(ChatColor.GRAY + "" + p.getName() + " has gone to you");
                      }else{
                          p.sendMessage(ChatColor.GRAY + "Invalid arguments. Specify an online player");
                      }
                    }
                }
            }else if(alias.equalsIgnoreCase("bring") && sender instanceof Player){
                if(!p.hasPermission("teleport.bring")){
                    p.sendMessage(ChatColor.GRAY + "Command access denied");
                }else{
                    if(args.length == 0) {
                        p.sendMessage(ChatColor.GRAY + "Invalid arguments. Specify an online player");
                    }else{
                        if(t != null) {
                            Location pLoc = p.getLocation();
                            t.teleport(pLoc);
                            t.sendMessage(ChatColor.GRAY + "" + p.getName() + " has brought you to them");
                          }else{
                              p.sendMessage(ChatColor.GRAY + "Invalid arguments. Specify an online player");
                 }
               }
             }
           }else if(alias.equalsIgnoreCase("setspawn") && sender instanceof Player){
               if(p.hasPermission("teleport.setSpawn")){
                   config.set("Spawn.World", p.getLocation().getWorld().getName());
                   config.set("Spawn.X", p.getLocation().getX());
                   config.set("Spawn.Y", p.getLocation().getY());
                   config.set("Spawn.Z", p.getLocation().getZ());
                   saveConfig();
               }else{
                   p.sendMessage(ChatColor.GRAY + "Command access denied");
               }
           }else if(alias.equalsIgnoreCase("spawn") && sender instanceof Player){
              if(p.hasPermission("teleport.spawn")){
                  World w = Bukkit.getServer().getWorld(getConfig().getString("Spawn.World"));
                  double x = getConfig().getDouble("Spawn.X");
                  double y = getConfig().getDouble("Spawn.Y");
                  double z = getConfig().getDouble("Spawn.Z");
               
                  p.teleport(new Location(w, x, y, z));
              }else{
                  p.sendMessage(ChatColor.GRAY + "Command access denied");
              }
           }
            return true;
         }
      }
    
    This is code for TelepotCommands, a plugin exclusively for a server I am currently making. You'll find the code in there. And now I get an internal error for an unhandled exception, no idea what for. An hour of googling and clicking around these forums, didn't find anything that helped me solve this. What am I doing wrong? Log:
    Code:
    [16:40:02] [Server thread/INFO]: DoctorQueefPhD issued server command: /setspawn
    [16:40:02] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'setspawn' in plugin TeleportCommands v0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:650) ~[Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerConnection.java:1344) [Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1179) [Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_77]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_77]
        at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:747) [Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_77]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
        at me.SeductiveWalrus.primaryCommands.onCommand(primaryCommands.java:35) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        ... 15 more
    Plugin YML:
    Code:
    name: TeleportCommands
    main: me.SeductiveWalrus.primaryCommands
    version: 0.1
    author: SeductiveWalrus
    commands:
       goto:
          description: Brings executor to specified target
          usage: /<command> <player>
       bring:
          description:  Brings specified target to player
          usage: /<command> <player>
       setspawn:
          description: Sets spawn to executor's location and world
          usage: /<command>
       spawn:
          description: Brings the executor to spawn
          usage: /<command>
    permissions:
       teleport.goto:
          description: Gives player access to /goto
       teleport.bring:
          description: Gives player access to /bring
       teleport.setSpawn:
          description: Gives player access to /setspawn
       teleport.spawn:
          description: Gives player access to /spawn
     
  2. Offline

    Tecno_Wizard

    @gamingmind99 you have a couple of major mistakes.

    Do not blindly cast the command sender to a player. It isn't always a player.
    The error you are seeing is being caused by you not checking to make sure args[0] exists before accessing it.
     
  3. Offline

    timtower Moderator Moderator

    Moved to plugin development.
    Changed title to be descriptive.
     
  4. Offline

    PhantomUnicorns

    @gamingmind99
    Code:
    Player t = Bukkit.getPlayerExact(args[0]);
    
    You did this before testing if args.length is > 0
     
  5. Offline

    gamingmind99

    Thanks so much guys. I'll get cracking on fixing those things.

    Sorry, friend. I'll keep your changes in mind for next time I post :D
     
  6. Offline

    gamingmind99

    now, I really do hate to be spoon fed, but I can't solve this NoSuchMethodError
    Man it sucks being new to something :(

    Anyway, It's occurring with this PlayerData gathering plugin I'm making so I can use the data for further use
    Here's the stuff:

    Main:

    Code:
    package me.SeductiveWalrus;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class data extends JavaPlugin{
        public Permission playerdata = new Permission("pData.command");
        public void onEnable(){
    LINE 15 -->  new listener(this);
             Bukkit.getServer().getLogger().info("PlayerData by SeductiveWalrus has been enabled");
        }
      
        public void onDisable(){
            Bukkit.getServer().getLogger().info("PlayerData has been disabled");
            saveConfig();
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("playerdata")) {
              
                Player p = (Player) sender;
                if(p.hasPermission("pData.command")){
                    if(args.length > 0){
                        Player t = Bukkit.getPlayerExact(args[0]);
                        if (t != null && t instanceof Player){
                            p.sendMessage(ChatColor.GRAY + "Fetching player information for UID " + t.getUniqueId() + "...");
                            p.sendMessage(ChatColor.GRAY + "Address:" + getConfig().getString(t.getUniqueId() + ".Address"));
                            p.sendMessage(ChatColor.GRAY + "LastSeen:" + getConfig().getString(t.getUniqueId() + ".LastSeen"));
                            p.sendMessage(ChatColor.GRAY + "Blocks Broken:" + getConfig().getInt(t.getUniqueId() + ".BlocksBroken"));
                        }else  p.sendMessage(ChatColor.GRAY + "Invalid arguments. Specify an online player");
                    }else p.sendMessage(ChatColor.GRAY + "Invalid arguments. Specify an online player");
                }
              }
            return true;
        }
    }
    listener:

    Code:
    package me.SeductiveWalrus;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class listener extends JavaPlugin implements Listener {
        data config;
      
        public listener(data plugin){
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            config = plugin;
        }
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
          
            config.getConfig().set(p.getUniqueId() + ".Name", p.getName());
            config.getConfig().set(p.getUniqueId() + ".Address", p.getAddress());
            saveConfig();
            if(!p.hasPlayedBefore()){
                config.getConfig().set(p.getUniqueId() + ".BlocksBroken", 0);
                saveConfig();
            }
            }
        @EventHandler
        public void onBreak(BlockBreakEvent e){
            Player p = e.getPlayer();
            int blocksbroken = config.getConfig().getInt(p.getUniqueId() + ".BlocksBroken");
            config.getConfig().set(p.getUniqueId() + ".BlocksBroken", blocksbroken++);
         }
        @EventHandler
        public void onQuit(PlayerQuitEvent e){
            saveConfig();
        }
        }
    
    Error:

    Code:
    [19:12:45] [Server thread/ERROR]: Error occurred while enabling PlayerData v0.1 (Is it up to date?)
    java.lang.NoSuchMethodError: me.SeductiveWalrus.listener.<init>(Lme/SeductiveWalrus/data;)V
        at me.SeductiveWalrus.data.onEnable(data.java:15) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:271) ~[Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugin(CraftServer.java:376) [Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugins(CraftServer.java:326) [Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at net.minecraft.server.v1_11_R1.MinecraftServer.t(MinecraftServer.java:421) [Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at net.minecraft.server.v1_11_R1.MinecraftServer.l(MinecraftServer.java:382) [Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at net.minecraft.server.v1_11_R1.MinecraftServer.a(MinecraftServer.java:337) [Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at net.minecraft.server.v1_11_R1.DedicatedServer.init(DedicatedServer.java:272) [Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:544) [Spigot.jar:git-Spigot-d4f98a3-cb61ac0]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_77]
    Thanks so much for reading you guys! Sorry for being a pest but I've already tried to solve this myself and failed
     
    Last edited: Mar 2, 2017
  7. Offline

    PhantomUnicorns

    You don't register it in the constructor, you do it in the onEnable (Like you have done it in the constructor) Also instead of "hard coding" the data class, I would just use JavaPlugin (If you need something from data like a custom variable do not do this). Also you have your listener extending JavaPlugin, only one class can do this! Finally, you have all of your classes not capitalized, naming conventions state that class names should be Capitalized!

    Second post:
    What is the point of:
    Code:
    public Permission sSpawn = new Permission("teleport.setSpawn");
    public Permission Spawn = new Permission("teleport.spawn");
    public Permission gto = new Permission("teleport.goto");
    public Permission bring = new Permission("teleport.bring");
    
    In the main post... You legit do not use them :p
     
Thread Status:
Not open for further replies.

Share This Page