1.16.x Custom drops

Discussion in 'Spigot Plugin Development' started by awgshka, Oct 16, 2020.

  1. Wrote this code, but items are not dropping. What can cause it?
    Code (Java):
    @EventHandler
        public void OnDragonDeath(EntityDeathEvent e){
            if(((CraftEntity) e.getEntity()).getHandle() instanceof EntityEnderDragon){
                Random random = new Random();
                float ChanceRolling = random.nextFloat();

                double NetheriteHelmetChance = (float) Main.plugin.getCustomConfig("items").getDouble("Armour.NetheriteHelmet.chance") / 100;
                ItemStack NetheriteHelmet = new ItemStack(Material.NETHERITE_HELMET);
                double NetheriteChestplateChance = (float) Main.plugin.getCustomConfig("items").getDouble("Armour.NetheriteChestplate.chance") / 100;
                ItemStack NetheriteChestplate = new ItemStack(Material.NETHERITE_CHESTPLATE);
                double NetheriteLeggingsChance = (float) Main.plugin.getCustomConfig("items").getDouble("Armour.NetheriteLeggings.chance") / 100;
                ItemStack NetheriteLeggings = new ItemStack(Material.NETHERITE_LEGGINGS);
                double NetheriteBootsChance = (float) Main.plugin.getCustomConfig("items").getDouble("Armour.NetheriteBoots.chance") / 100;
                ItemStack NetheriteBoots = new ItemStack(Material.NETHERITE_BOOTS);

                double NetheritePickaxeChance = (float) Main.plugin.getCustomConfig("items").getDouble("Instruments.NetheritePickaxe.chance") / 100;
                ItemStack NetheritePickaxe = new ItemStack(Material.NETHERITE_PICKAXE);
                double NetheriteAxeChance = (float) Main.plugin.getCustomConfig("items").getDouble("Instruments.NetheriteAxe.chance") / 100;
                ItemStack NetheriteAxe = new ItemStack(Material.NETHERITE_AXE);
                double NetheriteShovelChance = (float) Main.plugin.getCustomConfig("items").getDouble("Instruments.NetheriteShovel.chance") / 100;
                ItemStack NetheriteShovel = new ItemStack(Material.NETHERITE_SHOVEL);
                double NetheriteHoeChance = (float) Main.plugin.getCustomConfig("items").getDouble("Instruments.NetheriteHoe.chance") / 100;
                ItemStack NetheriteHoe = new ItemStack(Material.NETHERITE_HOE);

                double elytrachance = (float) Main.plugin.getCustomConfig("items").getDouble("Elytras.chance") / 100;
                ItemStack elytra = new ItemStack(Material.ELYTRA);

                double MendingChance = (float) Main.plugin.getCustomConfig("items").getDouble("EnchantedBooks.Mending.chance") / 100;
                ItemStack Mending = new ItemStack(Material.ENCHANTED_BOOK);
                Mending.addEnchantment(Enchantment.MENDING, 1);

                double Efficiency4Chance = (float) Main.plugin.getCustomConfig("items").getDouble("EnchantedBooks.Efficiency.lvl4.chance") / 100;
                ItemStack Efficiency4 = new ItemStack(Material.ENCHANTED_BOOK);
                Efficiency4.addEnchantment(Enchantment.MENDING, 4);

                double Efficiency5Chance = (float) Main.plugin.getCustomConfig("items").getDouble("EnchantedBooks.Efficiency.lvl5.chance") / 100;
                ItemStack Efficiency5 = new ItemStack(Material.ENCHANTED_BOOK);
                Efficiency4.addEnchantment(Enchantment.MENDING, 5);
               
                if(ChanceRolling <= elytrachance) {
                    int Dropped = Main.plugin.getCustomConfig("items").getInt("Elytras.dropped");
                    int Maximum = Main.plugin.getCustomConfig("items").getInt("Elytras.maximum");
                    if(Dropped < Maximum){
                        ((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation().getWorld().dropItem(((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation(), elytra);
                        Main.plugin.getCustomConfig("items").set("Elytras.dropped", Dropped++);
                        Main.plugin.saveCustomConfigs();
                    }
                }

                if(ChanceRolling <= NetheriteHelmetChance){
                    int Dropped = Main.plugin.getCustomConfig("items").getInt("Armour.NetheriteHelmet.dropped");
                    int Maximum = Main.plugin.getCustomConfig("items").getInt("Armour.NetheriteHelmet.maximum");
                    if(Dropped < Maximum){
                        ((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation().getWorld().dropItem(((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation(), NetheriteHelmet);
                        Main.plugin.getCustomConfig("items").set("Armour.NetheriteHelmet.dropped", Dropped++);
                        Main.plugin.saveCustomConfigs();
                    }
                }

                if(ChanceRolling <= NetheriteChestplateChance){
                    int Dropped = Main.plugin.getCustomConfig("items").getInt("Armour.NetheriteChestplate.dropped");
                    int Maximum = Main.plugin.getCustomConfig("items").getInt("Armour.NetheriteChestplate.maximum");
                    if(Dropped < Maximum){
                        ((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation().getWorld().dropItem(((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation(), NetheriteChestplate);
                        Main.plugin.getCustomConfig("items").set("Armour.NetheriteChestplate.dropped", Dropped++);
                        Main.plugin.saveCustomConfigs();
                    }
                }

                if(ChanceRolling <= NetheriteLeggingsChance){
                    int Dropped = Main.plugin.getCustomConfig("items").getInt("Armour.NetheriteLeggings.dropped");
                    int Maximum = Main.plugin.getCustomConfig("items").getInt("Armour.NetheriteLeggings.maximum");
                    if(Dropped < Maximum){
                        ((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation().getWorld().dropItem(((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation(), NetheriteLeggings);
                        Main.plugin.getCustomConfig("items").set("Armour.NetheriteLeggings.dropped", Dropped++);
                        Main.plugin.saveCustomConfigs();
                    }
                }

                if(ChanceRolling <= NetheriteBootsChance){
                    int Dropped = Main.plugin.getCustomConfig("items").getInt("Armour.NetheriteBoots.dropped");
                    int Maximum = Main.plugin.getCustomConfig("items").getInt("Armour.NetheriteBoots.maximum");
                    if(Dropped < Maximum){
                        ((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation().getWorld().dropItem(((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation(), NetheriteBoots);
                        Main.plugin.getCustomConfig("items").set("Armour.NetheriteBoots.dropped", Dropped++);
                        Main.plugin.saveCustomConfigs();
                    }
                }

                if(ChanceRolling <= NetheritePickaxeChance){
                    int Dropped = Main.plugin.getCustomConfig("items").getInt("Instruments.NetheritePickaxe.dropped");
                    int Maximum = Main.plugin.getCustomConfig("items").getInt("Instruments.NetheritePickaxe.maximum");
                    if(Dropped < Maximum){
                        ((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation().getWorld().dropItem(((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation(), NetheritePickaxe);
                        Main.plugin.getCustomConfig("items").set("Instruments.NetheritePickaxe.dropped", Dropped++);
                        Main.plugin.saveCustomConfigs();
                    }
                }

                if(ChanceRolling <= NetheriteAxeChance){
                    int Dropped = Main.plugin.getCustomConfig("items").getInt("Instruments.NetheriteAxe.dropped");
                    int Maximum = Main.plugin.getCustomConfig("items").getInt("Instruments.NetheriteAxe.maximum");
                    if(Dropped < Maximum){
                        ((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation().getWorld().dropItem(((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation(), NetheriteAxe);
                        Main.plugin.getCustomConfig("items").set("Instruments.NetheriteAxe.dropped", Dropped++);
                        Main.plugin.saveCustomConfigs();
                    }
                }

                if(ChanceRolling <= NetheriteShovelChance){
                    int Dropped = Main.plugin.getCustomConfig("items").getInt("Instruments.NetheriteShovel.dropped");
                    int Maximum = Main.plugin.getCustomConfig("items").getInt("Instruments.NetheriteShovel.maximum");
                    if(Dropped < Maximum){
                        ((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation().getWorld().dropItem(((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation(), NetheriteShovel);
                        Main.plugin.getCustomConfig("items").set("Instruments.NetheriteShovel.dropped", Dropped++);
                        Main.plugin.saveCustomConfigs();
                    }
                }

                if(ChanceRolling <= NetheriteHoeChance){
                    int Dropped = Main.plugin.getCustomConfig("items").getInt("Instruments.NetheriteHoe.dropped");
                    int Maximum = Main.plugin.getCustomConfig("items").getInt("Instruments.NetheriteHoe.maximum");
                    if(Dropped < Maximum){
                        ((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation().getWorld().dropItem(((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation(), NetheriteHoe);
                        Main.plugin.getCustomConfig("items").set("Instruments.NetheriteHoe.dropped", Dropped++);
                        Main.plugin.saveCustomConfigs();
                    }
                }

                if(ChanceRolling <= MendingChance){
                    ((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation().getWorld().dropItem(((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation(), Mending);
                }

                if(ChanceRolling <= Efficiency4Chance){
                    ((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation().getWorld().dropItem(((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation(), Efficiency4);
                }

                if(ChanceRolling <= Efficiency5Chance){
                    ((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation().getWorld().dropItem(((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation(), Efficiency5);
                }
            }
        }
    Code (Java):
    public class CustomDragon implements CommandExecutor {
        int MaxRandomX = Main.plugin.getCustomConfig("dragon").getInt("Dragon.Coordinates.MaxRandomX");
        int MaxRandomZ = Main.plugin.getCustomConfig("dragon").getInt("Dragon.Coordinates.MaxRandomZ");
        float Modifier = (float) Main.plugin.getCustomConfig("dragon").getDouble("Dragon.Spawned");

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Random x = new Random();
            Random z = new Random();
            int XCoordinate = x.nextInt(MaxRandomX);
            int ZCoordinate = z.nextInt(MaxRandomZ);
            Bukkit.broadcastMessage("Dragon spawned. Its coo-rds:");
            Bukkit.broadcastMessage("X: " + XCoordinate);
            Bukkit.broadcastMessage("Z: " + ZCoordinate);
            World overworld = getServer().getWorld("world");
            EntityEnderDragon dragon = new EntityEnderDragon(EntityTypes.ENDER_DRAGON, ((CraftWorld) overworld).getHandle());
            dragon.getDragonControllerManager().setControllerPhase(DragonControllerPhase.HOLDING_PATTERN);
            dragon.setLocation(XCoordinate, 70, ZCoordinate, ((CraftWorld) overworld).getHandle().random.nextFloat() * 360.0F, 0.0F);
            ((CraftWorld) overworld).getHandle().addEntity(dragon);
            float OldHealth = dragon.getHealth();
            float NewHealth = OldHealth + ((OldHealth * 5 / 100) * Modifier);
            dragon.setHealth(NewHealth);
            return true;
        }
    }

     
  2. Try changing the if statement to
    Code (Text):
    if(e.getEntity() instanceof EnderDragon){
      //code
    }
    Make sure you import from org.bukkit.entity
     
  3. Also it looks like you are doing a file read every time you are getting the drop chance. This is much less efficient then getting the drop chance once and storing it in memory, i.e. a HashMap
     
  4. But I'm summoning EntityEnderDragon?
     
  5. EntityEnderDragon is just the vanilla name for an ender dragon. Bukkit does the conversion for you, so instanceof EnderDragon will work.

    Actually once you use YamlConfiguration#load() it stores the entire file into a HashMap for you, which is why you have to use the save method to write changes to disk. Good eye though :)

    Have you tried adding debug messages to make sure that the chances and other properties are what you expect? If getDouble() doesn't find anything at the requested path it will return 0 so make sure that it's getting the correct value loaded into the variable. Also you can remove the casts to float, you're grabbing a double, casting it to a float, and saving it to a double variable. You should also try not to repeat yourself so much like this. Instead of copy+pasting, write it into a method you can call over and over again!
     
  6. Well, by debugging I understood that event is not even called :unsure:
    Even with instance of EnderDragon
     
    #6 awgshka, Oct 17, 2020
    Last edited: Oct 17, 2020
  7. Did you register the event handler?
     
  8. For dropping the item, create a variable for the world the ender dragon is in.

    Code (Java):
    World dragonWorld = ((EntityEnderDragon) e.getEntity().getWorld();
    As well as this, create a location variable for the dragon:

    Code (Java):
    Location dragLoc = ((EntityEnderDragon) e.getEntity()).getBukkitEntity().getLocation();
    And finally, using those world & location variables, use this:

    Code (Java):
    dragonWorld.dropItemNaturally(dragLoc, <Item>);
     
  9. Yep, I did
     
  10. So the question is here.
     
  11. Please show the full code where you registered it then, including the main class. The EntityDeathEvent is called whenever an ender dragon dies, when everything is properly registered. Also please show where you put your debug statements. As a general note, your code has some serious unnecessary code repetition, which makes it really hard to read/manage. If you want to improve your code (and I would really advise you to do so), please see:
    As an aside, why are you using EntityEnderDragon and NMS? Nowhere in your code do you use anything that requires NMS. It is true that it is required to make a truly custom entity (see the plugin in my banner :) ), but nothing you are doing here warrants NMS. Whenever you can, you should always stick to Spigot API as it stable and works on multiple versions.