Solved Setting armor contents

Discussion in 'Spigot Plugin Development' started by ExpDev, May 12, 2017.

  1. Ok, so now this doesn't work?

    Code (Text):
    ItemStack[] armor = new ItemStack[4];
    armor[0] = new ItemStack(Material.DIAMOND_BOOTS, 1);
    armor[1] = new ItemStack(Material.DIAMOND_LEGGINGS, 1);
    armor[2] = new ItemStack(Material.DIAMOND_CHESTPLATE, 1);
    armor[3] = new ItemStack(Material.DIAMOND_HELMET, 1);

    System.out.println(Arrays.toString(armor));

    Player#.getInventory().setArmorContents(armor);
    Player#.getInventory().setContents(kit.getContents());

    p.updateInventory();
    Output:
    Code (Text):
    [ItemStack{DIAMOND_BOOTS x 1}, ItemStack{DIAMOND_LEGGINGS x 1}, ItemStack{DIAMOND_CHESTPLATE x 1}, ItemStack{DIAMOND_HELMET x 1}]
    No errors.

    I read that 0 = should be boots, 1 = legs, 2 = chest, and 3 = helm. Btw, other way around doesn't work either. However setting inventory contents work.
     
    • Friendly Friendly x 1
  2. Try using the methods for each slot, setBoots, setChestplate, setHelmet, setLeggings
     
    • Like Like x 1
  3. I've tried. doesn't seem to be working either.

    EDIT: I'm on <version>1.11.2-R0.1-SNAPSHOT</version> if relevant.
     
  4. setContents replaces _everything_ so use addItem
     
    • Agree Agree x 1
  5. Also setting the armor contents will replace anything they are already wearing, so if they enchanted gear and do the kit to get the extra stuff they will loose their enchanted stuff, Ouch.
     
    • Agree Agree x 1
  6. That is the point.
     
  7. Ok, setContents is erasing the armor you just set so setContents first then add the armor
     
    • Agree Agree x 1
    • Useful Useful x 1
  8. How about make sure that you reset their amor by setting it to null or to an empty array, update their inv and then use the seperate functions like setBoots etc. Btw would be nice if there was any exception/error thrown if you could post that.
     
  9. @ExpDev to elaborate on what he's saying. This is what you have.

    To reiterate what's been said, you are setting the armor to the player. But then you are immediately erasing that armor by resetting the entire inventory on the next line (which includes armor slots).

    Swap those two lines around so you set the entire inventory first, and then set the armor contents afterwards.

    Code (Text):
    Player#.getInventory().setContents(kit.getContents());
    Player#.getInventory().setArmorContents(armor);
     
    • Agree Agree x 1
  10. Thank you. I could not seem to find this information in the documentation?
     
  11. https://hub.spigotmc.org/javadocs/s...setContents(org.bukkit.inventory.ItemStack[])

    [​IMG]

    ;)

    I dont think these numbers are right (they may be for forge or mcp) but it shows you all the slots that is in a player's inventory:

    [​IMG]

    The crafting grid is part of the inventory, but its a separate view. So it doesnt get included in this context of the API.
     
    • Like Like x 1
    • Informative Informative x 1