# HG changeset patch # User Steve Losh # Date 1312943157 14400 # Node ID 7102fee989ed704d3367a1e22774e52ee3913654 # Parent 6f54b979c6b581e6d53ee6febf3fca1e8265a477 JUMPBOT 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 {}