Skip to content

Commit

Permalink
Merge pull request #1 from taufiqkh/master
Browse files Browse the repository at this point in the history
Added nRepl support and very basic config file reading, removed deprecated bukkit event type and priority enums.
  • Loading branch information
CmdrDats committed Mar 3, 2012
2 parents 00978e8 + c107581 commit 38b950a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 24 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -2,7 +2,11 @@ pom.xml
*jar
/lib/
/classes/
.classpath
.lein-failures
.lein-deps-sum
.project
*~
bin
tmp
target
2 changes: 1 addition & 1 deletion javasrc/cljminecraft/ClojurePlugin.java
Expand Up @@ -45,7 +45,7 @@ public void onDisable() {
String name = getDescription().getName();
System.out.println("Disabling "+name+" clojure Plugin");
if ("clj-minecraft".equals(name)) {
onEnable("cljminecraft.core", "onenable");
onEnable("cljminecraft.core", "ondisable");
} else {
onEnable(name+".core", "disable-plugin");
}
Expand Down
3 changes: 2 additions & 1 deletion project.clj
Expand Up @@ -3,8 +3,9 @@
:dependencies [[org.clojure/clojure "1.3.0"]
[org.clojure/tools.logging "0.2.3"]
[swank-clojure/swank-clojure "1.3.3"]
[org.clojure/tools.nrepl "0.0.5"]
]
:dev-dependencies [[org.bukkit/bukkit "1.0.0-R1-SNAPSHOT"]]
:dev-dependencies [[org.bukkit/bukkit "1.1-R5-SNAPSHOT"]]

:repl-options [:init nil :caught clj-stacktrace.repl/pst+]
:javac-options {:destdir "classes/"}
Expand Down
23 changes: 23 additions & 0 deletions src/cljminecraft/config.clj
@@ -0,0 +1,23 @@
(ns cljminecraft.config
"Provides a thin wrapper for bukkit config"
(:use cljminecraft.logging))

(defrecord Config [bukkit-config defaults])

(defn load-config [plugin defaults]
"Loads the bukkit config file for the given plugin and sets defaults"
(let [bukkit-config (.getConfig plugin)
config-defaults (if (nil? defaults) {} defaults)]
(.addDefaults bukkit-config config-defaults)
(Config. bukkit-config config-defaults)))

(defn get-keyword [config entry-key accepted-values]
"Gets the keyword for the specified entry key, or the default if no entry
exists or the entry is not in the set of accepted values. If no such default
exists, returns nil."
(let [config-entry (keyword (.get (:bukkit-config config) entry-key))]
(if (contains? accepted-values config-entry)
config-entry
(let [default (get (:defaults config) entry-key)]
(warn (format "Unrecognised repl type: %s, using default %s" config-entry (pr-str keyword)))
default))))
52 changes: 30 additions & 22 deletions src/cljminecraft/core.clj
@@ -1,22 +1,15 @@
(ns cljminecraft.core
(:require [clojure.set :as set])
(:require [swank.swank])
(:use [clojure.tools.logging]))
(:require [clojure.set :as set]
[swank.swank]
[clojure.tools.nrepl])
(:use [cljminecraft.logging]
[cljminecraft.config]))

(declare clj-server*)
(declare clj-plugin*)
(declare clj-plugin-manager*)
(declare clj-plugin-desc*)

(defmacro log-info [str]
`(info (.getName ~(symbol "*ns*")) ":" ~str))

(defmacro log-warn [str]
`(warn (.getName ~(symbol "*ns*")) ":" ~str))

(defmacro log-debug [str]
`(debug (.getName ~(symbol "*ns*")) ":" ~str))

(defmacro auto-proxy
"Automatically build a proxy, stubbing out useless entries, ala: http://www.brool.com/index.php/snippet-automatic-proxy-creation-in-clojure"
[interfaces variables & args]
Expand All @@ -30,29 +23,44 @@
(defmacro map-enums [enumclass]
`(apply merge (map #(hash-map (keyword (.name %)) %) (~(symbol (apply str (name enumclass) "/values"))))))

(def event-types (map-enums org.bukkit.event.Event$Type))
(def event-priorities (map-enums org.bukkit.event.Event$Priority))

(def plugins (ref {}))

(defonce swank* nil)
(def repl-types* #{:nrepl :swank})

(def repl-type (ref nil))

(def repl-server (agent nil))

(defn broadcast-msg [message]
(.broadcastMessage clj-server* message))

(defn start-clojure []
(if (nil? swank*)
(def swank* (swank.swank/start-repl 4005))))
(defn start-clojure [new-repl-type]
(dosync
(when (nil? @repl-type)
(ref-set repl-type new-repl-type)
(send-off repl-server
(fn [_] (case new-repl-type
:nrepl
(let [nrepl-port 4006]
(info (format "Starting nRepl server on port %d" nrepl-port))
(clojure.tools.nrepl/start-server nrepl-port)
)
; Default to swank
(let [swank-port 4005]
; Swank server provides its own log notification
(swank.swank/start-repl 4005))))))))

(defn onenable [plugin]
(def clj-plugin* plugin)
(def clj-server* (.getServer plugin))
(def clj-plugin-manager* (.getPluginManager clj-server* ))
(def clj-plugin-desc* (.getDescription plugin))
(start-clojure)
(log-info "Clojure started")
(let [repl-key "repl"
config (load-config plugin {repl-key :swank})]
(start-clojure (get-keyword config repl-key repl-types*)))
(info "Clojure started")
)

(defn ondisable [plugin]
(log-info "Clojure stopped"))
(info "Clojure stopped"))

11 changes: 11 additions & 0 deletions src/cljminecraft/logging.clj
@@ -0,0 +1,11 @@
(ns cljminecraft.logging
(:require [clojure.tools.logging :as logging]))

(defmacro info [str]
`(logging/info (.getName ~(symbol "*ns*")) ":" ~str))

(defmacro warn [str]
`(logging/warn (.getName ~(symbol "*ns*")) ":" ~str))

(defmacro debug [str]
`(logging/debug (.getName ~(symbol "*ns*")) ":" ~str))

0 comments on commit 38b950a

Please sign in to comment.