Need help with Collection Players

Discussion in 'Plugin Development' started by shotgun528, Apr 11, 2015.

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

    shotgun528

    My Error is The type of the expression must be an array type but it resolved to Collection

    and the code:

    Code:
                        Collection players = Bukkit.getServer().getOnlinePlayers();
                        for(int i = 0; i < players.size(); i++){
                            Player targets = players[i]; //<--- Here is the Error
                            targets.getInventory().addItem(item);
                          
                          
                            return true;
     
  2. Offline

    GODofCRAFTERS

    Why are you complicating things with multiple array declarations? Just use the integrated java for loop. It's much easier. Delete the code you've written and replace it with the following:
    Code:
    for(Player p : Bukkit.getServer().getOnlinePlayers()){
                            p.getInventory().addItem(item);
                            return true;
    }
    If want me to explain it, just say so, it's a very handy tool in developing.
     
  3. Offline

    shotgun528

    didnt thinked about that ty :D

    Okay Only the Sender get the Item....
    CODE:


    Code:
    int x = Integer.parseInt(args[1]);
                        ItemStack item = ItemFormat.getItemStackbyName(args[0], x);
                       
                        for(Player players : Bukkit.getServer().getOnlinePlayers()){
                       
                            players.getInventory().addItem(item);
    Okay if i Use the command with name /ga Stone 1 Only the sender get the Item Here The ItemFormat class
    Code:
    package Functions;
    
    import org.bukkit.inventory.ItemStack;
    
    public class ItemFormat {
       
        public static ItemStack getItemStackbyName(String name, int x){
           
            if(name.equalsIgnoreCase("stone")){
                return new ItemStack (1, x);
            }else if(name.equalsIgnoreCase("opapple")){
                return new ItemStack(322, x, (short) 1);
            }
            return null;
           
        }
    
    }
    
     
    Last edited by a moderator: Apr 11, 2015
  4. Offline

    GODofCRAFTERS

    So what is the problem exactly?
     
  5. Offline

    shotgun528

    Only the Sender Get The Items if i write it with Words Example if i write /giveall 1 1 All works Perfectly but if i write /giveall Stone 1 Only i get the stone :eek:

    Code:
    package Commands;
    
    import java.util.Collection;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    
    import Functions.ItemFormat;
    
    public class COMMAND_GiveAll implements CommandExecutor{
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            Player p = (Player)sender;
           
           
            if(sender instanceof Player){
                if(!p.hasPermission("greenland.giveall")){
                    p.sendMessage("keine rechte!");
                    return true;
                }
               
                   
                    if(args.length == 0){
                        p.sendMessage("/ga (Item) (anzahl)");
                        return true;
                       
                       
                   
                       
                    }else if(args.length == 2){
                        try{
                        int it = Integer.parseInt(args[0]);
                        int x = Integer.parseInt(args[1]);
                        for(Player players : Bukkit.getServer().getOnlinePlayers()){
                           
                            players.getInventory().addItem(new ItemStack(it, x));
                           
                           
                           
                           
                           
                           
                           
                        }
                       
                       
                    }catch(NumberFormatException giveall){
                       
                       
                       
                       
                       
                        int x = Integer.parseInt(args[1]);
                        ItemStack item = ItemFormat.getItemStackbyName(args[0], x);
                       
                        for(Player players : Bukkit.getServer().getOnlinePlayers()){
                       
                            players.getInventory().addItem(item);
                           
                           
                            return true;
                        }
                       
                       
                   
                   
                   
                   
                }
               
               
            }
           
           
           
               
            }
       
            return false;
        }
    }
    
     
    Last edited by a moderator: Apr 11, 2015
  6. Offline

    GODofCRAFTERS

    According to your code, it's impossible that only you get it unless you are the only person online. Also, try generalizing the exception to just any exception, print the StackTrace and see where you hit an error. Later you can specify it as NumberFormatException.
     
  7. Offline

    shotgun528

  8. Offline

    GODofCRAFTERS

    Well depends, on what you want help with. I'm not a spoon-feeder. But I can suggest what you need to do. At best it's all a trial and error method.
     
  9. Offline

    mythbusterma

    What's the point of this? Bukkit already prints stack traces for errors in commands and EventHandlers.
    @shotgun528

    Use Player#updateInventory() after updating a player's inventory.
     
Thread Status:
Not open for further replies.

Share This Page