Solved Invisible Lore

Discussion in 'Plugin Development' started by khave, Aug 28, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    khave

    Hello Bukkit.

    I'm having some trouble decoding an invisible String. I make the String invisible by adding the ChatColor COLOR_CHAR before each letter. I then decode it by replacing all the characters § with nothing. This works for the EntityDamageByEntityEvent when hitting someone, but does not work when trying to get an ItemStacks lore and then trying to decode it. On the last line, I set the decoded name (and this obviously works on the EntityDamageByEntityEvent), but when I try to get it from an itemstack by doing so:
    Code:
    public static MoreItemsItem castToMoreItem(ItemStack itemStack, Player player){
            if(itemStack != null && itemStack.getItemMeta() != null && itemStack.getItemMeta().hasLore()) {
                String itemName = itemStack.getItemMeta().getLore().get(itemStack.getItemMeta().getLore().size() - 1)
    .replaceAll("§", "");
    
                player.sendMessage(itemName);
                ItemManager itemManager = new ItemManager(itemName);
                if(itemManager.exists()) {//This just checks if the item is found in the config.
                    return new MoreItemsItem(itemName);
                }
            }
            return null;
        }
    It does not seem to work. As you can see, I've tried debugging and the line returns empty. I have checked if it's the correct line and it is..
    If I try to print it to the console it prints a lot of weird characters.

    Thanks in advance for helping me.
     
  2. Offline

    LordDarthBob

    @khave
    Try using .replace('§', ''); instead of .replaceAll
     
  3. Offline

    khave

    No.. The String is still empty.
     
  4. @khave
    Show your "encode" code
     
  5. Offline

    khave

    I don't believe it will help, as it works with when damaging entities, but here you go:

    Code:
    public String convertToInvisibleString(String s) {
            String hidden = "";
            for (char c : s.toCharArray()) hidden += ChatColor.COLOR_CHAR+""+c;
            return hidden;
        }
     
  6. Offline

    au2001

    @khave Use String#replace(ChatColor.COLOR_CHAR + "", "")

    And if your text contains characters from "g" to "z" or capital letters, they'll be visible.
    Maybe you should use something like a "key" that you save in the config only containing 0-9 and a-f characters...
     
    khave likes this.
  7. Offline

    khave

    Thank you very much! I do save only a key in the lore and then I have all variables in a config.
     
Thread Status:
Not open for further replies.

Share This Page