Search code examples
formattingtitlebukkitinventory

Bukkit.invetory removes unnecessary chatcolor formatiing 1.13.2


Dear StackOverflow community,

Im stuck at this problem for like a few months now, I have searched alot on the internet but I can't see someone that has the same problem as me.

So basically when I create a inventory with Bukkit.createInventory(player, slots, title), the title of the inventory with e.getView().getTitle() (InventoryClickEvent) is not the same as the entered title when I create the inventory.

The title entered when creating the inventory is for example §2§5§r§3§lInventory§n§7§n§4 and ends up being §3§lInventory§4 after I get the title with e.getView().getTitle(). Watch how some formatting codes get deleted.

So the real problem is that the inventory title does not equal to the title it should be, it seems like the unnecessary formatting codes get removed when returning the title from the created inventory. This problem only occurs in minecraft servers running 1.13.2.

I already tried to create the inventory with plugin.getServer().createInventory(player, slots, title) with no succes. I also tried to get the title with e.getClickedInventory().getTitle() instead of e.getView().getTitle() with no succes either. But when I remove all formatting codes from the title it works.

I hope anyone can help me with this and can explain why this happens.

Thanks


Solution

  • Problem:

    It's because the scoreboard title isn't stored as a String anymore...

    Minecraft 1.8 uses:

        public String getName() {
            return this.getInventory().getName();
        }
    

    and Minecraft 1.13.2 uses:

        public String getName() {
            return CraftChatMessage.fromComponent(this.getInventory().getDisplayName());
        }
    

    The title is now basically being reformatted and the method fromComponent(...) seems to be dropping all unnecessary formattings unfortunately.

    Solution:

    Since I don't really know what exactly you want to do with the title, I can just suggest you remove the colour codes when comparing the title or whatever you need it for.

    String title = ChatColor.stripColor(e.getView().getTitle());
    

    If you really need the colour codes, you could store them in a HashMap<Player, String> titles ore similar.