Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

prismarine-chat

PrismarineJS36.6kMIT1.11.0TypeScript support: included

Wrapper for a minecraft chat message

prismarine, chat

readme

prismarine-chat

NPM version Build Status Discord Gitter Irc Try it on gitpod

A parser for a minecraft chat message

Usage

const registry = require('prismarine-registry')('1.16')
const ChatMessage = require('prismarine-chat')(registry)

const msg = new ChatMessage({"text":"Example chat mesasge"})
console.log(msg.toString()) // Example chat message

API

ChatMessage(message,[displayWarning])

  • message - Can be either text or a minecraft chat JSON object
  • displayWarning - Display warnings if true, default to false

chat.toString([lang])

Flattens the message in to plain-text

  • lang - (optional) - Set a custom lang (defaults to registry.language)

chat.toMotd([lang], parent)

Converts to motd format

  • lang - (optional) - Set a custom lang (defaults to registry.language)
  • parent - Set a custom lang (defaults to mcData.language)

chat.getText(idx, [lang])

Returns a text part from the message

  • idx - Index of the part
  • lang - (optional) - Set a custom lang (defaults to registry.language)

chat.toAnsi([lang], [codes])

Converts to ansi format

  • lang - (optional) - Set a custom lang (defaults to registry.language)
  • codes - (optional) - Specify which ANSI formatting codes should be used for each Minecraft color code

chat.toHTML([lang], [codes], [allowedFormats])

Converts to escaped HTML

  • lang - (optional) - Set a custom lang (defaults to registry.language)
  • codes - (optional) - Specify which CSS style props should be used for each Minecraft color code
  • allowedFormats - The set of allowed formats. Default is ['color', 'bold', 'strikethrough', 'underlined', 'italic']

chat.length()

Returns the count of text extras and child ChatMessages Does not count recursively in to the children

chat.append(ChatMessage)

Appends another ChatMessage or a string

chat.clone()

Returns a clone of the ChatMessage

const registry = require('prismarine-registry')('1.16')
const { MessageBuilder } = require('prismarine-chat')(registry)

const msg = new MessageBuilder().setText('Example chat mesasge')
console.log(JSON.stringify(msg)) // The string as a message component

static ChatMessage.fromNotch(msg)

Returns a prismarine-chat representation of the message recieved from the 'chat' packet, example shown here

static ChatMessage.fromNetwork(messageType, messageParameters)

(1.19+) Loads a chat message sent by server that needs to be formatted on client side.

MessageBuilder()

setBold (val: boolean) : this

setItalic (val: boolean) : this

setUnderlined (val: boolean) : this

setStrikethrough (val: boolean) : this

setObfuscated (val: boolean) : this

setColor (val: string) : this

setText (val: string) : this

check code for examples (and explanations) from here on

setFont (val: string) : this

setTranslate (val: string) : this

setInsertion (val: string) : this

setKeybind (val: string) : this

setScore (name: string, objective: string) : this

setClickEvent (action: string, value: object) : this

setHoverEvent (action: string, data: object, type?: 'contents'|'value') : this

addExtra (...val: MessageBuilder | string) : this

addWith (...val: MessageBuilder | string) : this

resetFormatting () : void

sets every one of the formatting options to false and sets text to reset type

toJSON () : object

builder.toJSON() is the same thing as JSON.stringify(builder)

toString () : string

runs JSON.stringify() on this

static MessageBuilder.fromString(str, {colorSeparator = '&'}) : MessageBuilder

convert string with color codes like &4Hello&cWorld to a MessageBuilder object

processNbtMessage

This method is internally called by fromNotch.

mcpc 1.20.3 uses NBT instead of JSON in some places to store chat, so the schema is a bit different. processNbtMessage normalizes the JS object obtained from nbt derealization to the old JSON schema, so it can be used to instantiate a ChatMessage.

changelog

History

1.11.0

1.10.1

1.10.0

1.9.1

1.9.0

1.8.0

  • Better translation handling (@ChipmunkMC)
  • Allow specifying which ANSI formatting codes to use (@GhqstMC)
  • Added a missing type in the loader (@Jarco-dev)

1.7.2

  • Fix broken type definitions (@IceTank) #81

1.7.1

  • Fix release

1.7.0

  • Support 1.19 client side message formatting (@extremeheat)
  • Fix motd color codes bleeding through their with block (@U9g)

1.6.1

  • Update mcdata

1.6.0

  • Added number type to getText()'s idx argument
  • Remove resets on empty strings

1.5.0

  • allow ChatMessage constructor to accept strings with legacy color codes (§) be converted to new json

1.4.1

  • Remake ChatMessage#clone (@u9g)

1.4.0

  • Add a example (@u9g)
  • Add hex color code support (@U5B)
  • use rest arg to allow many withs or extras (@u9g)
  • Add missing json attribute to typescript defintions (@Paulomart & @u9g)

1.3.3

  • fix typings

1.3.2

  • Properly export loader function and export ChatMessage & MessageBuilder as types

1.3.1

  • export ChatMessage object in typings

1.3.0

  • add typings (@Gjum)

1.2.0

  • Added fromNotch() (@u9g)
  • Add support for array chat messages (@mat-1)

1.1.0

  • Added MessageBuilder (@u9g)

1.0.4

  • Added clone() and append() (@builder-247)
  • update mojangson to 2.0.0 (@u9g)
  • Add trailing color reset to .toAnsi() (@DrMoraschi)

1.0.3

  • by default hide warnings

1.0.2

  • ignore mojangson parsing error

1.0.1

  • make lang option in toAnsi optional

1.0.0

  • initial implementation
  • extracted from mineflayer