Solved Making a randomly enchanting List

Discussion in 'Plugin Development' started by harvmaster, Aug 4, 2015.

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

    harvmaster

    Hi so i've been trying to make something that will automatically enchant an item when a chest is restocked.
    I was wandering if I just simply do this is a list and use java's Random() class or I had to use a method in the game to get all of the safe enchantments (I only want safe enchants with a random level).

    Code:
    static List<Material> ironChestItems = Arrays.asList(
                Material.IRON_HELMET, Material.IRON_CHESTPLATE, Material.IRON_LEGGINGS, Material.IRON_BOOTS
                );
       
        static List<Material> diamondChestItems = Arrays.asList(
                Material.DIAMOND_HELMET, Material.DIAMOND_CHESTPLATE, Material.DIAMOND_LEGGINGS, Material.DIAMOND_BOOTS
                );
       
        static List<Material> diamondWeoponChestItems = Arrays.asList(
                Material.DIAMOND_SWORD, Material.DIAMOND_AXE
                );
       
        static List<Material> ironWeoponChestItems = Arrays.asList(
                Material.IRON_SWORD, Material.IRON_AXE
                );
               
        static List<String> meleeEnchantsC = Arrays.asList(
                "Crushing", "Headless", "Lifesteal", "Lifevamp", "Nightmare", "Overcharged", "Scramble", "SKilldrain"
                );
       
        static List<String> meleeEnchants = Arrays.asList(
                "Sharpness", "Smite", "Bane of arthropods", "knockback", "Fire aspect", "Unbreaking", "Looting"
                );
       
        static List<String> armorEnchantsC = Arrays.asList(
                "Agility", "Barrier", "Block", "Demonic", "Dodge", "Jump", "Shadow", "Shield", "Voidwalker"
                );
       
        static List<Enchantment> armorEnchants = Arrays.asList(
               
                );
       
        static List<String> bowEnchantsc = Arrays.asList(
                "Laced", "Skull_Pierce", "Multishot"
                );
               
       
       
        public static void chestRestock(String dungeonName) {
           
            int chestX = CustomEnchants.plugin.getConfig().getInt("dungeons." + dungeonName + ".x");
            int chestY = CustomEnchants.plugin.getConfig().getInt("dungeons." + dungeonName + ".y");
            int chestZ = CustomEnchants.plugin.getConfig().getInt("dungeons." + dungeonName + ".z");
            int chestDif = CustomEnchants.plugin.getConfig().getInt("dungeons." + dungeonName + "Difficulty");
           
           
            Random random = new Random();
            //Random itemType = new Random();
           
            Location loc = new Location(Bukkit.getWorld("world"), chestX, chestY, chestZ);
            Chest c = (Chest) loc.getBlock().getState();
            Inventory cInv = c.getInventory();
           
            //cInv.addItem(new ItemStack(Material.DIAMOND_SWORD));
            for (int i = 0; i <= random.nextInt(6); i++){
       
                Bukkit.getLogger().info(i + 1 + " items");
                if (chestDif >= 5) {
                    if (random.nextInt(2) == 1) {
                        ItemStack item = new ItemStack(diamondChestItems.get(random.nextInt(diamondChestItems.size())));
                        ItemStack item2 = new ItemStack(Material.DIAMOND_SWORD);
                        enchantItem(item2, cInv);
                       
                    } else {
                        cInv.addItem(new ItemStack(diamondWeoponChestItems.get(random.nextInt(diamondWeoponChestItems.size()))));
                    }
                }
               
                else if (random.nextInt(2) == 1) {
                    if (random.nextInt(2) == 1) {
                        ItemStack item = new ItemStack(diamondChestItems.get(random.nextInt(diamondChestItems.size())));
                        ItemStack item2 = new ItemStack(Material.DIAMOND_SWORD);
                        enchantItem(item, cInv);
                        enchantItem(item2, cInv);
                    } else {
                        ItemStack item = new ItemStack(diamondChestItems.get(random.nextInt(diamondChestItems.size())));
                        enchantItem(item, cInv);
                    }
                }
               
                else {
                    if (random.nextInt(2) == 1) {
                        cInv.addItem(new ItemStack(diamondChestItems.get(random.nextInt(diamondChestItems.size()))));
                    } else {
                        cInv.addItem(new ItemStack(diamondWeoponChestItems.get(random.nextInt(diamondWeoponChestItems.size()))));
                    }
                }
               
            }
        }
       
        public static void enchantItem(ItemStack item, Inventory cInv) {
            Random random = new Random();
            @SuppressWarnings("deprecation")
            int itemSwitch = item.getTypeId();
           
            for (int i = 0; i <= random.nextInt(7); i++) {
           
                //List<CustomEnchantment> customEnch = EnchantmentAPI.getAllValidEnchants(item);
               
                switch (itemSwitch) {
                    case 276:
                        String enchantToAddSword = new String(meleeEnchants.get(random.nextInt(meleeEnchants.size())));
                        EnchantmentAPI.getEnchantment(enchantToAddSword).addToItem(item, random.nextInt(4));
                        //EnchantmentAPI.getEnchantment("Protection").addToItem(item, 5);
                    break;
                   
                    case 310:
                        String enchantToAddHelmet = new String(armorEnchantsC.get(random.nextInt(armorEnchantsC.size())));
                        EnchantmentAPI.getEnchantment(enchantToAddHelmet).addToItem(item, random.nextInt(4));
                        Enchantment e = item.addEnchantment(armorEnchants.get(random.nextInt(armorEnchants.size())), random.nextInt(4));
                        //EnchantmentAPI.getEnchantmentNames();
                    break;
                   
                    default:
                       
                }
               
                if (i >= 5) cInv.addItem(item);
            }
        }
    I am using EnchantApi for this as well and I am also open to any help with either making more code more optimized or compact.
    Thanks in advanced, Harvmaster
     
  2. Offline

    Cycryl

    Random random = new Random();
    int r = random.nextInt(10);
    int l = random.nextInt(3)+1;
    switch(r)
    case 0 : //add enchantment with level l break;
    case 1 : //add different enchantment with level l break;
    case 2 : //add different enchantment with level l break;
    etc.....
     
  3. Offline

    harvmaster

    Thank you for the reply, but I have figured out how to do this with the List<enchantment>
     
  4. Offline

    SantaClawz69

    Please make this a "Solved" matter so other developers don't waste their time looking at something that's already solved, thanks!
     
Thread Status:
Not open for further replies.

Share This Page