minecraft-protocol
TypeScript icon, indicating that this package has built-in type declarations

1.51.0 • Public • Published

minecraft protocol

NPM version Build Status Discord Gitter Irc

Try it on gitpod

Parse and serialize minecraft packets, plus authentication and encryption.

Features

  • Supports Minecraft PC version 1.7.10, 1.8.8, 1.9 (15w40b, 1.9, 1.9.1-pre2, 1.9.2, 1.9.4), 1.10 (16w20a, 1.10-pre1, 1.10, 1.10.1, 1.10.2), 1.11 (16w35a, 1.11, 1.11.2), 1.12 (17w15a, 17w18b, 1.12-pre4, 1.12, 1.12.1, 1.12.2), and 1.13 (17w50a, 1.13, 1.13.1, 1.13.2-pre1, 1.13.2-pre2, 1.13.2), 1.14 (1.14, 1.14.1, 1.14.3, 1.14.4) , 1.15 (1.15, 1.15.1, 1.15.2) and 1.16 (20w13b, 20w14a, 1.16-rc1, 1.16, 1.16.1, 1.16.2, 1.16.3, 1.16.4, 1.16.5), 1.17 (21w07a, 1.17, 1.17.1), 1.18 (1.18, 1.18.1 and 1.18.2), 1.19 (1.19, 1.19.1, 1.19.2, 1.19.3, 1.19.4), 1.20 (1.20, 1.20.1, 1.20.2, 1.20.3, 1.20.4, 1.20.5, 1.20.6), 1.21 (1.21, 1.21.1, 1.21.3)
  • Parses all packets and emits events with packet fields as JavaScript objects.
  • Send a packet by supplying fields as a JavaScript object.
  • Client
    • Authenticating and logging in
    • Encryption
    • Compression
    • Both online and offline mode
    • Respond to keep-alive packets
    • Follow DNS service records (SRV)
    • Ping a server for status
  • Server
    • Online/Offline mode
    • Encryption
    • Compression
    • Handshake
    • Keep-alive checking
    • Ping status
  • Robust test coverage.
  • Optimized for rapidly staying up to date with Minecraft protocol updates.

Want to contribute on something important for PrismarineJS ? go to https://github.com/PrismarineJS/mineflayer/wiki/Big-Prismarine-projects

Third Party Plugins

node-minecraft-protocol is pluggable.

Projects Using node-minecraft-protocol

  • mineflayer - Create minecraft bots with a stable, high level API.
  • mcserve - Runs and monitors your minecraft server, provides real-time web interface, allow your users to create bots.
  • flying-squid - Create minecraft servers with a high level API, also a minecraft server by itself.
  • pakkit - A GUI tool to monitor Minecraft packets in real time, allowing you to view their data and interactively edit and resend them.
  • minecraft-packet-debugger - A tool to capture Minecraft packets in a buffer then view them in a browser.
  • aresrpg - An open-source mmorpg minecraft server.
  • SteveProxy - Proxy for Minecraft with the ability to change the gameplay using plugins.
  • and several thousands others

Installation

npm install minecraft-protocol

Documentation

Usage

Echo client example

const mc = require('minecraft-protocol');
const client = mc.createClient({
  host: "localhost",   // optional
  port: 25565,                 // set if you need a port that isn't 25565
  username: 'Bot',             // username to join as if auth is `offline`, else a unique identifier for this account. Switch if you want to change accounts
  // version: false,           // only set if you need a specific version or snapshot (ie: "1.8.9" or "1.16.5"), otherwise it's set automatically
  // password: '12345678'      // set if you want to use password-based auth (may be unreliable). If specified, the `username` must be an email
});

client.on('playerChat', function (ev) {
  // Listen for chat messages and echo them back.
  const content = ev.formattedMessage
    ? JSON.parse(ev.formattedMessage)
    : ev.unsignedChat
      ? JSON.parse(ev.unsignedContent)
      : ev.plainMessage
  const jsonMsg = JSON.parse(packet.message)
  if (ev.senderName === client.username) return
  client.chat(JSON.stringify(content))
});

Set auth to offline if the server is in offline mode. If auth is set to microsoft, you will be prompted to login to microsoft.com with a code in your browser. After signing in on your browser, the client will automatically obtain and cache authentication tokens (under your specified username) so you don't have to sign-in again.

To switch the account, update the supplied username. By default, cached tokens will be stored in your user's .minecraft folder, or if profilesFolder is specified, they'll instead be stored there. For more information on bot options see the API doc.

Note: SRV records will only be looked up if the port is unspecified or set to 25565 and if the host is a valid non-local domain name.

Client example joining a Realm

Example to connect to a Realm that the authenticating account is owner of or has been invited to:

const mc = require('minecraft-protocol');
const client = mc.createClient({
  realms: {
    pickRealm: (realms) => realms[0] // Function which recieves an array of joined/owned Realms and must return a single Realm. Can be async
  },
  auth: 'microsoft'
})

Hello World server example

For a more up to date example, see examples/server/server.js.

const mc = require('minecraft-protocol')
const nbt = require('prismarine-nbt')
const server = mc.createServer({
  'online-mode': true,   // optional
  encryption: true,      // optional
  host: '0.0.0.0',       // optional
  port: 25565,           // optional
  version: '1.18'
})
const mcData = require('minecraft-data')(server.version)

function chatText (text) {
  return mcData.supportFeature('chatPacketsUseNbtComponents')
    ? nbt.comp({ text: nbt.string(text) })
    : JSON.stringify({ text })
}

server.on('playerJoin', function(client) {
  const loginPacket = mcData.loginPacket

  client.write('login', {
    ...loginPacket,
    enforceSecureChat: false,
    entityId: client.id,
    hashedSeed: [0, 0],
    maxPlayers: server.maxPlayers,
    viewDistance: 10,
    reducedDebugInfo: false,
    enableRespawnScreen: true,
    isDebug: false,
    isFlat: false
  })

  client.write('position', {
    x: 0,
    y: 255,
    z: 0,
    yaw: 0,
    pitch: 0,
    flags: 0x00
  })

  const message = {
    translate: 'chat.type.announcement',
    with: [
      'Server',
      'Hello, world!'
    ]
  }
  if (mcData.supportFeature('signedChat')) {
    client.write('player_chat', {
      plainMessage: message,
      signedChatContent: '',
      unsignedChatContent: chatText(message),
      type: 0,
      senderUuid: 'd3527a0b-bc03-45d5-a878-2aafdd8c8a43', // random
      senderName: JSON.stringify({ text: 'me' }),
      senderTeam: undefined,
      timestamp: Date.now(),
      salt: 0n,
      signature: mcData.supportFeature('useChatSessions') ? undefined : Buffer.alloc(0),
      previousMessages: [],
      filterType: 0,
      networkName: JSON.stringify({ text: 'me' })
    })
  } else {
    client.write('chat', { message: JSON.stringify({ text: message }), position: 0, sender: 'me' })
  }
})

Testing

  • Ensure your system has the java executable in PATH.
  • MC_SERVER_JAR_DIR=some/path/to/store/minecraft/server/ MC_USERNAME=email@example.com MC_PASSWORD=password npm test

Debugging

You can enable some protocol debugging output using DEBUG environment variable:

DEBUG="minecraft-protocol" node [...]

On Windows:

set DEBUG=minecraft-protocol
node your_script.js

Contribute

Please read https://github.com/PrismarineJS/prismarine-contribute

History

See history

Related

  • node-rcon can be used to access the rcon server in the minecraft server
  • map-colors can be used to convert any image into a buffer of minecraft compatible colors

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.51.04,678latest

Version History

VersionDownloads (Last 7 Days)Published
1.51.04,678
1.50.0273
1.49.012
1.48.00
1.47.0292
1.46.05
1.45.09
1.44.034
1.43.210
1.43.13
1.43.02
1.42.02
1.41.212
1.41.11
1.41.00
1.40.3496
1.40.21
1.40.10
1.40.00
1.39.00
1.38.10
1.38.00
1.37.00
1.36.25
1.36.15
1.36.00
1.35.117
1.35.0269
1.34.06
1.33.01
1.32.20
1.32.11
1.32.01
1.31.01
1.30.05
1.29.11
1.29.03
1.28.11
1.28.03
1.27.24
1.27.10
1.27.00
1.26.535
1.26.40
1.26.30
1.26.20
1.26.10
1.26.00
1.25.022
1.24.21
1.24.10
1.24.00
1.23.33
1.23.28
1.23.141
1.23.09
1.22.0110
1.21.028
1.20.23
1.20.11
1.20.00
1.19.021
1.18.00
1.17.011
1.16.016
1.15.02
1.14.00
1.13.028
1.12.41
1.12.30
1.12.24
1.12.16
1.12.031
1.11.025
1.10.02
1.9.49
1.9.32
1.9.21
1.9.10
1.9.02
1.8.31
1.8.26
1.8.10
1.8.00
1.7.00
1.6.00
1.5.30
1.5.20
1.5.13
1.5.00
1.4.11
1.4.00
1.3.20
1.3.10
1.3.00
1.2.00
1.1.30
1.1.20
1.1.11
1.1.00
1.0.11
1.0.00
0.19.60
0.19.50
0.19.40
0.19.30
0.19.20
0.19.10
0.19.00
0.18.31
0.18.20
0.18.10
0.18.00
0.17.20
0.17.11
0.17.00
0.16.63
0.16.50
0.16.40
0.16.30
0.16.20
0.16.10
0.16.00
0.15.00
0.14.00
0.13.40
0.13.30
0.13.20
0.13.10
0.13.00
0.12.30
0.12.20
0.12.10
0.12.00
0.11.60
0.11.50
0.11.40
0.11.30
0.11.20
0.11.10
0.11.00
0.10.13
0.10.00
0.9.00
0.8.10
0.8.00
0.7.91
0.7.80
0.7.70
0.7.60
0.7.50
0.7.40
0.7.30
0.7.20
0.7.10
0.7.00
0.6.70
0.6.60
0.6.50
0.6.40
0.6.30
0.6.20
0.6.10
0.6.00
0.5.20
0.5.10
0.5.00
0.4.00
0.3.00
0.2.00
0.1.00
0.0.30
0.0.20

Package Sidebar

Install

npm i minecraft-protocol

Weekly Downloads

6,597

Version

1.51.0

License

BSD-3-Clause

Unpacked Size

307 kB

Total Files

111

Last publish

Collaborators

  • rom1504
  • dcbartlett
  • mappum
  • superjoe
  • zuazo
  • wtfaremyinitials
  • roblabla