Solved How to check weather any item in a player's inventory contains a specific name

Discussion in 'Spigot Plugin Development' started by HarleySwtfus, Mar 27, 2020.

  1. I have this code:

    Code (Java):

        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e) {
            Player p = e.getPlayer();
            for (ItemStack s : p.getInventory().getContents()) {
                if (s.getItemMeta().getDisplayName().contains("Building")) {
                    if (p.getInventory().contains(s)) {
                        p.sendMessage("you have permit :D");
                        return;
                    } else {
                        p.sendMessage("Must have permit!");
                        e.setCancelled(true);
                    }
                }
            }
        }
     
    But I want to make it so if a player has a "paper" in their inventory with the displayName that contains the word "Building", it executes code. How would I do this? At the moment, whether I have the item or not, it lets me place the block. Throwing this error: https://hastebin.com/vejokikage.cs
     
  2. What is line 21?

    Also some other points,

    your looping through each item of the inventory then you check if the inventory contains the item...(useless check as its in the inventory already)

    Your not checking if the item/inventory is null

    And finally(most likely why its not working your not checking if the item even has a meta in the first place),your straight checking for item name not knowing if it even has one

    Also that return does nothing as your already in a if statement and there is no more code outside
     
  3. My bad, line 21 is
    if (s.getItemMeta().getDisplayName().contains("Building")) {

    I've added if s.hasItemMeta(), and it still does not work.

    New code:

    Code (Java):

        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e) {
            Player p = e.getPlayer();
            for (ItemStack s : p.getInventory().getContents()) {
                if (s != null) {
                    if (s.hasItemMeta()) {
                        if (s.getItemMeta().getDisplayName().contains("Building")) {
                            if (p.getInventory().contains(s)) {
                                p.sendMessage("you have permit :D");
                                return;
                            } else {
                                p.sendMessage("Must have permit!");
                                e.setCancelled(true);
                            }
     
  4. Can you check if s.getItemMeta().getDisplayName() is null? It feels like you are trying to iterate over items with some having no specific names. That should be giving NullPointerException
     
  5. new code,
    Code (Java):

        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e) {
            Player p = e.getPlayer();
            for (ItemStack s : p.getInventory().getContents()) {
                if (s != null) {
                    if (s.getItemMeta().getDisplayName() != null) {
                        if (s.getItemMeta().getDisplayName().contains("Building")) {
                            if (p.getInventory().contains(s)) {
                                p.sendMessage("you have permit :D");
                                return;
                            } else {
                                p.sendMessage("Must have permit!");
                                e.setCancelled(true);
                            }
    I can still place blocks even when I don't have this "permit" in my hand:
    https://gyazo.com/a26c937b9e364ac2716de386d6d7d41a

    EDIT: When I have the permit, it sends the message, but when I dont have the permit, it still lets me build but no messages send.
     
  6. Oh, I see the problem! Your else only covers one if clause! By the way, you used == null there, it should be !=null for displayName!


    Anyways, you should leave the check for permit wherever it is, but remove its else brackets! The way you are doing this, it will only check for one item and then decide upon that! You need to put a e.setCancelled(true) outside of the for loop. If the player has the permit, it will already say 'You have the permit' and end the function. But if they don't, that way it will come until the end where the event gets cancelled, because they don't have the permit!
     
  7. You placed the else at the wrong statement you need to place it where you check if it has the display name
     
  8. Do you mean like this?
    Code (Java):

        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e) {
            Player p = e.getPlayer();
            for (ItemStack s : p.getInventory().getContents()) {
                if (s != null) {
                    if (s.getItemMeta().getDisplayName() != null) {
                        if (s.getItemMeta().getDisplayName().contains("Building")) {
                            if (p.getInventory().contains(s)) {
                                p.sendMessage("you have permit :D");
                                return;
                            }
                        } else {
                            p.sendMessage("Must have permit!");
                            e.setCancelled(true);
                        }
                        e.setCancelled(true);
                    }
                }
            }
        }
    Do you mean like this?
    Code (Java):

        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e) {
            Player p = e.getPlayer();
            for (ItemStack s : p.getInventory().getContents()) {
                if (s != null) {
                    if (s.getItemMeta().getDisplayName() != null) {
                        if (s.getItemMeta().getDisplayName().contains("Building")) {
                            if (p.getInventory().contains(s)) {
                                p.sendMessage("you have permit :D");
                                return;
                            }
                        }
                        p.sendMessage("hi");
                        e.setCancelled(true);
                    }
                }
            }
     
  9. The setCancelled function should be out of for loop, you would not want to cancel an event while you are still iterating through objects

    I am on mobile hope I didn't mess up the brackets. Try this:

    https://hastebin.com/wihitocico.java
     
  10. Ah yes, I forgot that I had that for loop there, so the return; now cancels the for loop, fixed it. Thank you :)
     
  11. Glad it worked! Good luck with everything ;)
     
    • Like Like x 1
  12. Thank you!
     
    • Like Like x 1