Use mc.createServer() to instantiate a server.
Configuration Options:
online-mode: Boolean. Enables/disables authentication.encryption: Boolean. Enables/disables encryption.host: String. The host address to bind to.port: Number. The port to listen on.version: String. The Minecraft version the server should run (e.g., '1.18').
Once the server is running, you can listen for events like playerJoin and use client.write(packetName, data) to send packets to connected clients.
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: mcData.supportFeature('chatTypeIsHolder') ? { chatType: 1 } : 0,
senderUuid: 'd3527a0b-bc03-45d5-a878-2aafdd8c8a43',
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' })
}
})