Solved how to modify an integer in a lore [Thx arhanozkardes]

Discussion in 'Spigot Plugin Development' started by SlydEkinox, Mar 22, 2020.

  1. Hi,

    In first, im sorry for my bad English (Im french..)

    I have a problem with my plugin, I am trying to modify the lore of an item, the goal being to remove a number each time this item is used with a right click to create a customized durability.

    The code: https://hastebin.com/urilumezim.java (Sorry i don't know how to paste the code in this forum)

    Errors: Nothing
     
  2. Hey,

    You can use ItemMeta for that.
    And my proposal to you is use ItemStack for this too.

    If you don't know : ItemStack and ItemMeta you can write it here again.
    Good Luck!
     
  3. why im supposed to do that ? Because i tried before but I did not succeed ^^'
     
  4. So, this is the most basic way to work with ItemStack and ItemMeta:

    Code (Java):
    List<String> bookLore = new ArrayList<>();
    bookLore.add(ChatColor.LIGHT_PURPLE + "Line 1");
    bookLore.add(ChatColor.DARK_PURPLE + "Line2");
    ItemStack book = new ItemStack(Material.BOOK);
    ItemMeta bookMeta = book.getItemMeta();
    bookMeta.setDisplayName(ChatColor.RED + "Book's Name");
    bookMeta.setLore(bookLore);
    book.setItemMeta(bookMeta);
    If you don't understand the point, just look to the code for 2-3 minutes. Then I believe, you will understand perfectly!
    If not, you can write here.
     
  5. did you bother to look at my code? I want to change the lore of an item when right clicking with it,

    But i want to create a customize durability so the lore must be a integer :/
     
  6. so you don't see/saw (idk) my code...
     
  7. Snippet from similar question
    Code (Text):
    ItemStack itemToChange =  targetItem<-you provide it
    ItemMeta im=itemToChange.getItemMeta();
    List<String> targetLore = im.getLore();
    for(String s : targetLore){
    if(s.contains("FreeSocket"){
    targetLore.remove(s);
    targetLore.add("Socketed Gem");
    }
    }
    im.setLore(targetLore);
    itemToChange.setItemMeta(im);
    Mid loop changing
    Code (Text):
    String replacement = s.replaceFirst("FreeGem","SocketedGem");
    targetLore.remove(s);
    targetLore.add(replacement);
    Best would be taking the line,replacing/modifying,remove the line then add it again with the replacement

    By your code you split the string accordingly so the only thing left is your int,cast it to a integer
    Integer.parseInt(string) modify it,replace it and ....
     
  8. Also please show how the line look like,so we can suggest how to split it
     
  9. I need an integer in the lore, a number if you want ...........

    Okay i'll be clearer..

    What I want is a number in the lore of the item which decreases when we right click with the item in hand

    i can't do that with your method :/

    So you have : The command for give the item (the first screenshot)

    and the interact (i have any errors)
     

    Attached Files:

  10. he told you how just without telling you how.
    #getItemMeta returns a copy
    #getLore returns a copy
    you have to use #setLore to update the lore
    then you have to use #setItemMeta to update the item meta.
     
  11. I know i know, but i need to do that 25 times... i don't want to check the exactly number for replace by the exactly number, i need a loop but i don't know how to make that loop
     
  12. Im sorry (seriously im very sorry) , thank you i have mixed your method with my method and it work ! I set the thread on solved !