manipulation of other player's inventory

Discussion in 'Plugin Development' started by kevinossia, May 3, 2013.

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



    Can someone post an example on how I can manipulate someone's inventory using a command "take" with the argument being the item/itemstack that they have in their inventory?

    I can't figure this out.

    Sorry if my question is unclear.
  2. Offline


    You can acces a players inventory with this:
    From there you need to just google how to modify a players inventory, there are lots of tutorials/posts about it. No one can help you much more unless you post what code you have so we can see what you're doing wrong.
  3. Offline


    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. Player p = (Player) sender;
    4. if(label.equalsIgnoreCase("take")) {
    5. if(args.length < 3) {
    6. p.sendMessage("§cNot enough argumemts");
    7. } else if(args.length == 3) {
    8. Player target = Bukkit.getPlayer(args[0].trim());
    9. int id = Integer.parseInt(args[1]);
    10. int amount = Integer.parseInt(args[2]);
    11. ItemStack item = new ItemStack(id,amount);
    12. target.getInventory().remove(item);
    13. target.updateInventory();
    14. }
    15. }
    16. return false;
    17. }

    You probably want to add some checks if the target player's inventory contains that ItemStack and if the id is a valid block id, and if the target player has that amount of that material
  4. Offline


    Thank you!

    Ok, I tried that code that you posted, but the target player still keeps their material. They had 64 dirt, and I tried to remove 32, but they still had it. It says that updateInventory() is deprecated; is this why it doesn't update her inventory?

    Whoa, nevermind. It suddenly works! still doesn't work! lol. Ok, it only seems to update the player's inventory and remove the item if I enter the full amount they have, ie if they have 64 dirt, I can only remove the full 64, I cannot remove 32 or 16. Any reason why this happens?
    Here's my code:
    else if(args.length == 3)
    Player target = Bukkit.getPlayer(args[0].trim());
    int id = Integer.parseInt(args[1]);
    int amount = Integer.parseInt(args[2]);
    ItemStack item = new ItemStack(id, amount);
    PlayerInventory inventory = target.getInventory();
    s.sendMessage("§cThat player doesn't have that item!");

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 1, 2016
Thread Status:
Not open for further replies.

Share This Page