/
conjcraft_main.clj
80 lines (67 loc) · 2.22 KB
/
conjcraft_main.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
(ns conjcraft)
(import clojure.lang.Reflector)
(import java.io.File)
; Reflection helpers
(defn inst [clas & args]
(Reflector/invokeConstructor clas (to-array args)))
(defn callit [target method & args]
(Reflector/invokeStaticMethod target (str method)
(to-array args)))
; Core helpers
(defn load-sibling
"Load a clojure file that is on the same folder as this"
[filename]
(load-file
(.getAbsolutePath (File. (-> *file* File. .getParent) (str (name filename) ".clj")))))
(defn mapit
"Convert java hashmap into a clojure hashmap"
[hashmap] (into {} hashmap))
; The state
(def Block)
(def ItemStack)
(def ModLoader)
(def blocks)
(def items)
(def materials)
(def cur-block-id 180)
; Wrapping the reflection calls into more frindly functions
(defn add-recipe [stack array]
(callit ModLoader 'addRecipe stack (to-array array)))
(defn register-block [block]
(callit ModLoader 'registerBlock block))
(defn name-block [block name]
(callit ModLoader 'addName block name))
(defn item-stack [block n] (inst ItemStack block n))
(defn intg [x] (Integer. x))
(defn create-block
"Example of how to a basic block in clojure."
[name texturefile]
(let [droppedfn (constantly (intg cur-block-id))
quantityfn (constantly (intg 7))
;Integer. required: http://dev.clojure.org/jira/browse/CLJ-445
block (inst Block (intg cur-block-id) (intg 0)
(materials "sand") droppedfn quantityfn)
texture (callit ModLoader 'addOverride "/terrain.png" texturefile)]
(doto block
(.hardness 0.0)
(.lightValue 1.0)
(.blockName name)
(.setTexture texture))
(register-block block)
(name-block block name)
(def cur-block-id (inc cur-block-id))
block))
; Function loaded from java, and other modules.
(load-sibling :recipe_dsl)
(load-sibling :recipes)
(defn call [jblocks jitems jmaterials Block-cls ItemStack-cls ModLoader-cls]
(do
(def Block Block-cls)
(def ItemStack ItemStack-cls)
(def ModLoader ModLoader-cls)
(def items (mapit jitems))
(def materials (mapit jmaterials))
(def blocks (assoc (mapit jblocks)
"clojure" (create-block "Clojure Block" "/16_clojure.png")
"github" (create-block "Github Block" "/16_github.png")))
(create-recipes)))