Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.20.5 #1309

Merged
merged 22 commits into from
Oct 12, 2024
Merged

1.20.5 #1309

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
- name: Setup Java JDK
uses: actions/[email protected]
with:
java-version: '17'
java-version: '21'
distribution: 'adopt'
- name: Install dependencies
run: npm install
Expand Down
3 changes: 2 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Parse and serialize minecraft packets, plus authentication and encryption.

* Supports Minecraft PC version 1.7.10, 1.8.8, 1.9 (15w40b, 1.9, 1.9.1-pre2, 1.9.2, 1.9.4),
1.10 (16w20a, 1.10-pre1, 1.10, 1.10.1, 1.10.2), 1.11 (16w35a, 1.11, 1.11.2), 1.12 (17w15a, 17w18b, 1.12-pre4, 1.12, 1.12.1, 1.12.2), and 1.13 (17w50a, 1.13, 1.13.1, 1.13.2-pre1, 1.13.2-pre2, 1.13.2), 1.14 (1.14, 1.14.1, 1.14.3, 1.14.4)
, 1.15 (1.15, 1.15.1, 1.15.2) and 1.16 (20w13b, 20w14a, 1.16-rc1, 1.16, 1.16.1, 1.16.2, 1.16.3, 1.16.4, 1.16.5), 1.17 (21w07a, 1.17, 1.17.1), 1.18 (1.18, 1.18.1 and 1.18.2), 1.19 (1.19, 1.19.1, 1.19.2, 1.19.3, 1.19.4), 1.20 (1.20, 1.20.1, 1.20.2, 1.20.3 and 1.20.4)
, 1.15 (1.15, 1.15.1, 1.15.2) and 1.16 (20w13b, 20w14a, 1.16-rc1, 1.16, 1.16.1, 1.16.2, 1.16.3, 1.16.4, 1.16.5), 1.17 (21w07a, 1.17, 1.17.1), 1.18 (1.18, 1.18.1 and 1.18.2), 1.19 (1.19, 1.19.1, 1.19.2, 1.19.3, 1.19.4), 1.20, 1.20.1, 1.20.2, 1.20.3, 1.20.4, 1.20.5
* Parses all packets and emits events with packet fields as JavaScript
objects.
* Send a packet by supplying fields as a JavaScript object.
Expand Down Expand Up @@ -142,6 +142,7 @@ server.on('playerJoin', function(client) {

client.write('login', {
...loginPacket,
enforceSecureChat: false,
entityId: client.id,
hashedSeed: [0, 0],
maxPlayers: server.maxPlayers,
Expand Down
1 change: 1 addition & 0 deletions examples/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ server.on('playerJoin', function (client) {
// send init data so client will start rendering world
client.write('login', {
...loginPacket,
enforceSecureChat: false,
entityId: client.id,
isHardcore: false,
gameMode: 0,
Expand Down
1 change: 1 addition & 0 deletions examples/server_channel/server_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ server.on('playerJoin', function (client) {

client.write('login', {
...loginPacket,
enforceSecureChat: false,
entityId: client.id,
isHardcore: false,
gameMode: 0,
Expand Down
1 change: 1 addition & 0 deletions examples/server_custom_channel/server_custom_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const loginPacket = mcData.loginPacket
server.on('playerJoin', function (client) {
client.write('login', {
...loginPacket,
enforceSecureChat: false,
entityId: client.id,
isHardcore: false,
gameMode: 0,
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"minecraft-wrap": "^1.2.3",
"mocha": "^10.0.0",
"power-assert": "^1.0.0",
"standard": "^17.0.0"
"standard": "^17.0.0",
"prismarine-registry": "^1.8.0"
},
"dependencies": {
"@types/readable-stream": "^4.0.0",
Expand All @@ -51,15 +52,15 @@
"endian-toggle": "^0.0.0",
"lodash.get": "^4.1.2",
"lodash.merge": "^4.3.0",
"minecraft-data": "^3.55.0",
"minecraft-data": "^3.71.0",
"minecraft-folder-path": "^1.2.0",
"node-fetch": "^2.6.1",
"node-rsa": "^0.4.2",
"prismarine-auth": "^2.2.0",
"prismarine-chat": "^1.10.0",
"prismarine-nbt": "^2.5.0",
"prismarine-realms": "^1.2.0",
"protodef": "^1.8.0",
"protodef": "^1.17.0",
"readable-stream": "^4.1.0",
"uuid-1345": "^1.0.1",
"yggdrasil": "^1.4.0"
Expand Down
1 change: 1 addition & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class Client extends EventEmitter {
this.splitter.pipe(this.deserializer)
} else {
this.serializer.pipe(this.compressor)
if (globalThis.debugNMP) this.decompressor.on('data', (data) => { console.log('DES>', data.toString('hex')) })
this.decompressor.pipe(this.deserializer)
}

Expand Down
9 changes: 5 additions & 4 deletions src/client/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ module.exports = function (client, options) {
})
})

client.on('message_header', (packet) => {
client.on('message_header', (packet) => { // [1.19.2]
updateAndValidateChat(packet.senderUuid, packet.previousSignature, packet.signature, packet.messageHash)

client._lastChatHistory.push({
Expand Down Expand Up @@ -369,13 +369,14 @@ module.exports = function (client, options) {

if (message.startsWith('/')) {
const command = message.slice(1)
if (mcData.supportFeature('useChatSessions')) {
if (mcData.supportFeature('useChatSessions')) { // 1.19.3+
const { acknowledged, acknowledgements } = getAcknowledgements()
client.write('chat_command', {
const canSign = client.profileKeys && client._session
client.write((mcData.supportFeature('seperateSignedChatCommandPacket') && canSign) ? 'chat_command_signed' : 'chat_command', {
command,
timestamp: options.timestamp,
salt: options.salt,
argumentSignatures: (client.profileKeys && client._session) ? signaturesForCommand(command, options.timestamp, options.salt, options.preview, acknowledgements) : [],
argumentSignatures: canSign ? signaturesForCommand(command, options.timestamp, options.salt, options.preview, acknowledgements) : [],
messageCount: client._lastSeenMessages.pending,
acknowledged
})
Expand Down
8 changes: 6 additions & 2 deletions src/client/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ module.exports = function (client, options) {
client.on('server_data', (packet) => {
client.serverFeatures = {
chatPreview: packet.previewsChat,
enforcesSecureChat: packet.enforcesSecureChat
enforcesSecureChat: packet.enforcesSecureChat // in LoginPacket v>=1.20.5
}
})

client.once('login', () => {
client.once('login', (packet) => {
if (packet.enforcesSecureChat) client.serverFeatures.enforcesSecureChat = packet.enforcesSecureChat
const mcData = require('minecraft-data')(client.version)
if (mcData.supportFeature('useChatSessions') && client.profileKeys && client.cipher && client.session.selectedProfile.id === client.uuid.replace(/-/g, '')) {
client._session = {
Expand Down Expand Up @@ -52,6 +53,9 @@ module.exports = function (client, options) {
client.write('configuration_acknowledged', {})
}
client.state = states.CONFIGURATION
client.on('select_known_packs', () => {
client.write('select_known_packs', { packs: [] })
})
// Server should send finish_configuration on its own right after sending the client a dimension codec
// for login (that has data about world height, world gen, etc) after getting a login success from client
client.once('finish_configuration', () => {
Expand Down
53 changes: 53 additions & 0 deletions src/datatypes/compiler-minecraft.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ module.exports = {
code += ' if ((item & 128) === 0) return { value: data, size: cursor - offset }\n'
code += '}'
return compiler.wrapCode(code)
}],
arrayWithLengthOffset: ['parametrizable', (compiler, array) => {
let code = ''
if (array.countType) {
code += 'const { value: count, size: countSize } = ' + compiler.callType(array.countType) + '\n'
} else if (array.count) {
code += 'const count = ' + array.count + '\n'
code += 'const countSize = 0\n'
} else {
throw new Error('Array must contain either count or countType')
}
code += 'if (count > 0xffffff) throw new Error("array size is abnormally large, not reading: " + count)\n'
code += 'const data = []\n'
code += 'let size = countSize\n'
code += `for (let i = 0; i < count + ${array.lengthOffset}; i++) {\n`
code += ' const elem = ' + compiler.callType(array.type, 'offset + size') + '\n'
code += ' data.push(elem.value)\n'
code += ' size += elem.size\n'
code += '}\n'
code += 'return { value: data, size }'
return compiler.wrapCode(code)
}]
},
Write: {
Expand Down Expand Up @@ -72,6 +93,19 @@ module.exports = {
code += '}\n'
code += 'return offset'
return compiler.wrapCode(code)
}],
arrayWithLengthOffset: ['parametrizable', (compiler, array) => {
let code = ''
if (array.countType) {
code += 'offset = ' + compiler.callType('value.length', array.countType) + '\n'
} else if (array.count === null) {
throw new Error('Array must contain either count or countType')
}
code += 'for (let i = 0; i < value.length; i++) {\n'
code += ' offset = ' + compiler.callType('value[i]', array.type) + '\n'
code += '}\n'
code += 'return offset'
return compiler.wrapCode(code)
}]
},
SizeOf: {
Expand All @@ -96,6 +130,25 @@ module.exports = {
code += '}\n'
code += 'return size'
return compiler.wrapCode(code)
}],
arrayWithLengthOffset: ['parametrizable', (compiler, array) => {
let code = ''
if (array.countType) {
code += 'let size = ' + compiler.callType('value.length', array.countType) + '\n'
} else if (array.count) {
code += 'let size = 0\n'
} else {
throw new Error('Array must contain either count or countType')
}
if (!isNaN(compiler.callType('value[i]', array.type))) {
code += 'size += value.length * ' + compiler.callType('value[i]', array.type) + '\n'
} else {
code += 'for (let i = 0; i < value.length; i++) {\n'
code += ' size += ' + compiler.callType('value[i]', array.type) + '\n'
code += '}\n'
}
code += 'return size'
return compiler.wrapCode(code)
}]
}
}
36 changes: 35 additions & 1 deletion src/datatypes/minecraft.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ module.exports = {
compressedNbt: [readCompressedNbt, writeCompressedNbt, sizeOfCompressedNbt],
restBuffer: [readRestBuffer, writeRestBuffer, sizeOfRestBuffer],
entityMetadataLoop: [readEntityMetadata, writeEntityMetadata, sizeOfEntityMetadata],
topBitSetTerminatedArray: [readTopBitSetTerminatedArray, writeTopBitSetTerminatedArray, sizeOfTopBitSetTerminatedArray]
topBitSetTerminatedArray: [readTopBitSetTerminatedArray, writeTopBitSetTerminatedArray, sizeOfTopBitSetTerminatedArray],
arrayWithLengthOffset: [readArrayWithLengthOffset, writeArrayWithLengthOffset, sizeOfArrayWithLengthOffset]
}
const PartialReadError = require('protodef').utils.PartialReadError

Expand Down Expand Up @@ -180,3 +181,36 @@ function sizeOfTopBitSetTerminatedArray (value, { type }) {
}
return size
}

//
const { getCount, sendCount, calcCount, tryDoc } = require('protodef/src/utils')

function readArrayWithLengthOffset (buffer, offset, typeArgs, rootNode) {
const results = {
value: [],
size: 0
}
let value
let { count, size } = getCount.call(this, buffer, offset, typeArgs, rootNode)
offset += size
results.size += size
for (let i = 0; i < count + typeArgs.lengthOffset; i++) {
({ size, value } = tryDoc(() => this.read(buffer, offset, typeArgs.type, rootNode), i))
results.size += size
offset += size
results.value.push(value)
}
return results
}

// no changes
function writeArrayWithLengthOffset (value, buffer, offset, typeArgs, rootNode) {
offset = sendCount.call(this, value.length, buffer, offset, typeArgs, rootNode)
return value.reduce((offset, v, index) => tryDoc(() => this.write(v, buffer, offset, typeArgs.type, rootNode), index), offset)
}

function sizeOfArrayWithLengthOffset (value, typeArgs, rootNode) {
let size = calcCount.call(this, value.length, typeArgs, rootNode)
size = value.reduce((size, v, index) => tryDoc(() => size + this.sizeOf(v, typeArgs.type, rootNode), index), size)
return size + typeArgs
}
12 changes: 10 additions & 2 deletions src/server/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ module.exports = function (client, server, options) {
if (client.supportFeature('chainedChatWithHashing')) { // 1.19.1+
client.write('server_data', {
previewsChat: options.enableChatPreview,
enforceSecureProfile: options.enforceSecureProfile
// Note: in 1.20.5+ user must send this with `login`
enforcesSecureChat: options.enforceSecureProfile
})
}

Expand All @@ -211,7 +212,14 @@ module.exports = function (client, server, options) {

function onClientLoginAck () {
client.state = states.CONFIGURATION
client.write('registry_data', { codec: options.registryCodec || {} })
if (client.supportFeature('segmentedRegistryCodecData')) {
for (const key in options.registryCodec) {
const entry = options.registryCodec[key]
client.write('registry_data', entry)
}
} else {
client.write('registry_data', { codec: options.registryCodec || {} })
}
client.once('finish_configuration', () => {
client.state = states.PLAY
server.emit('playerJoin', client)
Expand Down
4 changes: 2 additions & 2 deletions src/version.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

module.exports = {
defaultVersion: '1.20.4',
supportedVersions: ['1.7', '1.8.8', '1.9.4', '1.10.2', '1.11.2', '1.12.2', '1.13.2', '1.14.4', '1.15.2', '1.16.5', '1.17.1', '1.18.2', '1.19', '1.19.2', '1.19.3', '1.19.4', '1.20', '1.20.1', '1.20.2', '1.20.4']
defaultVersion: '1.20.5',
supportedVersions: ['1.7', '1.8.8', '1.9.4', '1.10.2', '1.11.2', '1.12.2', '1.13.2', '1.14.4', '1.15.2', '1.16.5', '1.17.1', '1.18.2', '1.19', '1.19.2', '1.19.3', '1.19.4', '1.20', '1.20.1', '1.20.2', '1.20.4', '1.20.5']
}
57 changes: 41 additions & 16 deletions test/clientTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,37 +107,62 @@ for (const supportedVersion of mc.supportedVersions) {
auth: 'offline'
}))
client.on('error', err => done(err))
const lineListener = function (line) {
const match = line.match(/\[Server thread\/INFO\]: (?:\[Not Secure\] )?<(.+?)> (.+)/)
if (!match) return
assert.strictEqual(match[1], 'Player')
assert.strictEqual(match[2], 'hello everyone; I have logged in.')
wrap.writeServer('say hello\n')
wrap.off('line', lineListener)
}
wrap.on('line', lineListener)
let chatCount = 0
client.on('login', function (packet) {
assert.strictEqual(packet.gameMode, 0)
client.chat('hello everyone; I have logged in.')

client.on('state', (state) => {
console.log('Client now in state', state)
})
// Dump some data for easier debugging

// ** Dump some server data **
fs.rmSync(MC_SERVER_DIR + '_registry_data.json', { force: true })
client.on('raw.registry_data', (buffer) => {
fs.writeFileSync(MC_SERVER_DIR + '_registry_data.bin', buffer)
})
client.on('registry_data', (json) => {
fs.writeFileSync(MC_SERVER_DIR + '_registry_data.json', JSON.stringify(json))
if (json.codec) { // Pre 1.20.5, codec is 1 json
fs.writeFileSync(MC_SERVER_DIR + '_registry_data.json', JSON.stringify(json))
} else { // 1.20.5+, codec is many nbt's each with their own ids, merge them
let currentData = {}
if (fs.existsSync(MC_SERVER_DIR + '_registry_data.json')) {
currentData = JSON.parse(fs.readFileSync(MC_SERVER_DIR + '_registry_data.json', 'utf8'))
}
currentData[json.id] = json
fs.writeFileSync(MC_SERVER_DIR + '_registry_data.json', JSON.stringify(currentData))
}
console.log('Wrote registry data')
})
client.on('login', (packet) => {
fs.writeFileSync(MC_SERVER_DIR + '_login.json', JSON.stringify(packet))
if (fs.existsSync(MC_SERVER_DIR + '_registry_data.json')) {
// generate a loginPacket.json for minecraft-data
const codec = JSON.parse(fs.readFileSync(MC_SERVER_DIR + '_registry_data.json'))
fs.writeFileSync(MC_SERVER_DIR + '_loginPacket.json', JSON.stringify({
...packet,
dimensionCodec: JSON.parse(fs.readFileSync(MC_SERVER_DIR + '_registry_data.json')).codec
dimensionCodec: codec.codec || codec
}, null, 2))
console.log('Wrote loginPacket.json')
}
})
// ** End dumping code **

const lineListener = function (line) {
const match = line.match(/\[Server thread\/INFO\]: (?:\[Not Secure\] )?<(.+?)> (.+)/)
if (!match) return
assert.strictEqual(match[1], 'Player')
assert.strictEqual(match[2], 'hello everyone; I have logged in.')
wrap.writeServer('say hello\n')
wrap.off('line', lineListener)
}
wrap.on('line', lineListener)
let chatCount = 0

client.on('login', function (packet) {
if (packet.worldState) { // 1.20.5+
assert.strictEqual(packet.worldState.gamemode, 'survival')
} else {
assert.strictEqual(packet.gameMode, 0)
}
client.chat('hello everyone; I have logged in.')
})
client.on('playerChat', function (data) {
chatCount += 1
assert.ok(chatCount <= 2)
Expand Down
2 changes: 1 addition & 1 deletion test/common/clientHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = client => {
})
client.on('registry_data', (data) => {
client.registry ??= Registry(client.version)
client.registry.loadDimensionCodec(data.codec)
client.registry.loadDimensionCodec(data.codec || data)
})

client.on('playerJoin', () => {
Expand Down
Loading
Loading