UTF characters not working in titles for me.

Discussion in 'Spigot Plugin Development' started by avighnash, May 23, 2016.

  1. Hello! I have this snippet of code:

    Code (Text):
    package us.universalpvp.te.listeners;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;

    import net.md_5.bungee.api.ChatColor;
    import us.universalpvp.te.utils.TitleManager;

    public class TitleListener implements Listener {

        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            TitleManager tm = new TitleManager();

            tm.sendTimedTitleAndSubtitle(p, ChatColor.translateAlternateColorCodes('&', "  GRAND OPENING! "),
                    ChatColor.translateAlternateColorCodes('&', "Grand opening soon! &c&l⇉  &b&lplay.universalpvp.us"), 10,
                    60, 10);

        }

    }
     
    and as you can see, I have two in my string. Well, when I go on the server, the doesn't show up. Why is that?
     
  2. Because Unicode is locale specific. Instead of pasting Unicode directly into your code you need to escape those characters. This is the exact same reason why pasting the section symbol Unicode directly into code is bad. ;)
     
  3. Wait here is the symbol:

    Why does it not show up?
    \
     
  4. Then what should I do to make it so I can see the character?
     
  5. Escape characters. They're prefixed with "\u" then numbers that follow. There's actually a converter which converts a Unicode character you have into a formatted escape character; you can check that out here: https://www.branah.com/unicode-converter
    Just use that formatted character (ie. "/u0061" except the forward slash is a backward slash) and add it to your string.
     
  6. Okay this is my new class"

    Code (Text):
    package us.universalpvp.te.listeners;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;

    import net.md_5.bungee.api.ChatColor;
    import us.universalpvp.te.utils.TitleManager;

    public class TitleListener implements Listener {

        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            TitleManager tm = new TitleManager();

            tm.sendTimedTitleAndSubtitle(p, ChatColor.translateAlternateColorCodes('&', "\ud83c\udf1f GRAND OPENING! \ud83c\udf1f"),
                    ChatColor.translateAlternateColorCodes('&', "Grand opening soon! &c&l⇉  &b&lplay.universalpvp.us"), 10,
                    60, 10);

        }

    }
     
    and the \ud83c\udf1f was from what I saw in the utf thing you showed me, but it still shows blank.
     
  7. Why are you translating color codes if there are none to be found in the first string? Try removing that and see if it works.
     
  8. Code (Text):
     public void ModifyAllowedCharacters() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException
    {
      Field field = SharedConstants.class.getDeclaredField("allowedCharacters");
      field.setAccessible(true);
      Field modifiersField = Field.class.getDeclaredField( "modifiers" );
      modifiersField.setAccessible( true );
      modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
      String oldallowedchars = (String)field.get(null);
      String suits = "\u2665\u2666\u2663\u2660";
      StringBuilder sb = new StringBuilder();
      sb.append( oldallowedchars );
      sb.append( suits );
      field.set( null, sb.toString() );
    }
    Found that off Bukkit :p
     
  9. Still no work.
     
  10. Provide the source link and credit the author.
     
    • Agree Agree x 1
  11. What is hat supposed to do? Lol
     
  12. Whenever I use Unicode characters I define them as constant char variables:
    Code (Text):

    public static final char HEART = '\u2665';

    public void something(Player player)
    {
         player.sendMessage("You da best yo! " + HEART);
    }

     
    Try this and see if it works.
     
  13. Well, there is the first error:
    ClassCode:
    Code (Text):
    package us.universalpvp.te.listeners;

    import java.lang.reflect.Field;
    import java.lang.reflect.Modifier;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;

    import net.md_5.bungee.api.ChatColor;
    import net.minecraft.server.v1_8_R3.SharedConstants;
    import us.universalpvp.te.utils.TitleManager;

    public class TitleListener implements Listener {

        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            TitleManager tm = new TitleManager();
            try {
                this.ModifyAllowedCharacters();
            } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e1) {
                e1.printStackTrace();
            }

            tm.sendTimedTitleAndSubtitle(p, "  GRAND OPENING! ",
                    ChatColor.translateAlternateColorCodes('&', "Grand opening soon! &c&l⇉  &b&lplay.universalpvp.us"), 10,
                    60, 10);
            //
        }

        public void ModifyAllowedCharacters()
                throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
            Field field = SharedConstants.class.getDeclaredField("allowedCharacters");
            field.setAccessible(true);
            Field modifiersField = Field.class.getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
            String oldallowedchars = (String) field.get(null);
            String suits = "\ud83c\udf1f";
            StringBuilder sb = new StringBuilder();
            sb.append(oldallowedchars);
            sb.append(suits);
            field.set(null, sb.toString());
        }

    }
     
    error:

    Code (Text):
    [16:36:06 ERROR]: Could not pass event PlayerJoinEvent to CustomTabList v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerList.onPlayerJoin(PlayerList.java:298) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerList.a(PlayerList.java:157) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.LoginListener.b(LoginListener.java:144) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.LoginListener.c(LoginListener.java:54) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.NetworkManager.a(NetworkManager.java:231) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.ServerConnection.c(ServerConnection.java:148) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:814) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_77]
    Caused by: java.lang.ClassCastException: [C cannot be cast to java.lang.String
            at us.universalpvp.te.listeners.TitleListener.ModifyAllowedCharacters(TitleListener.java:40) ~[?:?]
            at us.universalpvp.te.listeners.TitleListener.onJoin(TitleListener.java:22) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_77]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_77]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_77]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_77]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            ... 14 more
    and it is pointed towards this line:

    Code (Text):
            String oldallowedchars = (String) field.get(null);
     
     
  14. But for me, there is the red line thingy, and when I hover over it, it says Invalid character constant.
     
  15. Manually type it in; there shouldn't be any syntax issues. Also, ensure your artifacts are compiling with UTF-8.
     
  16. I manually typed it in, doesn't work. Still underlined.
     
  17. What should I do now?
     
  18. Fix your server's charset encoding to utf8 and then you can use escaped utf8 Unicode characters... this isn't rocket science.. None of that extra code posted above is needed.