# HG changeset patch # User Steve Losh # Date 1313538989 14400 # Node ID fb4326dadfe8a657131aa92520b775bb311c7f59 # Parent 508af48cc605975a3e1ee729b41fa531033546f8 Add an example bot and fix the death event. diff -r 508af48cc605 -r fb4326dadfe8 src/clojurecraft/in.clj --- a/src/clojurecraft/in.clj Mon Aug 15 14:35:32 2011 -0400 +++ b/src/clojurecraft/in.clj Tue Aug 16 19:56:29 2011 -0400 @@ -141,7 +141,7 @@ (defn- read-packet-updatehealth [bot conn] (let [payload (assoc {} :health (-read-short conn))] - (if (= (:health payload) 0) + (if (<= (:health payload) 0) (events/fire-dead bot)) payload)) diff -r 508af48cc605 -r fb4326dadfe8 src/examples/givebot.clj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/examples/givebot.clj Tue Aug 16 19:56:29 2011 -0400 @@ -0,0 +1,58 @@ +(ns examples.givebot + (:require [clojure.contrib.string :as s]) + (:require [clojurecraft.core :as core]) + (:require [clojurecraft.events :as events]) + (:require [clojurecraft.actions :as actions])) + +(def item-map {"tnt" "46", "lever" "69"}) +(def WANT-RE #"^<(\w+)> i want (\d+)? ?(.+)?$") + +(defn- get-item [matches] + (get matches 3)) + +(defn- get-username [matches] + (get matches 1)) + +(defn- get-number [matches] + (or (get matches 2) "1")) + +(defn- get-numbers + "Return a sequence of numbers that add up to the desired number of objects. + + e.g.: + + 1 -> [1] + 64 -> [64] + 128 -> [64 64] + 129 -> [64 64 1]" + [matches] + (loop [number (Integer. (get-number matches)) + numbers []] + (if (<= number 0) + numbers + (recur (- number 64) + (conj numbers (min 64 number)))))) + + +(defn- give-string [username item number] + (str "/give " username " " item " " number)) + +(defn- handle-chat [bot message] + (let [matches (re-find WANT-RE (s/lower-case message))] + (when matches + (let [item (item-map (get-item matches)) + username (get-username matches) + numbers (get-numbers matches)] + (map #(actions/chat bot (give-string username item %)) numbers))))) + +(defn handle-dead [bot] + [(actions/chat bot "WHY?!") + (actions/respawn bot)]) + + +(defn make-givebot [server username] + (let [bot (core/connect server username)] + (events/add-handler bot :dead #'handle-dead) + bot)) + +