d9fe18146e9d

Chat action.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Fri, 12 Aug 2011 20:47:39 -0400
parents 771c6a626c93
children 32e1c608e927
branches/tags (none)
files docs/source/actions.rst src/clojurecraft/actions.clj src/clojurecraft/core.clj src/clojurecraft/data.clj

Changes

diff -r 771c6a626c93 -r d9fe18146e9d docs/source/actions.rst
--- a/docs/source/actions.rst	Fri Aug 12 19:01:19 2011 -0400
+++ b/docs/source/actions.rst	Fri Aug 12 20:47:39 2011 -0400
@@ -13,6 +13,28 @@
 Technically you don't need to use ``force``, because the REPL's printing will force
 the result to be evaluated, but conceptually it's a good habit to get into.
 
+chat
+----
+
+``(clojurecraft.actions/chat bot message)``
+
+Tells the bot to send a chat message.
+
+Note: you *will* get kicked from the server if your message is too long.  Here's the
+formula for the vanilla Minecraft server::
+
+    (defn too-long? [bot message]
+      (> (+ 3 (count (:username bot)) (count message))
+         100))
+
+It's up to *you* to avoid sending messages that are too long.  This action doesn't
+handle it because there are multiple options you might want:
+
+  * Ignore messages that are too long.
+  * Split them into multiple messages.
+
+You might also be writing a bot for a modded server that allows longer messages.
+
 jump
 ----
 
diff -r 771c6a626c93 -r d9fe18146e9d src/clojurecraft/actions.clj
--- a/src/clojurecraft/actions.clj	Fri Aug 12 19:01:19 2011 -0400
+++ b/src/clojurecraft/actions.clj	Fri Aug 12 20:47:39 2011 -0400
@@ -1,5 +1,6 @@
 (ns clojurecraft.actions
   (:use [clojurecraft.util])
+  (:use [clojurecraft.out])
   (:require [clojurecraft.physics :as physics]))
 
 (defn move [bot x-change y-change z-change]
@@ -22,3 +23,6 @@
           (alter player assoc-in [:loc :onground] false)
           (alter player assoc :velocity physics/JUMP-VELOCITY))))))
 
+(defn chat [bot message]
+  (delay
+    (write-packet bot :chat {:message message})))
diff -r 771c6a626c93 -r d9fe18146e9d src/clojurecraft/core.clj
--- a/src/clojurecraft/core.clj	Fri Aug 12 19:01:19 2011 -0400
+++ b/src/clojurecraft/core.clj	Fri Aug 12 20:47:39 2011 -0400
@@ -109,7 +109,7 @@
         outqueue (LinkedBlockingQueue.)
         actionqueue (LinkedBlockingQueue.)
         world (get-world server)
-        bot (Bot. conn outqueue actionqueue nil world (ref {})
+        bot (Bot. conn username outqueue actionqueue nil world (ref {})
                   (atom {}) (atom {}))]
 
     (println "connecting")
diff -r 771c6a626c93 -r d9fe18146e9d src/clojurecraft/data.clj
--- a/src/clojurecraft/data.clj	Fri Aug 12 19:01:19 2011 -0400
+++ b/src/clojurecraft/data.clj	Fri Aug 12 20:47:39 2011 -0400
@@ -79,6 +79,9 @@
 ;
 ;   Don't ever touch this -- the writing thread will handle it.
 ;
+; username -> String
+;   The username of the bot.
+;
 ; outqueue -> LinkedBlockingQueue
 ;   A queue of packets to write, so we can coordinate the writes
 ;   to avoid mixing packets together.
@@ -105,6 +108,6 @@
 ;
 ; packet-counts-in  -> (atom {:packet-type integer})
 ; packet-counts-out -> (atom {:packet-type integer})
-(defrecord Bot [connection outqueue actionqueue player world
+(defrecord Bot [connection username outqueue actionqueue player world
                 event-handlers packet-counts-in packet-counts-out])