# HG changeset patch # User Steve Losh # Date 1309206264 14400 # Node ID b9f9991e61754d51ff6a4e33fdcb587a993b3e32 # Parent 6799868ce43d248b5bd77c99ed833d1e3066643d Clean. Up. EVERYTHING. diff -r 6799868ce43d -r b9f9991e6175 src/clojurecraft/core.clj --- a/src/clojurecraft/core.clj Sun Jun 26 18:47:29 2011 -0400 +++ b/src/clojurecraft/core.clj Mon Jun 27 16:24:24 2011 -0400 @@ -26,7 +26,8 @@ (defn input-handler [bot] - (let [conn (:connection bot)] + (let [conn (:connection bot) + test 1] (while (nil? (:exit @conn)) (read-packet bot))) (println "done")) diff -r 6799868ce43d -r b9f9991e6175 src/clojurecraft/in.clj --- a/src/clojurecraft/in.clj Sun Jun 26 18:47:29 2011 -0400 +++ b/src/clojurecraft/in.clj Mon Jun 27 16:24:24 2011 -0400 @@ -67,76 +67,57 @@ ; Reading Packets ------------------------------------------------------------------ -(defn- read-packet-keepalive [bot] +(defn- read-packet-keepalive [bot conn] {}) -(defn- read-packet-handshake [bot] - (let [conn (:connection bot) - payload (assoc {} - :hash (-read-string-ucs2 conn))] - payload)) +(defn- read-packet-handshake [bot conn] + (assoc {} + :hash (-read-string-ucs2 conn))) -(defn- read-packet-login [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn) - :unknown (-read-string-ucs2 conn) - :seed (-read-long conn) - :dimension (-read-byte conn))] - payload)) +(defn- read-packet-login [bot conn] + (assoc {} + :eid (-read-int conn) + :unknown (-read-string-ucs2 conn) + :seed (-read-long conn) + :dimension (-read-byte conn))) -(defn- read-packet-chat [bot] - (let [conn (:connection bot) - payload (assoc {} - :message (-read-string-ucs2 conn))] - payload)) +(defn- read-packet-chat [bot conn] + (assoc {} + :message (-read-string-ucs2 conn))) -(defn- read-packet-timeupdate [bot] - (let [conn (:connection bot) - payload (assoc {} - :time (-read-long conn))] - payload)) +(defn- read-packet-timeupdate [bot conn] + (assoc {} + :time (-read-long conn))) -(defn- read-packet-equipment [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn) - :slot (-read-short conn) - :itemid (-read-short conn) - :unknown (-read-short conn))] - payload)) +(defn- read-packet-equipment [bot conn] + (assoc {} + :eid (-read-int conn) + :slot (-read-short conn) + :itemid (-read-short conn) + :unknown (-read-short conn))) -(defn- read-packet-spawnposition [bot] - (let [conn (:connection bot) - payload (assoc {} - :x (-read-int conn) - :y (-read-int conn) - :z (-read-int conn))] - payload)) +(defn- read-packet-spawnposition [bot conn] + (assoc {} + :x (-read-int conn) + :y (-read-int conn) + :z (-read-int conn))) -(defn- read-packet-useentity [bot] - (let [conn (:connection bot) - payload (assoc {} - :user (-read-int conn) - :target (-read-int conn) - :leftclick (-read-bool conn))] - payload)) +(defn- read-packet-useentity [bot conn] + (assoc {} + :user (-read-int conn) + :target (-read-int conn) + :leftclick (-read-bool conn))) -(defn- read-packet-updatehealth [bot] - (let [conn (:connection bot) - payload (assoc {} - :health (-read-short conn))] - payload)) +(defn- read-packet-updatehealth [bot conn] + (assoc {} + :health (-read-short conn))) -(defn- read-packet-respawn [bot] - (let [conn (:connection bot) - payload (assoc {} - :world (-read-byte conn))] - payload)) +(defn- read-packet-respawn [bot conn] + (assoc {} + :world (-read-byte conn))) -(defn- read-packet-playerpositionlook [bot] - (let [conn (:connection bot) - payload (assoc {} +(defn- read-packet-playerpositionlook [bot conn] + (let [payload (assoc {} :x (-read-double conn) :stance (-read-double conn) :y (-read-double conn) @@ -147,411 +128,383 @@ (dosync (alter (:player bot) merge {:location payload})) payload)) -(defn- read-packet-playerdigging [bot] - (let [conn (:connection bot) - payload (assoc {} - :status (-read-byte conn) - :x (-read-int conn) - :y (-read-byte conn) - :z (-read-int conn) - :face (-read-byte conn))] - payload)) +(defn- read-packet-playerdigging [bot conn] + (assoc {} + :status (-read-byte conn) + :x (-read-int conn) + :y (-read-byte conn) + :z (-read-int conn) + :face (-read-byte conn))) -(defn- read-packet-playerblockplacement [bot] - (let [conn (:connection bot) - payload (assoc {} - :x (-read-int conn) - :y (-read-byte conn) - :z (-read-int conn) - :direction (-read-byte conn) - :id (-read-short conn) - :amount (-read-byte conn) - :damage (-read-short conn))] - payload)) +(defn- read-packet-playerblockplacement [bot conn] + (assoc {} + :x (-read-int conn) + :y (-read-byte conn) + :z (-read-int conn) + :direction (-read-byte conn) + :id (-read-short conn) + :amount (-read-byte conn) + :damage (-read-short conn))) -(defn- read-packet-holdingchange [bot] - (let [conn (:connection bot) - payload (assoc {} - :slot (-read-short conn))] - payload)) +(defn- read-packet-holdingchange [bot conn] + (assoc {} + :slot (-read-short conn))) -(defn- read-packet-usebed [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn) - :inbed (-read-byte conn) - :x (-read-int conn) - :y (-read-byte conn) - :z (-read-int conn))] - payload)) +(defn- read-packet-usebed [bot conn] + (assoc {} + :eid (-read-int conn) + :inbed (-read-byte conn) + :x (-read-int conn) + :y (-read-byte conn) + :z (-read-int conn))) -(defn- read-packet-animate [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn) - :animate (-read-byte conn))] - payload)) +(defn- read-packet-animate [bot conn] + (assoc {} + :eid (-read-int conn) + :animate (-read-byte conn))) -(defn- read-packet-entityaction [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn) - :action (-read-byte conn))] - payload)) +(defn- read-packet-entityaction [bot conn] + (assoc {} + :eid (-read-int conn) + :action (-read-byte conn))) -(defn- read-packet-namedentityspawn [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn) - :playername (-read-string-ucs2 conn) - :x (-read-int conn) - :y (-read-int conn) - :z (-read-int conn) - :rotation (-read-byte conn) - :pitch (-read-byte conn) - :currentitem (-read-short conn))] - payload)) - -(defn- read-packet-pickupspawn [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn) - :item (-read-short conn) - :count (-read-byte conn) - :damagedata (-read-short conn) - :x (-read-int conn) - :y (-read-int conn) - :z (-read-int conn) - :rotation (-read-byte conn) - :pitch (-read-byte conn) - :roll (-read-byte conn))] - payload)) - -(defn- read-packet-collectitem [bot] - (let [conn (:connection bot) - payload (assoc {} - :collectedeid (-read-int conn) - :collectoreid (-read-int conn))] - payload)) +(defn- read-packet-namedentityspawn [bot conn] + (assoc {} + :eid (-read-int conn) + :playername (-read-string-ucs2 conn) + :x (-read-int conn) + :y (-read-int conn) + :z (-read-int conn) + :rotation (-read-byte conn) + :pitch (-read-byte conn) + :currentitem (-read-short conn))) -(defn- read-packet-addobjectvehicle [bot] - (let [conn (:connection bot) - payload (let [basepacket (assoc {} - :eid (-read-int conn) - :type (-read-byte conn) - :x (-read-int conn) - :y (-read-int conn) - :z (-read-int conn) - :moar (-read-int conn))] - (if (< 0 (:moar basepacket)) - basepacket - (assoc basepacket - :unknownx (-read-int conn) - :unknowny (-read-int conn) - :unknownz (-read-int conn))))] - payload)) +(defn- read-packet-pickupspawn [bot conn] + (assoc {} + :eid (-read-int conn) + :item (-read-short conn) + :count (-read-byte conn) + :damagedata (-read-short conn) + :x (-read-int conn) + :y (-read-int conn) + :z (-read-int conn) + :rotation (-read-byte conn) + :pitch (-read-byte conn) + :roll (-read-byte conn))) -(defn- read-packet-mobspawn [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn) - :type (-read-byte conn) - :x (-read-int conn) - :y (-read-int conn) - :z (-read-int conn) - :yaw (-read-byte conn) - :pitch (-read-byte conn) - :datastream (-read-metadata conn))] - payload)) +(defn- read-packet-collectitem [bot conn] + (assoc {} + :collectedeid (-read-int conn) + :collectoreid (-read-int conn))) -(defn- read-packet-entitypainting [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn) - :type (-read-string-ucs2 conn) - :x (-read-int conn) - :y (-read-int conn) - :z (-read-int conn) - :direction (-read-int conn))] - payload)) +(defn- read-packet-addobjectvehicle [bot conn] + (let [basepacket (assoc {} + :eid (-read-int conn) + :type (-read-byte conn) + :x (-read-int conn) + :y (-read-int conn) + :z (-read-int conn) + :moar (-read-int conn))] + (if (< 0 (:moar basepacket)) + basepacket + (assoc basepacket + :unknownx (-read-int conn) + :unknowny (-read-int conn) + :unknownz (-read-int conn))))) -(defn- read-packet-stanceupdate [bot] - (let [conn (:connection bot) - payload (assoc {} - :unknown1 (-read-float conn) - :unknown2 (-read-float conn) - :unknown3 (-read-bool conn) - :unknown4 (-read-bool conn) - :unknown5 (-read-float conn) - :unknown6 (-read-float conn))] - payload)) +(defn- read-packet-mobspawn [bot conn] + (assoc {} + :eid (-read-int conn) + :type (-read-byte conn) + :x (-read-int conn) + :y (-read-int conn) + :z (-read-int conn) + :yaw (-read-byte conn) + :pitch (-read-byte conn) + :datastream (-read-metadata conn))) -(defn- read-packet-entityvelocity [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn) - :velocityx (-read-short conn) - :velocityy (-read-short conn) - :velocityz (-read-short conn))] - payload)) - -(defn- read-packet-entitydestroy [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn))] - payload)) +(defn- read-packet-entitypainting [bot conn] + (assoc {} + :eid (-read-int conn) + :type (-read-string-ucs2 conn) + :x (-read-int conn) + :y (-read-int conn) + :z (-read-int conn) + :direction (-read-int conn))) -(defn- read-packet-entity [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn))] - payload)) - -(defn- read-packet-entityrelativemove [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn) - :dx (-read-byte conn) - :dy (-read-byte conn) - :dz (-read-byte conn))] - payload)) +(defn- read-packet-stanceupdate [bot conn] + (assoc {} + :unknown1 (-read-float conn) + :unknown2 (-read-float conn) + :unknown3 (-read-bool conn) + :unknown4 (-read-bool conn) + :unknown5 (-read-float conn) + :unknown6 (-read-float conn))) -(defn- read-packet-entitylook [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn) - :yaw (-read-byte conn) - :pitch (-read-byte conn))] - payload)) +(defn- read-packet-entityvelocity [bot conn] + (assoc {} + :eid (-read-int conn) + :velocityx (-read-short conn) + :velocityy (-read-short conn) + :velocityz (-read-short conn))) -(defn- read-packet-entitylookandrelativemove [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn) - :dx (-read-byte conn) - :dy (-read-byte conn) - :dz (-read-byte conn) - :yaw (-read-byte conn) - :pitch (-read-byte conn))] - payload)) +(defn- read-packet-entitydestroy [bot conn] + (assoc {} + :eid (-read-int conn))) + +(defn- read-packet-entity [bot conn] + (assoc {} + :eid (-read-int conn))) -(defn- read-packet-entityteleport [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn) - :x (-read-int conn) - :y (-read-int conn) - :z (-read-int conn) - :yaw (-read-byte conn) - :pitch (-read-byte conn))] - payload)) - -(defn- read-packet-entitystatus [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn) - :entitystatus (-read-byte conn))] - payload)) +(defn- read-packet-entityrelativemove [bot conn] + (assoc {} + :eid (-read-int conn) + :dx (-read-byte conn) + :dy (-read-byte conn) + :dz (-read-byte conn))) -(defn- read-packet-attachentity [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn) - :vehicleid (-read-int conn))] - payload)) +(defn- read-packet-entitylook [bot conn] + (assoc {} + :eid (-read-int conn) + :yaw (-read-byte conn) + :pitch (-read-byte conn))) -(defn- read-packet-entitymetadata [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn) - :metadata (-read-metadata conn))] - payload)) - -(defn- read-packet-prechunk [bot] - (let [conn (:connection bot) - payload (assoc {} - :x (-read-int conn) - :z (-read-int conn) - :mode (-read-bool conn))] - payload)) +(defn- read-packet-entitylookandrelativemove [bot conn] + (assoc {} + :eid (-read-int conn) + :dx (-read-byte conn) + :dy (-read-byte conn) + :dz (-read-byte conn) + :yaw (-read-byte conn) + :pitch (-read-byte conn))) -(defn- read-packet-mapchunk [bot] - (let [conn (:connection bot) - payload (let [predata (assoc {} - :x (-read-int conn) - :y (-read-short conn) - :z (-read-int conn) - :sizex (-read-byte conn) - :sizey (-read-byte conn) - :sizez (-read-byte conn) - :compressedsize (-read-int conn))] - (assoc predata - :compresseddata - (-read-bytearray conn - (:compressedsize predata))))] - payload)) +(defn- read-packet-entityteleport [bot conn] + (assoc {} + :eid (-read-int conn) + :x (-read-int conn) + :y (-read-int conn) + :z (-read-int conn) + :yaw (-read-byte conn) + :pitch (-read-byte conn))) + +(defn- read-packet-entitystatus [bot conn] + (assoc {} + :eid (-read-int conn) + :entitystatus (-read-byte conn))) -(defn- read-packet-multiblockchange [bot] - (let [conn (:connection bot) - payload (assoc {} - :chunkx (-read-int conn) - :chunkz (-read-int conn) - :arraysize (-read-short conn) - :coordinatearray (-read-shortarray conn arraysize) - :typearray (-read-bytearray conn arraysize) - :metadataarray (-read-bytearray conn arraysize))] - payload)) +(defn- read-packet-attachentity [bot conn] + (assoc {} + :eid (-read-int conn) + :vehicleid (-read-int conn))) -(defn- read-packet-blockchange [bot] - (let [conn (:connection bot) - payload (assoc {} - :x (-read-int conn) - :y (-read-byte conn) - :z (-read-int conn) - :blocktype (-read-byte conn) - :blockmetadata (-read-byte conn))] - payload)) +(defn- read-packet-entitymetadata [bot conn] + (assoc {} + :eid (-read-int conn) + :metadata (-read-metadata conn))) -(defn- read-packet-playnoteblock [bot] - (let [conn (:connection bot) - payload (assoc {} +(defn- read-packet-prechunk [bot conn] + (assoc {} + :x (-read-int conn) + :z (-read-int conn) + :mode (-read-bool conn))) + +(defn- read-packet-mapchunk [bot conn] + (let [predata (assoc {} :x (-read-int conn) :y (-read-short conn) :z (-read-int conn) - :instrumenttype (-read-byte conn) - :pitch (-read-byte conn))] - payload)) + :sizex (-read-byte conn) + :sizey (-read-byte conn) + :sizez (-read-byte conn) + :compressedsize (-read-int conn))] + (assoc predata + :compresseddata + (-read-bytearray conn + (:compressedsize predata))))) + +(defn- read-packet-multiblockchange [bot conn] + (assoc {} + :chunkx (-read-int conn) + :chunkz (-read-int conn) + :arraysize (-read-short conn) + :coordinatearray (-read-shortarray conn arraysize) + :typearray (-read-bytearray conn arraysize) + :metadataarray (-read-bytearray conn arraysize))) -(defn- read-packet-explosion [bot] - (let [conn (:connection bot) - payload (let [prerecords (assoc {} - :x (-read-int conn) - :y (-read-short conn) - :z (-read-int conn) - :unknownradius (-read-byte conn) - :recordcount (-read-byte conn))] - (assoc prerecords - :records (-read-bytearray conn - (* 3 (:recordcount prerecords)))))] - payload)) +(defn- read-packet-blockchange [bot conn] + (assoc {} + :x (-read-int conn) + :y (-read-byte conn) + :z (-read-int conn) + :blocktype (-read-byte conn) + :blockmetadata (-read-byte conn))) -(defn- read-packet-soundeffect [bot] - (let [conn (:connection bot) - payload (assoc {} - :effectid (-read-int conn) - :x (-read-int conn) - :y (-read-byte conn) - :z (-read-int conn) - :sounddata (-read-int conn))] - payload)) +(defn- read-packet-playnoteblock [bot conn] + (assoc {} + :x (-read-int conn) + :y (-read-short conn) + :z (-read-int conn) + :instrumenttype (-read-byte conn) + :pitch (-read-byte conn))) + +(defn- read-packet-explosion [bot conn] + (let [prerecords (assoc {} + :x (-read-int conn) + :y (-read-short conn) + :z (-read-int conn) + :unknownradius (-read-byte conn) + :recordcount (-read-byte conn))] + (assoc prerecords + :records (-read-bytearray conn + (* 3 (:recordcount prerecords)))))) -(defn- read-packet-newinvalidstate [bot] - (let [conn (:connection bot) - payload (assoc {} - :reason (-read-byte conn))] - payload)) +(defn- read-packet-soundeffect [bot conn] + (assoc {} + :effectid (-read-int conn) + :x (-read-int conn) + :y (-read-byte conn) + :z (-read-int conn) + :sounddata (-read-int conn))) + +(defn- read-packet-newinvalidstate [bot conn] + (assoc {} + :reason (-read-byte conn))) + +(defn- read-packet-thunderbolt [bot conn] + (assoc {} + :eid (-read-int conn) + :unknown (-read-bool conn) + :x (-read-int conn) + :y (-read-int conn) + :z (-read-int conn))) -(defn- read-packet-thunderbolt [bot] - (let [conn (:connection bot) - payload (assoc {} - :eid (-read-int conn) - :unknown (-read-bool conn) - :x (-read-int conn) - :y (-read-int conn) - :z (-read-int conn))] - payload)) +(defn- read-packet-openwindow [bot conn] + (assoc {} + :windowid (-read-byte conn) + :inventorytype (-read-byte conn) + :windowtitle (-read-string-utf8 conn) + :numberofslots (-read-byte conn))) + +(defn- read-packet-closewindow [bot conn] + (assoc {} + :windowid (-read-byte conn))) -(defn- read-packet-openwindow [bot] - (let [conn (:connection bot) - payload (assoc {} - :windowid (-read-byte conn) - :inventorytype (-read-byte conn) - :windowtitle (-read-string-utf8 conn) - :numberofslots (-read-byte conn))] - payload)) +(defn- read-packet-setslot [bot conn] + (assoc {} + :windowid (-read-byte conn) + :slot (-read-short conn) + :itemid (-read-short conn) + :itemcount (-read-byte conn) + :itemuses (-read-short conn))) -(defn- read-packet-closewindow [bot] - (let [conn (:connection bot) - payload (assoc {} - :windowid (-read-byte conn))] - payload)) +(defn- -read-packet-windowitems-payloaditem [] + (let [payload (assoc {} :itemid (-read-short conn))] + (if (= (:itemid payload) -1) + payload + (assoc payload + :count (-read-byte conn) + :uses (-read-short conn))))) -(defn- read-packet-setslot [bot] - (let [conn (:connection bot) - payload (assoc {} - :windowid (-read-byte conn) - :slot (-read-short conn) - :itemid (-read-short conn) - :itemcount (-read-byte conn) - :itemuses (-read-short conn))] - payload)) +(defn- read-packet-windowitems [bot conn] + (let [prepay (assoc {} + :windowid (-read-byte conn) + :count (-read-short conn)) + pay (repeatedly (:count prepay) + -read-packet-windowitems-payloaditem)] + (assoc prepay :payload pay))) + +(defn- read-packet-updateprogressbar [bot conn] + (assoc {} + :windowid (-read-byte conn) + :progressbar (-read-short conn) + :value (-read-short conn))) + +(defn- read-packet-transaction [bot conn] + (assoc {} + :windowid (-read-byte conn) + :actionnumber (-read-short conn) + :accepted (-read-short conn))) -(defn- read-packet-windowitems [bot] - (let [conn (:connection bot) - payload (letfn [(-read-payload-item [] - (let [payload (assoc {} :itemid (-read-short conn))] - (if (= (:itemid payload) -1) - payload - (assoc payload - :count (-read-byte conn) - :uses (-read-short conn)))))] - (let [prepay (assoc {} - :windowid (-read-byte conn) - :count (-read-short conn)) - pay (repeatedly (:count prepay) -read-payload-item)] - (assoc prepay :payload pay)))] - payload)) +(defn- read-packet-updatesign [bot conn] + (assoc {} + :x (-read-int conn) + :y (-read-short conn) + :z (-read-int conn) + :text1 (-read-string-ucs2 conn) + :text2 (-read-string-ucs2 conn) + :text3 (-read-string-ucs2 conn) + :text4 (-read-string-ucs2 conn))) -(defn- read-packet-updateprogressbar [bot] - (let [conn (:connection bot) - payload (assoc {} - :windowid (-read-byte conn) - :progressbar (-read-short conn) - :value (-read-short conn))] - payload)) +(defn- read-packet-mapdata [bot conn] + (let [pretext (assoc {} + :unknown1 (-read-int conn) + :unknown2 (-read-short conn) + :textlength (-read-int conn))] + (assoc pretext :text (-read-bytearray (:textlength pretext))))) + +(defn- read-packet-incrementstatistic [bot conn] + (assoc {} + :statisticid (-read-int conn) + :amount (-read-byte conn))) + +(defn- read-packet-disconnectkick [bot conn] + (assoc {} + :reason (-read-string-ucs2 conn))) + -(defn- read-packet-transaction [bot] - (let [conn (:connection bot) - payload (assoc {} - :windowid (-read-byte conn) - :actionnumber (-read-short conn) - :accepted (-read-short conn))] - payload)) - -(defn- read-packet-updatesign [bot] - (let [conn (:connection bot) - payload (assoc {} - :x (-read-int conn) - :y (-read-short conn) - :z (-read-int conn) - :text1 (-read-string-ucs2 conn) - :text2 (-read-string-ucs2 conn) - :text3 (-read-string-ucs2 conn) - :text4 (-read-string-ucs2 conn))] - payload)) - -(defn- read-packet-mapdata [bot] - (let [conn (:connection bot) - payload (let [pretext (assoc {} - :unknown1 (-read-int conn) - :unknown2 (-read-short conn) - :textlength (-read-int conn))] - (assoc pretext :text (-read-bytearray (:textlength pretext))))] - payload)) - -(defn- read-packet-incrementstatistic [bot] - (let [conn (:connection bot) - payload (assoc {} - :statisticid (-read-int conn) - :amount (-read-byte conn))] - payload)) - -(defn- read-packet-disconnectkick [bot] - (let [conn (:connection bot) - payload (assoc {} - :reason (-read-string-ucs2 conn))] - payload)) - +(def- packet-readers {:keepalive read-packet-keepalive + :handshake read-packet-handshake + :login read-packet-login + :chat read-packet-chat + :timeupdate read-packet-timeupdate + :equipment read-packet-equipment + :spawnposition read-packet-spawnposition + :useentity read-packet-useentity + :updatehealth read-packet-updatehealth + :respawn read-packet-respawn + :playerpositionlook read-packet-playerpositionlook + :playerdigging read-packet-playerdigging + :playerblockplacement read-packet-playerblockplacement + :holdingchange read-packet-holdingchange + :usebed read-packet-usebed + :animate read-packet-animate + :entityaction read-packet-entityaction + :namedentityspawn read-packet-namedentityspawn + :pickupspawn read-packet-pickupspawn + :collectitem read-packet-collectitem + :addobjectvehicle read-packet-addobjectvehicle + :mobspawn read-packet-mobspawn + :entitypainting read-packet-entitypainting + :stanceupdate read-packet-stanceupdate + :entityvelocity read-packet-entityvelocity + :entitydestroy read-packet-entitydestroy + :entity read-packet-entity + :entityrelativemove read-packet-entityrelativemove + :entitylook read-packet-entitylook + :entitylookandrelativemove read-packet-entitylookandrelativemove + :entityteleport read-packet-entityteleport + :entitystatus read-packet-entitystatus + :attachentity read-packet-attachentity + :entitymetadata read-packet-entitymetadata + :prechunk read-packet-prechunk + :mapchunk read-packet-mapchunk + :multiblockchange read-packet-multiblockchange + :blockchange read-packet-blockchange + :playnoteblock read-packet-playnoteblock + :explosion read-packet-explosion + :soundeffect read-packet-soundeffect + :newinvalidstate read-packet-newinvalidstate + :thunderbolt read-packet-thunderbolt + :openwindow read-packet-openwindow + :closewindow read-packet-closewindow + :setslot read-packet-setslot + :windowitems read-packet-windowitems + :updateprogressbar read-packet-updateprogressbar + :transaction read-packet-transaction + :updatesign read-packet-updatesign + :mapdata read-packet-mapdata + :incrementstatistic read-packet-incrementstatistic + :disconnectkick read-packet-disconnectkick}) ; Reading Wrappers ----------------------------------------------------------------- (defn read-packet [bot] @@ -561,63 +514,6 @@ (if (= nil packet-type) (do (println (str "UNKNOWN PACKET TYPE: " (Integer/toHexString packet-id))) - (/ 1 0)) - (do - (identity (str "\n--PACKET--> " packet-type)) - (identity - (case packet-type - :keepalive (read-packet-keepalive bot) - :handshake (read-packet-handshake bot) - :login (read-packet-login bot) - :chat (read-packet-chat bot) - :timeupdate (read-packet-timeupdate bot) - :equipment (read-packet-equipment bot) - :spawnposition (read-packet-spawnposition bot) - :useentity (read-packet-useentity bot) - :updatehealth (read-packet-updatehealth bot) - :respawn (read-packet-respawn bot) - :playerpositionlook (read-packet-playerpositionlook bot) - :playerdigging (read-packet-playerdigging bot) - :playerblockplacement (read-packet-playerblockplacement bot) - :holdingchange (read-packet-holdingchange bot) - :usebed (read-packet-usebed bot) - :animate (read-packet-animate bot) - :entityaction (read-packet-entityaction bot) - :namedentityspawn (read-packet-namedentityspawn bot) - :pickupspawn (read-packet-pickupspawn bot) - :collectitem (read-packet-collectitem bot) - :addobjectvehicle (read-packet-addobjectvehicle bot) - :mobspawn (read-packet-mobspawn bot) - :entitypainting (read-packet-entitypainting bot) - :stanceupdate (read-packet-stanceupdate bot) - :entityvelocity (read-packet-entityvelocity bot) - :entitydestroy (read-packet-entitydestroy bot) - :entity (read-packet-entity bot) - :entityrelativemove (read-packet-entityrelativemove bot) - :entitylook (read-packet-entitylook bot) - :entitylookandrelativemove (read-packet-entitylookandrelativemove bot) - :entityteleport (read-packet-entityteleport bot) - :entitystatus (read-packet-entitystatus bot) - :attachentity (read-packet-attachentity bot) - :entitymetadata (read-packet-entitymetadata bot) - :prechunk (read-packet-prechunk bot) - :mapchunk (read-packet-mapchunk bot) - :multiblockchange (read-packet-multiblockchange bot) - :blockchange (read-packet-blockchange bot) - :playnoteblock (read-packet-playnoteblock bot) - :explosion (read-packet-explosion bot) - :soundeffect (read-packet-soundeffect bot) - :newinvalidstate (read-packet-newinvalidstate bot) - :thunderbolt (read-packet-thunderbolt bot) - :openwindow (read-packet-openwindow bot) - :closewindow (read-packet-closewindow bot) - :setslot (read-packet-setslot bot) - :windowitems (read-packet-windowitems bot) - :updateprogressbar (read-packet-updateprogressbar bot) - :transaction (read-packet-transaction bot) - :updatesign (read-packet-updatesign bot) - :mapdata (read-packet-mapdata bot) - :incrementstatistic (read-packet-incrementstatistic bot) - :disconnectkick (read-packet-disconnectkick bot))))) - (identity "\n\n\n"))) + (/ 1 0)) + ((packet-type packet-readers) bot conn)))) diff -r 6799868ce43d -r b9f9991e6175 src/clojurecraft/out.clj --- a/src/clojurecraft/out.clj Sun Jun 26 18:47:29 2011 -0400 +++ b/src/clojurecraft/out.clj Mon Jun 27 16:24:24 2011 -0400 @@ -53,39 +53,27 @@ ; Writing Packets ------------------------------------------------------------------ (defn- write-packet-keepalive [conn _] - (-write-byte conn (:keepalive packet-ids))) + nil) (defn- write-packet-handshake [conn {username :username}] - (-write-byte conn (:handshake packet-ids)) - (-write-string-ucs2 conn username)) (defn- write-packet-login [conn {version :version, username :username}] - (-write-byte conn (:login packet-ids)) - (-write-int conn version) (-write-string-ucs2 conn username) (-write-long conn 0) (-write-byte conn 0)) (defn- write-packet-chat [conn {message :message}] - (-write-byte conn (:chat packet-ids)) - (-write-string-ucs2 conn message)) (defn- write-packet-respawn [conn {world :world}] - (-write-byte conn (:respawn packet-ids)) - (-write-bool conn world)) (defn- write-packet-player [conn {onground :onground}] - (-write-byte conn (:player packet-ids)) - (-write-bool conn onground)) (defn- write-packet-playerposition [conn {x :x y :y stance :stance z :z onground :onground}] - (-write-byte conn (:playerposition packet-ids)) - (-write-double conn x) (-write-double conn y) (-write-double conn stance) @@ -93,15 +81,11 @@ (-write-bool conn onground)) (defn- write-packet-playerlook [conn {yaw :yaw pitch :pitch onground :onground}] - (-write-byte conn (:playerlook packet-ids)) - (-write-float conn yaw) (-write-float conn pitch) (-write-bool conn onground)) (defn- write-packet-playerpositionlook [conn {x :x y :y stance :stance z :z yaw :yaw pitch :pitch onground :onground}] - (-write-byte conn (:playerpositionlook packet-ids)) - (-write-double conn x) (-write-double conn y) (-write-double conn stance) @@ -111,8 +95,6 @@ (-write-bool conn onground)) (defn- write-packet-playerdigging [conn {status :status x :x y :y z :z face :face}] - (-write-byte conn (:playerdigging packet-ids)) - (-write-byte conn status) (-write-int conn x) (-write-byte conn y) @@ -120,8 +102,6 @@ (-write-byte conn face)) (defn- write-packet-playerblockplacement [conn {x :x y :y z :z direction :direction id :id amount :amount damage :damage}] - (-write-byte conn (:playerblockplacement packet-ids)) - (-write-int conn x) (-write-byte conn y) (-write-int conn z) @@ -131,13 +111,9 @@ (-write-short conn damage)) (defn- write-packet-holdingchange [conn {slot :slot}] - (-write-byte conn (:holdingchange packet-ids)) - (-write-short conn slot)) (defn- write-packet-usebed [conn {eid :eid inbed :inbed x :x y :y z :z}] - (-write-byte conn (:usebed packet-ids)) - (-write-int conn eid) (-write-byte conn inbed) (-write-int conn x) @@ -145,20 +121,14 @@ (-write-int conn z)) (defn- write-packet-animation [conn {eid :eid animate :animate}] - (-write-byte conn (:animation packet-ids)) - (-write-int conn eid) (-write-byte conn animate)) (defn- write-packet-entityaction [conn {eid :eid action :action}] - (-write-byte conn (:entityaction packet-ids)) - (-write-int conn eid) (-write-byte conn action)) (defn- write-packet-pickupspawn [conn {eid :eid item :item count :count damagedata :damagedata x :x y :y z :z rotation :rotation pitch :pitch roll :roll}] - (-write-byte conn (:pickupspawn packet-ids)) - (-write-int conn eid) (-write-short conn item) (-write-byte conn count) @@ -171,8 +141,6 @@ (-write-byte int conn roll)) (defn- write-packet-entitypainting [conn {eid :eid title :title x :x y :y z :z direction :direction}] - (-write-byte conn (:entitypainting packet-ids)) - (-write-int conn eid) (-write-string-ucs2 conn title) (-write-int conn x) @@ -181,8 +149,6 @@ (-write-int conn direction)) (defn- write-packet-stanceupdate [conn {unknown1 :unknown1 unknown2 :unknown2 unknown3 :unknown3 unknown4 :unknown4 unknown5 :unknown5 unknown6 :unknown6 }] - (-write-byte conn (:stanceupdate packet-ids)) - (-write-float conn unknown1) (-write-float conn unknown2) (-write-bool conn unknown3) @@ -191,28 +157,20 @@ (-write-float conn unknown6)) (defn- write-packet-entityvelocity [conn {eid :eid velocityx :velocityx velocityy :velocityy velocityz :velocityz}] - (-write-byte conn (:entityvelocity packet-ids)) - (-write-int conn eid) (-write-short conn velocityx) (-write-short conn velocityy) (-write-short conn velocityz)) (defn- write-packet-attachentity [conn {eid :eid vehicleid :vehicleid}] - (-write-byte conn (:attachentity packet-ids)) - (-write-int conn eid) (-write-int conn vehicleid)) (defn- write-packet-entitymetadata [conn {eid :eid metadata :metadata}] - (-write-byte conn (:attachentity packet-ids)) - (-write-int conn eid) (-write-metadata conn metadata)) (defn- write-packet-multiblockchange [conn {chunkx :chunkx chunkz :chunkz arraysize :arraysize coordinatearray :coordinatearray typearray :typearray metadataarray :metadataarray}] - (-write-byte conn (:multiblockchange packet-ids)) - (-write-int conn chunkx) (-write-int conn chunkz) (-write-short conn arraysize) @@ -221,8 +179,6 @@ (-write-bytearray conn metadataarray)) (defn- write-packet-blockchange [conn {x :x y :y z :z blocktype :blocktype blockmetadata :blockmetadata}] - (-write-byte conn (:blockchange packet-ids)) - (-write-int conn x) (-write-byte conn y) (-write-int conn z) @@ -234,8 +190,6 @@ nil) (defn- write-packet-soundeffect [conn {effectid :effectid x :x y :y z :z sounddata :sounddata}] - (-write-byte conn (:soundeffect packet-ids)) - (-write-int conn effectid) (-write-int conn x) (-write-byte conn y) @@ -243,26 +197,18 @@ (-write-int conn sounddata)) (defn- write-packet-newinvalidstate [conn {reason :reason}] - (-write-byte conn (:newinvalidstate packet-ids)) - (-write-byte conn reason)) (defn- write-packet-openwindow [conn {windowid :windowid inventorytype :inventorytype windowtitle :windowtitle numberofslots :numberofslots}] - (-write-byte conn (:openwindow packet-ids)) - (-write-byte conn windowid) (-write-byte conn inventorytype) (-write-string-utf8 conn windowtitle) (-write-byte conn numberofslots)) (defn- write-packet-closewindow [conn {windowid :windowid}] - (-write-byte conn (:closewindow packet-ids)) - (-write-byte conn windowid)) (defn- write-packet-windowclick [conn {windowid :windowid slot :slot rightclick :rightclick actionnumber :actionnumber shift :shift itemid :itemid itemcount :itemcount itemuses :itemuses}] - (-write-byte conn (:windowclick packet-ids)) - (-write-byte conn windowid) (-write-short conn slot) (-write-byte conn rightclick) @@ -273,15 +219,11 @@ (-write-short conn itemuses)) (defn- write-packet-transaction [conn {windowid :windowid actionnumber :actionnumber accepted :accepted}] - (-write-byte conn (:transaction packet-ids)) - (-write-byte conn windowid) (-write-short conn actionnumber) (-write-bool conn accepted)) (defn- write-packet-updatesign [conn {x :x y :y z :z text1 :text1 text2 :text2 text3 :text3 text4 :text4}] - (-write-byte conn (:transaction packet-ids)) - (-write-int conn x) (-write-short conn y) (-write-int conn z) @@ -291,59 +233,55 @@ (-write-string-ucs2 conn text4)) (defn- write-packet-incrementstatistic [conn {statisticid :statisticid amount :amount}] - (-write-byte conn (:incrementstatistic packet-ids)) - (-write-int conn statisticid) (-write-byte conn amount)) (defn- write-packet-disconnectkick [conn {reason :reason}] - (-write-byte conn (:disconnectkick packet-ids)) - (-write-string-ucs2 conn reason)) +(def- packet-writers {:keepalive write-packet-keepalive + :handshake write-packet-handshake + :login write-packet-login + :chat write-packet-chat + :respawn write-packet-respawn + :player write-packet-player + :playerposition write-packet-playerposition + :playerlook write-packet-playerlook + :playerpositionlook write-packet-playerpositionlook + :playerdigging write-packet-playerdigging + :playerblockplacement write-packet-playerblockplacement + :holdingchange write-packet-holdingchange + :usebed write-packet-usebed + :animation write-packet-animation + :entityaction write-packet-entityaction + :pickupspawn write-packet-pickupspawn + :entitypainting write-packet-entitypainting + :stanceupdate write-packet-stanceupdate + :entityvelocity write-packet-entityvelocity + :attachentity write-packet-attachentity + :entitymetadata write-packet-entitymetadata + :multiblockchange write-packet-multiblockchange + :blockchange write-packet-blockchange + :explosion write-packet-explosion + :soundeffect write-packet-soundeffect + :newinvalidstate write-packet-newinvalidstate + :openwindow write-packet-openwindow + :closewindow write-packet-closewindow + :windowclick write-packet-windowclick + :transaction write-packet-transaction + :updatesign write-packet-updatesign + :incrementstatistic write-packet-incrementstatistic + :disconnectkick write-packet-disconnectkick}) + ; Writing Wrappers ----------------------------------------------------------------- (defn- flushc [conn] (doto (:out @conn) (.flush))) (defn write-packet [bot packet-type payload] - (println (str "<--WRITING-- " packet-type)) - (println payload) - (let [conn (:connection bot)] - (case packet-type - :keepalive (write-packet-keepalive conn payload) - :handshake (write-packet-handshake conn payload) - :login (write-packet-login conn payload) - :chat (write-packet-chat conn payload) - :respawn (write-packet-respawn conn payload) - :player (write-packet-player conn payload) - :playerposition (write-packet-playerposition conn payload) - :playerlook (write-packet-playerlook conn payload) - :playerpositionlook (write-packet-playerpositionlook conn payload) - :playerdigging (write-packet-playerdigging conn payload) - :playerblockplacement (write-packet-playerblockplacement conn payload) - :holdingchange (write-packet-holdingchange conn payload) - :usebed (write-packet-usebed conn payload) - :animation (write-packet-animation conn payload) - :entityaction (write-packet-entityaction conn payload) - :pickupspawn (write-packet-pickupspawn conn payload) - :entitypainting (write-packet-entitypainting conn payload) - :stanceupdate (write-packet-stanceupdate conn payload) - :entityvelocity (write-packet-entityvelocity conn payload) - :attachentity (write-packet-attachentity conn payload) - :entitymetadata (write-packet-entitymetadata conn payload) - :multiblockchange (write-packet-multiblockchange conn payload) - :blockchange (write-packet-blockchange conn payload) - :explosion (write-packet-explosion conn payload) - :soundeffect (write-packet-soundeffect conn payload) - :newinvalidstate (write-packet-newinvalidstate conn payload) - :openwindow (write-packet-openwindow conn payload) - :closewindow (write-packet-closewindow conn payload) - :windowclick (write-packet-windowclick conn payload) - :transaction (write-packet-transaction conn payload) - :updatesign (write-packet-updatesign conn payload) - :incrementstatistic (write-packet-incrementstatistic conn payload) - :disconnectkick (write-packet-disconnectkick conn payload)) - (flushc conn)) - (println "written\n\n")) + (let [conn (:connection bot) + handler (packet-type packet-writers)] + (-write-byte conn (packet-type packet-ids)) + (handler conn payload) + (flushc conn)))