Solved Dropping all Items in an Inventory on the Ground

Discussion in 'Plugin Development' started by KeybordPiano459, Nov 28, 2012.

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

    KeybordPiano459

    How do I drop every item in a player's inventory on the ground at a certain location? I know how to clear the player's inventory.
     
  2. Offline

    Scizzr

    Just an example:

    Code:
    Player p = ???; //Need a player here!
    Location loc = p.getLocation().clone();
    Inventory inv = p.getInventory();
    for (ItemStack item : inv.getContents()) {
        if (item != null) {
            loc.getWorld().dropItemNaturally(loc, item.clone());
        }
    }
    inv.clear();
    
    Edit: Fixed to check against null items
     
  3. Offline

    SchulziHD

    This works perfect for me.
    Code:
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(sender instanceof Player) {
                Player player = (Player) sender;
                World world = player.getWorld();
             
                if(command.getLabel().equalsIgnoreCase("dropall")) {
                    if(args.length == 0) {
                        List<ItemStack> items = new ArrayList<ItemStack>();
                     
                        for(int i = 0; i < player.getInventory().getSize(); i++) {
                            items.add(player.getInventory().getItem(i));
                        }
                     
                        player.getInventory().clear();
                     
                        for(ItemStack item : items) {
                            world.dropItem(player.getLocation(), item).setPickupDelay(20);
                        }
                     
                        items.clear();
                        player.sendMessage("dropped all items!");
                        return true;
                    } else {
                        return false;
                    }
                }
                return false;
            }
            return false;
        }
    But I'm getting this error when sending the command:
    Code:
    22:30:08 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'drop
    all' in plugin BlockCounter v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    0)
            at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:5
    02)
            at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.
    java:915)
            at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:828)
     
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:810)
            at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:282)
            at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:111)
            at net.minecraft.server.ServerConnection.b(SourceFile:35)
            at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
            at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:561)
            at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:213)
            at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.IllegalArgumentException: Cannot drop a Null item.
            at org.apache.commons.lang.Validate.notNull(Validate.java:203)
            at org.bukkit.craftbukkit.CraftWorld.dropItem(CraftWorld.java:283)
            at me.Schulzi.BlockCounter.BlockCounter.onCommand(BlockCounter.java:57)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
            ... 15 more
    But never mind, this got nothing to do with the item drops hopefully :)
     
  4. Offline

    KeybordPiano459

  5. Offline

    Scizzr

    Check the updated code. I fixed a small bug :D

    Caused by: java.lang.IllegalArgumentException: Cannot drop a Null item.

    Try using the code I added:
    Code:
    if (item != null) {
        //...
    }
    
    Check out my cleaner, shorter code

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
    KeybordPiano459 likes this.
  6. Offline

    KeybordPiano459

    Thanks again :D
     
  7. Offline

    SchulziHD

    Oh, that's it. Thanks for correcting!
     
Thread Status:
Not open for further replies.

Share This Page