Head Drops from Creeper, Zombie, Player, etc.

Discussion in 'Resources' started by pizzafreak08, Nov 1, 2012.

Thread Status:
Not open for further replies.
  1. I build a Class for my plugin, so all Monsters supported yet and the Player drop their head.
    These are:
    - Creeper
    - Skeleton
    - Zombie
    - Player
    - Wither Skeleton (vanilla included)

    I used the code from stirante's "Snippets for 1.4" to change the NBT-Data on the Player Skulls.
    The NBT-Data gets deleted on dropping the Items naturally, so the Head gets directly added to the Inventory of the Killer.

    Hope you find this useful. Notes and suggestions are welcome.

    pizzafreak08

    Code:

    Code:
    package me.pizzafreak08.TimoliaBeta;
     
    import java.util.Random;
     
    import net.minecraft.server.NBTTagCompound;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.craftbukkit.inventory.CraftItemStack;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.inventory.ItemStack;
     
    public class EventListener implements Listener {
     
        private Random rand = new Random();
     
        public EventListener() {
     
        }
     
        @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
        public void onPlayerDeath(PlayerDeathEvent event) {
            if (rand.nextInt(100) < 20) {
                Player dead = event.getEntity();
                Player killer = event.getEntity().getKiller();
                DamageCause cause = event.getEntity().getLastDamageCause().getCause();
     
                if (cause == DamageCause.ENTITY_ATTACK && killer != null && (killer.getItemInHand().getType() == Material.IRON_SWORD || killer.getItemInHand().getType() == Material.DIAMOND_SWORD)) {
                    CraftItemStack item = new CraftItemStack(Material.SKULL_ITEM, 1, (byte) 3);
                    killer.getInventory().addItem(setSkin(item, dead.getName()));
                    killer.sendMessage(ChatColor.RED + "You beheaded " + dead.getName() + "!");
                }
            }
        }
     
        @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
        public void onEntityDeath(EntityDeathEvent event) {
            if (rand.nextInt(100) < 10) {
                EntityType t = event.getEntity().getType();
                DamageCause cause = event.getEntity().getLastDamageCause().getCause();
     
                if (cause == DamageCause.ENTITY_ATTACK && event.getEntity().getKiller() != null) {
                    Location loc = event.getEntity().getLocation();
     
                    if (t == EntityType.ZOMBIE) {
                        dropSkull(loc, 2);
                    } else if (t == EntityType.SKELETON) {
                        dropSkull(loc, 0);
                    } else if (t == EntityType.CREEPER) {
                        dropSkull(loc, 4);
                    }
                }
            }
        }
     
        private ItemStack setSkin(ItemStack item, String nick) {
            CraftItemStack craftStack = null;
            net.minecraft.server.ItemStack itemStack = null;
            if (item instanceof CraftItemStack) {
                craftStack = (CraftItemStack) item;
                itemStack = craftStack.getHandle();
            } else if (item instanceof ItemStack) {
                craftStack = new CraftItemStack(item);
                itemStack = craftStack.getHandle();
            }
            NBTTagCompound tag = itemStack.tag;
            if (tag == null) {
                tag = new NBTTagCompound();
            }
            tag.setString("SkullOwner", nick);
            itemStack.tag = tag;
            return craftStack;
        }
     
        private void dropSkull(Location loc, int type) {
            loc.getWorld().dropItemNaturally(loc, new ItemStack(Material.SKULL_ITEM, 1, (byte) type));
        }
    }
    
     
    stirante likes this.
  2. Offline

    stirante

    If you want to drop item with nbt do this:
    Code:java
    1.  
    2. Item item = world.dropItemNaturally(loc, myNamedItem);
    3. item.setItemStack(myNamedItem);
    4.  
     
  3. Thanks! It worked. But i don't understand why you have to set the ItemStack twice. Do you also know a way to get the NBT-Data of a Block so you can mine the Skulls and get them back. As of now the Head gets blank when i place it and destroy it again.
     
  4. Offline

    stirante

    I don't think blocks have their nbt tags. Maybe use Metadata.
     
  5. but mobspawners and so on? is it all metadata? i thought they added nbt on blocks in 1.4 but okay i will try it with metadata.
     
  6. Offline

    KeybordPiano459

    Mobspawners have metadata for different mobs.
     
  7. i have another problem now. how do i get the type of skull (creeper, zombie, ...) when it is placed. getdata didnt seem to work. i want to use it in the BlockFromToEvent because the crepper zombie heads are buggy when placed underwater
     
  8. Offline

    stirante

    Check damage value or something.
     
  9. Offline

    KeybordPiano459

  10. but how do i check the data value. getdata returns an integer whether i placed the head on the floor or on the wall but not the type. @keybord thats not helpful if i dont know HOW to get the value
     
  11. Offline

    KeybordPiano459

    I think it's block.getData()
     
  12. like i said before that doesnt work....
     
  13. Offline

    KeybordPiano459

    Yes it does, do something like this (untested)
    Code:java
    1. Block block = event.getBlock();
    2. if (block.getData() == 4 /*or whatever the head you need is*/) {
    3. //do something
    4. }

    You can cancel blockplaceevent if it's underwater or something.
     
  14. yeah but that gives me the direction the skull is facing not the type unfortunately :/ but thanks! :)
     
  15. Offline

    Bob3104558

    I think i misunderstood what this code does, but im looking for a plugin that makes the skulls drop legit like the wither skull, but for the other mobs creeper, zombie, skeleton and steve
     
  16. Offline

    Bob3104558

    Then can someone make this into a plugin i would really like to use it on my server
     
  17. Offline

    stirante

    I know how you could get player's head. Check if He was killed with sword and if He was punched in head. I fyou want I can write code for it :)
     
Thread Status:
Not open for further replies.

Share This Page