[Share] Clickable and Hoverable Chat Messages

Discussion in 'Resources' started by Blackveil, Apr 24, 2014.

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


    I was curious how to do this, and I figured out how to do it myself. So I am sharing my code snippet to those who wish to do this.

    1. import net.minecraft.server.v1_7_R3.ChatClickable;
    2. import net.minecraft.server.v1_7_R3.ChatHoverable;
    3. import net.minecraft.server.v1_7_R3.ChatMessage;
    4. import net.minecraft.server.v1_7_R3.ChatModifier;
    5. import net.minecraft.server.v1_7_R3.EnumClickAction;
    6. import net.minecraft.server.v1_7_R3.EnumHoverAction;
    7. import net.minecraft.server.v1_7_R3.IChatBaseComponent;
    8. import net.minecraft.server.v1_7_R3.MinecraftServer;
    9. import net.minecraft.server.v1_7_R3.PlayerList;

    pname - This is the name of the sender.
    ChatMsg - This is the chat message the player is sending.
    ClickableMsg - This is what the message will send/give whenever clicked.
    HoverMsg - This is the message which displays in the box when you hover over. (Note: You can only set the HoverMsg the "EnumHoverAction" is "EnumHoverAction.SHOW_TEXT"!

    This method is useful for COMMANDS or HINTS. It is easily transferrable to normal chat aswell, but this method will only send a hoverable message to one player.

    1. public static void printHoverable(String pname, String ChatMsg, String ClickableMsg, String HoverMsg) {
    2. IChatBaseComponent base;
    3. base = new ChatMessage(ChatMsg);
    4. base.setChatModifier(new ChatModifier());
    5. base.getChatModifier().setChatClickable(new ChatClickable(EnumClickAction.SUGGEST_COMMAND, ClickableMsg));
    6. base.getChatModifier().a(new ChatHoverable(EnumHoverAction.SHOW_TEXT, new ChatMessage(HoverMsg)));
    7. PlayerList list = MinecraftServer.getServer().getPlayerList();
    8. list.getPlayer(pname).sendMessage(base);
    9. }

  2. Offline



    Why would you ever use MinecraftServer,getServer().getPlayerList() and the PlayerList class. You are basically just importing unecessary NMS code. What you should do is have a Player parameter in the void, and at the end do p.sendMessage(base);
    xTrollxDudex likes this.
  3. Offline


    A better way is to use raw JSON so unnecessary volatile versioned code does not need to be depended on.
    Chlorek likes this.
  4. Offline


    this same code
    For 1.7.5:
    2. ChatModifier modifier = new ChatModifier();
    3. modifier.setChatClickable(new ChatClickable(EnumClickAction.SUGGEST_COMMAND, ClickableMsg));
    4. modifier.a(new ChatHoverable(EnumHoverAction.SHOW_TEXT, new ChatMessage(HoverMsg)));
    5. base.setChatModifier(modifier);
    RentAMonkey likes this.
Thread Status:
Not open for further replies.

Share This Page