How to addItem with a durability value?

Discussion in 'Plugin Development' started by Th3Controller, Jun 1, 2012.

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

    Th3Controller

    I was recently trying to develop a private plugin but when I hatched an idea it has to do with Spawn Eggs.

    The problem?
    I cannot figure out how to add an item into the players inventory pertaining a specific durability.

    Whats the goal?
    When a player right clicks a chicken with a blaze rod the chicken will disappear and a spawn egg relating to a chicken will pop up in the inventory.

    Any help would be very much appreciated!

    Any help please?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  2. The inventory's .addItem() accepts ItemStack... and ItemStack accepts id, amoun, durability, enchantments, etc, just look at the available arguments in ItemStack :) enchantments are done with methods tough.
     
  3. Offline

    Th3Controller

    Can you give me an example code? Since whenever I try to do it it seems incorrect. I'm a noob at this I'm very sorry if this is a nuisance.
     
  4. Your editor should help you with this primitive stuff... like giving you the arguments of a method and stuff like that... but anyway:
    Code:
    new ItemStack(item material or id, amount, data/damage value);
    new ItemStack(Material.WOOL, 64, (short)4);
    EDIT: yeah, should've casted it in the example too.... so mind you that the 3rd argument must be a short.
     
  5. Offline

    Th3Controller

    Actually I was hoping you were going to give me another way to do it because I tried to do that however in Eclipse it still turns up red. If it helps I posted my code below
    Code:
        public void PlayerInteractingEntity(PlayerInteractEntityEvent event){
            Player p = event.getPlayer();
            Entity rightclick = event.getRightClicked();
            PlayerInventory inventory = p.getInventory();
            ItemStack eggItem = new ItemStack(Material.MONSTER_EGG, 1, 93);
            int index = p.getInventory().firstEmpty();
            if(event.getRightClicked().getType() == EntityType.CHICKEN && p.getItemInHand().getType() == Material.STICK){
                rightclick.remove();
                inventory.addItem(eggItem);
                p.sendMessage(ChatColor.RED+"You caught the chicken!");
            }
        }
     
  6. Offline

    CorrieKay

    if you highlight the code that is red, it will tell you the issue, and possibly give you a quick fix.
     
  7. Offline

    Technius

    Which line is turning red?
     
  8. Offline

    Th3Controller

    Yes I know but if I try to fix it that way it removes the damage value. I want a player to spawn a spawn egg containing a chicken data value.
    This line is marked red
    Code:
    new ItemStack(Material.MONSTER_EGG, 1, 93)
     
  9. Offline

    Technius

    Did you import everything?
     
  10. Offline

    Th3Controller

    Yes
    Code:
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
     
  11. The 3rd argument must be a short, not an integer, and the last quick fix is casting that to short, which does this:
    Code:
    new ItemStack(Material.MONSTER_EGG, 1, (short)93);
     
  12. Offline

    Th3Controller

    Thank you guys that solved a big part of my problem.
    Now my problem is that whenever the item spawns the inventory of the player it is not updated. The player has to right click on the egg for it to update. Any thoughts?

    ANOTHER problem, it seems to me that no spawn eggs spawn on the players inventory whenever there's no chicken spawn egg present.
     
  13. Offline

    Njol

    player.updateInventory()
     
    Th3Controller likes this.
  14. Offline

    Th3Controller

    Why am I not kissing you now? Please come over so I can kiss you. But why is it saying its deprecated? Why is it saying its only a temporary work around?
     
  15. Offline

    CorrieKay

    they should fix it later in which you dont need to update the inventory. eventually when they fix the issue, the method will be removed, im assuming.
     
    Th3Controller likes this.
  16. Offline

    Th3Controller

    You guys are my heroes! You guys helped me so much! I am new to Bukkit (not really new I've made my first plugin called anti-bedrock).

    I could have given up on this project if it weren't for you guys, I've been browsing the Docs for hours and still couldn't find it but now you guys saved me!

    If anyone of you have Skype I would like to add you since I need help with making my first configuration file, its ok if you guys don't feel like helping out a noob.
     
  17. Th3Controller likes this.
  18. Offline

    Th3Controller

    I sort of agree with what your saying, I guess I would only need help if I really need it like at this time but the problem is already fixed. Anyways thank you for your time fixing my problem, I really do appreciate it.
     
  19. Offline

    CorrieKay

    just ask here, what do you need help with the config file with? :)
     
  20. Offline

    Th3Controller

    Well I haven't really started with configuration files but my friend helped me out trying to make a banning plugin which is incomplete. But I'm going to show you if this is correct.
    Code:
        public void createFiles(){
            File folder = new File("plugins/Livecraft");
            File file = new File("plugins/Livecraft/config.yml");
            if(!folder.exists()){
                folder.mkdir();
            }
            if(!file.exists()){
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
     
  21. Offline

    CorrieKay

    sorry for being late, but yes. This should create the file. However, theres an easier way of doing this, just use plugin.getDataFolder() instead of "plugins/Livecraft/config.yml"
    And in fact, you dont need to check to create it before saving to it. Just start using it from plugin.getConfig() and it will save the file when need be correctly, etc.
     
Thread Status:
Not open for further replies.

Share This Page