1.16.5 Checking the item name in players inventory

Discussion in 'Spigot Plugin Development' started by MoonNetwork, Jun 14, 2021.

  1. Hi again guys. I am trying to make workshop ya know. And players must have some ingredients to craft a item. I wrote a code line for it but it didnt work. I mean when I add the name check part it stop working. There are no errors on eclipe and console. Also nothing happens when I click to the item in the gui. This is the code line.

    Code (Text):

    if (clicked != null && clicked.hasItemMeta() && clicked.getItemMeta().hasDisplayName()
                && clicked.getItemMeta().getDisplayName().equalsIgnoreCase(Utils.chat("&a&lİşle &fEnoryum Botları"))) {
            ItemStack item = new ItemStack(Material.PRISMARINE_SHARD);
            if (!(wait.containsKey(p.getUniqueId())) && p.getInventory().containsAtLeast(item, 45) && item.hasItemMeta() && item.getItemMeta().hasDisplayName() && item.getItemMeta().getDisplayName().equalsIgnoreCase(Utils.chat("&bEnoryum"))) {
          ...
     
  2. Maybe do some System.out.println(); testing or strip color codes to make sure those aren't getting in your way. To test this I'd print the display name so then you can see why it isn't matching.
     
  3. Oh. I will use chatcolor wait. I thought they are same but I will try it.
     
  4. I might be wrong but ive seen it before where the symbol § is used for a color code instead of & try checking for the first symbol shown instead,
    I have no clue what Utils.chat is for so that may fix the above problem but im not sure, Alternatively if you want to keep the & symbol you could try:

    Code (Java):
    clicked.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.translateAlternateColorCodes('&', "&a&lİşle &fEnoryum Botları"))
    Edit: The below method works aswell and looks more tidy than the above
     
    #4 ItsUhFinn, Jun 14, 2021
    Last edited: Jun 14, 2021
  5. I think you're mistaken, I just mean strip the chat colors from both strings. So you'd do:
    Code (Java):
    ChatColor.stripColor(clicked.getItemMeta().getDisplayName()).equalsIgnoreCase("İşle Enoryum Botları");
    which basically just gets rid of all the colors so you can just check with the plain text.
     
  6. Ahh sorry sorry. Also I am translating the § to & in Utils class
    Code (Text):

    public static String chat(String s) {
            return ChatColor.translateAlternateColorCodes('&', s);
        }
     
  7. Still same. The problem is not the color code issue. I tought I am checking the items name wrong.
     
  8. Maybe throw in some System.out.println()'s after the if statements to properly locate exactly which line is the issue
     
  9. This is the line. When I check the items name nothing happens.

    Code (Text):

            if (!(wait.containsKey(p.getUniqueId())) && p.getInventory().containsAtLeast(item, 45) && item.hasItemMeta() && item.getItemMeta().hasDisplayName() && item.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.AQUA + "Enoryum"))
     
  10. Oh I see the issue here,
    Essentially you are creating a new itemstack of prismarine shard with no extra data and then straight after you are checking if the display name is ChatColor.AQUA + "Enoryum", it cant physically have that name unless you havent shown us some of the lines

    No wonder it cant get through the if gate
     
  11. But how can I create the item stack with the name? If you want to check this is the full line code.
    https://paste.md-5.net/jopilesumi.java
     
  12. Code (Java):
    ItemStack item = new ItemStack(Material.PRISMARINE_SHARD);
    ItemMeta im = item.getItemMeta();
    im.setDisplayName("NAME");
    //Any other ItemMeta values you want eg lore
    item.setItemMeta(im);
    Thats just an example and I wrote it on my phone so sorry if there are any spelling mistakes
     
  13. Oh wow, I don't know how I overlooked that! You'll also want to add that item to the player's inventory after then you should be good @MoonNetwork