How to give a chicken an inventory?

Discussion in 'Plugin Development' started by Slash9211, Aug 24, 2014.

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


    I was wondering how would I give/make an inventory for a chicken? Cause I'm trying to put a players items into a chickens inventory then on the chickens death drop the items... Or is there a way to check if the chicken died then drop the players items?
  2. Offline


    Put a PlayerInteractEvent, check if it's a chicken, save the items in a configuration/hasmap, and when you kill it, get the death location (with EntityDeathEvent of course), create a for loop and spawn all the items by doing:
    1. Bukkit.getServer().getWorld(loc.getWorld().getName()).dropItem(p.getLocation(), materials);
  3. Offline


    Hashmaps wouldn't be recommended since after a restart the items would clear, but it's your choice.
  4. Offline


    Why would you do Bukkit.getServer().getWorld(loc.getWorld().getName()) to get the world when you can just do loc.getWorld()?

    You can give the chicken an inventory by storing the chicken and an Inventory in a HashMap, and saving/loading it from a file on reload/restart.
    Skye, Jake6177 and ZeusAllMighty11 like this.
  5. Offline


    They're equivalent, so it doesn't really matter, your just using the shorter way.
  6. Offline


    Totom3, Skye, Dragonphase and 6 others like this.
  7. Offline


    1. Player getPlayer(Player p) {
    2. for(Player player : Bukkit.getOnlinePlayers()) {
    3. if(player == p) {
    4. return player;
    5. }
    6. }
    7. return p;
    8. }

    Dragonphase and kps1796 like this.
  8. Offline


  9. Offline


  10. Offline


    Do you know what facetious means? If not, look it up. Then look at the code. ;)
  11. Offline


    Once I saw this after looking up a dev's project: Boolean.valueOf(Boolean.valueOf(true));
  12. Offline


    ok... Finally had a chance to log on and... yet nothing...
  13. Offline


    The shorter way is also more efficient.
    HashMaps are actually recommended because they would be faster than saving the config each time. To add persistence, one would save the HashMap to a file and load it on startup.
    Slash9211 Your 'inventory' is basically a Map<UUID, ItemStack[]> where you can store the contents for the Inventory and link it to the UUID of the Chicken. When the chicken dies, simply
    • Get the Chicken's UUID
    • Get the ItemStack[] from the Map
    • Ensure it's not null
    • Loop over the ItemStack array and drop them on the Chicken's Location
    If you want the Inventories to persist, simply save them to a config by creating a section for the stringified UUID and add all ItemStacks in there (hint: you can simply call set(path, ItemStack instance)). Same for loading, but somewhat reversed.
  14. Offline


    It's simple. I'll walk you through it.

    1).Create an arraylist of Chicken called inventoryChickens.

    2).Create an arraylist of UUID called playersInInventories.

    3).When a player right clicks a chicken, check if it is not already in inventoryChickens and if not add it to inventoryChickens.

    4).Now open an inventory with the player's UUID and the chickens UUID to a string with a slash in between.
    Ex: Player's UUID is 102d23487fdgs2 and chicken's is 19382yruh23871, create an inventory with a name of "102d23487fdgs2/19382yruh23871"

    5).Now when they close the inventory, save it in the config (look at this link it's awesome) with a key of the player's UUID and the chickens UUID to a string with a slash in between.
    Ex: Player's UUID is 102d23487fdgs2 and chicken's is 19382yruh23871, store the inventory with a name of "102d23487fdgs2/19382yruh23871"

    Now that we have that out of the way, we need to add the functionality so they can actually use the chicken.

    6).Listen for the EntityInteractEntity event and check if the entity is right clicked and if the entity is a chicken.

    7).Now check if we have a saved inventory in the config by using the getString() method and using the key of the player's UUID and the chickens UUID to a string with a slash in between.
    Ex: Player's UUID is 102d23487fdgs2 and chicken's is 19382yruh23871, check for an inventory with a name of "102d23487fdgs2/19382yruh23871"

    8).If we find the inventory, great. Now open it, add the player's UUID to playersInInventories, and the player's good for now. If we don't find the inventory, refer to the third instruction and create a new inventory.

    9).Now, if we have found the inventory, we now have to listen for the inventory close and when it happens we'll do three things:
    (1). Check if the player's UUID is in playersInInventories. If not, return.
    (2). Now we just refer instruction 5 and save the inventory again.
    (3). Remove the player's UUID from playersInInventories.

    10). Also make sure to set it so the chickens don't despawn.

    And we're all done, now chickens hold inventories!
Thread Status:
Not open for further replies.

Share This Page