getting an int from an item lore

Discussion in 'Spigot Plugin Development' started by Blaze_4_Dayz, Jan 5, 2019.

  1. I need to be able to get a int from an item lore that a play has right clicked with it in their hand.
    I have this so far

    Code (Java):

      [B]public[/B] [B]static[/B] [B]int[/B] getItemAmount(ItemStack ie){

          ItemStack item = (ItemStack) ie;

          [B]if[/B](item.getItemMeta().getLore() != [B]null[/B] && item != [B]null[/B]){

          [B]for[/B](String line : item.getItemMeta().getLore()) {

          [B]if[/B](line.startsWith("Amount: ")){

          String level = line.replace("Amount: ", "");

          level = ChatColor.[I]stripColor[/I](level);

            Events.[I]mcoins[/I] = Integer.[I]parseInt[/I](level);

                      }

                  }

              }

        return Events.[I]mcoins[/I];

          }
     
    it throws no errors and always returns 0 *
     
  2. First of all, you need to make sure there is an item before making sure that the item has item meta, then finally you see if it has lore.

    you are gonna get a lot of null pointers with that code.

    But anyways back to your question, I don't really know what is wrong, I would try debugging. Like, print out the value of level after you remove the "Amount: ". then you can see if what it actually is. Also, can you share your events class?
     
  3. Use regex to extract only digits.
    str = str.replaceAll("\\D+","");
     
  4. ItemStack ie .... ItemStack item = (ItemStack) ie; what are you trying to achieve with this?
     
    • Agree Agree x 2
  5. 1. i don't set where you re set the itemmeta to the item
    2. [B]if[/B](line.startsWith("Amount: ")){ the item most likely has color so it wouldn't go past this point. Perhaps try
    if (ChatColor.stripColor(line).startsWith("Amount: "){
    I can barely read this code there is like 1 million [/I]
     
  6. I believe by adding a chat color to the string you are adding in non-numerical symbols?
     
    • Agree Agree x 1
  7. I have no erros in console
    I would also rather not show my whole class, as it is mostly custom methods
     
    #7 Blaze_4_Dayz, Jan 6, 2019
    Last edited: Jan 6, 2019
  8. im stripping these, with this level = ChatColor.stripColor(level);
     
  9. I don't know why it did the " "
     
  10. do I change the "//D+" to "amount: "?
     
  11. If you’re using 1.13.2 and above, you could store it using the new CustomItemTagContainer instead.
     
    • Agree Agree x 1
  12. you didnt chatcolor.strip color the line you were checking
    Code (Text):
          [B]if[/B](line.startsWith("Amount: ")){
     
    this is wrong if the item lore is colored
     
    • Agree Agree x 1
  13. Strahan

    Benefactor

    As previously mentioned, WTF is the point behind ItemStack item = (ItemStack) ie;? Just directly use the variable that was passed to the function. You also do not need to cast something when it already matches type.

    if (item.getItemMeta().getLore() != null && item != null){ Logic is processed left to right, so that item != null should be the first thing not the last.

    Code (Text):
    String level = line.replace("Amount: ", "");
    level = ChatColor.stripColor(level);
    Instead of doing that, combine it - String level = ChatColor.stripColor(line.replace("Amount: ", "")); though I wouldn't even do that, I'd use regex as prior mentioned.

    I don't mean to be rude; but with the skill level you are exhibiting, nobody is gonna steal your methods dude. Not to mention if the rest of the class is akin to this, posting your code is probably a good idea so the community can help you iron it out and give pointers on making it more efficient.
     
    • Funny Funny x 2
  14. Strahan

    Benefactor

    Instead of suppressing deprecation warnings, use non deprecated methods.
     
  15. how would I do that?
     
  16. Strahan

    Benefactor

    In this particular case, by using .name() or .toString()
     
  17. that isn't really the topic of this thread, I am asking how to get a int from a lore, the entity death event works fine
     
  18. Strahan

    Benefactor

    I know. I just felt like mentioning that it's better to not just ignore it when your compiler is trying to talk to you. If you didn't care, why did you ask me to elaborate? :p

    PS: So did you ever get it working?
     

Share This Page