Solved resetting item lore

Discussion in 'Plugin Development' started by kangkyuchang, Dec 29, 2019.

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

    kangkyuchang

    I wanna edit new item lore from original item lore.This coding changes "★☆☆☆☆☆☆☆" when you right click on an item with the lore "☆☆☆☆☆☆☆☆". but is not.
    Code:
    @EventHandler
    public void WE(PlayerInteractEvent event)
    {
        Player p = event.getPlayer();
        Random r = new Random();
        ItemStack item = p.getItemInHand();
        if (item == null || !item.hasItemMeta() || !item.getItemMeta().hasLore())  return;
        List<String> elore;
        for (String lore : item.getItemMeta().getLore())
          {
            if(event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK))
    {
                if(lore.matches("§f☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆"))
            {
                    switch(r.nextInt(1))
                 {
                        case 0:
                        elore = item.getItemMeta().getLore();
                        elore.add("★☆☆☆☆☆☆☆");
                        item.getItemMeta().setLore(elore);
                        item.setItemMeta(item.getItemMeta());
    }
    }
    }
    
      }
    }
     
    Last edited: Dec 29, 2019
  2. Offline

    timtower Administrator Administrator Moderator

    @kangkyuchang Get the itemmeta, modify it, set it again.
    You can't do it the way you are doing now.
     
  3. Offline

    kangkyuchang

    @timtower What exactly does it mean to get the itemmeta and modify it?
     
  4. Offline

    timtower Administrator Administrator Moderator

  5. Offline

    kangkyuchang

    @timtower Sorry, I don't understand. I'm only two weeks from starting plugin development so I don't know.
    item.getItemMeta().setLore(Arrays.asList("§f★ ☆ ☆ ☆ ☆ ☆ ☆ ☆")); right?
     
  6. Offline

    timtower Administrator Administrator Moderator

    @kangkyuchang You need a variable for the itemmeta.
    getItemMeta returns a copy of the original, changing it does nothing the way you do it.
     
  7. Offline

    kangkyuchang

Thread Status:
Not open for further replies.

Share This Page