Clearing and setting an inventory

Discussion in 'Plugin Development' started by SaxSalute, Mar 24, 2014.

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

    SaxSalute

    I'm writing a plugin to manage 2v2 arena matches, but I can't seem to figure out how to manipulate player inventories. I have tried using this code to set the player's boots to iron boots. I figured that would be a basic way to test inventory setting and getting.

    Code:java
    1. plugin.getServer().getPlayer(playerName).getInventory().setBoots(new ItemStack(Material.IRON_BOOTS, 1));


    How can I make this work right? Also, what's the best way to outright clear a player's inventory?
     
  2. Offline

    jthort

    SaxSalute Take a look at this tutorial, it could help http://wiki.bukkit.org/Plugin_Tutorial#.28Player.29_Inventory_Manipulation

    You can simply add items to a players inventory by doing the following
    Code:
    player.getInventory().addItem(sword);
              player.getInventory().setChestplate(goldchestplate);
              player.getInventory().setLeggings(diamondlegs);
              player.getInventory().setHelmet(chainhelm);
              player.getInventory().setBoots(chainboots);
    To clear the players inventory, you can do this
    Code:
    player.getInventory().clear();
    You can also add enchantments to the itemStacks
    Code:
    sword.addEnchantment(Enchantment.DAMAGE_ALL, 2);
    To add coloring to leather, you can do the following
    Code:
              ItemStack legs = new ItemStack(Material.LEATHER_LEGGINGS);
              LeatherArmorMeta legss = (LeatherArmorMeta)legs.getItemMeta();
              legss.setColor(Color.BLACK);
    
    Then change the meta by doing the following
    Code:
              legs.setItemMeta(legss);
    
    Using this method to set the name
    Code:
    public static ItemStack setName(ItemStack is, String name){
                ItemMeta m = is.getItemMeta();
                m.setDisplayName(name);
                is.setItemMeta(m);
                return is;
            }
    I hope this gave you a better understanding on manipulating a players inventory, any more questions feel free to ask
     
  3. Offline

    SaxSalute

    I tried this out and still nothing. This is what I have:
    Code:java
    1. Inventory i = player.getInventory();
    2.  
    3. i.clear();
    4. i.addItem(new ItemStack(Material.ENDER_PEARL, 1));


    The inventory doesn't clear and the ender pearl doesn't get added.
     
  4. Offline

    jthort

    SaxSalute That should work fine, are you sure that method or event is firing? Did you register your events?
     
  5. Offline

    SaxSalute

    I think I have a deeper issue with my version control. I wasn't sure why that wasn't working, so I changed the color of a line of text to see what happened, and it didn't change. For some reason the changes I'm making while I'm on my laptop aren't exporting anything different from what I made before I cloned my repo to my laptop last night.
     
Thread Status:
Not open for further replies.

Share This Page