Problem with Sending Command to Player

Discussion in 'Plugin Development' started by bartboy8, May 11, 2012.

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

    bartboy8

    Help Please Im trying to send the setHelmet command to a set player and I don't know how to do it. I have the Player variable set to targetPlayer but that is about it here is my code:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player)sender;
            if(commandLabel.equalsIgnoreCase("helmet")){
                if(args.length < 2) return false;
                if(args.length > 3) return false;
                if(args.length == 0){
                    Player targetplayer = player.getServer().getPlayer(args[0]);
                    PlayerInventory pi = player.getInventory();
                    args.equals(targetplayer);
                }
                   
            if(args[1].equalsIgnoreCase("bedrock")){
            PlayerInventory pi = player.getInventory();
            ItemStack bedrock = new ItemStack(Material.BEDROCK, 1);
            pi.setHelmet(bedrock);
            player.sendMessage(ChatColor.GREEN + "Helmet Set To Bedrock!");
            }
     
  2. Offline

    Nitnelave

    are you aware that your if(args.length == 0) is useless? You already checked if it was <2, and returned (left the method). It never gets called.

    The way you wrote your method, you would get the helmet if you typed "/cmd whatever bedrock". Is that what you tried?
     
  3. Offline

    bartboy8

    Yes I tried that but I already have it so I get the helmet when I do the cmd "/helmet whateverblock" but when I do "/helmet [playername] [blockname] it still gives me the helmet please help!
     
  4. Offline

    Craftiii4

    that is because you are getting the inventory of 'player' and you are defining player as the command sender not what is after the base command.
     
  5. Offline

    bartboy8

    So what could I do to make it so I can do /helmet glass. or /helmet [playername] [block] and it would still work also here is the code for one of the blocks
    Code:
            if(args[1].equalsIgnoreCase("bedrock")){
                if(player.hasPermission("da.set.bedrock"));
            PlayerInventory pi = player.getInventory();
            ItemStack bedrock = new ItemStack(Material.BEDROCK, 1);
            pi.setHelmet(bedrock);
            player.sendMessage(ChatColor.GREEN + "Helmet Set To Bedrock!");
            }
     
  6. Offline

    Craftiii4

    Use Bukkit.getplayer()

    Then test that it is a player, if yes then run the code, else error with player does not exist or is not online.
     
  7. Offline

    Nitnelave

    In case you have 1 argument, the args[0] is the name of the block, and if you have 2 arguments, args[0] is the player's name, to use with getPlayer(), and args[1] is the block
     
Thread Status:
Not open for further replies.

Share This Page