DropInventory plugin bug.

Discussion in 'Plugin Development' started by samuel123abc, May 18, 2014.

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

    samuel123abc

    Code:
    package me.Samuel.DropInventory;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin{
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("drop")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "The console doesn't have an inventory! :)");
                    return true;
                }
                Player player = (Player) sender;
                for (ItemStack i : player.getInventory().getContents()) {
                    if (i != null) {
                        Location y = player.getLocation().add(0, -2, 0);
                        player.getWorld().dropItem(y, i);
                        player.getInventory().remove(i);
                        player.sendMessage(ChatColor.GOLD + "Your whole inventory has been dropped on the ground!");
                    }
                }
            }
            return true;
        }
    }
    This is kind of not working for me. The basic concept is to get the player that issued the command "/drop"'s inventory. This is working and it is putting the items where they should be. Though here's the problem. When I issue the command it prints "Your whole inventory has been dropped on the ground!" the more than one time (One line per itemstack in inventory). If any kind and helpful developer can help me and understands my problem, help!

    Sincerely,

    Sam
     
  2. Offline

    St3venAU

    It's because you have sendMessage inside the for loop. Move it outside the loop and it will only say it once.
     
  3. Offline

    samuel123abc

    Ok, will try it, thanks alot.
     
Thread Status:
Not open for further replies.

Share This Page