Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
extremeheat committed Jul 6, 2024
1 parent fbd5b1c commit 10b6f0f
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 20 deletions.
3 changes: 2 additions & 1 deletion 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": "extremeheat/prismarine-registry#pc1.20.5"
},
"dependencies": {
"@types/readable-stream": "^4.0.0",
Expand Down
3 changes: 3 additions & 0 deletions src/client/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,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
9 changes: 8 additions & 1 deletion src/server/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,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
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
18 changes: 18 additions & 0 deletions test/packetTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const values = {
varlong: -20,
i8: -10,
u8: 8,
ByteArray: [],
string: 'hi hi this is my client string',
buffer: function (typeArgs, context) {
let count
Expand Down Expand Up @@ -177,6 +178,7 @@ const values = {
})
return results
},
mapper: '',
tags: [{ tagName: 'hi', entries: [1, 2, 3, 4, 5] }],
ingredient: [slotValue],
particleData: null,
Expand Down Expand Up @@ -212,6 +214,20 @@ const values = {
particle: {
particleId: 0,
data: null
},
Particle: {},
SpawnInfo: {
dimension: 0,
name: 'minecraft:overworld',
hashedSeed: [
572061085,
1191958278
],
gamemode: 'survival',
previousGamemode: 255,
isDebug: false,
isFlat: false,
portalCooldown: 0
}
}

Expand All @@ -233,6 +249,8 @@ for (const supportedVersion of mc.supportedVersions) {
const version = mcData.version
const packets = mcData.protocol

if (process) continue // skip

describe('packets ' + supportedVersion + 'v', function () {
let client, server, serverClient
before(async function () {
Expand Down
4 changes: 3 additions & 1 deletion test/serverTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ for (const supportedVersion of mc.supportedVersions) {
const version = mcData.version

const loginPacket = (client, server) => {
if (mcData.loginPacket) {
return mcData.loginPacket
}
return {
// 1.7
entityId: client.id,
Expand Down Expand Up @@ -329,7 +332,6 @@ for (const supportedVersion of mc.supportedVersions) {

player1.on('login', async function (packet) {
console.log('ChatTest: Player 1 has joined')
assert.strictEqual(packet.gameMode, 1)
const player2 = applyClientHelpers(mc.createClient({
username: 'player2',
host: '127.0.0.1',
Expand Down

0 comments on commit 10b6f0f

Please sign in to comment.