Help with Mob Drops

Discussion in 'Plugin Development' started by SantaClawz69, Sep 2, 2014.

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

    SantaClawz69

    Hey I have some custom items I made with some ItemStacks and I'm trying to get mobs to drop them for a 0.1% chance. I'm also trying to make it so if a player with the permission node types /crate it gives them the items. Sadly I've coded some of the things but I'm getting some errors I'll comment in the code where I'm getting these errors.

    Code:
    package me.Bryan.Crates;
     
    import java.util.ArrayList;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin {
     
        public void onEnable() {
            this.getLogger().info("Crates has been Enabled");
     
        }
     
        public void onDisable() {
            this.getLogger().info("Crates has been Disabled");
        }
     
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            ItemStack RareKey = new ItemStack(Material.STICK);
            ItemMeta meta = RareKey.getItemMeta();
            meta.setDisplayName(ChatColor.RED + "Crate Key");
            ArrayList<String> lore = new ArrayList<String>();
            lore.add(ChatColor.BLUE
                    + "This is a Crate Key that can open up a rare crate box");
     
            ItemStack RareCrate = new ItemStack(Material.CHEST);
            meta.setDisplayName(ChatColor.BLUE + "Rare Crate");
            lore.add(ChatColor.RED
                    + "This is a Crate that can hold rare things");
            ItemStack RegKey = new ItemStack(Material.CHEST);
            meta.setDisplayName(ChatColor.BLUE + "Key");
            lore.add(ChatColor.RED + "This is a regular crate key that opens regular crates");
           
           
            ItemStack RegCrate = new ItemStack(Material.CHEST);
            meta.setDisplayName(ChatColor.RED + "Crate");
            lore.add(ChatColor.RED + "This is a regular Crate that can hold things that may be useful");
            RareCrate.setItemMeta(meta);
            RareKey.setItemMeta(meta);
            RegKey.setItemMeta(meta);
            RegCrate.setItemMeta(meta);
            Player player = (Player) sender;
            if (player.hasPermission("Crates.Admin")) {
                if (cmd.getName().equalsIgnoreCase("CrateItems")) {
                    player.sendMessage("Both a crate and a crate key spawned in your inventory");
                    player.getInventory().addItem(RareKey);
                    player.getInventory().addItem(RareCrate);
                    player.getInventory().addItem(RegKey);
                    player.getInventory().addItem(RegCrate);
                    player.updateInventory();
                }
            }
            return true;
        }
        @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
        public void onMobDeath(EntityDeathEvent event) {
            ItemStack ItemsToDrop = new ItemStack(RareKey, 1); //Errors are here whenever I try to make the mob drop my custom items
            event.getDrops().add(ItemsToDrop);
     
        }
     
           
        }
    
    Please if anyone has suggestions or knows how to fix please tell me. I used to know how to fix this but I took a break from Bukkit and now I forgot xD. Thanks guys.
     
  2. Offline

    ColonelHedgehog

    Could you please post the precise error you are getting? The stack trace would be the most helpful.
     
  3. Offline

    hintss

    RareKey isn't a variable in the scope of the event listener (and pls 2 follow naming conventions kthx)
     
  4. A few lines are wrong (i think)! first you did say 'ItemStack RareKey = new ItemStack(Material.STICK);
    ItemMeta meta = RareKey.getItemMeta(); ' thats totally fine. Then you did set the displayname okey!
    But you did make an arraylsit named Lore did add text to the lore but jou did never Set the lore (meta.setLore(lore)
    And i dont know if you just can give all the items the itemmeta 'meta'.
    T think the error is there becuase you will give the player 4 items white the itemmeta RareKey.getItemMeta(); whit 4 lores (O no not 4 lores the lores arnt set) and you did change the displayname for the same meta 4 times!
    Solution;
    Make 4 diffrend meta`s and set the lore

    Adn for the drops
    Make the itemstacks like one and give the itemstack! And it looks better :D
    >> Like this >>
    Code:java
    1. public ItemStack RareKey() { Material m = Material.TRIPWIRE_HOOK;
    2. ItemStack t = new ItemStack(m);
    3. ItemMeta i = t.getItemMeta();
    4. i.setDisplayName(ChatColor.AQUA + "Am i rare?");
    5. i.setLore(Arrays.asList(ChatColor.GRAY + "Yes you are!"));
    6. t.setItemMeta(i);
    7. return t; }

    And then you just do this event.getDrops().add(RareKey());

    If i did something wrong sorry, if you didnt have any problems whit the part i did tell abaout then im sorry.
    But i hope that i did help you :D
    Edit: i did the lore this way, you can do it your way dont forgot to set the lore
     
Thread Status:
Not open for further replies.

Share This Page