Getting information from item lores

Discussion in 'Plugin Development' started by InfamousSheep, Apr 19, 2014.

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

    InfamousSheep

    Okay, so I want to decipher information from an items lore. In this case, I'm storing the level and EXP of an item. I know how to get the integer from a string like this:
    EXP: 36
    However, I'm having trouble getting that value if there is more info after it like this:
    EXP: 36/256
    Keep in mind that '256' value wont always be the same either.
    Any feed back is appreciated, thanks in advance.
     
  2. Offline

    akabarblake

    do you mean durability, not EXP? I never knew you could store that.
     
  3. Offline

    InfamousSheep

    No, it's custom. So I'm adding an item that you can level up but I'm displaying and gathering information through the lore of the item.
     
  4. Offline

    akabarblake

    Oh, yes I've seen that.
    Code I got from Plugin tut. it may help.
    Code:java
    1. public Map<String, Integer> wool_colors = new HashMap<>();
    2.  
    3. // Run this on plugin startup (ideally reading from a file instead of copied out row by row):
    4. wool_colors.put("orange", 1);
    5. wool_colors.put("magenta", 2);
    6. wool_colors.put("light blue", 3);
    7. ..
    8. wool_colors.put("black", 15);
    9.  
    10. // Run this in response to user commands - turn "green" into 13
    11. int datavalue = 0;
    12. if (wool_colors.containsKey(argument)) {
    13. datavalue = wool_colors.get(argument);
    14. } else {
    15. try { datavalue = Integer.parseInt(argument); }
    16. catch (Exception e) {}
    17. }

    Its an example with wool colors.
     
  5. Offline

    InfamousSheep

    I don't think this is what I'm looking for or has any relevance to what I'm trying to achieve with this. But thanks anyway.
     
  6. Offline

    Maurdekye

    InfamousSheep You could use a regular expression;
    Code:java
    1. Pattern getExp = Pattern.compile("EXP: (\n+)/\n+");
    2. String loreline = item.getItemMeta().getLore().get(0);
    3. int level = Integer.parseInt(getExp.matcher(loreline));
    4.  
    5. // Do things related to item's level down here


    Do keep in mind that you should probably define the getExp variable globally once, and then use it whenever you want to get the exp from an item, as compiling regular expressions takes lots of resources.
     
  7. Offline

    nuclearmissile

    I agree with Maurdekye, regex's are probably the way to go with String handling. If you're using the / to separate data values, use StringName.split("/"); That'll break the string into individually readable string numbers easily. For example, the 36/256, with split("/"), will return an array containing the string "36" and the string "256". From there, use each string's position in the array to determine what significance it has.
     
  8. Offline

    InfamousSheep

    I have an error under "parseInt" which states "The mothos parseInt(String) in the type Interger is not applicable for the arguments (Matcher)"
    Code:
    Pattern getExp = Pattern.compile("EXP: (\n+)/\n+");
    String loreline = player.getItemInHand().getItemMeta().getLore().get(1);
    int level = Integer.parseInt(getExp.matcher(loreline));
     
  9. Offline

    Maurdekye

    InfamousSheep Oh, sorry. Replace that line with this;
    Code:java
    1. int level = Integer.parseInt(getExp.matcher(loreline).group(0));
     
Thread Status:
Not open for further replies.

Share This Page