82b346fcfbf2

Dead event.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Fri, 12 Aug 2011 21:38:25 -0400
parents f03a74a0782c
children 1d52b74b6239
branches/tags (none)
files docs/source/events.rst src/clojurecraft/events.clj src/clojurecraft/in.clj

Changes

diff -r f03a74a0782c -r 82b346fcfbf2 docs/source/events.rst
--- a/docs/source/events.rst	Fri Aug 12 21:10:22 2011 -0400
+++ b/docs/source/events.rst	Fri Aug 12 21:38:25 2011 -0400
@@ -36,7 +36,7 @@
 
 ::
 
-    (defn chat-handler [message]
+    (defn chat-handler [bot message]
       [... actions ...])
 
 Chat events are fired when a chat message arrives.
@@ -53,3 +53,19 @@
     (defn message-is-own? [bot message]
       (clojure.contrib.string/substring? (str "<" (:username bot) ">")
                                          message))
+
+``:dead``
+`````````
+
+::
+
+    (defn dead-handler [bot]
+      [... actions ...])
+
+This event is fired when your bot dies.
+
+You'll probably want to respawn when this happens::
+
+    (defn dead-handler [bot]
+      [(clojurecraft.actions/chat bot "WHY YOU DO THIS?")
+       (clojurecraft.actions/respawn bot)])
diff -r f03a74a0782c -r 82b346fcfbf2 src/clojurecraft/events.clj
--- a/src/clojurecraft/events.clj	Fri Aug 12 21:10:22 2011 -0400
+++ b/src/clojurecraft/events.clj	Fri Aug 12 21:38:25 2011 -0400
@@ -17,6 +17,10 @@
         queue-action-group #(dorun (map queue-action %))]
     (dorun (map queue-action-group action-groups))))
 
+
 (defn fire-chat [bot message]
   (fire-handler bot :chat message))
 
+(defn fire-dead [bot]
+  (fire-handler bot :dead))
+
diff -r f03a74a0782c -r 82b346fcfbf2 src/clojurecraft/in.clj
--- a/src/clojurecraft/in.clj	Fri Aug 12 21:10:22 2011 -0400
+++ b/src/clojurecraft/in.clj	Fri Aug 12 21:38:25 2011 -0400
@@ -138,8 +138,11 @@
          :leftclick (-read-bool conn)))
 
 (defn- read-packet-updatehealth [bot conn]
-  (assoc {}
-         :health (-read-short conn)))
+  (let [payload (assoc {}
+                       :health (-read-short conn))]
+    (if (= (:health payload) 0)
+      (events/fire-dead bot))
+    payload))
 
 (defn- read-packet-respawn [bot conn]
   (assoc {}
@@ -327,9 +330,10 @@
          :pitch (-read-byte conn)))
 
 (defn- read-packet-entitystatus [bot conn]
-  (assoc {}
-         :eid (-read-int conn)
-         :entitystatus (-read-byte conn)))
+  (let [payload (assoc {}
+                       :eid (-read-int conn)
+                       :entitystatus (-read-byte conn))]
+    payload))
 
 (defn- read-packet-attachentity [bot conn]
   (assoc {}