2015-05-29 07:24:12 -04:00
# Documentation
## mc.createServer(options)
2015-09-23 17:26:50 -04:00
Returns a `Server` instance and starts listening. All clients will be
automatically logged in and validated against mojang's auth.
2015-05-29 07:24:12 -04:00
2015-11-09 11:23:15 -05:00
`options` is an object containing the properties :
* port : default to 25565
2020-09-27 15:24:04 -04:00
* host : default to undefined which means listen to all available ipv4 and ipv6 adresses
(see https://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback for details)
2015-11-09 11:23:15 -05:00
* kickTimeout : default to `10*1000` (10s), kick client that doesn't answer to keepalive after that time
* checkTimeoutInterval : default to `4*1000` (4s), send keepalive packet at that period
* online-mode : default to true
* beforePing : allow customisation of the answer to ping the server does.
It takes a function with argument response and client, response is the default json response, and client is client who sent a ping.
It can take as third argument a callback. If the callback is passed, the function should pass its result to the callback, if not it should return.
2022-05-14 18:51:18 -04:00
If the result is `false` instead of a response object then the connection is terminated and no ping is returned to the client.
2021-07-06 06:42:00 -04:00
* beforeLogin : allow customisation of client before the `success` packet is sent.
It takes a function with argument client and should be synchronous for the server to wait for completion before continuing execution.
2015-11-09 11:23:15 -05:00
* motd : default to "A Minecraft server"
2022-05-13 18:49:08 -04:00
* motdMsg : A json object of the chat message to use instead of `motd` . Can be build using [prismarine-chat ](https://github.com/PrismarineJS/prismarine-chat ) and calling .toJSON(). Not used with legacy pings.
2015-11-09 11:23:15 -05:00
* maxPlayers : default to 20
* keepAlive : send keep alive packets : default to true
2022-04-10 08:45:04 -04:00
* version : the version of the server, defaults to the latest version. Set version to `false` to enable dynamic cross version support.
2022-05-14 18:39:20 -04:00
* fallbackVersion (optional) : the version that should be used as a fallback, if the client version isn't supported, only works with dynamic cross version support.
2021-12-26 09:52:52 -05:00
* favicon (optional) : the favicon to set, base64 encoded
2016-02-20 10:43:08 -05:00
* customPackets (optional) : an object index by version/state/direction/name, see client_custom_packet for an example
2017-01-28 17:50:33 -05:00
* errorHandler : A way to override the default error handler for client errors. A function that takes a Client and an error.
The default kicks the client.
2018-08-03 15:11:33 -04:00
* hideErrors : do not display errors, default to false
2022-03-08 09:40:57 -05:00
* agent : a http agent that can be used to set proxy settings for yggdrasil authentication confirmation (see proxy-agent on npm)
* validateChannelProtocol (optional) : whether or not to enable protocol validation for custom protocols using plugin channels for the connected clients. Defaults to true
2015-11-09 11:23:15 -05:00
2016-02-20 10:22:18 -05:00
## mc.Server(version,[customPackets])
2015-11-09 11:23:15 -05:00
Create a server instance for `version` of minecraft.
2015-05-29 07:24:12 -04:00
2021-05-01 19:12:37 -04:00
### server.writeToClients(clients, name, params)
Write a packet to all `clients` but encode it only once.
2015-09-23 17:26:50 -04:00
### server.onlineModeExceptions
2015-05-29 07:24:12 -04:00
This is a plain old JavaScript object. Add a key with the username you want to
be exempt from online mode or offline mode (whatever mode the server is in).
Make sure the entries in this object are all lower case.
2015-09-23 17:26:50 -04:00
### server.clients
2015-05-29 07:24:12 -04:00
Javascript object mapping a `Client` from a clientId.
2015-09-23 17:26:50 -04:00
### server.playerCount
2015-05-29 07:24:12 -04:00
The amount of players currently present on the server.
2015-09-23 17:26:50 -04:00
### server.maxPlayers
2015-05-29 07:24:12 -04:00
The maximum amount of players allowed on the server.
2015-09-23 17:26:50 -04:00
### server.motd
2015-05-29 07:24:12 -04:00
The motd that is sent to the player when he is pinging the server
2015-09-23 17:26:50 -04:00
### server.favicon
2015-05-29 07:24:12 -04:00
A base64 data string representing the favicon that will appear next to the server
on the mojang client's multiplayer list.
2015-09-23 17:26:50 -04:00
### `connection` event
Called when a client connects, but before any login has happened. Takes a
`Client` parameter.
### `login` event
Called when a client is logged in against server. Takes a `Client` parameter.
2022-06-06 11:51:28 -04:00
### `listening` event
Called when the server is listening for connections. This means that the server is ready to accept incoming connections.
### `close` event
Called when the server is no longer listening to incoming connections.
2018-06-03 12:31:54 -04:00
2015-05-29 07:24:12 -04:00
## mc.createClient(options)
Returns a `Client` instance and perform login.
`options` is an object containing the properties :
* username
* port : default to 25565
2022-07-29 12:03:20 -04:00
* auth : the type of account to use, either `microsoft` , `mojang` , or `offline` . default to 'offline'
2021-01-29 19:21:03 -05:00
* password : can be omitted
* (microsoft account) leave this blank to use device code auth. If you provide
a password, we try to do username and password auth, but this does not always work.
* (mojang account) If provided, we auth with the username and password. If this
is blank, and `profilesFolder` is specified, we auth with the tokens there instead.
If neither `password` or `profilesFolder` are specified, we connect in offline mode.
2015-05-29 07:24:12 -04:00
* host : default to localhost
2021-12-26 09:53:25 -05:00
* session : An object holding clientToken, accessToken and selectedProfile. Generated after logging in using username + password with mojang auth or after logging in using microsoft auth. `clientToken` , `accessToken` and `selectedProfile: {name: '<username>', id: '<selected profile uuid>'}` can be set inside of `session` when using createClient to login with a client and access Token instead of a password. `session` is also emitted by the `Client` instance with the event 'session' after successful authentication.
* clientToken : generated if a password is given or can be set when when using createClient
* accessToken : generated if a password or microsoft account is given or can be set when using createBot
* selectedProfile : generated if a password or microsoft account is given. Can be set as a object with property `name` and `id` that specifies the selected profile.
* name : The selected profiles in game name needed for logging in with access and client Tokens.
* id : The selected profiles uuid in short form (without `-` ) needed for logging in with access and client Tokens.
2020-08-02 19:48:23 -04:00
* authServer : auth server, default to https://authserver.mojang.com
* sessionServer : session server, default to https://sessionserver.mojang.com
2015-05-29 07:24:12 -04:00
* keepAlive : send keep alive packets : default to true
2020-08-02 19:37:35 -04:00
* closeTimeout : end the connection after this delay in milliseconds if server doesn't answer to ping, default to `120*1000`
2020-10-25 11:14:22 -04:00
* noPongTimeout : after the server opened the connection, wait for a default of `5*1000` after pinging and answers without the latency
2018-05-01 15:06:20 -04:00
* checkTimeoutInterval : default to `30*1000` (30s), check if keepalive received at that period, disconnect otherwise.
2016-01-31 15:44:06 -05:00
* version : 1.8 or 1.9 or false (to auto-negotiate): default to 1.8
2016-02-20 10:43:08 -05:00
* customPackets (optional) : an object index by version/state/direction/name, see client_custom_packet for an example
2018-08-03 15:11:33 -04:00
* hideErrors : do not display errors, default to false
2018-11-09 11:06:12 -05:00
* skipValidation : do not try to validate given session, defaults to false
2020-02-11 06:31:24 -05:00
* stream : a stream to use as connection
* connect : a function taking the client as parameter and that should client.setSocket(socket)
and client.emit('connect') when appropriate (see the proxy examples for an example of use)
* agent : a http agent that can be used to set proxy settings for yggdrasil authentication (see proxy-agent on npm)
2021-11-04 16:19:32 -04:00
* fakeHost : (optional) hostname to send to the server in the set_protocol packet
2021-01-29 19:21:03 -05:00
* profilesFolder : optional
* (mojang account) the path to the folder that contains your `launcher_profiles.json` . defaults to your minecraft folder if it exists, otherwise the local directory. set to `false` to disable managing profiles
* (microsoft account) the path to store authentication caches, defaults to .minecraft
* onMsaCode(data) : (optional) callback called when signing in with a microsoft account
with device code auth. `data` is an object documented [here ](https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-device-code#device-authorization-response )
2021-09-24 11:07:14 -04:00
* id : a numeric client id used for referring to multiple clients in a server
2022-03-08 09:40:57 -05:00
* validateChannelProtocol (optional) : whether or not to enable protocol validation for custom protocols using plugin channels. Defaults to true
2021-01-29 19:21:03 -05:00
2016-01-24 21:53:34 -05:00
2016-02-20 10:22:18 -05:00
## mc.Client(isServer,version,[customPackets])
2015-05-29 07:24:12 -04:00
2015-11-09 11:23:15 -05:00
Create a new client, if `isServer` is true then it is a server-side client, otherwise it's a client-side client.
Takes a minecraft `version` as second argument.
2015-05-29 07:24:12 -04:00
2016-08-10 14:06:40 -04:00
### client.write(name, params)
write a packet
2021-09-24 11:07:14 -04:00
### client.writeRaw(buffer)
write a raw buffer as a packet
2020-10-25 09:25:44 -04:00
### client.end(reason, fullReason)
2016-08-11 12:40:26 -04:00
2020-10-25 09:25:44 -04:00
Ends the connection with `reason` or `fullReason`
If `fullReason` is not defined, then the `reason` will be used.
`fullReason` is a JSON object, which represents [chat ](https://wiki.vg/Chat ) message.
2016-08-11 12:40:26 -04:00
2021-09-24 11:07:14 -04:00
### client.connect(port, host)
Used by the [Client Class ](https://github.com/PrismarineJS/node-minecraft-protocol/blob/d9d01c8be4921bb38e7b59d9c18afd771615ba22/src/client.js ) to connect to a server, done by createClient automatically
### client.setSocket(socket)
Sets the client's connection socket.
### client.registerChannel(name, typeDefinition, custom)
Registers a plugin channel with the given `name` and protodef `typeDefinition`
### client.unregisterChannel(name)
Unregisters a plugin channel.
### client.writeChannel(channel, params)
Write to [Plugin Channels ](https://wiki.vg/Plugin_channels )
2015-09-23 17:26:50 -04:00
### client.state
2015-05-29 07:24:12 -04:00
The internal state that is used to figure out which protocol state we are in during
packet parsing. This is one of the protocol.states.
2015-09-23 17:26:50 -04:00
### client.isServer
2015-05-29 07:24:12 -04:00
True if this is a connection going from the server to the client,
False if it is a connection from client to server.
2015-09-23 17:26:50 -04:00
### client.socket
2015-05-29 07:24:12 -04:00
Returns the internal nodejs Socket used to communicate with this client.
2015-09-23 17:26:50 -04:00
### client.uuid
2015-05-29 07:24:12 -04:00
A string representation of the client's UUID. Note that UUIDs are unique for
each players, while playerNames, as of 1.7.7, are not unique and can change.
2015-09-23 17:26:50 -04:00
### client.username
2015-05-29 07:24:12 -04:00
The user's username.
2015-09-23 17:26:50 -04:00
### client.session
2015-05-29 07:24:12 -04:00
2015-11-09 11:23:15 -05:00
The user's session, as returned by the Yggdrasil system. (only client-side)
### client.profile
The player's profile, as returned by the Yggdrasil system. (only server-side)
2015-05-29 07:24:12 -04:00
2015-12-21 18:07:27 -05:00
### client.latency
The latency of the client, in ms. Updated at each keep alive.
2021-09-24 11:07:14 -04:00
### client.customPackets
An object index by version/state/direction/name, see client_custom_packet for an example
### client.protocolVersion
The client's protocol version
### client.version
The client's version
2015-09-23 17:26:50 -04:00
### `packet` event
2021-08-01 13:40:42 -04:00
Called with every packet parsed. Takes four paramaters, the JSON data we parsed, the packet metadata (name, state), the buffer (raw data) and the full buffer (includes surplus data and may include the data of following packets on versions below 1.8)
2015-09-23 17:26:50 -04:00
### `raw` event
Called with every packet, but as a buffer. Takes two params, the buffer
2015-10-02 20:14:01 -04:00
and the packet metadata (name, state)
2015-09-23 17:26:50 -04:00
2021-09-24 11:07:14 -04:00
### `connect` event
when the socket connects to the server, this is emitted
### `end` event
Called when the client's connection is disconnected. Takes the reason as parameter
### `session` event
Called when user authentication is resolved. Takes session data as parameter.
2015-09-23 17:26:50 -04:00
### `state` event
Called when the protocol changes state. Takes the new state and old state as
parameters.
2021-09-24 11:07:14 -04:00
### `error` event
2018-06-03 12:31:54 -04:00
2021-09-24 11:07:14 -04:00
Called when an error occurs within the client. Takes an Error as parameter.
2018-06-03 12:31:54 -04:00
2015-09-23 17:26:50 -04:00
### per-packet events
Check out the [minecraft-data docs ](https://prismarinejs.github.io/minecraft-data/?v=1.8&d=protocol ) to know the event names and data field names.
2016-08-10 14:06:40 -04:00
### client.writeChannel(channel, params)
write a packet to a plugin channel
### client.registerChannel(name, typeDefinition, [custom])
Register a channel `name` using the protodef `typeDefinition` and sending the register packet if `custom` is true.
Start emitting channel events of the given name on the client object.
### client.unregisterChannel(name, [custom])
Unregister a channel `name` and send the unregister packet if `custom` is true.
2015-05-29 07:24:12 -04:00
## Not Immediately Obvious Data Type Formats
2015-11-09 11:23:15 -05:00
Note : almost all data formats can be understood by looking at
[minecraft-data docs ](https://prismarinejs.github.io/minecraft-data/?v=1.8&d=protocol )
or [minecraft-data protocol.json ](https://github.com/PrismarineJS/minecraft-data/blob/master/data/1.8/protocol.json )
2015-05-29 07:24:12 -04:00
2015-11-09 11:23:15 -05:00
### entityMetadata
2015-05-29 07:24:12 -04:00
Value looks like this:
```js
[
2015-09-23 17:26:50 -04:00
{type: 1, value: 2, key: 3},
{type: 2, value: 3, key: 4},
2015-05-29 07:24:12 -04:00
...
]
```
2015-09-23 17:26:50 -04:00
Where the key is the numeric metadata key and the value is the value of the
correct data type. You can figure out the types [here ](http://wiki.vg/Entities#Entity_Metadata_Format )
2015-11-09 11:23:15 -05:00
## mc.ping(options, callback)
2021-06-19 16:03:26 -04:00
`options` is an object containing the following:
2021-11-04 16:20:56 -04:00
* host : default to localhost
* port : default to 25565
* version: default to most recent version
2020-07-14 10:22:21 -04:00
2021-06-19 16:03:26 -04:00
Ping a minecraft server and return a promise or use an optional callback containing the information about it
returns: `promise( <pending> ).then(pingResult).catch(err)`
callback `callback(err, pingResults)`
2015-11-09 11:23:15 -05:00
`pingResults` :
## Old version
* `prefix`
* `protocol`
* `version`
* `motd`
* `playerCount`
* `maxPlayers`
## New version
* `description`
* `players`
* `max`
* `online`
* `sample`
* `id`
* `name`
* `version`
* `name`
* `protocol`
* `favicon`
* `latency`
## mc.states
The minecraft protocol states.
## mc.supportedVersions
The supported minecraft versions.
2022-04-13 07:50:42 -04:00
## mc.defaultVersion
The current default minecraft version.
2015-11-09 11:23:15 -05:00
## mc.createSerializer({ state = states.HANDSHAKING, isServer = false , version})
Returns a minecraft protocol [serializer ](https://github.com/roblabla/ProtoDef#serializerprotomaintype ) for these parameters.
## mc.createDeserializer({ state = states.HANDSHAKING, isServer = false, packetsToParse = {"packet": true}, version })
Returns a minecraft protocol [deserializer ](https://github.com/roblabla/ProtoDef#parserprotomaintype ) for these parameters.