Solved Reading multiple lines of lore and adding them?

Discussion in 'Plugin Development' started by SmallDesk, Jan 10, 2018.

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

    SmallDesk

    Hello! So part of what this plugin is supposed to do is read lores, and add what is written on them, but I have a problem were it can only read the first line and register it. ex.

    Item:
    Diamond Sword
    Line 1: +1000 Dexterity

    That would add 1000 Dexterity to me. However, it cannot add multiple lines. ex.

    Item:
    Diamond Sword
    Line 1: +1000 Dexterity
    Line 2: +1000 Dexterity

    That would not give me 2000 Dexterity, it would give me 1000.

    Come of my code that makes this work:

    Code:
    this.dexterityRegex = Pattern.compile("[+](\\d+)[ ](" + LoreAttributes.config.getString("lore.dexterity.keyword").toLowerCase() + ")");
    
    This is how it reads if the lore is written correctly.
    
    private int getDexterity(LivingEntity entity) {
            Integer damage = Integer.valueOf(0);
            ItemStack[] arrayOfItemStack;
            int j = (arrayOfItemStack = entity.getEquipment().getArmorContents()).length;
            for (int i = 0; i < j; i++)
            {
              ItemStack item = arrayOfItemStack;
              if ((item != null) &&
                (item.hasItemMeta()) &&
                (item.getItemMeta().hasLore()))
              {
                List<String> lore = item.getItemMeta().getLore();
                String allLore = lore.toString().toLowerCase();
              
                Matcher valueMatcher = this.dexterityRegex.matcher(allLore);
                if (valueMatcher.find()) {
                  damage = Integer.valueOf(damage.intValue() + Integer.valueOf(valueMatcher.group(1)).intValue());
                }
              }
            }
            ItemStack item = entity.getEquipment().getItemInHand();
            if ((item != null) &&
              (item.hasItemMeta()) &&
              (item.getItemMeta().hasLore()))
            {
              Object lore = item.getItemMeta().getLore();
              String allLore = lore.toString().toLowerCase();
            
              Matcher valueMatcher = this.dexterityRegex.matcher(allLore);
              if (valueMatcher.find()) {
                damage = Integer.valueOf(damage.intValue() + Integer.valueOf(valueMatcher.group(1)).intValue());
              }
            }
            return damage.intValue();
          }
    
    This is how it registers the Dexterity, if I have any.
    
    if (getDexterity(sender) != 0) {
            message.add(ChatColor.GRAY + LoreAttributes.config.getString("lore.dexterity.keyword") + ": " + ChatColor.WHITE + getDexterity(sender));
        }
    
    And this right here tells me in chat how much Dexterity I have in total.

    Thank you for your time. :)
     
    Last edited by a moderator: Jan 10, 2018
  2. No idea what your code does... but here is some from the top of my head:
    Code:
    private int getDexterity (Player p) {
      
        int dexterity = 0;
       
        // Iterate over armor set and held item
        for (int i = 0; i < 5; i++) {
            // 0-3 : Armor, 4 : Held Item
            ItemStack target = (i < 4 ? p.getInventory().getArmorContents[i] : p.getInventory().getItem(p.getHeldItemSlot()));
           
            // If item has Lore
            if(target != null && target.hasItemMeta() && target.getItemMeta().hasLore()) {
                List<String> lore = target.getItemMeta().getLore();
                // For each line in lore...
                for (int j = 0; j < lore.size(); j++) {
                    String s = lore.get(j).toLowerCase();
                    // If line ends with dexterity, so you could also have +1 Health or some other value
                    if(s.endsWith(" dexterity")) {
                        // Singles out the number only, so if you had +1000 Dexterity, it would remove the "+" and " Dexterity"
                        dexterity+=(Integer.parseInt(s.substring(1, s.length()-10)));
                    }
                }
            }
          
        }
      
    }
     
    Last edited: Jan 13, 2018
    adventuretc likes this.
  3. List<String> lore = item.getItemMeta().getLore();
    String allLore = lore.toString().toLowerCase();

    Try turning this into a for loop and check each one individually to see if it contains Dexterity
     
  4. Offline

    SmallDesk

    Thank you, I'll try it.

    I see what you're doing here, but there is a problem with the "i" variable, it says it's a duplicate.

    Hey, I tried adding a for loop, but since I am not entirely sure how they work, I may have done it wrong. Could you give code with the for loop please?

    My Code:
    Code:
    private int getDexterity(LivingEntity entity) {
            Integer damage = Integer.valueOf(0);
            ItemStack[] arrayOfItemStack;
            int j = (arrayOfItemStack = entity.getEquipment().getArmorContents()).length;
            for (int i = 0; i < j; i++)
            {
              ItemStack item = arrayOfItemStack[I];
              if ((item != null) && 
                (item.hasItemMeta()) && 
                (item.getItemMeta().hasLore()))
              {
                  int r = (arrayOfItemStack = entity.getEquipment().getArmorContents()).length;
                  for(int d = 10; d < r; d++){
                List<String> lore = item.getItemMeta().getLore();
                String allLore = lore.toString().toLowerCase();
               
                Matcher valueMatcher = this.dexterityRegex.matcher(allLore);
                if (valueMatcher.find()) {
                  damage = Integer.valueOf(damage.intValue() + Integer.valueOf(valueMatcher.group(1)).intValue());
                }
              }
              }
            ItemStack item1 = entity.getEquipment().getItemInHand();
            if ((item1 != null) && 
              (item1.hasItemMeta()) && 
              (item1.getItemMeta().hasLore()))
            {
              Object lore = item1.getItemMeta().getLore();
              String allLore = lore.toString().toLowerCase();
             
              Matcher valueMatcher = this.dexterityRegex.matcher(allLore);
              if (valueMatcher.find()) {
                damage = Integer.valueOf(damage.intValue() + Integer.valueOf(valueMatcher.group(1)).intValue());
              }
            }
            return damage.intValue();
          }
            return j;
      }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 12, 2018
  5. Offline

    SmallDesk

    It still gives the same errors with the .getArmorContents and HeldItemSlot.
     
  6. for (int i = 0; i < j; i++)
    {
    ItemStack item = arrayOfItemStack;

    You used I instead of i
    also
    ItemStack[] arrayOfItemStack;
    int j = (arrayOfItemStack = entity.getEquipment().getArmorContents()).length;
    why not set arrayOfItemStack by itself to make things a little clearer?

    What are these errors, can you get them from your console and copy paste them here?
     
    Last edited: Jan 13, 2018
  7. Offline

    SmallDesk


    The error is not really in game, it says the variable "i" is a duplicate, in Eclipse.

    EDIT: @awesomebutter234 I changed the I to i, I somehow didn't notice that lol. But it works just like before and only reads the first line.
     
    Last edited: Jan 13, 2018
  8. Are you still using the code you had at the beginning, or the code that nathan showed you in order to understand how the for loop works?
     
  9. Offline

    SmallDesk

    I was using the code that Nathan showed me.
     
  10. Try sending the lore string to the console or yourself to see if it’s actually reading all of the lore, but not applying the value of the second one.
     
  11. Offline

    SmallDesk

    I've just got this to finally work! The for loop that you told me to put was actually supposed to be put in a different place.

    I'll place my code right here for any reference that anyone ever needs:

    private int getDexterity(LivingEntity entity) {
    Integer damage = Integer.valueOf(0);
    ItemStack[] arrayOfItemStack;
    int j = (arrayOfItemStack = entity.getEquipment().getArmorContents()).length;
    for (int i = 0; i < j; i++)
    {
    ItemStack item = arrayOfItemStack;
    if ((item != null) &&
    (item.hasItemMeta()) &&
    (item.getItemMeta().hasLore()))
    {
    List<String> lore = item.getItemMeta().getLore();
    String allLore = lore.toString().toLowerCase();

    Matcher valueMatcher = this.dexterityRegex.matcher(allLore);
    for(int x = 0; x < j; x++){
    if (valueMatcher.find()) {
    damage = Integer.valueOf(damage.intValue() + Integer.valueOf(valueMatcher.group(1)).intValue());
    }
    }
    }
    }
    ItemStack item = entity.getEquipment().getItemInHand();
    if ((item != null) &&
    (item.hasItemMeta()) &&
    (item.getItemMeta().hasLore()))
    {
    Object lore = item.getItemMeta().getLore();
    String allLore = lore.toString().toLowerCase();

    Matcher valueMatcher = this.dexterityRegex.matcher(allLore);
    for(int x = 0; x < j; x++){
    if (valueMatcher.find()) {
    damage = Integer.valueOf(damage.intValue() + Integer.valueOf(valueMatcher.group(1)).intValue());
    }
    }
    return damage.intValue();
    }
    return j;
    }


    SOLVED.
     
Thread Status:
Not open for further replies.

Share This Page