fc293cb5f83e

More bot refactoring.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Fri, 24 Jun 2011 21:05:15 -0400
parents 60a56dc38ec2
children 6799868ce43d
branches/tags (none)
files src/clojurecraft/core.clj src/clojurecraft/in.clj src/clojurecraft/out.clj

Changes

--- a/src/clojurecraft/core.clj	Fri Jun 24 20:54:17 2011 -0400
+++ b/src/clojurecraft/core.clj	Fri Jun 24 21:05:15 2011 -0400
@@ -10,24 +10,24 @@
 (declare conn-handler)
 (declare login)
 
-(defn login [conn]
+(defn login [bot]
   ; Send handshake
-  (write-packet conn :handshake {:username "timmy"})
+  (write-packet bot :handshake {:username "timmy"})
 
   ; Get handshake
-  (read-packet conn)
+  (read-packet bot)
 
   ; Send login
-  (write-packet conn :login {:version 13 :username "timmy"})
+  (write-packet bot :login {:version 13 :username "timmy"})
 
   ; Get login
-  (read-packet conn))
+  (read-packet bot))
 
 
 (defn input-handler [bot]
   (let [conn (:connection bot)]
     (while (nil? (:exit @conn))
-      (read-packet conn)))
+      (read-packet bot)))
   (println "done"))
 
 
@@ -39,7 +39,7 @@
         bot {:connection conn}]
 
     (println "connecting")
-    (login conn)
+    (login bot)
     (println "connected and logged in")
 
     (println "starting read handler")
@@ -54,6 +54,6 @@
 
 
 ; Scratch --------------------------------------------------------------------------
-(def bot (connect minecraft-local))
+;(def bot (connect minecraft-local))
 ;(disconnect bot)
 
--- a/src/clojurecraft/in.clj	Fri Jun 24 20:54:17 2011 -0400
+++ b/src/clojurecraft/in.clj	Fri Jun 24 21:05:15 2011 -0400
@@ -449,9 +449,10 @@
 
 
 ; Reading Wrappers -----------------------------------------------------------------
-(defn read-packet [conn]
-  (let [packet-id (int (-read-byte conn))
-                  packet-type (packet-types packet-id)]
+(defn read-packet [bot]
+  (let [conn (:connection bot)
+        packet-id (int (-read-byte conn))
+        packet-type (packet-types packet-id)]
     (if (= nil packet-type)
       (println (str "UNKNOWN PACKET TYPE: " (Integer/toHexString packet-id))) 
       (do
--- a/src/clojurecraft/out.clj	Fri Jun 24 20:54:17 2011 -0400
+++ b/src/clojurecraft/out.clj	Fri Jun 24 21:05:15 2011 -0400
@@ -316,42 +316,41 @@
 (defn- flushc [conn]
   (doto (:out @conn) (.flush)))
 
-(defn write-packet [conn packet-type payload]
-  (case packet-type
-    :keepalive            (write-packet-handshake 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)
+(defn write-packet [bot packet-type payload]
+  (let [conn (:connection bot)]
+    (case packet-type
+      :keepalive            (write-packet-handshake 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)))
 
-    )
-  (flushc conn))
-