Skip to content

Create Formatted Text

The text package can be used to create formatted text, including coloured text, hoverable and clickable text, and much more.

Should I Use console Instead?

You can use console and a MOTD creator to create formatted text, here is a comparison between features.

consoletext
Colours, bold, underline, italic, strikethrough, obfuscated.Everything available in console + hover and clickable text.
Simple and compact, less readable.Better readability, suitable for more complicated messages.

Add Dependencies

Include text as dependency in package.json.

js
// package.json
{
  // ...
  dependencies: {
    "text": "0.1.0",
    // ...
  }
}

If text is not installed, you can run /pully install to install any missing dependencies.

Creating Formatted Text

text.createText creates a Text object, a basic string is a valid text.

js
let text = require("text");

let myMsg = text.createText("Hello JSCore!");
text.sendText(myMsg);

Using Styles

You can add styles to your text with style attributes.

js
let text = require("text");

let myMsg = text.createText({
  content: "Hello JSCore!",
  color: "#ff9900",
  italic: true,
  bold: true,
});
text.sendText(myMsg);

All available style attributes are:

Style attributeDescriptionType
boldThick text?Boolean
italicSlanted text?Boolean
obfuscatedCannot see text?Boolean
underlinedLine underneath?Boolean
colorWhat colour?Hex code, integer (of hex number), TextColor or Formatting
shadowContrast of shadowInteger, can be negative for negative contrast
fontFont to useString or Identifier

text.sendText takes anything that can be converted to a Text object as argument. The code below sends the Text directly.

js
let text = require("text");

text.sendText("Hello JSCore!");

text.sendText({
  content: "Hello JSCore!",
  color: "#ff9900",
  italic: true,
  bold: true,
});

Hoverable Text

You can show a hint when a text is hovered.

js
let text = require("text");

text.sendText({
  content: "Hello JSCore!",
  color: "#ff9900",
  italic: true,
  bold: true,
  hover: "I said hello!",
});

Hover can takes anything that can be converted to a Text.

js
let text = require("text");

text.sendText({
  content: "Hello JSCore!",
  color: "#ff9900",
  italic: true,
  bold: true,
  hover: {
    content: "Red hover text!",
    underlined: true,
    color: "#ff2222",
  },
});

A hover event can also be an item or an entity, you can specify the type of thing to show.

js
let text = require("text");

text.sendText({
  content: "Hello JSCore (text)!",
  hover: {
    text: "Hoi!", // or anything that can be converted to Text
  },
});

text.sendText({
  content: "Hello JSCore (item)!",
  hover: {
    item: /* ItemStack */,
  },
});

text.sendText({
  content: "Hello JSCore (entity)!",
  hover: {
    entity: /* EntityContent */,
  },
});

Clickable Text

Do varioius things when the text is clicked.

Run Command

js
let text = require("text");

text.sendText({
  content: "Restart JSCore!",
  click: {
    run: "jscore restart",
  },
});

// shorthand: starts with a `/`
text.sendText({
  content: "Restart JSCore!",
  click: "/jscore restart",
});

Suggest Text

js
text.sendText({
  content: "Restart JSCore?",
  click: {
    // can also be a chat message if doesn't start with `/`
    suggest: "/jscore restart",
  },
});

Open URL

js
text.sendText({
  content: "Join Discord",
  click: {
    url: "https://discord.gg/XfSZ5tc7Sk",
  },
});

// shorthand: starts with `https://` or `http://`
text.sendText({
  content: "Join Discord",
  click: "https://discord.gg/XfSZ5tc7Sk",
});

Open File

js
text.sendText({
  content: "Open JSCore folder",
  click: {
    file: "config/jscore",
  },
});

Multi-component Text

A Text can have multiple chunks of different styles, we can create this by passing an array into text.sendText.

js
let text = require("text");

let clickableText = text.createText([
  {
    content: "[A]",
    click: "https://example.com",
  },
  " ",
  {
    content: "[B]",
    click: "/jscore snapshot create",
  },
]);

text.sendText([
  "Unstyle text\n",
  {
    content: "Orange text",
    color: "#ff9900",
    italic: true,
    bold: true,
  },
  " ",
  clickableText,
]);

Example: Private Message Button

This example assumes you have read the section on listening to events.

Add listener and fabric-api-events as dependencies.

js
let text = require("text");

// receivedMessage is of type net.minecraft.text.Text
addEventListener("clientModifyReceiveGameMessageEvent", (receivedMessage) => {
  // we need to check if a message is a player message
  // which has format `<playerName> player message`
  let textContent = text.getString(receivedMessage);
  let firstWord = textContent.split(" ")[0];

  // do nothing to the message if it is not a player message
  if (!firstWord.startsWith("<") || !firstWord.endsWith(">")) return;

  let playerName = firstWord.slice(1, -1);

  // Add a clickable text `[M]` after the original message
  return text.createText([
    receivedMessage,
    " ",
    {
      content: "[M]",
      color: "#fab387",
      hover: `Reply to ${playerName}`,
      click: {
        suggest: `/msg ${playerName} `,
      },
    },
  ]);
});

Delete Existing Text

You can remove any text you have created using the cancel() function.

js
let text = require("text");

let sentText = text.sendText("This text will disappear.");
sentText.remove();

Note that this would leave a this message has been removed by the server message in its place. This could be useful if you want to collapse a multiline message, or remove outdated output.