Enchantment Items From Config

Discussion in 'Plugin Development' started by slater96, Sep 15, 2012.

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

    slater96

    I have in my config, classes with items and armor that you can configure and I would like to make it so I can add enchantments as well to certain items. Is there an ID you can use for enchantments and any way to make it so that you must have the item specified in items to enchant it. My config is set out like this.
    Code:
    Classes:
        Archer:
            Items: 268:1,261:1,262:256,320:2
            Enchantments: 
            Armor: 298,299,300,301
    Thanks in advance
     
  2. Offline

    zippy120

    You could either use the Enchantment ID's and use Enchantment.getById(int i); or the enchantment names with Enchantment.getByName(String s);

    After you've gotten the enchantment, try theEnchantmant.canEnchantItem(ItemStack is); and theEnchantment.conflictsWith(Enchantment other); (assuming theEnchantment is, well, the enchantment :p ) To check if the enchantment can be applied.
     
    slater96 likes this.
  3. Offline

    slater96

    Ok thanks, never knew you could check like that.
    How would I go about making it configurable? Here's what I've started:
    Code:
        public boolean giveEnchantments(Player p, PlayerInventory inv, Block b) {
            Sign sign = (Sign) b.getState();
            String classname= ChatColor.stripColor(sign.getLine(1));
            String[] enchantments = plugin.getConfig().getString("Classes." + classname+ ".Enchantments").split(",");
            for (int i = 0; i < enchantments.length; i++) {
                if (enchantments[i].contains("@")) {
                    String[] enchantments2 = enchantments[i].split("@");
                    if (enchantments2[1].contains(":")) {
                        String[] enchantments3 = enchantments2[1].split(":");
                        try {
                            itemid = Integer.parseInt(enchantments2[0]);
                            level = Integer.parseInt(enchantments3[0]);
                            enchant = Integer.parseInt(enchantments3[1]);
                        } catch (NumberFormatException e) {
                        }
                    } else {
                        try {
                            itemid = Integer.parseInt(enchantments2[0]);
                            level = 1;
                            enchant = Integer.parseInt(enchantments2[1]
                        } catch (NumberFormatException e) {
                        }
                    }
                if (enchant > 0 || itemid > 0) {
                    if (level != 0) {
                        this.applyEnchantments(p, itemid, enchant, level);
                    } else {
                        p.sendMessage(ChatColor.RED + "Level not found, you must specify a level!");
                    }
                }
            }
            return true;
        }
       
        @SuppressWarnings("deprecation")
        public boolean applyEnchantments(Player p, int itemid, int enchant, int level) {
            Material m = Material.getMaterial(itemid);
            if (m == null) {
                p.sendMessage(ChatColor.RED + "Item ID not found: " + itemid);
                return false;
            }
            p.getInventory().getItem(itemid).addEnchantment(Enchantment.getById(enchant), level);
            p.updateInventory();
            return true;
        }
    I would like it set out like this in config
    Enchantments: 268:[email protected]
    This would mean 268 which is a wooden sword would be enchanted with id 16 which is sharpness at level 2. Thanks in advance!
     
  4. Offline

    zippy120

    That code looks like it should work fine, what issues are you having?

    (Small side note: instead of
    Code:
    p.getInventory().getItem(itemid).addEnchantment(Enchantment.getById(enchant), level);
    it doesn't look like it gives the player the item, why not:
    Code:
    p.getInventory.addItem((new ItemStack(m)).addEnchantmnet(Enchantment.getById(enchant), level));
    )
     
Thread Status:
Not open for further replies.

Share This Page