Head Drops

Discussion in 'Spigot Plugin Development' started by TheMeowster, Apr 15, 2017.

  1. how can you make if mobs die, they have a percentage chance to drop their heads?
     
  2. Code (Text):

    public int getRandom(int lower, int upper) {
        return new Random().nextInt((upper - lower) + 1) + lower;
    }

        @EventHandler
        public void onEnityDeath(EntityDeathEvent e){
            if (getRandom(1, 100) < 10) {
                if (e.getEntity().getType().equals(EntityType.ZOMBIE)) {
                    // drop zombie skull
                }
            }
        }
     
     
  3. how would that work percentage wise?
     
  4. From what it says it's generating a number between 1 and 100, and then if the number is less than 10 it gives the head.
    Therefore it's 10% chance
     
  5. i did this
    Code (Text):
             
              if (this.getConfig().getBoolean("zombie") == true){
               if (getRandom(1, 100) <= this.getConfig().getInt("zombiechance")) {
                   if (e.getEntity().getType().equals(EntityType.ZOMBIE)) {
                  e.setDroppedExp(0);
                   }
                    }
    Code (Text):
    zombie: true
    zombiechance: 100
    and it doesn't work
     
  6. any other help? I haven't been able to make anything work
     
  7. Try it withouth the config first.
     
  8. And the current code just stops exp from dropping, it doesn't drop any heads.
    To drop heads, create a new ItemStack, set the material to SKULL_ITEM and the data to the data value of the head you want (fr om http://minecraft.gamepedia.com/Mob_head#Block_and_item_data) and then drop the item at the location of the dead zombie.
     
  9. that's not true, i get exp and head drops
     
  10. You aren't getting any heads
     
  11. i'm not getting heads for entities that have _ in their name like iron_golem and cave_spider and ocelots but i am getting heads from zombies, creeper etc.

    The problems that I am having are:
    • getting head drops from iron golems, mooshrooms, ocelots, cave spiders, zombie pigman and magma cube
    • the heads shows up as alex or steve instead of the entity type
    • getting money for selling the head in the GUI and removing the head from player's inventory (currently when clicking to sell, nothing happens)
    Code (Text):
    public class ChaosOPHeads extends JavaPlugin implements Listener{
        private Economy economy;
        public void onEnable() {
            saveDefaultConfig();
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
         public void open(Player player) {
                Inventory inv = Bukkit.createInventory(null, 6*9, ChatColor.DARK_GRAY + "Sell A Head");
               
                int inventorySlot = 0;
               
                for (String mobList : getConfig().getStringList("Mobs")) {
                    String[] mobArray = mobList.split(":");
                    String mobType = mobArray[0];
                    String mobSkullName = mobArray[1];
                    String mobPrice = mobArray[3];
                    String mobTypeEnabled = mobArray[4];
                   
                    if (Boolean.valueOf(mobTypeEnabled) == true) {
                        ItemStack is = new ItemStack(Material.SKULL_ITEM, 1, (short)3);
                        SkullMeta sm = (SkullMeta) is.getItemMeta();
                        sm.setDisplayName(ChatColor.GREEN + mobType.replace("_", " ").toLowerCase() + "'s head");
                        sm.setOwner(mobSkullName);
                        sm.setLore(Arrays.asList(ChatColor.GRAY + "Click to sell and", ChatColor.GRAY + "earn $" + mobPrice));
                        is.setItemMeta(sm);
                       
                        inv.setItem(inventorySlot, is);
                       
                        inventorySlot++;
                    }
                }
               
                player.openInventory(inv);
            }
         public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                if (cmd.getName().equalsIgnoreCase("head")) {
                    if (!(sender instanceof Player)) {        
                        sender.sendMessage(ChatColor.RED + "You must be a player to use this command!");
                        return false;
                    }
                    Player player = (Player) sender;
                    ItemStack is = player.getItemInHand();
                       
                    for (String mobList : getConfig().getStringList("Mobs")) {
                        String[] mobArray = mobList.split(":");
                        String mobType = mobArray[0];
                        String mobTypeEnabled = mobArray[4];
                       
                        if (Boolean.valueOf(mobTypeEnabled) == true) {
                            if ((is.getType() == Material.SKULL_ITEM) && (is.hasItemMeta()) && (is.getItemMeta().getDisplayName().equals(ChatColor.GREEN + mobType.replace("_", " ").toLowerCase() + "'s head"))) {
                                open(player);
                                return true;
                            }
                        }
                        else{
                            player.sendMessage(ChatColor.RED + "You must hold a mob skull to use this command!");
                        }
                    }
                    return true;
                }
                return true;
         }
           
            @EventHandler
            public void onInventoryClick(InventoryClickEvent event) {
               
                Player player = (Player) event.getWhoClicked();
               
                ItemStack is = event.getCurrentItem();

                if ((event.getCurrentItem() != null) && (event.getCurrentItem().getType() != Material.AIR)) {
                    if (event.getInventory().getName().equals(ChatColor.DARK_GRAY + "Sell A Head")) {
                        event.setCancelled(true);
                        for (String mobList : getConfig().getStringList("Mobs")) {
                            String[] mobArray = mobList.split(":");
                            String mobType = mobArray[0];
                            String mobPrice = mobArray[3];
                            String mobTypeEnabled = mobArray[4];
                           
                            if (Boolean.valueOf(mobTypeEnabled) == true) {
                                if ((event.getCurrentItem().getType() == Material.SKULL_ITEM) && (is.hasItemMeta()) && (is.getItemMeta().getDisplayName().equals(ChatColor.GREEN + mobType.replace("_", " ") + "'s Head"))) {
                                    boolean containsMobSkull = false;
                                   
                                    for (ItemStack itemStack : player.getInventory().getContents()) {
                                        if ((itemStack != null) && (itemStack.getType() != Material.AIR)) {
                                            if ((itemStack.getType() == Material.SKULL_ITEM) && (itemStack.hasItemMeta()) && (itemStack.getItemMeta().getDisplayName().equals(ChatColor.GREEN + mobType.replace("_", " ") + "'s Head"))) {
                                                player.getInventory().remove(itemStack);
                                                containsMobSkull = true;
                                                break;
                                            }
                                        }
                                    }
                                   
                                    if (containsMobSkull == true) {
                                        player.sendMessage(ChatColor.GREEN + "You have sold a Mob skull for " + ChatColor.GOLD + "$" + mobPrice + ChatColor.GREEN + ".");
                                        player.playSound(player.getLocation(), Sound.LEVEL_UP, 1.0F, 1.0F);
                                        Integer price = Integer.parseInt(mobPrice);
                                        economy.depositPlayer(player.getName(), price);
                                    } else {
                                        player.sendMessage(ChatColor.RED + "You don't have that item in your inventory to sell.");
                                        player.playSound(player.getLocation(), Sound.ANVIL_LAND, 1.0F, 1.0F);
                                    }
                                   
                                    player.closeInventory();
                                    event.setCancelled(true);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
           
            @EventHandler
            public void onEntityDeath(EntityDeathEvent event) {
                if (event.getEntity().getKiller() instanceof Player) {
                    for (String mobList : getConfig().getStringList("Mobs")) {
                        String[] mobArray = mobList.split(":");
                        String mobType = mobArray[0];
                        String mobSkullName = mobArray[1];
                        String mobDropChance = mobArray[2];
                        String mobTypeEnabled = mobArray[4];
                       
                        if (Boolean.valueOf(mobTypeEnabled) == true) {
                            String mobTypeDefaultName = event.getEntityType().getName();
                           
                            if (mobType.equalsIgnoreCase(mobTypeDefaultName)) {
                                double r = Math.random();
                                int s = (int) Math.pow(10, 1);
                                double fr = (double) Math.round(r * s) / s;
                                   
                                if (fr < Double.valueOf(mobDropChance)) {
                                    ItemStack is = new ItemStack(Material.SKULL_ITEM, 1, (short)3);
                                    SkullMeta sm = (SkullMeta) is.getItemMeta();
                                    sm.setDisplayName(ChatColor.GREEN + mobType.replace("_", " ").toLowerCase() + "'s head");
                                    sm.setOwner(mobSkullName);
                                    sm.setLore(Arrays.asList(ChatColor.GRAY + "A trophy for killing", ChatColor.GRAY + "the mob."));
                                    is.setItemMeta(sm);
                                       
                                    event.getDrops().add(is);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
    }
    config:
    Code (Text):
    Mobs:
    - BLAZE:MHF_Blaze:0.7:100:true
    - ZOMBIE:MHF_Zombie:0.7:100:true
    - SPIDER:MHF_Spider:0.7:100:true
    - CAVE_SPIDER:MHF_CaveSpider:0.7:100:true
    - ENDERMAN:MHF_Enderman:0.7:100:true
    - GHAST:MHF_Ghast:0.7:100:true
    - IRON_GOLEM:MHF_Golem:0.7:100:true
    - MAGMA_CUBE:MHF_LavaSlime:0.7:100:true
    - PIG_ZOMBIE:MHF_PigZombie:0.7:100:true
    - CREEPER:MHF_Creeper:0.7:100:true
    - SKELETON:MHF_Skeleton:0.7:100:true
    - SLIME:MHF_Slime:0.7:100:true
    - WITHER_SKELETON:MHF_WSkeleton:0.7:100:true
    - CHICKEN:MHF_Chicken:0.7:100:true
    - COW:MHF_Cow:0.7:100:true
    - MUSHROOM_COW:MHF_MushroomCow:0.7:100:true
    - OCELOT:MHF_Ocelot:0.7:100:true
    - PIG:MHF_Pig:0.7:100:true
    - SHEEP:MHF_Sheep:0.7:100:true
    - SQUID:MHF_Squid:0.7:100:true
    - VILLAGER:MHF_Villager:0.7:100:true
     
    #11 TheMeowster, Apr 16, 2017
    Last edited: Apr 17, 2017
  12. Good. For other mobs besides the 4 you get with the normal Minecraft skull, you can create a skull with a custom skull owner and set that to an account which has the skin of the mob you want. Mojang provides some accounts with these skins listed right here. Just set the skull owner in the ItemMeta to these "players".

    Edit: Ignore this. Didn't see the post before.
     
  13. well is there any way of fixing the other problems that i am currently having?
     
  14. Sorry I didn't have time back then but right now I have and I've tried out your current plugin.
    The head drops probably work for me. I've got the right head when killing an ocelot.
    Now I'll check the command and edit this when I've found the issue.
     
    #14 Kage0x3B, Apr 17, 2017
    Last edited: Apr 17, 2017
  15. i found a solution and i fixed the GUI (so heads are taken away and a message says that they gained x amount of money) except i found that when selling multiple skulls of the same type, all of those skulls are taken away but they get a message that they got the price of one. All i have left is to somehow use vault api to add money to the player (price according to config x the amount of those skull type), fix the mobs who arent dropping skulls and fixing the sell message (which is what i talked about before). And i tried again, ocelot heads (or any head really) don't drop from ocelots (as well as others that i think i mentioned)
     
  16. When selling heads, check if the amount of the ItemStack you are currently just removing is bigger than 1, if yes, decrease it instead of removing the whole stack. Is your server maybe in offline mode? That sometimes is a problem with skins.. And then just weird Minecraft behavior.
     
  17. the server i am using is a run.bat xD . but surely if it was a problem with skins, there would still be a drop head?
     
  18. Then just log something in the entity death event handler and also log the content of if-statements.
    You can easily find typos like this (like in the inventory click handler for the gui).
     
  19. but if it was a typo, wouldn't it affect all the skulls instead of just some?
     
  20. Mabye it's a typo in the config
    Which heads don't work right now?