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 * maxPlayers : default to 20
* keepAlive : send keep alive packets : default to true * keepAlive : send keep alive packets : default to true
* version : 1.8 or 1.9 : default to 1.8 * 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]) ## mc.Server(version,[customPackets])
@ -75,7 +75,7 @@ Returns a `Client` instance and perform login.
* keepAlive : send keep alive packets : default to true * keepAlive : send keep alive packets : default to true
* checkTimeoutInterval : default to `10*1000` (10s), check if keepalive received at that period, disconnect otherwise. * 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 * 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]) ## mc.Client(isServer,version,[customPackets])

View file

@ -6,20 +6,22 @@ if(process.argv.length < 4 || process.argv.length > 6) {
} }
var customPackets={ var customPackets={
"play":{ "1.8": {
"toClient":{ "play": {
"my_custom_packet": { "toClient": {
"id": "0x7A", "my_custom_packet": {
"fields": [ "id": "0x7A",
{ "fields": [
"name": "age", {
"type": "i64" "name": "age",
}, "type": "i64"
{ },
"name": "time", {
"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 mcData=require("minecraft-data")(version);
const direction = !isServer ? 'toServer' : 'toClient'; const direction = !isServer ? 'toServer' : 'toClient';
const packets = mcData.protocol.states[state][direction]; 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); const proto=createProtocol(mcData.protocol.types,packets);
return new Serializer(proto,"packet"); return new Serializer(proto,"packet");
} }
@ -52,7 +53,8 @@ function createDeserializer({ state = states.HANDSHAKING, isServer = false,
const mcData=require("minecraft-data")(version); const mcData=require("minecraft-data")(version);
const direction = isServer ? "toServer" : "toClient"; const direction = isServer ? "toServer" : "toClient";
const packets = mcData.protocol.states[state][direction]; 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); const proto=createProtocol(mcData.protocol.types,packets);
return new Parser(proto,"packet"); return new Parser(proto,"packet");
} }