Mob Drops

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

  1. I am making a plugin where there is a percentage chance of a mob dropping a head from what i specified in the config. They would also be able to sell the heads in a gui with an item (material being the head type in hand) saying how much it costs (also in a config) and the head type. However I am struggling on a few things:
    1. making the mobs drop their skulls
    2. the item in gui (material being the head type in hand) saying how much it costs (also in a config) and the head type in lore
    3. adding money to the player after they sold the head (cost in config)
    this is what i have so far:
    (i haven't done all the mobs)
    Code (Text):
    public class ChaosOPHeads extends JavaPlugin implements Listener{

        public void onEnable() {
            saveDefaultConfig();
        }
        public ItemStack NoStat(Player p, Material mat, int amt, String name, String lore1) {
            ItemStack item = new ItemStack(mat, amt);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(name);
            ArrayList<String> lore = new ArrayList<String>();
            lore.add(lore1);
            meta.setLore(lore);
            item.setItemMeta(meta);
            return item;
        }


        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 p = (Player) sender;
                ItemStack i = p.getItemInHand();
                Material m = i.getType();
                Inventory inv = Bukkit.createInventory(null, 9, "Heads");
                inv.setItem(0, NoStat(p, Material.EMERALD_BLOCK, 1, ChatColor.GREEN + "Sell Head", ChatColor.DARK_GRAY + "Click to sell your mob head!"));
                inv.setItem(4, NoStat(p, m, 1, ChatColor.GOLD + " Head", ChatColor.DARK_GRAY + "Worth $"));
                inv.setItem(8, NoStat(p, Material.BARRIER, 1, ChatColor.RED + "Close", ChatColor.DARK_GRAY + "Close menu"));
                p.openInventory(inv);
                return true;
            }
            return true;
        }
       
        public int getRandom(int lower, int upper) {
            return new Random().nextInt((upper - lower) + 1) + lower;
        }
       

            @EventHandler
            public void onEnityDeath(EntityDeathEvent e){
                if (this.getConfig().getBoolean("zombie") == true){
                if (getRandom(1, 100) <= this.getConfig().getInt("zombiechance")) {
                    if (e.getEntity().getType().equals(EntityType.ZOMBIE)) {
                   e.setDroppedExp(0);
                    }
    }
                }
                if (getRandom(1, 100) <= this.getConfig().getInt("skeletonchance")) {
                    if (!(this.getConfig().getBoolean("skeleton") == true)){
                        return;
                    }
                    if (e.getEntity().getType().equals(EntityType.SKELETON)) {
               
                    }
    }
                if (getRandom(1, 100) <= this.getConfig().getInt("irongolemchance")) {
                    if (!(this.getConfig().getBoolean("irongolem") == true)){
                        return;
                    }
                    if (e.getEntity().getType().equals(EntityType.SKELETON)) {
               
                    }    
            }
            }
    }
    Code (Text):
    zombie: true
    zombiechance: 20
    zombiecost: 100
    skeleton: true
    skeletonchance: 20
    skeletoncost: 100
    irongolem: true
    irongolemchance: 20
    irongolemcost: 100
     
  2. 1.
    Use e.getDrops().add(SkullItemStack); to add the Skullitem

    2. You could get the SkullMeta and get the Owner name and check in the config the cost

    3. Take a look into the Vault API

    SideNote:
    Please add a check if the Item in the players hand is null or if it's a head. Otherwise it will start throwing errors.

    For the config change it to
    Code (Text):
     zombie:
      enabled: true
      chance: 20
      cost: 100
    skeleton:
      enabled: true
      chance: 25
      cost:  150
    This is much more easy to read and edit as you can do
    Code (Text):

    config.get(e.getEntity().getType().toString() + ".enabled");
    instead of checking for every mob

    For booleans you don't need to do
    == true

    it will automatically check if it's true or false
     
    • Agree Agree x 1
  3. I am getting errors on part 1 because SkullItemStack isn't defined
    part 2 - how to you get the skull meta? And how do you get the owner name? (i am guessing owner is the mob who's head belongs to)
    i am also getting errors on 'config.get(e.getEntity().getType().toString() + ".enabled")'
    As well as that, how would i get the chance and cost using that config?
     
  4. I know skullitemStack isn't defined, it's just a temporary name that you put your itemstack with the skull in it.

    SkullMeta:
    Code (Text):
     SkullMeta sm = (SkullMeta) i.getItemMeta();
     
    To get the chance:
    this.getConfig().getInt(e.getEntity().getType() + ".chance");
     
  5. I did this for the event and set the chance to 100 in the config but nothing happens
    Code (Text):
            @EventHandler
            public void onEnityDeath(EntityDeathEvent e){
                String killed = e.getEntity().getName();
                 ItemStack item = new ItemStack(Material.SKULL_ITEM, 1, (short)3);
                  ItemMeta itemMeta = item.getItemMeta();
                  itemMeta.setDisplayName(killed + " Head");
                  ((SkullMeta)itemMeta).setOwner(killed);
                  item.setItemMeta(itemMeta);
                  Location deathevent = e.getEntity().getLocation();
                if (this.getConfig().getBoolean(e.getEntity().getType().toString() + ".enabled") == true){
                if (getRandom(1, 100) <= this.getConfig().getInt(e.getEntity().getType() + ".chance")) {
                        deathevent.getWorld().dropItem(deathevent, item);  
    }
                }
            }
     
  6. use DropItemNaturally
    set the owner to ("MHF_" + killed.toLowerCase().substring(0, 1).toUpperCase() + killed.substring(1).toLowerCase())

    Remove the == true because if you put that in code that is in your application nobody will hire you as it's insanely unprofessional
     
  7. what's DropitemNaturally? I don;t know how to use that
    Code (Text):
            @EventHandler
            public void onEnityDeath(EntityDeathEvent e){
                  String killed = e.getEntity().getName();
                     ItemStack item = new ItemStack(Material.SKULL_ITEM, 1, (short)3);
                      ItemMeta itemMeta = item.getItemMeta();
                      itemMeta.setDisplayName(killed + " Head");
                      ((SkullMeta)itemMeta).setOwner("MHF_" + killed.toLowerCase().substring(0, 1).toUpperCase() + killed.substring(1).toLowerCase());
                      item.setItemMeta(itemMeta);
                      Location deathevent = e.getEntity().getLocation();
                    if (this.getConfig().getBoolean(e.getEntity().getType().toString() + ".enabled")){
                    if (getRandom(1, 100) <= this.getConfig().getInt(e.getEntity().getType() + ".chance")) {
                            deathevent.getWorld().dropItem(deathevent, item);
        }          
    }
    }
    i haven't tested it but this is what I got after making the changes that you suggested
     
  8. Change this line to
    Code (Text):
     e.getDrops().add(item);
     
  9. there's no errors, but i am not getting any head drops :/
     
  10. Add a few Bukkit.getLogger().info("DEBUG 1 2 etc.."); in your code to see where it goes wrong. Maybe your config isn't properly made
     
  11. i changed it to this
    Code (Text):
            @EventHandler
            public void onEnityDeath(EntityDeathEvent e){
                  String killed = e.getEntity().getName();
                    if (this.getConfig().getBoolean(e.getEntity().getType().toString() + ".enabled")){
                    if (getRandom(1, 100) <= this.getConfig().getInt(e.getEntity().getType() + ".chance")) {
                         ItemStack item = new ItemStack(Material.SKULL_ITEM, 1, (short)3);
                          SkullMeta skullMeta = item.getItemMeta();
                          skullMeta.setDisplayName(killed + " Head");
                          ((SkullMeta)skullMeta).setOwner("MHF_" + killed.toLowerCase().substring(0, 1).toUpperCase() + killed.substring(1).toLowerCase());
                          item.setItemMeta(skullMeta);
                         e.getDrops().add(item);
        }          
    }
    }
    but then how to i fix "
    SkullMeta skullMeta = item.getItemMeta();
    skullMeta.setDisplayName(killed + " Head");"
    because i am getting errors because i changed it to skullmeta
     
  12. 1, is that code what i need to make it work (or are there some things that i need to change? 2, i wanted an ability to change whether the mob is enabled (e.g. it would only drop a spider head if spider was set to enabled in config). How would i do that using your example? Also
    Code (Text):
    String mobTypeDefaultName = event.getEntityType().getName();
    .getName() is deprecated
     
  13. Deprecated still means it works.
    Change the config to:


    - SPIDER:MHF_Spider:0.7:true

    and then
    Code (Text):

    boolean isEnabled = Boolean.parseBoolean(args[3]);
    if (! isEnabled) {
       return;
    }
     
  14. what about the rest of the mobs? would it just be
    - CREEPER:MHF_Creeper:0.7:true

    and if I wanted to add the amount it costs would i just make a new mobarray[3] and add it to the config?

    also there's an error on args[3] because it's not specified (like in an onCommand since it's in an eventhandler)
     
  15. can i state this at the beginning? because before the event handler, i want to get the mob type and the cost so that I can add economy
    Code (Text):
                      String[] mobArray = mobList.split(":");
                      String mobType = mobArray[0];
                      String mobSkullName = mobArray[1];
                      String mobDropChance = mobArray[3];
                      String mobEnabled = mobArray[2];
    also would this work in order to get the head type of the mob?
    Code (Text):
                ItemStack i = p.getItemInHand();
                Material m = i.getType();
                if (!(m == Material.SKULL)){
                    p.sendMessage(ChatColor.RED + "You must be holding a skull to use this command!");
                    return false;
                }
     
  16. but i want to make a gui that has [slot 0] a block where you can sell head [slot 4] the head saying what type it is and how much money you can sell if for which price in config per head type. Wouldn't that be before the eventhandler?

    also would this work in order to get the head type of the mob?

    ItemStack i = p.getItemInHand();
    Material m = i.getType();
    if (!(m == Material.SKULL)){
    p.sendMessage(ChatColor.RED + "You must be holding a skull to use this command!");
    return false;
    }
     
  17. I tried adding in the config other mobs but i don;t know if i got their names right (because some of them have weird names like Wskeleton)
    Code (Text):
    Mobs:
    - BLAZE:MHF_Blaze:true:0.9:100
    - ZOMBIE:MHF_Zombie:true:0.9:100
    - SPIDER:MHF_Spider:true:0.9:100
    - CAVESPIDER:MHF_CaveSpider:true:0.9:100
    - ENDERMAN:MHF_Enderman:true:0.9:100
    - GHAST:MHF_GHAST:true:0.9:100
    - GOLEM:MHF_Golem:true:0.9:100
    - LAVASLIME:MHF_LavaSlime:true:0.9:100
    - PIGZOMBIE:MHF_PigZombie:true:0.9:100
    - CREEPER:MHF_Creeper:true:0.9:100
    - SKELETON:MHF_Skeleton:true:0.9:100
    - SLIME:MHF_Slime:true:0.9:100
    - WSKELETON:MHF_WSkeleton:true:0.9:100
     
  18. i have been trying to do that and create a method but i am getting errors on what I am trying to make. Also even with what you gave me so far, skulls aren't dropping
     
    #18 TheMeowster, Apr 16, 2017
    Last edited: Apr 16, 2017
  19. Ok yeah that was why ^ . If forgot to. But instead of getting the skins of the heads, i get steve and alex. I didn't understand the part that you said on the economy the heads and the inventory :/

    Edit: the skin doesn't work only in the inventory but you can see it when you place it down
     
    #19 TheMeowster, Apr 16, 2017
    Last edited: Apr 16, 2017
  20. for which part?