Solved GiveAll problem

Discussion in 'Spigot Plugin Development' started by adrianpopa1998, Dec 2, 2019 at 8:01 PM.

  1. Hello. I'm trying to make a custom giveall plugin for 1.14.4 (because I can't found a good plugin for it) but my methode doesn't work. I'm trying using localIterator and Collection<? extends Player> = Bukkit.getServer().getOnlinePlayers();, but everytime I write the command, only one player recive the item.
    Code (Text):
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player players;
            if (cmd.getName().equalsIgnoreCase("giveall") && sender.hasPermission("steve.giveall")) {
                if (args.length == 0)
                {
                    ItemStack itm = ((Player) sender).getItemInHand();
                    ItemMeta imeta = itm.getItemMeta();
                    itm.setItemMeta(imeta);
                    for (Iterator localIterator = Bukkit.getOnlinePlayers().iterator(); localIterator.hasNext();) {
                        players = (Player) localIterator.next();
                        players.getInventory().addItem(itm);
                        players.sendMessage(culoare("&6Ai primit un item."));
                        return true;
                    }
                }
                return true;
            }
            return false;
        }
     
  2. Your return true; inside the for loop means that it returns true on the first iteration of the loop, simply remove that line and it should work
     
  3. Code (Text):
    for (Iterator localIterator = Bukkit.getOnlinePlayers().iterator(); localIterator.hasNext();) {
                        players = (Player) localIterator.next();
                        players.getInventory().addItem(itm);
                        players.sendMessage(culoare("&6Ai primit un item."));
                        return true;
                    }
    That is a very overcomplicated loop...
    You can literally just use
    Code (Text):
    for (Player p : Bukkit.getOnlinePlayers()){
    }
     
  4. you're a genius man, thank you <3 love you, no homo