1.12.2 Check if item has a color coded name.

Discussion in 'Spigot Plugin Development' started by SoulEmiya, Feb 4, 2020.

Thread Status:
Not open for further replies.
  1. What I'm trying to accomplish is that my plugin gives multiple spells to a item with a specific name (in this case it's "The Flower of Life"). But at the moment the plugin gives it to all items that get renamed to the correct name. What I want to do is that the plugin only give the spells to a item with a specific name and with a specific color code.

    This is the code that gives me the display name of the item.
    Code (Java):
    public void setSpell(final String name, final int id) {
            String[] list;
            if (this.hasSpell(name)) {
                final String spell = this.currentspell.get(name);
                list = this.getWandList(spell);
            }
            else {
                @SuppressWarnings("deprecation")
                final String itemname = Bukkit.getPlayer(name).getItemInHand().getItemMeta().getDisplayName();
                @SuppressWarnings("deprecation")
                final ItemStack itemtype = Bukkit.getPlayer(name).getItemInHand();
                list = this.getItemWandList(itemname, itemtype);
            }
            if (list == null) {
                return;
            }
            this.currentspell.put(name, list[id]);
        }
    And this code is what give the item with the correct name its spells.

    Code (Java):
    private String[] getItemWandList(final String itemname, final ItemStack itemtype) {
            if (itemname.equalsIgnoreCase("The Flower of Life")) {
                return Spells.helwand;
            }
            return null;
        }
    My question now is: How do I make it so that only a item with the correct name AND the correct color coding gets the spells.
     
  2. Color codes are part of the item's name. So instead of comparing it to "The Flower of Life", compare it to the coloured text.
    E.g. if the name was green, compare it like
    Code (Java):
    itemname.equalsIgnoreCase(ChatColor.GREEN + "The Flower of Life")
     
  3. Or stripColors.

    Or better when using Spigot 1.14.4+ set and check a custom value defined with Persistence Data API.
     
  4. I tried this but not only is the plugin not giving the spells to the right item they also become NULL on the one without the color code 5146e4f2b2116102a30c86727d880e56.png
     
  5. print the item name for the item it should be and see what its value is and what you should be comparing against
     
  6. Try to save the names in the config, it will be easy.
    Try this :

    Code (Java):
    itemname.equalsIgnoreCase(ChatColor.GREEN + "The Flower of Life")
    To:

    Code (Java):

    ChatColor.translateAlternateColorCodes('&', getConfig().getString("itemspellname"))
    in the config where

    Itemspellname = "&4The Flower of Life"
     
  7. What exactly is the item's display name? What did you set it to in the plugin?

    There may have been some overcomplications on the way, because if you're comparing the right string, it can't be different
     
  8. I use this to get the wand:

    Code (Java):
    public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("Only players may execute this command");
                return true;
            }
            final Player p = (Player)sender;
            if (p.hasPermission("helwand.get")) {
                final ItemStack item = new ItemStack(Material.DOUBLE_PLANT);
                final ItemMeta im = item.getItemMeta();
                im.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&6The Flower of Life"));
                final List<String> lore = new ArrayList<String>();
                lore.add("This flowers holds an unknown power");
                im.setLore(lore);
                item.setItemMeta(im);
                p.getInventory().addItem(new ItemStack[] { item });
                p.sendMessage(ChatColor.YELLOW + "You have received the Flower of Life!");
                return true;
            }
            p.sendMessage("You do not have the correct permission this use this command");
            return false;
        }
     
  9. Hm. Perhaps you could try to do some debugging prints.

    Print the display name of the item when you're comparing it. See if maybe there was an unintended problem somewhere
     
  10. Just saying, if the item does not have a displayname specified the getDisplayName will return null.
     
    • Agree Agree x 1
  11. It seems to have the correct name.
    [​IMG]

    Also when I add a second item to the command with a setDisplayName(item1.getDisplayName) it also gives me a item with the exact same name and coloring.
     
  12. Tried this but then I can still only use the one without color coding. This is probably because of the fact that I replaced the whole
    Code (Java):
    itemname.equalsIgnoreCase(ChatColor.GREEN + "The Flower of Life")
    with

    Code (Java):

    ChatColor.translateAlternateColorCodes('&', getConfig().getString("itemspellname"))
    Which forces me to add != null behind it cause of it being an if statement.

    Altho I also tried to do this:

    Code (Java):
    if (itemname.equalsIgnoreCase(ChatColor.translateAlternateColorCodes('&', WP.getConfig().getString("itemwandname")))) {
                return Spells.helwand;
            }
    Which also does not work.
     
  13. You could also try fetching a substring from the index 2 on, which should exclude the leading symbols.
     
  14. Strahan

    Benefactor

    Ugh please no, that's hacky as hell.

    This really shouldn't be that hard, unless I'm misunderstanding what you are trying to do. I took your flower creating code and threw it into my test plugin as "/cmd2" and made "/cmd1" check if it's the right thing.

    Code (Text):
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
      if (!(sender instanceof Player)) {
        sender.sendMessage("Only players may execute this command");
        return true;
      }
      final Player p = (Player)sender;
      final String itemDN = ChatColor.GOLD + "The Flower of Life";

      switch (cmd.getName().toLowerCase()) {
      case "cmd1":
        ItemStack inHand = p.getInventory().getItemInHand();
        p.sendMessage("You are " + ((isValidObject(itemDN, inHand))?"":"not ") + "holding the nifty flower thing");
        break;
     
      case "cmd2":
        final ItemStack item = new ItemStack(Material.DOUBLE_PLANT);
        final ItemMeta im = item.getItemMeta();

        im.setDisplayName(itemDN);
        im.setLore(new ArrayList<String>(Arrays.asList("This flowers holds an unknown power")));
        item.setItemMeta(im);

        p.getInventory().addItem(item);
        p.sendMessage(ChatColor.YELLOW + "You have received the Flower of Life!");
        break;
      }
      return true;
    }

    private boolean isValidObject(String sourceName, ItemStack itemToCheck) {
      if (itemToCheck == null || !itemToCheck.hasItemMeta()) return false;
     
      ItemMeta inHandMeta = itemToCheck.getItemMeta();
      if (!inHandMeta.hasDisplayName()) return false;
      if (!inHandMeta.getDisplayName().equals(sourceName)) return false;

      return true;  
    }
    I also changed the weird way you added the item to the player's inventory and compressed your lore setting. So when I am on my server, if I do /cmd1 with anything other than your flower, it tells me I don't have it. If I do hold it, it recognizes that. Fairly simple. If you are hardcoding the colored string, and only use one color, using translate alt is kinda overkill IMO. Though personally, I'd never hardcode it in the first place, I'd use config.
     
    • Agree Agree x 2
  15. This is almost exactly what I was looking for. The only thing that is missing here is that when it's the correct item it needs to return a Array of Strings which contains the spells. But this is something that I will find out on my own seeing as my question has been answerd.

    If you have any tips on what the best way to do this would be then feel free to reply to his message.

    Also since my question has been solved do I put this post on Solved or am I unable to get any notifications from this post after I do that? (First time posting something on the Spigot forms)
     
Thread Status:
Not open for further replies.