   (:require [cljminecraft.bukkit :as bk])
   (:require [cljminecraft.logging :as log]
             [cljminecraft.commands :as cmd]
-            [cljminecraft.world :as w])
+            [cljminecraft.world :as w]
+            [cljminecraft.items :as i])
   (:require [cljminecraft.files :as files]))
 
 (defonce plugin (atom nil))
 (defonce memorystones (atom #{}))
+(defonce memorizedstones (atom {}))
+
 
 ;; Persistence handling
 (defn memstone-to-file [{:keys [block name] :as memstone}]

  (let [{:keys [stones]} (files/read-json-file @plugin "stones.json")]
    (reset! memorystones (set (map memstone-from-file stones)))))

;; Event Handling
(defn sign-change [ev]
  (let [[type name] (seq (.getLines ev))]
    (cond

     (do
       (swap! memorystones conj {:block (.getBlock ev) :name name})
       (write-memstones)
       {:msg "You placed a Memory Stone. Well done."}))))

(defn memstone-for-block [block]
  (first (filter #(.equals block (:block %)) @memorystones)))

(defn sign-break [ev]
  (when-let [memstone (memstone-for-block (.getBlock ev))]
    (swap! memorystones disj memstone)
    (doseq [[player memorized] @memorizedstones]
      (if (contains? memorized (:name memstone))
        (unmemorize-stone! player memstone)))
    (write-memstones)
    {:msg "You broke a Memory Stone! oh dear."}))

(defn player-interact [ev]
  (if (and
       (= (.getAction ev) (get ev/actions :left_click_block))
       (i/is-block? (.getClickedBlock ev) :wall_sign :sign_post))
    (if-let [memstone (memstone-for-block (.getClickedBlock ev))]
      (do
        (memorize-stone! (.getName (pl/get-player ev)) memstone)
        {:msg (format "Memorized %s" (:name memstone))}))))

(defn player-login [ev]
  (read-memory (.getName (pl/get-player ev))))

(defn events
  []
  [(ev/event "block.sign-change" #'sign-change)
   (ev/event "block.block-break" #'sign-break)
   (ev/event "player.player-interact" #'player-interact)
   (ev/event "player.player-login" #'player-login)])

;; Command Handling
(defn get-memorized-stones [named]
  (let [stones (get @memorizedstones (.getName named) #{})]
    (filter #(contains? stones (:name %)) @memorystones)))

(defmethod cmd/convert-type :memorystone [sender type arg]
  (log/info "Converting %s" (get @memorizedstones (.getName sender) #{}))
  (first (filter #(= (.toLowerCase (:name %)) (.toLowerCase arg)) (get-memorized-stones sender))))

(defmethod cmd/param-type-tabcomplete :memorystone [sender type arg]
  (let [lower (.toLowerCase arg)]
    (filter #(.startsWith % lower) (map #(.toLowerCase (:name %)) (get-memorized-stones sender)))))

(defn go-command [sender memorystone]
  (when memorystone
