2018-05-13 16:50:16 -04:00
|
|
|
/* eslint-env mocha */
|
2015-10-01 20:02:54 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
const mc = require('../')
|
|
|
|
const assert = require('power-assert')
|
2021-05-01 19:12:37 -04:00
|
|
|
const { once } = require('events')
|
2022-08-15 18:57:26 -04:00
|
|
|
const nbt = require('prismarine-nbt')
|
|
|
|
const applyClientHelpers = require('./common/clientHelpers')
|
2017-05-09 20:49:13 -04:00
|
|
|
|
2021-06-28 22:58:21 -04:00
|
|
|
const { getPort } = require('./common/util')
|
|
|
|
|
2022-08-15 18:57:26 -04:00
|
|
|
const w = nbt.comp({
|
|
|
|
piglin_safe: nbt.byte(0),
|
|
|
|
natural: nbt.byte(1),
|
|
|
|
ambient_light: nbt.float(0),
|
|
|
|
infiniburn: nbt.string('minecraft:infiniburn_overworld'),
|
|
|
|
respawn_anchor_works: nbt.byte(0),
|
|
|
|
has_skylight: nbt.byte(1),
|
|
|
|
bed_works: nbt.byte(1),
|
|
|
|
has_raids: nbt.byte(1),
|
|
|
|
name: nbt.string('minecraft:overworld'),
|
|
|
|
logical_height: nbt.int(256),
|
|
|
|
shrunk: nbt.byte(0),
|
|
|
|
ultrawarm: nbt.byte(0),
|
|
|
|
has_ceiling: nbt.byte(0)
|
|
|
|
})
|
2015-10-01 20:02:54 -04:00
|
|
|
|
2021-06-12 22:54:16 -04:00
|
|
|
for (const supportedVersion of mc.supportedVersions) {
|
2021-06-28 22:58:21 -04:00
|
|
|
let PORT
|
2018-05-13 16:50:16 -04:00
|
|
|
const mcData = require('minecraft-data')(supportedVersion)
|
|
|
|
const version = mcData.version
|
|
|
|
|
2022-08-15 18:57:26 -04:00
|
|
|
const loginPacket = (client, server) => {
|
|
|
|
return {
|
|
|
|
// 1.7
|
|
|
|
entityId: client.id,
|
|
|
|
gameMode: 1,
|
|
|
|
dimension: (version.version >= 735 ? mcData.loginPacket.dimension : 0),
|
|
|
|
difficulty: 2,
|
|
|
|
maxPlayers: server.maxPlayers,
|
|
|
|
levelType: 'default',
|
|
|
|
// 1.8
|
|
|
|
reducedDebugInfo: (version.version >= 735 ? false : 0),
|
|
|
|
// 1.14
|
|
|
|
// removes `difficulty`
|
|
|
|
viewDistance: 10,
|
|
|
|
// 1.15
|
|
|
|
hashedSeed: [0, 0],
|
|
|
|
enableRespawnScreen: true,
|
|
|
|
// 1.16
|
|
|
|
// removed levelType
|
|
|
|
previousGameMode: version.version >= 755 ? 0 : 255,
|
|
|
|
worldNames: ['minecraft:overworld'],
|
|
|
|
dimensionCodec: version.version >= 755 ? mcData.loginPacket.dimensionCodec : (version.version >= 735 ? mcData.loginPacket.dimension : { name: '', type: 'compound', value: { dimension: { type: 'list', value: { type: 'compound', value: [w] } } } }),
|
|
|
|
worldName: 'minecraft:overworld',
|
|
|
|
isDebug: false,
|
|
|
|
isFlat: false,
|
|
|
|
// 1.16.2
|
|
|
|
isHardcore: false,
|
|
|
|
// 1.18
|
|
|
|
simulationDistance: 10,
|
|
|
|
// 1.19
|
|
|
|
// removed `dimension`
|
|
|
|
// removed `dimensionCodec`
|
|
|
|
registryCodec: {
|
2023-06-03 15:54:31 -04:00
|
|
|
type: 'compound',
|
|
|
|
name: '',
|
|
|
|
value: {}
|
2022-08-15 18:57:26 -04:00
|
|
|
},
|
2023-06-03 15:54:31 -04:00
|
|
|
worldType: 'minecraft:overworld',
|
2022-08-15 18:57:26 -04:00
|
|
|
death: undefined
|
|
|
|
// more to be added
|
|
|
|
}
|
|
|
|
}
|
2021-06-28 22:58:21 -04:00
|
|
|
|
2023-06-03 15:54:31 -04:00
|
|
|
function sendBroadcastMessage (server, clients, message, sender) {
|
2022-08-15 18:57:26 -04:00
|
|
|
if (mcData.supportFeature('signedChat')) {
|
|
|
|
server.writeToClients(clients, 'player_chat', {
|
2023-01-21 14:31:17 -05:00
|
|
|
plainMessage: message,
|
2022-08-15 18:57:26 -04:00
|
|
|
signedChatContent: '',
|
|
|
|
unsignedChatContent: JSON.stringify({ text: message }),
|
2023-06-03 15:54:31 -04:00
|
|
|
type: 0,
|
2022-08-15 18:57:26 -04:00
|
|
|
senderUuid: 'd3527a0b-bc03-45d5-a878-2aafdd8c8a43', // random
|
|
|
|
senderName: JSON.stringify({ text: sender }),
|
|
|
|
senderTeam: undefined,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
salt: 0n,
|
2023-01-21 14:31:17 -05:00
|
|
|
signature: mcData.supportFeature('useChatSessions') ? undefined : Buffer.alloc(0),
|
|
|
|
previousMessages: [],
|
|
|
|
filterType: 0,
|
|
|
|
networkName: JSON.stringify({ text: sender })
|
2022-08-15 18:57:26 -04:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
server.writeToClients(clients, 'chat', { message: JSON.stringify({ text: message }), position: 0, sender: sender || '0' })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-27 18:12:23 -05:00
|
|
|
describe('mc-server ' + supportedVersion + 'v', function () {
|
2022-08-15 18:57:26 -04:00
|
|
|
this.timeout(5000)
|
2023-12-27 18:48:10 -05:00
|
|
|
this.beforeEach(async function () {
|
2021-06-28 22:58:21 -04:00
|
|
|
PORT = await getPort()
|
|
|
|
console.log(`Using port for tests: ${PORT}`)
|
|
|
|
})
|
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
it('starts listening and shuts down cleanly', function (done) {
|
2017-07-13 08:03:52 -04:00
|
|
|
const server = mc.createServer({
|
2015-10-01 20:02:54 -04:00
|
|
|
'online-mode': false,
|
2016-02-23 13:37:47 -05:00
|
|
|
version: version.minecraftVersion,
|
2018-05-13 16:50:16 -04:00
|
|
|
port: PORT
|
|
|
|
})
|
|
|
|
let listening = false
|
|
|
|
server.on('listening', function () {
|
|
|
|
listening = true
|
|
|
|
server.close()
|
|
|
|
})
|
|
|
|
server.on('close', function () {
|
|
|
|
assert.ok(listening)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2022-08-15 18:57:26 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
it('kicks clients that do not log in', function (done) {
|
2017-07-13 08:03:52 -04:00
|
|
|
const server = mc.createServer({
|
2015-10-01 20:02:54 -04:00
|
|
|
'online-mode': false,
|
|
|
|
kickTimeout: 100,
|
|
|
|
checkTimeoutInterval: 10,
|
2016-02-23 13:37:47 -05:00
|
|
|
version: version.minecraftVersion,
|
2018-05-13 16:50:16 -04:00
|
|
|
port: PORT
|
|
|
|
})
|
|
|
|
let count = 2
|
|
|
|
server.on('connection', function (client) {
|
|
|
|
client.on('end', function (reason) {
|
2020-12-05 10:52:36 -05:00
|
|
|
assert.strictEqual(reason, 'LoginTimeout')
|
2018-05-13 16:50:16 -04:00
|
|
|
server.close()
|
|
|
|
})
|
|
|
|
})
|
2022-08-15 18:57:26 -04:00
|
|
|
server.on('close', resolve)
|
2018-05-13 16:50:16 -04:00
|
|
|
server.on('listening', function () {
|
|
|
|
const client = new mc.Client(false, version.minecraftVersion)
|
2022-08-15 18:57:26 -04:00
|
|
|
client.on('end', resolve)
|
2018-05-13 16:50:16 -04:00
|
|
|
client.connect(PORT, '127.0.0.1')
|
|
|
|
})
|
2015-10-01 20:02:54 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
function resolve () {
|
|
|
|
count -= 1
|
|
|
|
if (count <= 0) done()
|
2015-10-01 20:02:54 -04:00
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2022-08-15 18:57:26 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
it('kicks clients that do not send keepalive packets', function (done) {
|
2017-07-13 08:03:52 -04:00
|
|
|
const server = mc.createServer({
|
2015-10-01 20:02:54 -04:00
|
|
|
'online-mode': false,
|
|
|
|
kickTimeout: 100,
|
|
|
|
checkTimeoutInterval: 10,
|
2016-02-23 13:37:47 -05:00
|
|
|
version: version.minecraftVersion,
|
2018-05-13 16:50:16 -04:00
|
|
|
port: PORT
|
|
|
|
})
|
|
|
|
let count = 2
|
|
|
|
server.on('connection', function (client) {
|
|
|
|
client.on('end', function (reason) {
|
2020-12-05 10:52:36 -05:00
|
|
|
assert.strictEqual(reason, 'KeepAliveTimeout')
|
2018-05-13 16:50:16 -04:00
|
|
|
server.close()
|
|
|
|
})
|
|
|
|
})
|
2022-08-15 18:57:26 -04:00
|
|
|
server.on('close', resolve)
|
2018-05-13 16:50:16 -04:00
|
|
|
server.on('listening', function () {
|
2017-07-13 08:03:52 -04:00
|
|
|
const client = mc.createClient({
|
2015-10-01 20:02:54 -04:00
|
|
|
username: 'superpants',
|
|
|
|
host: '127.0.0.1',
|
2016-02-23 13:37:47 -05:00
|
|
|
port: PORT,
|
2015-10-01 20:02:54 -04:00
|
|
|
keepAlive: false,
|
2016-02-18 18:48:39 -05:00
|
|
|
version: version.minecraftVersion
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2022-08-15 18:57:26 -04:00
|
|
|
client.on('end', resolve)
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
|
|
|
function resolve () {
|
|
|
|
count -= 1
|
|
|
|
if (count <= 0) done()
|
2015-10-01 20:02:54 -04:00
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2022-08-15 18:57:26 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
it('responds to ping requests', function (done) {
|
2022-05-13 18:49:08 -04:00
|
|
|
const chatMotd = { // Generated with prismarine-chat MessageBuilder on version 1.16 may change in the future
|
|
|
|
extra: [{ color: 'red', text: 'Red text' }],
|
|
|
|
bold: true,
|
|
|
|
text: 'Example chat mesasge'
|
|
|
|
}
|
|
|
|
|
|
|
|
const server = mc.createServer({
|
|
|
|
'online-mode': false,
|
|
|
|
motd: 'test1234',
|
|
|
|
motdMsg: chatMotd,
|
|
|
|
'max-players': 120,
|
|
|
|
version: version.minecraftVersion,
|
|
|
|
port: PORT
|
|
|
|
})
|
|
|
|
server.on('listening', function () {
|
|
|
|
mc.ping({
|
|
|
|
host: '127.0.0.1',
|
|
|
|
version: version.minecraftVersion,
|
|
|
|
port: PORT
|
|
|
|
}, function (err, results) {
|
|
|
|
if (err) return done(err)
|
|
|
|
assert.ok(results.latency >= 0)
|
|
|
|
assert.ok(results.latency <= 1000)
|
|
|
|
delete results.latency
|
|
|
|
assert.deepEqual(results, {
|
|
|
|
version: {
|
|
|
|
name: version.minecraftVersion,
|
|
|
|
protocol: version.version
|
|
|
|
},
|
|
|
|
players: {
|
|
|
|
max: 120,
|
|
|
|
online: 0,
|
|
|
|
sample: []
|
|
|
|
},
|
2022-08-15 18:57:26 -04:00
|
|
|
description: {
|
2023-06-03 15:54:31 -04:00
|
|
|
extra: [{ color: 'red', text: 'Red text' }],
|
2022-05-13 18:49:08 -04:00
|
|
|
bold: true,
|
|
|
|
text: 'Example chat mesasge'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
server.close()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
server.on('close', done)
|
|
|
|
})
|
2022-08-15 18:57:26 -04:00
|
|
|
|
2022-05-13 18:49:08 -04:00
|
|
|
it('responds with chatMessage motd\'s', function (done) {
|
2017-07-13 08:03:52 -04:00
|
|
|
const server = mc.createServer({
|
2015-10-01 20:02:54 -04:00
|
|
|
'online-mode': false,
|
|
|
|
motd: 'test1234',
|
|
|
|
'max-players': 120,
|
2016-02-23 13:37:47 -05:00
|
|
|
version: version.minecraftVersion,
|
2018-05-13 16:50:16 -04:00
|
|
|
port: PORT
|
|
|
|
})
|
|
|
|
server.on('listening', function () {
|
2015-10-01 20:02:54 -04:00
|
|
|
mc.ping({
|
|
|
|
host: '127.0.0.1',
|
2016-02-23 13:37:47 -05:00
|
|
|
version: version.minecraftVersion,
|
2018-05-13 16:50:16 -04:00
|
|
|
port: PORT
|
|
|
|
}, function (err, results) {
|
|
|
|
if (err) return done(err)
|
|
|
|
assert.ok(results.latency >= 0)
|
|
|
|
assert.ok(results.latency <= 1000)
|
|
|
|
delete results.latency
|
2015-10-01 20:02:54 -04:00
|
|
|
assert.deepEqual(results, {
|
|
|
|
version: {
|
|
|
|
name: version.minecraftVersion,
|
|
|
|
protocol: version.version
|
|
|
|
},
|
|
|
|
players: {
|
|
|
|
max: 120,
|
|
|
|
online: 0,
|
|
|
|
sample: []
|
|
|
|
},
|
2018-09-24 16:07:34 -04:00
|
|
|
description: { text: 'test1234' }
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
|
|
|
server.close()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
server.on('close', done)
|
|
|
|
})
|
2022-08-15 18:57:26 -04:00
|
|
|
|
2021-07-06 06:42:00 -04:00
|
|
|
it('clients can be changed by beforeLogin', function (done) {
|
|
|
|
const notchUUID = '069a79f4-44e9-4726-a5be-fca90e38aaf5'
|
|
|
|
const server = mc.createServer({
|
|
|
|
'online-mode': false,
|
|
|
|
version: version.minecraftVersion,
|
|
|
|
port: PORT,
|
|
|
|
beforeLogin: (client) => {
|
|
|
|
client.uuid = notchUUID
|
|
|
|
}
|
|
|
|
})
|
|
|
|
server.on('listening', function () {
|
|
|
|
const client = mc.createClient({
|
|
|
|
username: 'notNotch',
|
|
|
|
host: '127.0.0.1',
|
|
|
|
version: version.minecraftVersion,
|
|
|
|
port: PORT
|
|
|
|
})
|
2023-06-03 15:54:31 -04:00
|
|
|
client.on('packet', (data, { name }) => {
|
2021-07-06 06:42:00 -04:00
|
|
|
if (name === 'success') {
|
|
|
|
assert.strictEqual(data.uuid, notchUUID, 'UUID')
|
|
|
|
server.close()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
server.on('close', done)
|
|
|
|
})
|
2022-08-15 18:57:26 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
it('clients can log in and chat', function (done) {
|
2017-07-13 08:03:52 -04:00
|
|
|
const server = mc.createServer({
|
2015-10-01 20:02:54 -04:00
|
|
|
'online-mode': false,
|
2016-02-23 13:37:47 -05:00
|
|
|
version: version.minecraftVersion,
|
2018-05-13 16:50:16 -04:00
|
|
|
port: PORT
|
|
|
|
})
|
2022-08-15 18:57:26 -04:00
|
|
|
const broadcast = (message, exclude) => sendBroadcastMessage(server,
|
|
|
|
Object.values(server.clients).filter(client => client !== exclude), message)
|
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
const username = ['player1', 'player2']
|
|
|
|
let index = 0
|
2023-12-27 18:48:10 -05:00
|
|
|
server.on('playerJoin', function (client) {
|
|
|
|
console.log('ChatTest: Player has joined')
|
2018-05-13 16:50:16 -04:00
|
|
|
assert.notEqual(client.id, null)
|
|
|
|
assert.strictEqual(client.username, username[index++])
|
|
|
|
broadcast(client.username + ' joined the game.')
|
|
|
|
client.on('end', function () {
|
|
|
|
broadcast(client.username + ' left the game.', client)
|
|
|
|
if (client.username === 'player2') server.close()
|
|
|
|
})
|
2022-08-15 18:57:26 -04:00
|
|
|
client.write('login', loginPacket(client, server))
|
|
|
|
|
|
|
|
const handleChat = (packet) => broadcast(`<${client.username}> ${packet.message}`)
|
|
|
|
client.on('chat', handleChat)
|
|
|
|
client.on('chat_message', handleChat)
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
|
|
|
server.on('close', done)
|
2022-08-15 18:57:26 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
server.on('listening', function () {
|
2022-08-15 18:57:26 -04:00
|
|
|
const player1 = applyClientHelpers(mc.createClient({
|
2015-10-01 20:02:54 -04:00
|
|
|
username: 'player1',
|
|
|
|
host: '127.0.0.1',
|
2016-02-23 13:37:47 -05:00
|
|
|
version: version.minecraftVersion,
|
2018-05-13 16:50:16 -04:00
|
|
|
port: PORT
|
2022-08-15 18:57:26 -04:00
|
|
|
}))
|
2023-12-27 18:48:10 -05:00
|
|
|
console.log('ChatTest: Player1 is joining...')
|
2023-01-21 14:31:17 -05:00
|
|
|
|
2022-08-15 18:57:26 -04:00
|
|
|
player1.on('login', async function (packet) {
|
2023-12-27 18:48:10 -05:00
|
|
|
console.log('ChatTest: Player 1 has joined')
|
2018-05-13 16:50:16 -04:00
|
|
|
assert.strictEqual(packet.gameMode, 1)
|
2022-08-15 18:57:26 -04:00
|
|
|
const player2 = applyClientHelpers(mc.createClient({
|
2015-10-01 20:02:54 -04:00
|
|
|
username: 'player2',
|
|
|
|
host: '127.0.0.1',
|
2016-02-23 13:37:47 -05:00
|
|
|
version: version.minecraftVersion,
|
2018-05-13 16:50:16 -04:00
|
|
|
port: PORT
|
2022-08-15 18:57:26 -04:00
|
|
|
}))
|
2015-10-01 20:02:54 -04:00
|
|
|
|
2023-12-27 18:48:10 -05:00
|
|
|
console.log('ChatTest: waiting for next message from P2')
|
2022-08-15 18:57:26 -04:00
|
|
|
const p1Join = await player1.nextMessage('player2')
|
2023-06-03 15:54:31 -04:00
|
|
|
|
2022-08-15 18:57:26 -04:00
|
|
|
assert.strictEqual(p1Join, '{"text":"player2 joined the game."}')
|
2023-12-27 18:48:10 -05:00
|
|
|
console.log('ChatTest: Got message from P2')
|
2022-08-15 18:57:26 -04:00
|
|
|
player2.chat('hi')
|
|
|
|
const p2hi = await player1.nextMessage('player2')
|
|
|
|
assert.strictEqual(p2hi, '{"text":"<player2> hi"}')
|
|
|
|
|
2023-12-27 18:48:10 -05:00
|
|
|
console.log('ChatTest: Waiting again for next message from P2')
|
2022-08-15 18:57:26 -04:00
|
|
|
player1.chat('hello')
|
|
|
|
const p1hello = await player2.nextMessage('player1')
|
|
|
|
assert.strictEqual(p1hello, '{"text":"<player1> hello"}')
|
|
|
|
|
|
|
|
player2.end()
|
|
|
|
const p2leaving = await player1.nextMessage('player2')
|
|
|
|
assert.strictEqual(p2leaving, '{"text":"player2 left the game."}')
|
|
|
|
player1.end()
|
|
|
|
})
|
|
|
|
})
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2022-08-15 18:57:26 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
it('kicks clients when invalid credentials', function (done) {
|
|
|
|
this.timeout(10000)
|
2017-07-13 08:03:52 -04:00
|
|
|
const server = mc.createServer({
|
2016-02-23 13:37:47 -05:00
|
|
|
version: version.minecraftVersion,
|
2018-05-13 16:50:16 -04:00
|
|
|
port: PORT
|
|
|
|
})
|
|
|
|
let count = 4
|
|
|
|
server.on('connection', function (client) {
|
|
|
|
client.on('end', function (reason) {
|
|
|
|
resolve()
|
|
|
|
server.close()
|
|
|
|
})
|
|
|
|
})
|
2022-08-15 18:57:26 -04:00
|
|
|
server.on('close', resolve)
|
2018-05-13 16:50:16 -04:00
|
|
|
server.on('listening', function () {
|
|
|
|
resolve()
|
2017-07-13 08:03:52 -04:00
|
|
|
const client = mc.createClient({
|
2015-10-01 20:02:54 -04:00
|
|
|
username: 'lalalal',
|
2018-05-13 16:50:16 -04:00
|
|
|
host: '127.0.0.1',
|
2016-02-23 13:37:47 -05:00
|
|
|
version: version.minecraftVersion,
|
2018-05-13 16:50:16 -04:00
|
|
|
port: PORT
|
|
|
|
})
|
2022-08-15 18:57:26 -04:00
|
|
|
client.on('end', resolve)
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
|
|
|
function resolve () {
|
|
|
|
count -= 1
|
|
|
|
if (count <= 0) done()
|
2015-10-01 20:02:54 -04:00
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2022-08-15 18:57:26 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
it('gives correct reason for kicking clients when shutting down', function (done) {
|
2017-07-13 08:03:52 -04:00
|
|
|
const server = mc.createServer({
|
2015-10-01 20:02:54 -04:00
|
|
|
'online-mode': false,
|
2016-02-23 13:37:47 -05:00
|
|
|
version: version.minecraftVersion,
|
2018-05-13 16:50:16 -04:00
|
|
|
port: PORT
|
|
|
|
})
|
|
|
|
let count = 2
|
2023-12-27 18:48:10 -05:00
|
|
|
server.on('playerJoin', function (client) {
|
2018-05-13 16:50:16 -04:00
|
|
|
client.on('end', function (reason) {
|
2020-12-05 10:52:36 -05:00
|
|
|
assert.strictEqual(reason, 'ServerShutdown')
|
2018-05-13 16:50:16 -04:00
|
|
|
resolve()
|
|
|
|
})
|
2022-08-15 18:57:26 -04:00
|
|
|
client.write('login', loginPacket(client, server))
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2022-08-15 18:57:26 -04:00
|
|
|
server.on('close', resolve)
|
2018-05-13 16:50:16 -04:00
|
|
|
server.on('listening', function () {
|
2017-07-13 08:03:52 -04:00
|
|
|
const client = mc.createClient({
|
2015-10-01 20:02:54 -04:00
|
|
|
username: 'lalalal',
|
|
|
|
host: '127.0.0.1',
|
2016-02-23 13:37:47 -05:00
|
|
|
version: version.minecraftVersion,
|
2018-05-13 16:50:16 -04:00
|
|
|
port: PORT
|
|
|
|
})
|
2023-12-27 18:48:10 -05:00
|
|
|
client.on('playerJoin', function () {
|
2018-05-13 16:50:16 -04:00
|
|
|
server.close()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
function resolve () {
|
|
|
|
count -= 1
|
|
|
|
if (count <= 0) done()
|
2015-10-01 20:02:54 -04:00
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2022-08-15 18:57:26 -04:00
|
|
|
|
2021-05-01 19:12:37 -04:00
|
|
|
it('encodes chat packet once and send it to two clients', function (done) {
|
|
|
|
const server = mc.createServer({
|
|
|
|
'online-mode': false,
|
|
|
|
version: version.minecraftVersion,
|
|
|
|
port: PORT
|
|
|
|
})
|
2023-12-27 18:48:10 -05:00
|
|
|
server.on('playerJoin', function (client) {
|
2022-08-15 18:57:26 -04:00
|
|
|
client.write('login', loginPacket(client, server))
|
2021-05-01 19:12:37 -04:00
|
|
|
})
|
|
|
|
server.on('close', done)
|
|
|
|
server.on('listening', async function () {
|
2022-08-15 18:57:26 -04:00
|
|
|
const player1 = applyClientHelpers(mc.createClient({
|
2021-05-01 19:12:37 -04:00
|
|
|
username: 'player1',
|
|
|
|
host: '127.0.0.1',
|
|
|
|
version: version.minecraftVersion,
|
|
|
|
port: PORT
|
2022-08-15 18:57:26 -04:00
|
|
|
}))
|
|
|
|
const player2 = applyClientHelpers(mc.createClient({
|
2021-05-01 19:12:37 -04:00
|
|
|
username: 'player2',
|
|
|
|
host: '127.0.0.1',
|
|
|
|
version: version.minecraftVersion,
|
|
|
|
port: PORT
|
2022-08-15 18:57:26 -04:00
|
|
|
}))
|
|
|
|
await Promise.all([once(player1, 'login'), once(player2, 'login')])
|
|
|
|
|
|
|
|
sendBroadcastMessage(server, Object.values(server.clients), 'A message from the server.')
|
|
|
|
|
2023-06-03 15:54:31 -04:00
|
|
|
const results = await Promise.all([player1.nextMessage(), player2.nextMessage()])
|
2022-08-15 18:57:26 -04:00
|
|
|
for (const msg of results) {
|
|
|
|
assert.strictEqual(msg, '{"text":"A message from the server."}')
|
|
|
|
}
|
|
|
|
|
2021-05-01 19:12:37 -04:00
|
|
|
player1.end()
|
|
|
|
player2.end()
|
|
|
|
await Promise.all([once(player1, 'end'), once(player2, 'end')])
|
|
|
|
server.close()
|
|
|
|
})
|
|
|
|
})
|
2023-06-03 15:54:31 -04:00
|
|
|
|
|
|
|
it('supports bundle packet', function (done) {
|
|
|
|
const server = mc.createServer({
|
|
|
|
'online-mode': false,
|
|
|
|
version: version.minecraftVersion,
|
|
|
|
port: PORT
|
|
|
|
})
|
2023-12-27 18:48:10 -05:00
|
|
|
server.on('playerJoin', function (client) {
|
2023-06-03 15:54:31 -04:00
|
|
|
client.on('end', function (reason) {
|
|
|
|
assert.strictEqual(reason, 'ServerShutdown')
|
|
|
|
})
|
|
|
|
client.write('login', loginPacket(client, server))
|
|
|
|
client.writeBundle([
|
|
|
|
['update_time', { age: 1, time: 2 }],
|
|
|
|
['close_window', { windowId: 0 }]
|
|
|
|
])
|
|
|
|
})
|
|
|
|
server.on('close', done)
|
|
|
|
server.on('listening', function () {
|
|
|
|
const client = mc.createClient({
|
|
|
|
username: 'lalalal',
|
|
|
|
host: '127.0.0.1',
|
|
|
|
version: version.minecraftVersion,
|
|
|
|
port: PORT
|
|
|
|
})
|
|
|
|
client.on('update_time', function () {
|
|
|
|
// Below handler synchronously defined should be guaranteed to be called after the above one
|
|
|
|
const d1 = Date.now()
|
|
|
|
client.on('close_window', function () {
|
|
|
|
server.close()
|
|
|
|
const d2 = Date.now()
|
|
|
|
if (mcData.supportFeature('hasBundlePacket') && (d2 - d1) > 1) {
|
|
|
|
throw new Error(`bundle packet constituents did not arrive at once : ${d1}, ${d2}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2021-06-12 22:54:16 -04:00
|
|
|
}
|