index custom packets by version

This commit is contained in:
Romain Beaumont 2016-02-20 16:43:08 +01:00
parent eba07e6b90
commit 91ccac44df
3 changed files with 22 additions and 18 deletions
doc
examples/client_custom_packets
src/transforms

View file

@ -18,7 +18,7 @@ automatically logged in and validated against mojang's auth.
* maxPlayers : default to 20
* keepAlive : send keep alive packets : default to true
* version : 1.8 or 1.9 : default to 1.8
* customPackets (optional) : an object index by state/direction/name, see client_custom_packet for an example
* customPackets (optional) : an object index by version/state/direction/name, see client_custom_packet for an example
## mc.Server(version,[customPackets])
@ -75,7 +75,7 @@ Returns a `Client` instance and perform login.
* keepAlive : send keep alive packets : default to true
* checkTimeoutInterval : default to `10*1000` (10s), check if keepalive received at that period, disconnect otherwise.
* version : 1.8 or 1.9 or false (to auto-negotiate): default to 1.8
* customPackets (optional) : an object index by state/direction/name, see client_custom_packet for an example
* customPackets (optional) : an object index by version/state/direction/name, see client_custom_packet for an example
## mc.Client(isServer,version,[customPackets])

View file

@ -6,20 +6,22 @@ if(process.argv.length < 4 || process.argv.length > 6) {
}
var customPackets={
"play":{
"toClient":{
"my_custom_packet": {
"id": "0x7A",
"fields": [
{
"name": "age",
"type": "i64"
},
{
"name": "time",
"type": "i64"
}
]
"1.8": {
"play": {
"toClient": {
"my_custom_packet": {
"id": "0x7A",
"fields": [
{
"name": "age",
"type": "i64"
},
{
"name": "time",
"type": "i64"
}
]
}
}
}
}

View file

@ -41,7 +41,8 @@ function createSerializer({ state = states.HANDSHAKING, isServer = false , versi
const mcData=require("minecraft-data")(version);
const direction = !isServer ? 'toServer' : 'toClient';
const packets = mcData.protocol.states[state][direction];
if(customPackets && customPackets[state] && customPackets[state][direction]) Object.keys(customPackets[state][direction]).forEach(name => packets[name]=customPackets[state][direction][name]);
const v=mcData.version.majorVersion;
if(customPackets && customPackets[v] && customPackets[v][state] && customPackets[v][state][direction]) Object.keys(customPackets[v][state][direction]).forEach(name => packets[name]=customPackets[v][state][direction][name]);
const proto=createProtocol(mcData.protocol.types,packets);
return new Serializer(proto,"packet");
}
@ -52,7 +53,8 @@ function createDeserializer({ state = states.HANDSHAKING, isServer = false,
const mcData=require("minecraft-data")(version);
const direction = isServer ? "toServer" : "toClient";
const packets = mcData.protocol.states[state][direction];
if(customPackets && customPackets[state] && customPackets[state][direction]) Object.keys(customPackets[state][direction]).forEach(name => packets[name]=customPackets[state][direction][name]);
const v=mcData.version.majorVersion;
if(customPackets && customPackets[v][state] && customPackets[v][state][direction]) Object.keys(customPackets[v][state][direction]).forEach(name => packets[name]=customPackets[v][state][direction][name]);
const proto=createProtocol(mcData.protocol.types,packets);
return new Parser(proto,"packet");
}