Skip to content

Instantly share code, notes, and snippets.

@tomjack
Created October 2, 2010 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomjack/bfe2264c4e44b164a3c5 to your computer and use it in GitHub Desktop.
Save tomjack/bfe2264c4e44b164a3c5 to your computer and use it in GitHub Desktop.
(defpackets alpha-protocol
(keep-alive 0x00)
(login 0x01
[:player-id :int]
[:server-name :string]
[:motd :string])
(handshake 0x02
[:name :string])
(chat 0x03
[:message :string])
(update-time 0x04
[:time :long])
(player-inventory 0x05
[:type :int]
[:count :short]
[:payload :item-payload :count count])
(spawn-position 0x06
[:x :int]
[:y :int]
[:z :int])
(flying 0x0A
[:flying? :bool])
(player-position 0x0B
[:x :double]
[:y :double]
[:stance :double]
[:z :double]
[:flying? :bool])
(player-look 0x0C
[:rotation :float]
[:pitch :float]
[:flying? :bool])
(player-position-look 0x0D
[:x :double]
[:y :double]
[:stance :double]
[:z :double]
[:rotation :float]
[:pitch :float]
[:flying? :bool])
(block-dig 0x0E
[:status :byte]
[:x :int]
[:y :byte]
[:z :int]
[:direction :byte])
(place-item 0x0F
[:type :short]
[:x :int]
[:y :byte]
[:z :int]
[:direction :byte])
(switch-item 0x10
[:entity :int]
[:item :short])
(inventory-add 0x11
[:item :short]
[:amount :byte]
[:life :short])
(arm-animation 0x12
[:entity :int]
[:forward-animation? :bool])
(entity-spawn 0x14
[:uid :int]
[:player :string]
[:x :int]
[:y :int]
[:z :int]
[:rotation :byte]
[:pitch :byte]
[:current-item :short])
(pickup-spawn 0x15
[:uid :int]
[:item :short]
[:count :byte]
[:x :int]
[:y :int]
[:z :int]
[:rotation :byte]
[:pitch :byte]
[:roll :byte])
(collect-item 0x16
[:collected :int]
[:collector :int])
(add-object 0x17
[:uid :int]
[:type :byte]
[:x :int]
[:y :int]
[:z :int])
(mob-spawn 0x18
[:uid :int]
[:type :byte]
;; coords in pixels
[:x :int]
[:y :int]
[:z :int]
[:rot :byte]
[:pitch :byte])
(destroy-entity 0x1D
[:uid :int])
(entity 0x1E
[:uid :int])
(relative-move 0x1F
[:uid :int]
[:x :byte]
[:y :byte]
[:z :byte])
(entity-look 0x20
[:uid :int]
[:rotation :byte]
[:pitch :byte])
(relative-move-look 0x21
[:uid :int]
[:x :byte]
[:y :byte]
[:z :byte]
[:rotation :byte]
[:pitch :byte])
(entity-teleport 0x22
[:uid :int]
[:x :int]
[:y :int]
[:z :int]
[:rotation :byte]
[:pitch :byte])
(pre-chunk 0x32
[:x :int]
[:z :int]
[:mode :bool])
(map-chunk 0x33
[:x :int]
[:y :short]
[:z :int]
[:size-x :byte]
[:size-y :byte]
[:size-z :byte]
[:chunk-size :int]
[:chunk :byte-array :length chunk-size])
(multi-block-change 0x34
[:chunk-x :int]
[:chunk-z :int]
[:array-size :short]
[:coords-array :coords-array :count array-size]
[:type-array :bytes :count array-size]
[:metadata-array :bytes :count array-size])
(block-change 0x35
[:x :int]
[:y :byte]
[:z :int]
[:type :byte]
[:metadata :byte])
(complex-entities 0x3B
[:x :int]
[:y :short]
[:z :int]
[:payload-length :short]
[:payload :byte-array :length payload-length])
(disconnect -0x01
[:message :string]))
(comment above macroexpands to
(defn alpha-protocol [data-in]
(let [packet-id__2022__auto__ (.readByte data-in)]
(case
packet-id__2022__auto__
0
(vector :keep-alive (let [] (into {} [])))
1
(vector
:login
(let [player-id (.readInt data-in)
server-name (.readUTF data-in)
motd (.readUTF data-in)]
(into
{}
[[:player-id player-id]
[:server-name server-name]
[:motd motd]])))
2
(vector
:handshake
(let [name (.readUTF data-in)] (into {} [[:name name]])))
3
(vector
:chat
(let [message (.readUTF data-in)]
(into {} [[:message message]])))
4
(vector
:update-time
(let [time (.readLong data-in)] (into {} [[:time time]])))
5
(vector
:player-inventory
(let [type (.readInt data-in)
count (.readShort data-in)
payload (loop [items__1961__auto__ nil
count__1962__auto__ count]
(if (pos? count__1962__auto__)
(let [item-id__1963__auto__
(.readShort data-in)
item__1964__auto__
(if
(= item-id__1963__auto__ -1)
{:item -1}
{:health (.readShort data-in),
:item -1,
:count (.readByte data-in)})]
(recur
(conj
items__1961__auto__
item__1964__auto__)
(dec count__1962__auto__)))
items__1961__auto__))]
(into {} [[:type type] [:count count] [:payload payload]])))
6
(vector
:spawn-position
(let [x (.readInt data-in)
y (.readInt data-in)
z (.readInt data-in)]
(into {} [[:x x] [:y y] [:z z]])))
10
(vector
:flying
(let [flying? (.readBoolean data-in)]
(into {} [[:flying? flying?]])))
11
(vector
:player-position
(let [x (.readDouble data-in)
y (.readDouble data-in)
stance (.readDouble data-in)
z (.readDouble data-in)
flying? (.readBoolean data-in)]
(into
{}
[[:x x]
[:y y]
[:stance stance]
[:z z]
[:flying? flying?]])))
12
(vector
:player-look
(let [rotation (.readFloat data-in)
pitch (.readFloat data-in)
flying? (.readBoolean data-in)]
(into
{}
[[:rotation rotation] [:pitch pitch] [:flying? flying?]])))
13
(vector
:player-position-look
(let [x (.readDouble data-in)
y (.readDouble data-in)
stance (.readDouble data-in)
z (.readDouble data-in)
rotation (.readFloat data-in)
pitch (.readFloat data-in)
flying? (.readBoolean data-in)]
(into
{}
[[:x x]
[:y y]
[:stance stance]
[:z z]
[:rotation rotation]
[:pitch pitch]
[:flying? flying?]])))
14
(vector
:block-dig
(let [status (.readByte data-in)
x (.readInt data-in)
y (.readByte data-in)
z (.readInt data-in)
direction (.readByte data-in)]
(into
{}
[[:status status]
[:x x]
[:y y]
[:z z]
[:direction direction]])))
15
(vector
:place-item
(let [type (.readShort data-in)
x (.readInt data-in)
y (.readByte data-in)
z (.readInt data-in)
direction (.readByte data-in)]
(into
{}
[[:type type]
[:x x]
[:y y]
[:z z]
[:direction direction]])))
16
(vector
:switch-item
(let [entity (.readInt data-in) item (.readShort data-in)]
(into {} [[:entity entity] [:item item]])))
17
(vector
:inventory-add
(let [item (.readShort data-in)
amount (.readByte data-in)
life (.readShort data-in)]
(into {} [[:item item] [:amount amount] [:life life]])))
18
(vector
:arm-animation
(let [entity (.readInt data-in)
forward-animation? (.readBoolean data-in)]
(into
{}
[[:entity entity]
[:forward-animation? forward-animation?]])))
20
(vector
:entity-spawn
(let [uid (.readInt data-in)
player (.readUTF data-in)
x (.readInt data-in)
y (.readInt data-in)
z (.readInt data-in)
rotation (.readByte data-in)
pitch (.readByte data-in)
current-item (.readShort data-in)]
(into
{}
[[:uid uid]
[:player player]
[:x x]
[:y y]
[:z z]
[:rotation rotation]
[:pitch pitch]
[:current-item current-item]])))
21
(vector
:pickup-spawn
(let [uid (.readInt data-in)
item (.readShort data-in)
count (.readByte data-in)
x (.readInt data-in)
y (.readInt data-in)
z (.readInt data-in)
rotation (.readByte data-in)
pitch (.readByte data-in)
roll (.readByte data-in)]
(into
{}
[[:uid uid]
[:item item]
[:count count]
[:x x]
[:y y]
[:z z]
[:rotation rotation]
[:pitch pitch]
[:roll roll]])))
22
(vector
:collect-item
(let [collected (.readInt data-in)
collector (.readInt data-in)]
(into {} [[:collected collected] [:collector collector]])))
23
(vector
:add-object
(let [uid (.readInt data-in)
type (.readByte data-in)
x (.readInt data-in)
y (.readInt data-in)
z (.readInt data-in)]
(into {} [[:uid uid] [:type type] [:x x] [:y y] [:z z]])))
24
(vector
:mob-spawn
(let [uid (.readInt data-in)
type (.readByte data-in)
x (.readInt data-in)
y (.readInt data-in)
z (.readInt data-in)
rot (.readByte data-in)
pitch (.readByte data-in)]
(into
{}
[[:uid uid]
[:type type]
[:x x]
[:y y]
[:z z]
[:rot rot]
[:pitch pitch]])))
29
(vector
:destroy-entity
(let [uid (.readInt data-in)] (into {} [[:uid uid]])))
30
(vector
:entity
(let [uid (.readInt data-in)] (into {} [[:uid uid]])))
31
(vector
:relative-move
(let [uid (.readInt data-in)
x (.readByte data-in)
y (.readByte data-in)
z (.readByte data-in)]
(into {} [[:uid uid] [:x x] [:y y] [:z z]])))
32
(vector
:entity-look
(let [uid (.readInt data-in)
rotation (.readByte data-in)
pitch (.readByte data-in)]
(into {} [[:uid uid] [:rotation rotation] [:pitch pitch]])))
33
(vector
:relative-move-look
(let [uid (.readInt data-in)
x (.readByte data-in)
y (.readByte data-in)
z (.readByte data-in)
rotation (.readByte data-in)
pitch (.readByte data-in)]
(into
{}
[[:uid uid]
[:x x]
[:y y]
[:z z]
[:rotation rotation]
[:pitch pitch]])))
34
(vector
:entity-teleport
(let [uid (.readInt data-in)
x (.readInt data-in)
y (.readInt data-in)
z (.readInt data-in)
rotation (.readByte data-in)
pitch (.readByte data-in)]
(into
{}
[[:uid uid]
[:x x]
[:y y]
[:z z]
[:rotation rotation]
[:pitch pitch]])))
50
(vector
:pre-chunk
(let [x (.readInt data-in)
z (.readInt data-in)
mode (.readBoolean data-in)]
(into {} [[:x x] [:z z] [:mode mode]])))
51
(vector
:map-chunk
(let [x (.readInt data-in)
y (.readShort data-in)
z (.readInt data-in)
size-x (.readByte data-in)
size-y (.readByte data-in)
size-z (.readByte data-in)
chunk-size (.readInt data-in)
chunk (let [bytes__1960__auto__ (byte-array chunk-size)]
(.read data-in bytes__1960__auto__ 0 chunk-size)
bytes__1960__auto__)]
(into
{}
[[:x x]
[:y y]
[:z z]
[:size-x size-x]
[:size-y size-y]
[:size-z size-z]
[:chunk-size chunk-size]
[:chunk chunk]])))
52
(vector
:multi-block-change
(let [chunk-x (.readInt data-in)
chunk-z (.readInt data-in)
array-size (.readShort data-in)
coords-array (vec
(repeatedly
array-size
(fn
[]
(let
[short__1965__auto__
(.readShort data-in)]
[(bit-shift-right
(bit-and
short__1965__auto__
61440)
12)
(bit-and short__1965__auto__ 255)
(bit-shift-right
(bit-and
short__1965__auto__
3840)
8)]))))
type-array (vec
(repeatedly
array-size
(fn [] (.readByte data-in))))
metadata-array (vec
(repeatedly
array-size
(fn [] (.readByte data-in))))]
(into
{}
[[:chunk-x chunk-x]
[:chunk-z chunk-z]
[:array-size array-size]
[:coords-array coords-array]
[:type-array type-array]
[:metadata-array metadata-array]])))
53
(vector
:block-change
(let [x (.readInt data-in)
y (.readByte data-in)
z (.readInt data-in)
type (.readByte data-in)
metadata (.readByte data-in)]
(into
{}
[[:x x] [:y y] [:z z] [:type type] [:metadata metadata]])))
59
(vector
:complex-entities
(let [x (.readInt data-in)
y (.readShort data-in)
z (.readInt data-in)
payload-length (.readShort data-in)
payload (let [bytes__1960__auto__ (byte-array
payload-length)]
(.read
data-in
bytes__1960__auto__
0
payload-length)
bytes__1960__auto__)]
(into
{}
[[:x x]
[:y y]
[:z z]
[:payload-length payload-length]
[:payload payload]])))
-1
(vector
:disconnect
(let [message (.readUTF data-in)]
(into {} [[:message message]]))))))(defn alpha-protocol [data-in]
(let [packet-id__2022__auto__ (.readByte data-in)]
(case
packet-id__2022__auto__
0
(vector :keep-alive (let [] (into {} [])))
1
(vector
:login
(let [player-id (.readInt data-in)
server-name (.readUTF data-in)
motd (.readUTF data-in)]
(into
{}
[[:player-id player-id]
[:server-name server-name]
[:motd motd]])))
2
(vector
:handshake
(let [name (.readUTF data-in)] (into {} [[:name name]])))
3
(vector
:chat
(let [message (.readUTF data-in)]
(into {} [[:message message]])))
4
(vector
:update-time
(let [time (.readLong data-in)] (into {} [[:time time]])))
5
(vector
:player-inventory
(let [type (.readInt data-in)
count (.readShort data-in)
payload (loop [items__1961__auto__ nil
count__1962__auto__ count]
(if (pos? count__1962__auto__)
(let [item-id__1963__auto__
(.readShort data-in)
item__1964__auto__
(if
(= item-id__1963__auto__ -1)
{:item -1}
{:health (.readShort data-in),
:item -1,
:count (.readByte data-in)})]
(recur
(conj
items__1961__auto__
item__1964__auto__)
(dec count__1962__auto__)))
items__1961__auto__))]
(into {} [[:type type] [:count count] [:payload payload]])))
6
(vector
:spawn-position
(let [x (.readInt data-in)
y (.readInt data-in)
z (.readInt data-in)]
(into {} [[:x x] [:y y] [:z z]])))
10
(vector
:flying
(let [flying? (.readBoolean data-in)]
(into {} [[:flying? flying?]])))
11
(vector
:player-position
(let [x (.readDouble data-in)
y (.readDouble data-in)
stance (.readDouble data-in)
z (.readDouble data-in)
flying? (.readBoolean data-in)]
(into
{}
[[:x x]
[:y y]
[:stance stance]
[:z z]
[:flying? flying?]])))
12
(vector
:player-look
(let [rotation (.readFloat data-in)
pitch (.readFloat data-in)
flying? (.readBoolean data-in)]
(into
{}
[[:rotation rotation] [:pitch pitch] [:flying? flying?]])))
13
(vector
:player-position-look
(let [x (.readDouble data-in)
y (.readDouble data-in)
stance (.readDouble data-in)
z (.readDouble data-in)
rotation (.readFloat data-in)
pitch (.readFloat data-in)
flying? (.readBoolean data-in)]
(into
{}
[[:x x]
[:y y]
[:stance stance]
[:z z]
[:rotation rotation]
[:pitch pitch]
[:flying? flying?]])))
14
(vector
:block-dig
(let [status (.readByte data-in)
x (.readInt data-in)
y (.readByte data-in)
z (.readInt data-in)
direction (.readByte data-in)]
(into
{}
[[:status status]
[:x x]
[:y y]
[:z z]
[:direction direction]])))
15
(vector
:place-item
(let [type (.readShort data-in)
x (.readInt data-in)
y (.readByte data-in)
z (.readInt data-in)
direction (.readByte data-in)]
(into
{}
[[:type type]
[:x x]
[:y y]
[:z z]
[:direction direction]])))
16
(vector
:switch-item
(let [entity (.readInt data-in) item (.readShort data-in)]
(into {} [[:entity entity] [:item item]])))
17
(vector
:inventory-add
(let [item (.readShort data-in)
amount (.readByte data-in)
life (.readShort data-in)]
(into {} [[:item item] [:amount amount] [:life life]])))
18
(vector
:arm-animation
(let [entity (.readInt data-in)
forward-animation? (.readBoolean data-in)]
(into
{}
[[:entity entity]
[:forward-animation? forward-animation?]])))
20
(vector
:entity-spawn
(let [uid (.readInt data-in)
player (.readUTF data-in)
x (.readInt data-in)
y (.readInt data-in)
z (.readInt data-in)
rotation (.readByte data-in)
pitch (.readByte data-in)
current-item (.readShort data-in)]
(into
{}
[[:uid uid]
[:player player]
[:x x]
[:y y]
[:z z]
[:rotation rotation]
[:pitch pitch]
[:current-item current-item]])))
21
(vector
:pickup-spawn
(let [uid (.readInt data-in)
item (.readShort data-in)
count (.readByte data-in)
x (.readInt data-in)
y (.readInt data-in)
z (.readInt data-in)
rotation (.readByte data-in)
pitch (.readByte data-in)
roll (.readByte data-in)]
(into
{}
[[:uid uid]
[:item item]
[:count count]
[:x x]
[:y y]
[:z z]
[:rotation rotation]
[:pitch pitch]
[:roll roll]])))
22
(vector
:collect-item
(let [collected (.readInt data-in)
collector (.readInt data-in)]
(into {} [[:collected collected] [:collector collector]])))
23
(vector
:add-object
(let [uid (.readInt data-in)
type (.readByte data-in)
x (.readInt data-in)
y (.readInt data-in)
z (.readInt data-in)]
(into {} [[:uid uid] [:type type] [:x x] [:y y] [:z z]])))
24
(vector
:mob-spawn
(let [uid (.readInt data-in)
type (.readByte data-in)
x (.readInt data-in)
y (.readInt data-in)
z (.readInt data-in)
rot (.readByte data-in)
pitch (.readByte data-in)]
(into
{}
[[:uid uid]
[:type type]
[:x x]
[:y y]
[:z z]
[:rot rot]
[:pitch pitch]])))
29
(vector
:destroy-entity
(let [uid (.readInt data-in)] (into {} [[:uid uid]])))
30
(vector
:entity
(let [uid (.readInt data-in)] (into {} [[:uid uid]])))
31
(vector
:relative-move
(let [uid (.readInt data-in)
x (.readByte data-in)
y (.readByte data-in)
z (.readByte data-in)]
(into {} [[:uid uid] [:x x] [:y y] [:z z]])))
32
(vector
:entity-look
(let [uid (.readInt data-in)
rotation (.readByte data-in)
pitch (.readByte data-in)]
(into {} [[:uid uid] [:rotation rotation] [:pitch pitch]])))
33
(vector
:relative-move-look
(let [uid (.readInt data-in)
x (.readByte data-in)
y (.readByte data-in)
z (.readByte data-in)
rotation (.readByte data-in)
pitch (.readByte data-in)]
(into
{}
[[:uid uid]
[:x x]
[:y y]
[:z z]
[:rotation rotation]
[:pitch pitch]])))
34
(vector
:entity-teleport
(let [uid (.readInt data-in)
x (.readInt data-in)
y (.readInt data-in)
z (.readInt data-in)
rotation (.readByte data-in)
pitch (.readByte data-in)]
(into
{}
[[:uid uid]
[:x x]
[:y y]
[:z z]
[:rotation rotation]
[:pitch pitch]])))
50
(vector
:pre-chunk
(let [x (.readInt data-in)
z (.readInt data-in)
mode (.readBoolean data-in)]
(into {} [[:x x] [:z z] [:mode mode]])))
51
(vector
:map-chunk
(let [x (.readInt data-in)
y (.readShort data-in)
z (.readInt data-in)
size-x (.readByte data-in)
size-y (.readByte data-in)
size-z (.readByte data-in)
chunk-size (.readInt data-in)
chunk (let [bytes__1960__auto__ (byte-array chunk-size)]
(.read data-in bytes__1960__auto__ 0 chunk-size)
bytes__1960__auto__)]
(into
{}
[[:x x]
[:y y]
[:z z]
[:size-x size-x]
[:size-y size-y]
[:size-z size-z]
[:chunk-size chunk-size]
[:chunk chunk]])))
52
(vector
:multi-block-change
(let [chunk-x (.readInt data-in)
chunk-z (.readInt data-in)
array-size (.readShort data-in)
coords-array (vec
(repeatedly
array-size
(fn
[]
(let
[short__1965__auto__
(.readShort data-in)]
[(bit-shift-right
(bit-and
short__1965__auto__
61440)
12)
(bit-and short__1965__auto__ 255)
(bit-shift-right
(bit-and
short__1965__auto__
3840)
8)]))))
type-array (vec
(repeatedly
array-size
(fn [] (.readByte data-in))))
metadata-array (vec
(repeatedly
array-size
(fn [] (.readByte data-in))))]
(into
{}
[[:chunk-x chunk-x]
[:chunk-z chunk-z]
[:array-size array-size]
[:coords-array coords-array]
[:type-array type-array]
[:metadata-array metadata-array]])))
53
(vector
:block-change
(let [x (.readInt data-in)
y (.readByte data-in)
z (.readInt data-in)
type (.readByte data-in)
metadata (.readByte data-in)]
(into
{}
[[:x x] [:y y] [:z z] [:type type] [:metadata metadata]])))
59
(vector
:complex-entities
(let [x (.readInt data-in)
y (.readShort data-in)
z (.readInt data-in)
payload-length (.readShort data-in)
payload (let [bytes__1960__auto__ (byte-array
payload-length)]
(.read
data-in
bytes__1960__auto__
0
payload-length)
bytes__1960__auto__)]
(into
{}
[[:x x]
[:y y]
[:z z]
[:payload-length payload-length]
[:payload payload]])))
-1
(vector
:disconnect
(let [message (.readUTF data-in)]
(into {} [[:message message]])))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment