Network (rnet)
Packets
The packet object passed to handlers, and how to read and change its fields.
Handlers, rnet.wait, rnet.record and rnet.new all give you a packet object.
Basic properties
| Property | Meaning |
|---|---|
p.name | The Mojang packet id, for example move_player_pos |
p.class | The class name |
p.dir | "out" or "in" |
p.time | When it was seen |
p.act | What happened to it: "pass", "blocked", ... |
p.blocked | true if a rule blocked it |
p.held | true if it is being held for intercept |
p.h | The internal handle number |
tostring(p) gives text such as Packet<out move_player_pos #12>.
Reading fields
Fields are read like normal properties:
rnet.on("move_player_pos", function(p)
print(p.x, p.y, p.z, p.onGround)
end, "out")
A field can be reached in three ways:
- By its friendly name from a schema (
p.x,p.message,p.health). - By its raw name as it exists in the game class.
- By type and position: a letter for the type, followed by a count.
p.d1is the first double,p.d2the second,p.i1the first int.
| Letter | Type |
|---|---|
b | boolean |
y | byte |
h | short |
i | int |
l | long |
f | float |
d | double |
s | string |
o | other (objects) |
A friendly name is only used when the packet's real field types match the schema, so a wrong guess can never mislabel data. When a packet has no schema, use p:dump() to see what it holds.
Writing fields
Assign to a field to change it. This is most useful inside rnet.intercept:
rnet.intercept("move_player_*", function(p)
p.onGround = true
end, "out")
Methods
p:get(name)
Reads a field by name.
p:set(name, value)
Writes a field. Returns true on success. p.name = value does the same.
p:fields()
Returns a table of every field, by friendly and raw names, plus _class.
p:list()
Returns every field value as an array, in order.
p:typed(kind)
Returns the values of one type as an array. kind is a letter from the table above or a name: "double", "float", "int", "long", "short", "byte", "bool", "string", "other".
p:doubles(), p:ints(), p:floats(), p:strings(), p:bools()
Shortcuts for typed.
p:is(pattern)
Returns true if the packet matches a pattern.
p:text()
Joins the packet's string fields with spaces. Handy for chat packets.
p:dump()
Returns a one-line description like move_player_pos{x=1.000, y=64.000, ...}.
Keeping and releasing
Packets are only valid while your handler runs, unless you keep them.
p:keep()
Keeps the packet alive after your handler returns. Returns the packet. Call p:free() when you are finished.
p:free()
Releases a kept packet.
p:send()
Sends the packet to the server.
p:receive()
Injects the packet as if the server had sent it.
p:release()
Lets a packet that is held by rnet.intercept continue.
p:drop()
Throws away a held packet.