Solved cant put % in AsyncPlayerChatEvent e.setFormat()

Discussion in 'Spigot Plugin Development' started by IsaacsToes, Apr 16, 2017.

  1. I dont know if there is anyway around this but if i put a % sign inside of the e.setFormat() it stops the code from running and reverts it back to the original way.
    Code (Text):
    package me.isaac.serverplugin.events;

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

    import me.isaac.serverplugin.Main;
    import net.md_5.bungee.api.ChatColor;

    public class LevelInChat implements Listener {

        @EventHandler
        public void onTalk(AsyncPlayerChatEvent e) {
       
            Player player = e.getPlayer();
            int level = Main.playerLevel.get(player.getName());
            int prestige = Main.playerPrestige.get(player.getName());
       
            double leveld = Main.playerLevel.get(player.getName());
            double xpd = Main.playerXp.get(player.getName());
            int a = (int) ((xpd / (leveld * 13)) * 100);
       
            String prefix = "&7[&6Lvl " + level + " &a" + a + " &c" + prestige + "&7] ";
       
            e.setFormat(ChatColor.translateAlternateColorCodes('&', prefix + player.getDisplayName() + ": " + e.getMessage()));
       
        }

    }
     
    Edit: Well i kinda cheated and used the alt code 'Alt 0137', which gives you this... ‰. looks close enough to what i need :p
     
    #1 IsaacsToes, Apr 16, 2017
    Last edited: Apr 16, 2017
    • Agree Agree x 1
  2. Mas

    Mas

    In AsyncPlayerChatEvent#setFormat you need to use the '%' sign as a placeholder.
    The first should be where the player name should go, and the second is where the message should go.

    Eg:
    Code (Java):
    event.setFormat("{" + level +"} %s: %s"
     
    • Informative Informative x 1
    • Like Like x 1
  3. Just a small note: Using the Bungee ChatColor class instead of the regular one from Bukkit, might cause some issues. It has at least for me, quite often. It'll give you an error in the console, no crash I believe but still worth notifying you of.
    Good luck!
     
  4. Just so you know, that's the per-mille symbol, which indicates X out of 1000 instead of 100.
     
  5. Thanks, didnt realize that i was using the bungee chat color :p usually dont, guess i didnt catch it.

    Thanks this is exactly what i needed.

    and as for ItsMas_
    I was only using the % to show to percent of the players level. The % was not needed for any calculations, or anything like that, just needed it to show up, but thanks for letting me know!