Help getting item lore

Discussion in 'Programming' started by Camaloony, Jun 1, 2016.

  1. Hey, I'm trying to see if the item right clicked contains a specific line of text in the lore but can't seem to get it to work, here is my code so far:

    Code (Text):
        public void onUse(PlayerInteractEvent event) {
            Player player = event.getPlayer();
                if(player.getItemInHand().getType() == Material.PAPER) {
                    if(event.getItem().getItemMeta().getDisplayName().toLowerCase().contains("cheque")) {
                        List<String> loreCurrent = player.getItemInHand().getItemMeta().getLore();
                        if (loreCurrent.contains("Right-Click this to deposit it!")){
                            player.sendMessage("Hello world");
  2. JustisR


    I like to imagine that my code will run as I expect it to first time too...
    Alas, that is seldom the case.

    Sure it's fun to compile it without any debug code worked in, and hope for the best.
    But when it ultimately fails to do what you want, go right back in and place System.out.print() lines all over the place, so you know what sections of your code are running, what aren't, and then know where in your code you need to make edits.

    For starters, you could put one right in the beginning of the event, to make sure it was registered and is firing properly.
    You did register your events, didn't you? ;)

    I'm going to assume you said yes.
    Now place some System.out.print() lines all up and down that body and find our where it stops heading for your "Hello world".

    (It'll print to console, in case that wasn't clear)
  3. If I remove the lore checking and just go of if the name contains "cheque", it works fine so everything works but the lore section if that's what you mean.
  4. JustisR


    You're running .contains() on a list.
    I'm fairly certain that compares the object's memory location, which would not the the same for different strings of the same pattern.

    Try iterating through it, and running .contains() on the individual strings of the lore, and compare them to whatever string you're checking for.
  5. Managed to fix it, I was saving the lore as a list so I just had to loop the list :)