Chat system with time at the hover has errors.

Discussion in 'Spigot Plugin Development' started by DasPxtrick, Jul 22, 2018.

  1. The message does not look as intended. Namely it looks like this:

    INFORMATION: IOEXEPTION IS MY USERNAME.
    Code (Text):
    IOExeption TextComponent{text=Wow...., BaseComponent(color=, bold=null, italic=null, underlined=null, strikethrough=null, obfuscated=null, extra=null, clickEvent=null, hoverEvent=HoverEvent(action=SHOW_TEXT, value=[TextComponent{text=Time ╗ 02:05:40, BaseComponent(color=, bold=null, italic=null, underlined=null, strikethrough=null, obfuscated=null, extra=null, clickEvent=null, hoverEvent=null)}]))}
    Actually, the time should be above the Hovern.

    Here is the code. I hope someone can help me.
    Code (Java):
    import net.md_5.bungee.api.chat.ComponentBuilder;
    import net.md_5.bungee.api.chat.HoverEvent;
    import net.md_5.bungee.api.chat.TextComponent;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;

    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class ChatListener implements Listener {

        @EventHandler
        public void onChat(AsyncPlayerChatEvent event) {
            Player player = event.getPlayer();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");

            TextComponent textComponent = new TextComponent();
            textComponent.setText(event.getMessage());
            textComponent.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§9§lT§9ime §8» §e" + simpleDateFormat.format(new Date())).create()));

            if(player.hasPermission("system.owner")) {
                event.setFormat("§4Owner §8❘ §4" + player.getDisplayName() + " §8» §7" + textComponent);
            } else {
                event.setFormat("§7" + player.getDisplayName() + " §8» §7" + event.getMessage());
            }
        }
    }
     
     
    #1 DasPxtrick, Jul 22, 2018
    Last edited: Jul 22, 2018
  2. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    You can't add a TextComponent to a String and just expect it to apply it's formatting to the message. You msut send the message to the player via the Player#Spigot#sendMessage() function, not by setting the event join message (which doesn't support formatting like that).
     
  3. Declare your date format as a constant rather than creating a new one on each chat.
     
  4. Could someone maybe provide a code example where I can see exactly how I can put my idea into action?
     
  5. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    No, I told you the issue. You could cancel the join message and send a message with the TextComponent using the Spigot player wrapper.