7102fee989ed

JUMPBOT
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Tue, 09 Aug 2011 22:25:57 -0400
parents 6f54b979c6b5
children 53bc495283f5
branches/tags (none)
files src/clojurecraft/core.clj src/clojurecraft/data.clj src/clojurecraft/events.clj src/clojurecraft/in.clj

Changes

diff -r 6f54b979c6b5 -r 7102fee989ed src/clojurecraft/core.clj
--- a/src/clojurecraft/core.clj	Tue Aug 09 21:55:15 2011 -0400
+++ b/src/clojurecraft/core.clj	Tue Aug 09 22:25:57 2011 -0400
@@ -100,7 +100,7 @@
         conn (ref {:in in :out out})
         outqueue (LinkedBlockingQueue.)
         world (get-world server)
-        bot (Bot. conn outqueue nil world
+        bot (Bot. conn outqueue nil world (ref {})
                   (atom {}) (atom {}))]
 
     (println "connecting")
diff -r 6f54b979c6b5 -r 7102fee989ed src/clojurecraft/data.clj
--- a/src/clojurecraft/data.clj	Tue Aug 09 21:55:15 2011 -0400
+++ b/src/clojurecraft/data.clj	Tue Aug 09 22:25:57 2011 -0400
@@ -100,8 +100,11 @@
 ;   Worlds themselves should never need to be updated after creation -- instead the
 ;   various refs inside them are updated.
 ;
+; event-handlers -> (ref {:event-type [handler ...]})
+;   A ref to a map of event handlers.
+;
 ; packet-counts-in  -> (atom {:packet-type integer})
 ; packet-counts-out -> (atom {:packet-type integer})
-(defrecord Bot [connection outqueue player world
+(defrecord Bot [connection outqueue player world event-handlers
                 packet-counts-in packet-counts-out])
 
diff -r 6f54b979c6b5 -r 7102fee989ed src/clojurecraft/events.clj
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/clojurecraft/events.clj	Tue Aug 09 22:25:57 2011 -0400
@@ -0,0 +1,16 @@
+(ns clojurecraft.events)
+
+(defn add-handler [bot event-type handler]
+  (dosync
+    (let [current-handlers (event-type @(:event-handlers bot))
+          updated-handlers (conj current-handlers handler)]
+      (alter (:event-handlers bot) assoc event-type updated-handlers))))
+
+(defn clear-handlers [bot event-type]
+  (dosync (alter (:event-handlers bot) dissoc event-type)))
+
+
+(defn fire-chat [bot message]
+  (dorun (map #((eval %) bot message)
+              (:chat @(:event-handlers bot)))))
+
diff -r 6f54b979c6b5 -r 7102fee989ed src/clojurecraft/in.clj
--- a/src/clojurecraft/in.clj	Tue Aug 09 21:55:15 2011 -0400
+++ b/src/clojurecraft/in.clj	Tue Aug 09 22:25:57 2011 -0400
@@ -3,6 +3,7 @@
   (:use [clojurecraft.mappings])
   (:use [clojurecraft.chunks])
   (:require [clojurecraft.data])
+  (:require [clojurecraft.events :as events])
   (:import [clojurecraft.data Location Entity Chunk])
   (:import (java.util.zip Inflater)))
 
@@ -106,8 +107,10 @@
          :dimension (-read-byte conn)))
 
 (defn- read-packet-chat [bot conn]
-  (assoc {}
-         :message (-read-string-ucs2 conn)))
+  (let [payload (assoc {}
+                       :message (-read-string-ucs2 conn))]
+    (events/fire-chat bot (:message payload))
+    payload))
 
 (defn- read-packet-timeupdate [bot conn]
   (let [payload (assoc {}