Make a luckyblock drop items from config

Discussion in 'Plugin Development' started by Cri16228, Aug 20, 2018.

Thread Status:
Not open for further replies.
  1. I'm making a lucky block plugin for fun with friends. This is my code now. How can I fix it and make it to drop a random item from the config using the int "identifier"?
    Show Spoiler
    Code:
    package it.capitancold.LuckyBlocks.Events;
    
    import it.capitancold.LuckyBlocks.DropsFile;
    import it.capitancold.LuckyBlocks.EnchantsAlias;
    import it.capitancold.LuckyBlocks.Main;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    import static java.lang.Integer.parseInt;
    
    public class LuckyBlock {
    
        public static void Drops(Block b, Player p) {
            Random r = new Random();
            int max = Main.instance.getConfig().getInt("Drops");
            for (String identifier : DropsFile.getLuckyBlock().getConfigurationSection("drops").getKeys(false)) {
                Boolean isDrop = DropsFile.getLuckyBlock().getBoolean("drops." + identifier + ".IsDrop");
                int drops = DropsFile.getLuckyBlock().getConfigurationSection("drops").getKeys(false).size();
                Integer amount = DropsFile.getLuckyBlock().getInt("drops." + identifier + ".Amount", 1);
                Short damage = (short) DropsFile.getLuckyBlock().getInt("drops." + identifier + ".Data", 0);
                List<String> enchant = DropsFile.getLuckyBlock().getStringList("drops." + identifier + ".Enchants");
                String[] enchants = new String[enchant.size()];
                enchant.toArray(enchants);
                ItemStack stack = new ItemStack(
                        Material.valueOf(
                                DropsFile.getLuckyBlock().getString("drops." + identifier + ".Material").toUpperCase()),
                        amount, damage);
                ItemMeta stackMeta = stack.getItemMeta();
                List<String> lores = DropsFile.getLuckyBlock().getStringList("drops." + identifier + ".Lores");
                List<String> lore = new ArrayList<String>();
                for (int i = 0; i < lores.size(); i++) {
                    lore.add(Main.color(lores.get(i)));
                    stackMeta.setLore(lore);
                }
                String name = Main.color(DropsFile.getLuckyBlock().getString("drops." + identifier + ".Name", ""));
                if (!name.isEmpty()) {
                    stackMeta.setDisplayName(name);
                }
                Boolean hideench = DropsFile.getLuckyBlock().getBoolean("drops." + identifier + ".HideEnchants", false);
                if (hideench) {
                    stackMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
                }
                stack.setItemMeta(stackMeta);
                if (enchants.length > 0) {
                    for (int i = 0; i < enchants.length; i++) {
                        try {
                            if (Enchantment.getByName(enchants[i].toUpperCase().split(",")[0]) != null) {
                                stack.addUnsafeEnchantment(
                                        Enchantment.getByName(enchants[i].toUpperCase().split(",")[0]),
                                        parseInt(enchants[i].split(",")[1]));
                            }
                        } finally {
                            if (EnchantsAlias.getAlias(enchants[i].split(",")[0]) != null) {
                                stack.addUnsafeEnchantment(
                                        EnchantsAlias.getAlias(enchants[i].split(",")[0]),
                                        parseInt(enchants[i].split(",")[1]));
                            }
                        }
                    }
                }
                            //Here I don't know how to take a random number from the identifier size (?)
                            p.getWorld().dropItemNaturally(b.getLocation(), stack);
                        }
                }
            }
        }
    }
    
     
  2. Offline

    SHMC-Dev

    Hi @Cri16228,

    if you are still interested in dropping Items from the config.yml then please let me know. I have written an Eventplugin, which configures lots of Items (with custom displayname, lore, enchants,....) some time ago.

    If you still have this question just let me know and I will post the whole java class.
    Kind regards
    SHMC-Dev
     
Thread Status:
Not open for further replies.

Share This Page