mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2024-11-14 19:04:59 -05:00
update server hello world
This commit is contained in:
parent
185450f0de
commit
df4af752a3
2 changed files with 62 additions and 13 deletions
|
@ -118,16 +118,23 @@ const client = mc.createClient({
|
||||||
For a more up to date example, see examples/server/server.js.
|
For a more up to date example, see examples/server/server.js.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const mc = require('minecraft-protocol');
|
const mc = require('minecraft-protocol')
|
||||||
|
const nbt = require('prismarine-nbt')
|
||||||
const server = mc.createServer({
|
const server = mc.createServer({
|
||||||
'online-mode': true, // optional
|
'online-mode': true, // optional
|
||||||
encryption: true, // optional
|
encryption: true, // optional
|
||||||
host: '0.0.0.0', // optional
|
host: '0.0.0.0', // optional
|
||||||
port: 25565, // optional
|
port: 25565, // optional
|
||||||
version: '1.16.3'
|
version: '1.20.4'
|
||||||
});
|
})
|
||||||
const mcData = require('minecraft-data')(server.version)
|
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) {
|
server.on('playerJoin', function(client) {
|
||||||
const loginPacket = mcData.loginPacket
|
const loginPacket = mcData.loginPacket
|
||||||
|
|
||||||
|
@ -141,7 +148,7 @@ server.on('playerJoin', function(client) {
|
||||||
enableRespawnScreen: true,
|
enableRespawnScreen: true,
|
||||||
isDebug: false,
|
isDebug: false,
|
||||||
isFlat: false
|
isFlat: false
|
||||||
});
|
})
|
||||||
|
|
||||||
client.write('position', {
|
client.write('position', {
|
||||||
x: 0,
|
x: 0,
|
||||||
|
@ -150,18 +157,35 @@ server.on('playerJoin', function(client) {
|
||||||
yaw: 0,
|
yaw: 0,
|
||||||
pitch: 0,
|
pitch: 0,
|
||||||
flags: 0x00
|
flags: 0x00
|
||||||
});
|
})
|
||||||
|
|
||||||
const msg = {
|
const message = {
|
||||||
translate: 'chat.type.announcement',
|
translate: 'chat.type.announcement',
|
||||||
"with": [
|
with: [
|
||||||
'Server',
|
'Server',
|
||||||
'Hello, world!'
|
'Hello, world!'
|
||||||
]
|
]
|
||||||
};
|
}
|
||||||
|
if (mcData.supportFeature('signedChat')) {
|
||||||
client.write("chat", { message: JSON.stringify(msg), position: 0, sender: '0' });
|
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
|
## Testing
|
||||||
|
|
|
@ -8,6 +8,13 @@ const options = {
|
||||||
const server = mc.createServer(options)
|
const server = mc.createServer(options)
|
||||||
const mcData = require('minecraft-data')(server.version)
|
const mcData = require('minecraft-data')(server.version)
|
||||||
const loginPacket = mcData.loginPacket
|
const loginPacket = mcData.loginPacket
|
||||||
|
const nbt = require('prismarine-nbt')
|
||||||
|
|
||||||
|
function chatText (text) {
|
||||||
|
return mcData.supportFeature('chatPacketsUseNbtComponents')
|
||||||
|
? nbt.comp({ text: nbt.string(text) })
|
||||||
|
: JSON.stringify({ text })
|
||||||
|
}
|
||||||
|
|
||||||
server.on('playerJoin', function (client) {
|
server.on('playerJoin', function (client) {
|
||||||
const addr = client.socket.remoteAddress
|
const addr = client.socket.remoteAddress
|
||||||
|
@ -49,14 +56,32 @@ server.on('playerJoin', function (client) {
|
||||||
flags: 0x00
|
flags: 0x00
|
||||||
})
|
})
|
||||||
|
|
||||||
const msg = {
|
const message = {
|
||||||
translate: 'chat.type.announcement',
|
translate: 'chat.type.announcement',
|
||||||
with: [
|
with: [
|
||||||
'Server',
|
'Server',
|
||||||
'Hello, world!'
|
'Hello, world!'
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
client.write('chat', { message: JSON.stringify(msg), position: 0, sender: '0' })
|
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' })
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
server.on('error', function (error) {
|
server.on('error', function (error) {
|
||||||
|
|
Loading…
Reference in a new issue