2015-04-16 16:07:18 -04:00
# minecraft protocol
2016-02-29 10:31:04 -05:00
[![NPM version ](https://img.shields.io/npm/v/minecraft-protocol.svg )](https://www.npmjs.com/package/minecraft-protocol)
2020-06-07 17:31:42 -04:00
[![Build Status ](https://github.com/PrismarineJS/node-minecraft-protocol/workflows/CI/badge.svg )](https://github.com/PrismarineJS/node-minecraft-protocol/actions?query=workflow%3A%22CI%22)
2018-11-25 10:51:30 -05:00
[![Discord ](https://img.shields.io/badge/chat-on%20discord-brightgreen.svg )](https://discord.gg/GsEFRM8)
[![Gitter ](https://img.shields.io/badge/chat-on%20gitter-brightgreen.svg )](https://gitter.im/PrismarineJS/general)
[![Irc ](https://img.shields.io/badge/chat-on%20irc-brightgreen.svg )](https://irc.gitter.im/)
2020-05-03 06:51:34 -04:00
2019-07-21 05:48:18 -04:00
[![Try it on gitpod ](https://img.shields.io/badge/try-on%20gitpod-brightgreen.svg )](https://gitpod.io/#https://github.com/PrismarineJS/node-minecraft-protocol)
2015-03-22 22:09:38 -04:00
2013-01-01 21:02:07 -05:00
Parse and serialize minecraft packets, plus authentication and encryption.
2013-01-01 04:14:38 -05:00
2013-01-01 16:01:43 -05:00
## Features
2017-04-13 08:07:33 -04:00
* 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),
2020-03-28 12:19:48 -04:00
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)
2024-12-04 15:21:43 -05:00
, 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)
2013-01-04 23:16:48 -05:00
* Parses all packets and emits events with packet fields as JavaScript
2013-01-01 16:01:43 -05:00
objects.
* Send a packet by supplying fields as a JavaScript object.
2013-01-04 23:16:48 -05:00
* Client
- Authenticating and logging in
2015-03-06 14:54:27 -05:00
- Encryption
- Compression
2013-01-04 23:16:48 -05:00
- Both online and offline mode
2024-05-22 02:12:48 -04:00
- Respond to keep-alive packets
- Follow DNS service records (SRV)
2013-01-04 23:16:48 -05:00
- Ping a server for status
* Server
2015-03-06 14:54:27 -05:00
- Online/Offline mode
- Encryption
- Compression
2013-01-04 23:16:48 -05:00
- Handshake
- Keep-alive checking
- Ping status
2015-09-24 06:28:41 -04:00
* Robust test coverage.
2013-01-01 16:01:43 -05:00
* Optimized for rapidly staying up to date with Minecraft protocol updates.
2020-05-09 18:42:06 -04:00
Want to contribute on something important for PrismarineJS ? go to https://github.com/PrismarineJS/mineflayer/wiki/Big-Prismarine-projects
2019-07-21 05:48:18 -04:00
2016-02-12 08:41:47 -05:00
## Third Party Plugins
node-minecraft-protocol is pluggable.
* [minecraft-protocol-forge ](https://github.com/PrismarineJS/node-minecraft-protocol-forge ) add forge support to minecraft-protocol
2013-01-01 16:01:43 -05:00
2013-02-12 13:58:53 -05:00
## Projects Using node-minecraft-protocol
2021-08-08 08:30:09 -04:00
* [mineflayer ](https://github.com/PrismarineJS/mineflayer/ ) - Create minecraft
2013-02-12 13:58:53 -05:00
bots with a stable, high level API.
2021-08-08 08:30:09 -04:00
* [mcserve ](https://github.com/andrewrk/mcserve ) - Runs and monitors your
2013-02-12 13:58:53 -05:00
minecraft server, provides real-time web interface, allow your users to
create bots.
2021-08-08 08:30:09 -04:00
* [flying-squid ](https://github.com/PrismarineJS/flying-squid ) - Create minecraft
2015-08-30 15:34:28 -04:00
servers with a high level API, also a minecraft server by itself.
2022-01-20 11:43:16 -05:00
* [pakkit ](https://github.com/Heath123/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 ](https://github.com/wvffle/minecraft-packet-debugger ) - A tool to capture Minecraft packets in a buffer then view them in a browser.
* [aresrpg ](https://github.com/aresrpg/aresrpg ) - An open-source mmorpg minecraft server.
* [SteveProxy ](https://github.com/SteveProxy/proxy ) - Proxy for Minecraft with the ability to change the gameplay using plugins.
2021-08-08 08:31:25 -04:00
* and [several thousands others ](https://github.com/PrismarineJS/node-minecraft-protocol/network/dependents?package_id=UGFja2FnZS0xODEzMDk0OQ%3D%3D )
2021-08-08 08:31:10 -04:00
2021-11-20 18:35:48 -05:00
## Installation
`npm install minecraft-protocol`
## Documentation
* [API doc ](API.md )
* [faq ](FAQ.md )
2023-07-22 08:59:37 -04:00
* [protocol doc ](https://prismarinejs.github.io/minecraft-data/?d=protocol ) and [wiki.vg/Protocol ](https://wiki.vg/Protocol )
2021-11-20 18:35:48 -05:00
2013-01-01 23:39:31 -05:00
## Usage
2013-01-01 04:14:38 -05:00
2013-01-03 21:42:35 -05:00
### Echo client example
2013-01-01 23:39:31 -05:00
```js
2023-06-03 15:32:19 -04:00
const mc = require('minecraft-protocol');
const client = mc.createClient({
2013-01-30 19:56:43 -05:00
host: "localhost", // optional
2024-05-22 02:12:48 -04:00
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
2013-01-01 23:39:31 -05:00
});
2023-06-03 15:32:19 -04:00
2024-05-22 02:12:48 -04:00
client.on('playerChat', function (ev) {
2013-01-03 21:42:35 -05:00
// Listen for chat messages and echo them back.
2024-05-22 02:12:48 -04:00
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))
2013-01-01 23:39:31 -05:00
});
```
2013-01-02 01:12:42 -05:00
2024-05-22 02:12:48 -04:00
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 ](./API.md ).
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.
2013-01-30 19:56:43 -05:00
2023-01-15 12:30:33 -05:00
### Client example joining a Realm
Example to connect to a Realm that the authenticating account is owner of or has been invited to:
```js
2023-06-03 15:32:19 -04:00
const mc = require('minecraft-protocol');
const client = mc.createClient({
2023-01-15 12:30:33 -05:00
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'
})
```
2013-01-03 22:01:17 -05:00
### Hello World server example
2023-12-27 18:48:10 -05:00
For a more up to date example, see examples/server/server.js.
2013-01-03 22:01:17 -05:00
```js
2024-02-25 19:14:49 -05:00
const mc = require('minecraft-protocol')
const nbt = require('prismarine-nbt')
2023-06-03 15:32:19 -04:00
const server = mc.createServer({
2013-01-04 01:45:57 -05:00
'online-mode': true, // optional
encryption: true, // optional
host: '0.0.0.0', // optional
port: 25565, // optional
2024-05-22 02:12:48 -04:00
version: '1.18'
2024-02-25 19:14:49 -05:00
})
2020-10-06 16:46:53 -04:00
const mcData = require('minecraft-data')(server.version)
2024-02-25 19:14:49 -05:00
function chatText (text) {
return mcData.supportFeature('chatPacketsUseNbtComponents')
? nbt.comp({ text: nbt.string(text) })
: JSON.stringify({ text })
}
2023-12-27 18:48:10 -05:00
server.on('playerJoin', function(client) {
2023-06-03 15:32:19 -04:00
const loginPacket = mcData.loginPacket
2020-10-06 16:46:53 -04:00
2014-04-11 13:40:15 -04:00
client.write('login', {
2023-12-27 18:48:10 -05:00
...loginPacket,
2024-10-12 17:55:36 -04:00
enforceSecureChat: false,
2013-01-04 22:47:54 -05:00
entityId: client.id,
2020-05-03 17:42:00 -04:00
hashedSeed: [0, 0],
2015-09-24 06:28:41 -04:00
maxPlayers: server.maxPlayers,
2020-10-06 16:46:53 -04:00
viewDistance: 10,
2020-05-03 17:42:00 -04:00
reducedDebugInfo: false,
2020-10-06 16:46:53 -04:00
enableRespawnScreen: true,
isDebug: false,
isFlat: false
2024-02-25 19:14:49 -05:00
})
2023-06-03 15:32:19 -04:00
2014-04-11 13:40:15 -04:00
client.write('position', {
2013-01-03 22:01:17 -05:00
x: 0,
2023-06-03 15:32:19 -04:00
y: 255,
2013-01-03 22:01:17 -05:00
z: 0,
yaw: 0,
pitch: 0,
2015-09-24 06:28:41 -04:00
flags: 0x00
2024-02-25 19:14:49 -05:00
})
2023-06-03 15:32:19 -04:00
2024-02-25 19:14:49 -05:00
const message = {
2015-09-24 06:28:41 -04:00
translate: 'chat.type.announcement',
2024-02-25 19:14:49 -05:00
with: [
2015-09-24 06:28:41 -04:00
'Server',
'Hello, world!'
]
2024-02-25 19:14:49 -05:00
}
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' })
}
})
2013-01-03 22:01:17 -05:00
```
2013-01-02 01:12:42 -05:00
## Testing
* Ensure your system has the `java` executable in `PATH` .
2015-09-29 16:41:41 -04:00
* `MC_SERVER_JAR_DIR=some/path/to/store/minecraft/server/ MC_USERNAME=email@example.com MC_PASSWORD=password npm test`
2013-01-02 01:47:18 -05:00
2015-05-29 07:24:12 -04:00
## Debugging
2013-04-15 00:52:32 -04:00
2016-03-21 05:38:23 -04:00
You can enable some protocol debugging output using `DEBUG` environment variable:
2013-04-15 00:52:32 -04:00
```bash
2016-03-21 05:38:23 -04:00
DEBUG="minecraft-protocol" node [...]
2013-04-15 00:52:32 -04:00
```
2023-06-03 15:32:19 -04:00
On Windows:
2018-11-25 13:39:34 -05:00
```
set DEBUG=minecraft-protocol
node your_script.js
```
2018-08-26 13:55:29 -04:00
## Contribute
Please read https://github.com/PrismarineJS/prismarine-contribute
2013-01-26 15:35:52 -05:00
## History
2014-10-19 11:55:27 -04:00
2015-05-29 07:24:12 -04:00
See [history ](HISTORY.md )
2017-07-13 08:22:42 -04:00
## Related
* [node-rcon ](https://github.com/pushrax/node-rcon ) can be used to access the rcon server in the minecraft server
2018-12-17 20:46:27 -05:00
* [map-colors][aresmapcolor] can be used to convert any image into a buffer of minecraft compatible colors
[aresmapcolor]: https://github.com/AresRPG/aresrpg-map-colors