Other players as arguments.

Discussion in 'Plugin Development' started by thtpetaaquanguy, Feb 6, 2016.

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

    thtpetaaquanguy

    I am trying to give a custom item to a player when the first argument is equal to a player online. For some reason I am getting an error when setting the target. Here is my code and the console error.

    Code:
    Code:
    package me.hm04.enderkeys;
    
    
    import java.util.Arrays;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener{
    
       
        public void loadConfiguration(){
            String[] prizes = {"DIAMOND","DIRT", "STONE"};
            getConfig().set("Prizes", Arrays.asList(prizes));
            getConfig().options().copyDefaults(true);
            saveConfig();
            reloadConfig();
        }
    
        @Override
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            loadConfiguration();
            System.out.println("EnderKeys has been enabled!");
    
           
        }
        @Override
        public void onDisable(){
            System.out.println("EnderKeys has been disabled.");
            saveConfig();
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player p = (Player) sender;
            Player target = Bukkit.getServer().getPlayer(args[0]);
            String key = ChatColor.GOLD + "Key";
            ItemStack myItem = new ItemStack(Material.NETHER_STAR);
            ItemMeta im = myItem.getItemMeta();
            im.setDisplayName(key);
            myItem.setItemMeta(im);
           
            if(args.length == 0){
                if(cmd.getName().equalsIgnoreCase("key")){
                p.getInventory().addItem(new ItemStack(myItem));
    
                }
            if(args.length == 1){
                target.getInventory().addItem(new ItemStack(myItem));
            }
            }
            return false;
        }
       
       
       
         @EventHandler
             public void onPlayerI(PlayerInteractEvent e){
            String key = ChatColor.GOLD + "Key";
            ItemStack myItem = new ItemStack(Material.NETHER_STAR);
            ItemMeta im = myItem.getItemMeta();
            im.setDisplayName(key);
            myItem.setItemMeta(im);
           
                Player p = (Player) e.getPlayer();
                Action action = e.getAction();
                List<String> list = getConfig().getStringList("Prizes");
                for(String str : list){
                ItemStack is = new ItemStack(Material.valueOf(str.toUpperCase()));
                if(p.getItemInHand().getType().equals(myItem)){
                    if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                        if(e.getClickedBlock().getType() == Material.ENDER_CHEST){
                            p.getInventory().addItem(new ItemStack(is));
                               
                            }
                        }
                    }
                   
                }
            
        }
    }
           
       
    
    
    Error:
    Code:
    null
    org.bukkit.command.CommandException: Unhandled exception executing command 'key' in plugin EnderKeys v2.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:620) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1106) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:966) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_20]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_20]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit.jar:git-Bukkit-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_20]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at me.hm04.enderkeys.Main.onCommand(Main.java:49) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            ... 15 more
    >
     
  2. Offline

    teej107

  3. Offline

    thtpetaaquanguy

    Last edited by a moderator: Feb 6, 2016
  4. Offline

    teej107

    Yes your error is that but I'm asking you what error is thrown? (Exception)
     
  5. Offline

    thtpetaaquanguy

    I read the post, still pretty confused on what you want. It is an unhandled exception executing command key.
     
  6. Offline

    Evonoucono

    @thtpetaaquanguy
    this shouldn't have to be handed to you!
     
  7. Offline

    thtpetaaquanguy

    I realize this, I need to know how to fix it, I have been looking everywhere on how to get players, it says the type I am using is deprecated.
     
  8. Offline

    Evonoucono

    You know, what if I like to send a command as a console? Or what if I want to send a command that doesn't use arguments? Hmmm curious thing!

    Edit: Yup curious thing indeed!
     
  9. Offline

    teej107

    That has nothing to do with your problem. Tell me the error that is getting thrown. (eg: NullPointerException, ArrayIndexOutOfBoundsException, ThepetaaquanguyWillNotTellMeException, IOException, etc....)
     
  10. Offline

    thtpetaaquanguy

    ArrayOutofBounds
     
  11. Offline

    teej107

  12. Offline

    RandomHashTags

    Off the top of my head, you need to check if the args[0] is online, or equal to null.

    Code:
    if(Bukkit.getPlayer(args[0]) == null)
        || !(Bukkit.getPlayer(args[0].isOnline())  {
      return;
    } else {
      Player target = Bukkit.getServer().getPlayer(args[0]);
      // Your code to execute
    }
     
  13. Offline

    teej107

    The top of your head is wrong :p Those methods aren't part of String and has nothing to do with the exception thrown.
     
  14. Offline

    RandomHashTags

    The command also needs an argument. I am just saying this because of the ArrayOutOfBounds. I use onCommand and hook arguments to players a lot.
    Example: /command argument0
    Example: /yourCommand <player name>
     
  15. Offline

    Xerox262

    Why check if the player is null? There's OfflinePlayer for a reason, you can still get information about the player from it, just not as much, unless they're actually online, in which case you can get the actual player from them and get all of the info you would normally get.
     
  16. You need to check that the argument actually exists.
    I think @teej107 has made it pretty obvious here...
     
Thread Status:
Not open for further replies.

Share This Page