Solved Adding Lore On EnchantItemEvent

Discussion in 'Plugin Development' started by John Cameron, Jun 27, 2013.

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

    John Cameron

    I'm making a new "enchantment" that will display it as a lore, the current code is here below.
    Code:
        @EventHandler
        public void onEnchantItemEvent(EnchantItemEvent event){
            if(!event.isCancelled()){
            ItemStack i = event.getItem();
            int l = event.getExpLevelCost();
            if(event.getItem().getType().toString().toLowerCase().contains("sword")){
            if(l >= 24){
                    ItemMeta im = i.getItemMeta();
                    List<String> lore = im.getLore();
                    lore.add("Corruption");
                    im.setLore(lore);
                    i.setItemMeta(im);
                }
            }
            }
        }
    There is no error and everything is called correctly, the sword check and level.
    So, how do I do it?
    Thanks! :D
     
  2. Offline

    RealDope

    What's the problem?
     
  3. Offline

    John Cameron

    The lore does not update :confused:
     
  4. Offline

    sheigutn

    add p.setItemOnCursor(i); at the end and add a few debug messages ;)
     
  5. Offline

    John Cameron

    Fixed it :D
    Code:
        @EventHandler
        public void onEnchantItemEvent(EnchantItemEvent event){
            if(!event.isCancelled()){
            ItemStack i = event.getItem();
            int l = event.getExpLevelCost();
            if(event.getItem().getType().toString().toLowerCase().contains("sword")){
            if(l >= 24){
                //Random r = new Random();
                //if((r.nextInt(6) + 1) == 4){
                    ItemMeta im = i.getItemMeta();
                    if(im != null){
                    List<String> lore;
                    if(i.getItemMeta().hasLore()){
                        lore = im.getLore();
                    }else{
                        lore = new ArrayList<String>();
                    }
                    lore.add("Corruption");
                    im.setLore(lore);
                    i.setItemMeta(im);
                    event.getEnchanter().updateInventory(); //Ik it's outdated, but it still works
                    }
                //}
            }
            }
            }
        }
    The lore was "null" (it didn't throw an error, bukkit needs to fix it), so I just added a check
     
Thread Status:
Not open for further replies.

Share This Page