60a56dc38ec2

Refactor out the bot.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Fri, 24 Jun 2011 20:54:17 -0400
parents a96c06febb53
children fc293cb5f83e
branches/tags (none)
files src/clojurecraft/core.clj src/clojurecraft/in.clj

Changes

--- a/src/clojurecraft/core.clj	Fri Jun 24 20:15:47 2011 -0400
+++ b/src/clojurecraft/core.clj	Fri Jun 24 20:54:17 2011 -0400
@@ -8,21 +8,8 @@
 (def minecraft-local {:name "localhost" :port 25565})
 
 (declare conn-handler)
+(declare login)
 
-; Connection Wrappers --------------------------------------------------------------
-(defn connect [server]
-  (let [socket (Socket. (:name server) (:port server))
-        in (DataInputStream. (.getInputStream socket))
-        out (DataOutputStream. (.getOutputStream socket))
-        conn (ref {:in in :out out})]
-    (doto (Thread. #(conn-handler conn)) (.start))
-    conn))
-
-(defn disconnect [conn]
-  (dosync (alter conn merge {:exit true})))
-
-
-; Connection Handling --------------------------------------------------------------
 (defn login [conn]
   ; Send handshake
   (write-packet conn :handshake {:username "timmy"})
@@ -36,16 +23,37 @@
   ; Get login
   (read-packet conn))
 
-(defn conn-handler [conn]
-  (println "connecting")
-  (login conn)
-  (while (nil? (:exit @conn))
-    (read-packet conn))
+
+(defn input-handler [bot]
+  (let [conn (:connection bot)]
+    (while (nil? (:exit @conn))
+      (read-packet conn)))
   (println "done"))
 
 
+(defn connect [server]
+  (let [socket (Socket. (:name server) (:port server))
+        in (DataInputStream. (.getInputStream socket))
+        out (DataOutputStream. (.getOutputStream socket))
+        conn (ref {:in in :out out})
+        bot {:connection conn}]
+
+    (println "connecting")
+    (login conn)
+    (println "connected and logged in")
+
+    (println "starting read handler")
+    (doto (Thread. #(input-handler bot)) (.start))
+
+    (println "all systems go, returning bot")
+    bot))
+
+(defn disconnect [bot]
+  (dosync (alter (:connection bot) merge {:exit true})))
+
+
 
 ; Scratch --------------------------------------------------------------------------
-(def server (connect minecraft-local))
-;(disconnect server)
+(def bot (connect minecraft-local))
+;(disconnect bot)
 
--- a/src/clojurecraft/in.clj	Fri Jun 24 20:15:47 2011 -0400
+++ b/src/clojurecraft/in.clj	Fri Jun 24 20:54:17 2011 -0400
@@ -451,65 +451,65 @@
 ; Reading Wrappers -----------------------------------------------------------------
 (defn read-packet [conn]
   (let [packet-id (int (-read-byte conn))
-        packet-type (packet-types packet-id)]
-    (println "\n--PACKET--> " packet-type)
-    (println
-      (case packet-type
-        :keepalive                 (read-packet-keepalive conn)
-        :handshake                 (read-packet-handshake conn)
-        :login                     (read-packet-login conn)
-        :chat                      (read-packet-chat conn)
-        :timeupdate                (read-packet-timeupdate conn)
-        :equipment                 (read-packet-equipment conn)
-        :spawnposition             (read-packet-spawnposition conn)
-        :useentity                 (read-packet-useentity conn)
-        :updatehealth              (read-packet-updatehealth conn)
-        :respawn                   (read-packet-respawn conn)
-        :playerpositionlook        (read-packet-playerpositionlook conn)
-        :playerdigging             (read-packet-playerdigging conn)
-        :playerblockplacement      (read-packet-playerblockplacement conn)
-        :holdingchange             (read-packet-holdingchange conn)
-        :usebed                    (read-packet-usebed conn)
-        :animate                   (read-packet-animate conn)
-        :entityaction              (read-packet-entityaction conn)
-        :namedentityspawn          (read-packet-namedentityspawn conn)
-        :pickupspawn               (read-packet-pickupspawn conn)
-        :collectitem               (read-packet-collectitem conn)
-        :addobjectvehicle          (read-packet-addobjectvehicle conn)
-        :mobspawn                  (read-packet-mobspawn conn)
-        :entitypainting            (read-packet-entitypainting conn)
-        :stanceupdate              (read-packet-stanceupdate conn)
-        :entityvelocity            (read-packet-entityvelocity conn)
-        :entitydestroy             (read-packet-entitydestroy conn)
-        :entity                    (read-packet-entity conn)
-        :entityrelativemove        (read-packet-entityrelativemove conn)
-        :entitylook                (read-packet-entitylook conn)
-        :entitylookandrelativemove (read-packet-entitylookandrelativemove conn)
-        :entityteleport            (read-packet-entityteleport conn)
-        :entitystatus              (read-packet-entitystatus conn)
-        :attachentity              (read-packet-attachentity conn)
-        :entitymetadata            (read-packet-entitymetadata conn)
-        :prechunk                  (read-packet-prechunk conn)
-        :mapchunk                  (read-packet-mapchunk conn)
-        :multiblockchange          (read-packet-multiblockchange conn)
-        :blockchange               (read-packet-blockchange conn)
-        :playnoteblock             (read-packet-playnoteblock conn)
-        :explosion                 (read-packet-explosion conn)
-        :soundeffect               (read-packet-soundeffect conn)
-        :newinvalidstate           (read-packet-newinvalidstate conn)
-        :thunderbolt               (read-packet-thunderbolt conn)
-        :openwindow                (read-packet-openwindow conn)
-        :closewindow               (read-packet-closewindow conn)
-        :setslot                   (read-packet-setslot conn)
-        :windowitems               (read-packet-windowitems conn)
-        :updateprogressbar         (read-packet-updateprogressbar conn)
-        :transaction               (read-packet-transaction conn)
-        :updatesign                (read-packet-updatesign conn)
-        :mapdata                   (read-packet-mapdata conn)
-        :incrementstatistic        (read-packet-incrementstatistic conn)
-        :disconnectkick            (read-packet-disconnectkick conn)
-
-        :else (str "UNKNOWN PACKET TYPE: " packet-id)
-        ))
+                  packet-type (packet-types packet-id)]
+    (if (= nil packet-type)
+      (println (str "UNKNOWN PACKET TYPE: " (Integer/toHexString packet-id))) 
+      (do
+        (println "\n--PACKET--> " packet-type)
+        (println
+          (case packet-type
+            :keepalive                 (read-packet-keepalive conn)
+            :handshake                 (read-packet-handshake conn)
+            :login                     (read-packet-login conn)
+            :chat                      (read-packet-chat conn)
+            :timeupdate                (read-packet-timeupdate conn)
+            :equipment                 (read-packet-equipment conn)
+            :spawnposition             (read-packet-spawnposition conn)
+            :useentity                 (read-packet-useentity conn)
+            :updatehealth              (read-packet-updatehealth conn)
+            :respawn                   (read-packet-respawn conn)
+            :playerpositionlook        (read-packet-playerpositionlook conn)
+            :playerdigging             (read-packet-playerdigging conn)
+            :playerblockplacement      (read-packet-playerblockplacement conn)
+            :holdingchange             (read-packet-holdingchange conn)
+            :usebed                    (read-packet-usebed conn)
+            :animate                   (read-packet-animate conn)
+            :entityaction              (read-packet-entityaction conn)
+            :namedentityspawn          (read-packet-namedentityspawn conn)
+            :pickupspawn               (read-packet-pickupspawn conn)
+            :collectitem               (read-packet-collectitem conn)
+            :addobjectvehicle          (read-packet-addobjectvehicle conn)
+            :mobspawn                  (read-packet-mobspawn conn)
+            :entitypainting            (read-packet-entitypainting conn)
+            :stanceupdate              (read-packet-stanceupdate conn)
+            :entityvelocity            (read-packet-entityvelocity conn)
+            :entitydestroy             (read-packet-entitydestroy conn)
+            :entity                    (read-packet-entity conn)
+            :entityrelativemove        (read-packet-entityrelativemove conn)
+            :entitylook                (read-packet-entitylook conn)
+            :entitylookandrelativemove (read-packet-entitylookandrelativemove conn)
+            :entityteleport            (read-packet-entityteleport conn)
+            :entitystatus              (read-packet-entitystatus conn)
+            :attachentity              (read-packet-attachentity conn)
+            :entitymetadata            (read-packet-entitymetadata conn)
+            :prechunk                  (read-packet-prechunk conn)
+            :mapchunk                  (read-packet-mapchunk conn)
+            :multiblockchange          (read-packet-multiblockchange conn)
+            :blockchange               (read-packet-blockchange conn)
+            :playnoteblock             (read-packet-playnoteblock conn)
+            :explosion                 (read-packet-explosion conn)
+            :soundeffect               (read-packet-soundeffect conn)
+            :newinvalidstate           (read-packet-newinvalidstate conn)
+            :thunderbolt               (read-packet-thunderbolt conn)
+            :openwindow                (read-packet-openwindow conn)
+            :closewindow               (read-packet-closewindow conn)
+            :setslot                   (read-packet-setslot conn)
+            :windowitems               (read-packet-windowitems conn)
+            :updateprogressbar         (read-packet-updateprogressbar conn)
+            :transaction               (read-packet-transaction conn)
+            :updatesign                (read-packet-updatesign conn)
+            :mapdata                   (read-packet-mapdata conn)
+            :incrementstatistic        (read-packet-incrementstatistic conn)
+            :disconnectkick            (read-packet-disconnectkick conn)))))
     (println "\n\n\n")))