$ npm install minecraft-protocol
Parse and serialize minecraft packets, plus authentication and encryption.
Want to contribute on something important for PrismarineJS ? go to https://github.com/PrismarineJS/mineflayer/wiki/Big-Prismarine-projects
node-minecraft-protocol is pluggable.
npm install minecraft-protocol
const mc = require('minecraft-protocol');
const client = mc.createClient({
host: "localhost", // optional
port: 25565, // optional
username: "email@example.com",
password: "12345678",
auth: 'microsoft' // optional; by default uses offline mode, if using a microsoft account, set to 'microsoft'
});
client.on('chat', function(packet) {
// Listen for chat messages and echo them back.
const jsonMsg = JSON.parse(packet.message);
if (jsonMsg.translate == 'chat.type.announcement' || jsonMsg.translate == 'chat.type.text') {
const username = jsonMsg.with[0].text;
const msg = jsonMsg.with[1];
if (username === client.username) return;
client.write('chat', {message: msg.text});
}
});
If the server is in offline mode, you may leave out the password
option and switch auth to offline
.
You can also leave out password
when using a Microsoft account. If provided, password based auth will be attempted first which may fail. Note: if using a Microsoft account, your account age must be >= 18 years old.
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'
})
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.20.4'
})
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,
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' })
}
})
java
executable in PATH
.MC_SERVER_JAR_DIR=some/path/to/store/minecraft/server/ MC_USERNAME=email@example.com MC_PASSWORD=password npm test
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
Please read https://github.com/PrismarineJS/prismarine-contribute
See history
© 2010 - cnpmjs.org x YWFE | Home | YWFE