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 Client = mc.Client
|
|
|
|
const Server = mc.Server
|
|
|
|
const net = require('net')
|
|
|
|
const assert = require('power-assert')
|
|
|
|
const getFieldInfo = require('protodef').utils.getFieldInfo
|
|
|
|
const getField = require('protodef').utils.getField
|
|
|
|
|
2021-06-28 22:58:21 -04:00
|
|
|
const { getPort } = require('./common/util')
|
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
function evalCount (count, fields) {
|
2019-08-20 18:14:37 -04:00
|
|
|
if (fields[count.field] in count.map) { return count.map[fields[count.field]] }
|
|
|
|
return count.default
|
2015-10-01 20:02:54 -04:00
|
|
|
}
|
|
|
|
|
2018-08-10 18:04:21 -04:00
|
|
|
const slotValue = {
|
2018-10-16 16:23:16 -04:00
|
|
|
present: true,
|
2018-08-10 18:04:21 -04:00
|
|
|
blockId: 5,
|
|
|
|
itemCount: 56,
|
|
|
|
itemDamage: 2,
|
|
|
|
nbtData: {
|
|
|
|
type: 'compound',
|
|
|
|
name: 'test',
|
|
|
|
value: {
|
2018-09-24 16:07:34 -04:00
|
|
|
test1: { type: 'int', value: 4 },
|
|
|
|
test2: { type: 'long', value: [12, 42] },
|
|
|
|
test3: { type: 'byteArray', value: [32] },
|
|
|
|
test4: { type: 'string', value: 'ohi' },
|
|
|
|
test5: { type: 'list', value: { type: 'int', value: [4] } },
|
|
|
|
test6: { type: 'compound', value: { test: { type: 'int', value: 4 } } },
|
|
|
|
test7: { type: 'intArray', value: [12, 42] }
|
2018-08-10 18:04:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-13 08:03:52 -04:00
|
|
|
const values = {
|
2019-08-03 19:29:14 -04:00
|
|
|
i32: 123456,
|
|
|
|
i16: -123,
|
|
|
|
u16: 123,
|
|
|
|
varint: 1,
|
2022-08-15 18:57:26 -04:00
|
|
|
varlong: -20,
|
2019-08-03 19:29:14 -04:00
|
|
|
i8: -10,
|
|
|
|
u8: 8,
|
|
|
|
string: 'hi hi this is my client string',
|
|
|
|
buffer: Buffer.alloc(8),
|
|
|
|
array: function (typeArgs, context) {
|
2018-05-13 16:50:16 -04:00
|
|
|
let count
|
2019-12-29 17:17:38 -05:00
|
|
|
if (typeof typeArgs.count === 'number') {
|
|
|
|
count = typeArgs.count
|
|
|
|
} else if (typeof typeArgs.count === 'object') {
|
2018-05-13 18:20:21 -04:00
|
|
|
count = evalCount(typeArgs.count, context)
|
|
|
|
} else if (typeArgs.count !== undefined) {
|
|
|
|
count = getField(typeArgs.count, context)
|
|
|
|
} else if (typeArgs.countType !== undefined) {
|
|
|
|
count = 1
|
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
const arr = []
|
2015-10-01 20:02:54 -04:00
|
|
|
while (count > 0) {
|
2018-05-13 16:50:16 -04:00
|
|
|
arr.push(getValue(typeArgs.type, context))
|
|
|
|
count--
|
2015-10-01 20:02:54 -04:00
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
return arr
|
2015-10-01 20:02:54 -04:00
|
|
|
},
|
2019-08-03 19:29:14 -04:00
|
|
|
container: function (typeArgs, context) {
|
2017-07-13 08:03:52 -04:00
|
|
|
const results = {
|
2018-05-13 16:50:16 -04:00
|
|
|
'..': context
|
|
|
|
}
|
|
|
|
Object.keys(typeArgs).forEach(function (index) {
|
2019-05-19 19:46:02 -04:00
|
|
|
const v = typeArgs[index].name === 'type' && typeArgs[index].type === 'string' && typeArgs[2] !== undefined &&
|
2019-08-20 18:14:37 -04:00
|
|
|
typeArgs[2].type !== undefined
|
|
|
|
? (typeArgs[2].type[1].fields['minecraft:crafting_shapeless'] === undefined ? 'crafting_shapeless' : 'minecraft:crafting_shapeless')
|
2018-05-13 18:20:21 -04:00
|
|
|
: getValue(typeArgs[index].type, results)
|
2018-05-13 16:50:16 -04:00
|
|
|
if (typeArgs[index].anon) {
|
2017-06-19 08:34:13 -04:00
|
|
|
Object.keys(v).forEach(key => {
|
2018-05-13 16:50:16 -04:00
|
|
|
results[key] = v[key]
|
2017-06-19 08:34:13 -04:00
|
|
|
})
|
2018-05-13 16:50:16 -04:00
|
|
|
} else {
|
|
|
|
results[typeArgs[index].name] = v
|
2017-06-19 08:34:13 -04:00
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2020-05-16 10:57:04 -04:00
|
|
|
delete results['..']
|
2018-05-13 16:50:16 -04:00
|
|
|
return results
|
2015-10-01 20:02:54 -04:00
|
|
|
},
|
2019-08-03 19:29:14 -04:00
|
|
|
count: 1, // TODO : might want to set this to a correct value
|
|
|
|
bool: true,
|
|
|
|
f64: 99999.2222,
|
|
|
|
f32: -333.444,
|
|
|
|
slot: slotValue,
|
|
|
|
nbt: {
|
2018-05-13 16:50:16 -04:00
|
|
|
type: 'compound',
|
|
|
|
name: 'test',
|
|
|
|
value: {
|
2018-09-24 16:07:34 -04:00
|
|
|
test1: { type: 'int', value: 4 },
|
|
|
|
test2: { type: 'long', value: [12, 42] },
|
|
|
|
test3: { type: 'byteArray', value: [32] },
|
|
|
|
test4: { type: 'string', value: 'ohi' },
|
|
|
|
test5: { type: 'list', value: { type: 'int', value: [4] } },
|
|
|
|
test6: { type: 'compound', value: { test: { type: 'int', value: 4 } } },
|
|
|
|
test7: { type: 'intArray', value: [12, 42] }
|
2015-11-15 11:48:25 -05:00
|
|
|
}
|
|
|
|
},
|
2019-08-03 19:29:14 -04:00
|
|
|
optionalNbt: {
|
2018-05-13 16:50:16 -04:00
|
|
|
type: 'compound',
|
|
|
|
name: 'test',
|
|
|
|
value: {
|
2018-09-24 16:07:34 -04:00
|
|
|
test1: { type: 'int', value: 4 },
|
|
|
|
test2: { type: 'long', value: [12, 42] },
|
|
|
|
test3: { type: 'byteArray', value: [32] },
|
|
|
|
test4: { type: 'string', value: 'ohi' },
|
|
|
|
test5: { type: 'list', value: { type: 'int', value: [4] } },
|
|
|
|
test6: { type: 'compound', value: { test: { type: 'int', value: 4 } } },
|
|
|
|
test7: { type: 'intArray', value: [12, 42] }
|
2015-11-15 11:48:25 -05:00
|
|
|
}
|
|
|
|
},
|
2019-08-03 19:29:14 -04:00
|
|
|
compressedNbt: {
|
2018-05-13 16:50:16 -04:00
|
|
|
type: 'compound',
|
|
|
|
name: 'test',
|
|
|
|
value: {
|
2018-09-24 16:07:34 -04:00
|
|
|
test1: { type: 'int', value: 4 },
|
|
|
|
test2: { type: 'long', value: [12, 42] },
|
|
|
|
test3: { type: 'byteArray', value: [32] },
|
|
|
|
test4: { type: 'string', value: 'ohi' },
|
|
|
|
test5: { type: 'list', value: { type: 'int', value: [4] } },
|
|
|
|
test6: { type: 'compound', value: { test: { type: 'int', value: 4 } } },
|
|
|
|
test7: { type: 'intArray', value: [12, 42] }
|
2016-02-18 19:47:44 -05:00
|
|
|
}
|
|
|
|
},
|
2019-08-03 19:29:14 -04:00
|
|
|
i64: [0, 1],
|
|
|
|
u64: [0, 1],
|
|
|
|
entityMetadata: [
|
2018-09-24 16:07:34 -04:00
|
|
|
{ key: 17, value: 0, type: 0 }
|
2015-10-01 20:02:54 -04:00
|
|
|
],
|
2020-06-22 21:06:04 -04:00
|
|
|
topBitSetTerminatedArray: [
|
|
|
|
{
|
|
|
|
slot: 0,
|
|
|
|
item: slotValue
|
|
|
|
},
|
|
|
|
{
|
|
|
|
slot: 1,
|
|
|
|
item: slotValue
|
|
|
|
}
|
|
|
|
],
|
2019-08-03 19:29:14 -04:00
|
|
|
objectData: {
|
2015-10-01 20:02:54 -04:00
|
|
|
intField: 9,
|
|
|
|
velocityX: 1,
|
|
|
|
velocityY: 2,
|
2015-11-30 16:19:56 -05:00
|
|
|
velocityZ: 3
|
2015-10-01 20:02:54 -04:00
|
|
|
},
|
2019-08-03 19:29:14 -04:00
|
|
|
UUID: '00112233-4455-6677-8899-aabbccddeeff',
|
|
|
|
position: { x: 12, y: 100, z: 4382821 },
|
|
|
|
position_ibi: { x: 12, y: 100, z: 4382821 },
|
|
|
|
position_isi: { x: 12, y: 100, z: 4382821 },
|
|
|
|
position_iii: { x: 12, y: 100, z: 4382821 },
|
|
|
|
restBuffer: Buffer.alloc(0),
|
|
|
|
switch: function (typeArgs, context) {
|
2018-05-13 16:50:16 -04:00
|
|
|
const i = typeArgs.fields[getField(typeArgs.compareTo, context)]
|
2018-05-13 18:20:21 -04:00
|
|
|
if (i === undefined) {
|
|
|
|
if (typeArgs.default === undefined) {
|
|
|
|
throw new Error("couldn't find the field " + typeArgs.compareTo +
|
|
|
|
' of the compareTo and the default is not defined')
|
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
return getValue(typeArgs.default, context)
|
|
|
|
} else { return getValue(i, context) }
|
2015-10-01 20:02:54 -04:00
|
|
|
},
|
2019-08-03 19:29:14 -04:00
|
|
|
option: function (typeArgs, context) {
|
2018-05-13 16:50:16 -04:00
|
|
|
return getValue(typeArgs, context)
|
2017-06-19 08:34:13 -04:00
|
|
|
},
|
2019-08-03 19:29:14 -04:00
|
|
|
bitfield: function (typeArgs, context) {
|
2018-05-13 16:50:16 -04:00
|
|
|
const results = {}
|
|
|
|
Object.keys(typeArgs).forEach(function (index) {
|
|
|
|
results[typeArgs[index].name] = 1
|
|
|
|
})
|
|
|
|
return results
|
2018-01-01 15:01:23 -05:00
|
|
|
},
|
2019-08-03 19:29:14 -04:00
|
|
|
tags: [{ tagName: 'hi', entries: [1, 2, 3, 4, 5] }],
|
|
|
|
ingredient: [slotValue],
|
2021-12-12 13:07:50 -05:00
|
|
|
particleData: null,
|
2022-07-21 16:56:34 -04:00
|
|
|
chunkBlockEntity: { x: 10, y: 11, z: 12, type: 25 },
|
|
|
|
command_node: {
|
|
|
|
flags: {
|
|
|
|
has_custom_suggestions: 1,
|
|
|
|
has_redirect_node: 1,
|
|
|
|
has_command: 1,
|
|
|
|
command_node_type: 2
|
|
|
|
},
|
|
|
|
children: [23, 29],
|
|
|
|
redirectNode: 83,
|
|
|
|
extraNodeData: {
|
|
|
|
name: 'command_node name',
|
|
|
|
parser: 'brigadier:double',
|
|
|
|
properties: {
|
|
|
|
flags: {
|
|
|
|
max_present: 1,
|
|
|
|
min_present: 1
|
|
|
|
},
|
|
|
|
min: -5.0,
|
|
|
|
max: 256.0
|
|
|
|
},
|
|
|
|
suggestionType: 'minecraft:summonable_entities'
|
|
|
|
}
|
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
}
|
2015-10-01 20:02:54 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
function getValue (_type, packet) {
|
|
|
|
const fieldInfo = getFieldInfo(_type)
|
2018-05-13 18:20:21 -04:00
|
|
|
if (typeof values[fieldInfo.type] === 'function') {
|
|
|
|
return values[fieldInfo.type](fieldInfo.typeArgs, packet)
|
|
|
|
} else if (values[fieldInfo.type] !== undefined) {
|
|
|
|
return values[fieldInfo.type]
|
|
|
|
} else if (fieldInfo.type !== 'void') {
|
|
|
|
throw new Error('No value for type ' + fieldInfo.type)
|
|
|
|
}
|
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
|
|
|
|
const packets = mcData.protocol
|
|
|
|
|
|
|
|
describe('packets ' + version.minecraftVersion, function () {
|
|
|
|
let client, server, serverClient
|
2021-06-28 22:58:21 -04:00
|
|
|
before(async function () {
|
|
|
|
PORT = await getPort()
|
2018-05-13 16:50:16 -04:00
|
|
|
server = new Server(version.minecraftVersion)
|
2021-06-28 22:58:21 -04:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
console.log(`Using port for tests: ${PORT}`)
|
|
|
|
server.once('listening', function () {
|
|
|
|
server.once('connection', function (c) {
|
|
|
|
serverClient = c
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
client = new Client(false, version.minecraftVersion)
|
|
|
|
client.setSocket(net.connect(PORT, 'localhost'))
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2021-06-28 22:58:21 -04:00
|
|
|
server.listen(PORT, 'localhost')
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
after(function (done) {
|
|
|
|
client.on('end', function () {
|
|
|
|
server.on('close', done)
|
|
|
|
server.close()
|
|
|
|
})
|
|
|
|
client.end()
|
|
|
|
})
|
|
|
|
let packetInfo
|
2018-05-13 18:20:21 -04:00
|
|
|
Object.keys(packets).filter(function (state) { return state !== 'types' })
|
|
|
|
.forEach(function (state) {
|
|
|
|
Object.keys(packets[state]).forEach(function (direction) {
|
|
|
|
Object.keys(packets[state][direction].types)
|
|
|
|
.filter(function (packetName) {
|
|
|
|
return packetName !== 'packet' &&
|
|
|
|
packetName.startsWith('packet_')
|
|
|
|
})
|
|
|
|
.forEach(function (packetName) {
|
|
|
|
packetInfo = packets[state][direction].types[packetName]
|
|
|
|
packetInfo = packetInfo || null
|
|
|
|
it(state + ',' + (direction === 'toServer' ? 'Server' : 'Client') + 'Bound,' + packetName,
|
|
|
|
callTestPacket(packetName.substr(7), packetInfo, state, direction === 'toServer'))
|
|
|
|
})
|
|
|
|
})
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
|
|
|
function callTestPacket (packetName, packetInfo, state, toServer) {
|
|
|
|
return function (done) {
|
|
|
|
client.state = state
|
|
|
|
serverClient.state = state
|
|
|
|
testPacket(packetName, packetInfo, state, toServer, done)
|
|
|
|
}
|
2015-10-01 20:02:54 -04:00
|
|
|
}
|
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
function testPacket (packetName, packetInfo, state, toServer, done) {
|
2015-10-01 20:02:54 -04:00
|
|
|
// empty object uses default values
|
2018-05-13 16:50:16 -04:00
|
|
|
const packet = getValue(packetInfo, {})
|
|
|
|
if (toServer) {
|
|
|
|
serverClient.once(packetName, function (receivedPacket) {
|
2015-10-01 20:02:54 -04:00
|
|
|
try {
|
2018-05-13 16:50:16 -04:00
|
|
|
assertPacketsMatch(packet, receivedPacket)
|
2015-10-01 20:02:54 -04:00
|
|
|
} catch (e) {
|
2018-05-13 16:50:16 -04:00
|
|
|
console.log(packet, receivedPacket)
|
|
|
|
throw e
|
2015-10-01 20:02:54 -04:00
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
client.write(packetName, packet)
|
2015-10-01 20:02:54 -04:00
|
|
|
} else {
|
2018-05-13 16:50:16 -04:00
|
|
|
client.once(packetName, function (receivedPacket) {
|
|
|
|
assertPacketsMatch(packet, receivedPacket)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
serverClient.write(packetName, packet)
|
2015-10-01 20:02:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
function assertPacketsMatch (p1, p2) {
|
|
|
|
packetInfo.forEach(function (field) {
|
|
|
|
assert.deepEqual(p1[field], p2[field])
|
|
|
|
})
|
|
|
|
Object.keys(p1).forEach(function (field) {
|
2018-05-13 18:20:21 -04:00
|
|
|
if (p1[field] !== undefined) {
|
|
|
|
assert.ok(field in p2, 'field ' + field +
|
|
|
|
' missing in p2, in p1 it has value ' + JSON.stringify(p1[field]))
|
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
|
|
|
Object.keys(p2).forEach(function (field) {
|
2020-05-16 10:57:04 -04:00
|
|
|
if (p2[field] !== undefined) {
|
|
|
|
assert.ok(field in p1, 'field ' + field + ' missing in p1, in p2 it has value ' +
|
|
|
|
JSON.stringify(p2[field]))
|
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2015-10-01 20:02:54 -04:00
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2021-06-12 22:54:16 -04:00
|
|
|
}
|