You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# <imgsrc="icon.png"alt="Mod Icon"width="32"height="32"/> Custom JSON Lib for Mindustry
2
2
3
3
Allows [Mindustry](https://github.com/Anuken/Mindustry) Mods to add and read custom JSON tags to act as libraries for other Mindustry Mods.
4
4
5
+
> [!NOTE]
6
+
> If your mod uses `CustomJsonLib`, `CustomJsonLib` will need to be installed in Mindustry as a mod, either through the Mod Browser or manually. Otherwise, all the functionality implemented herein will not be available.
7
+
5
8
## How do I use this library?
6
9
7
10
<details>
8
11
<summary>I want to implement new JSON tags</summary>
9
-
10
-
## My mod is written in Java
11
-
If your mod is written in Java (and is, hence, a Jar mod), use this method.
12
-
13
-
1.**Add the library as a dependency in your**`mod.[h]json`**file:**
14
-
15
-
> You can SKIP this step if your mod supports this library but does not *require* it to function.
16
-
17
-
JSON:
12
+
13
+
## My mod is written in Java
14
+
If your mod is written in Java (and is, hence, a Jar mod), use this method.
15
+
16
+
1.**Add the library as a dependency in your**`mod.[h]json`**file:**
17
+
18
+
> You can SKIP this step if your mod supports this library but does not *require* it to function.
19
+
20
+
JSON:
18
21
```json
19
22
"dependencies": [
20
-
"pyguy.jsonlib"
21
-
]
23
+
"pyguy.jsonlib"
24
+
]
22
25
```
23
-
HJSON:
26
+
HJSON:
24
27
```
25
28
dependencies: [
26
29
pyguy.jsonlib
27
30
]
28
31
```
29
-
30
-
2.**Add the library Jar file into your project as a Java dependency:**
31
-
32
-
This is a necessary step before you can compile your mod, since the library Jar contains the methods (functions) used to access custom JSON tags.\
33
-
To get the library file, either download the latest release of `CustomJsonLib.jar` (NOT `CustomJsonLibDesktop.jar`) from Releases, or compile your own (See [Building](#building) below).
34
-
35
-
36
-
Copy the file into a directory called `lib/` you must create on your mod's root directory:
32
+
33
+
2.**Add the library Jar file into your project as a Java dependency:**
34
+
35
+
This is a necessary step before you can compile your mod, since the library Jar contains the methods (functions) used to access custom JSON tags.\
36
+
To get the library file, either download the latest release of `CustomJsonLib.jar` (NOT `CustomJsonLibDesktop.jar`) from Releases, or compile your own (See [Building](#building) below).
37
+
38
+
39
+
Copy the file into a directory called `lib/` you must create on your mod's root directory:
37
40
```
38
41
- YourAwesomeMod/
39
42
- src/
@@ -42,69 +45,69 @@ Allows [Mindustry](https://github.com/Anuken/Mindustry) Mods to add and read cus
42
45
- lib/
43
46
- CustomJsonLib.jar
44
47
```
45
-
46
-
Assuming you're using Gradle as your build system, add the Jar file as a dependency in your mod's `build.gradle.kts`:
48
+
49
+
Assuming you're using Gradle as your build system, add the Jar file as a dependency in your mod's `build.gradle.kts`:
If you are using other build systems, ensure that you are adding the library as a Compile Only dependency. This is VERY important and your mod will not work properly otherwise.
60
-
61
-
62
-
Do note that most IDEs will not immediately detect the library after this step. Please restart your IDE or reload the Gradle script (ask your favorite Search Engine how to do this) for it to take effect.
63
-
64
-
3.**Use the library**
65
-
66
-
Now the library is part of your project. This does NOT mean it will be shipped with your Jar files, and it makes the files no larger, it just allows for compilation and usage of the library methods.
67
-
68
-
69
-
The content table for JSON tags is created and ready to be used from your mod's `init()` method onward. If you plan on checking all content for custom JSON tags, it is recommended to do so after the client loads, like so:
62
+
If you are using other build systems, ensure that you are adding the library as a Compile Only dependency. This is VERY important and your mod will not work properly otherwise.
63
+
64
+
65
+
Do note that most IDEs will not immediately detect the library after this step. Please restart your IDE or reload the Gradle script (ask your favorite Search Engine how to do this) for it to take effect.
66
+
67
+
3.**Use the library**
68
+
69
+
Now the library is part of your project. This does NOT mean it will be shipped with your Jar files, and it makes the files no larger, it just allows for compilation and usage of the library methods.
70
+
71
+
72
+
The content table for JSON tags is created and ready to be used from your mod's `init()` method onward. If you plan on checking all content for custom JSON tags, it is recommended to do so after the client loads, like so:
To know what methods this library supports, see [Using the Library](#using-the-library) below.
81
-
82
+
83
+
To know what methods this library supports, see [Using the Library](#using-the-library) below.
84
+
82
85
<br/>
83
-
84
-
## My mod is written in JavaScript and [H]JSON
85
-
If your mod is written in JavaScript and [H]JSON (and is, hence, a standard Mindustry mod), use this method.
86
-
87
-
1.**Add the library as a dependency in your**`mod.[h]json`**file:**
88
-
89
-
> You can SKIP this step if your mod supports this library but does not *require* it to function.
90
-
91
-
JSON:
86
+
87
+
## My mod is written in JavaScript and [H]JSON
88
+
If your mod is written in JavaScript and [H]JSON (and is, hence, a standard Mindustry mod), use this method.
89
+
90
+
1.**Add the library as a dependency in your**`mod.[h]json`**file:**
91
+
92
+
> You can SKIP this step if your mod supports this library but does not *require* it to function.
93
+
94
+
JSON:
92
95
```json
93
96
"dependencies": [
94
-
"pyguy.jsonlib"
95
-
]
97
+
"pyguy.jsonlib"
98
+
]
96
99
```
97
-
HJSON:
100
+
HJSON:
98
101
```
99
102
dependencies: [
100
103
pyguy.jsonlib
101
104
]
102
105
```
103
-
104
-
2.**Create a reference to JsonLibWrapper for your mod:**
105
-
106
-
Modded classpaths are not included into Rhino JS by default (this means that you cannot directly access the library from JS, you need some work for it).\
107
-
For this very reason, you need to create a reference that you can use within your mod. To do this, append this to the end of your `main.js` script:
106
+
107
+
2.**Create a reference to JsonLibWrapper for your mod:**
108
+
109
+
Modded classpaths are not included into Rhino JS by default (this means that you cannot directly access the library from JS, you need some work for it).\
110
+
For this very reason, you need to create a reference that you can use within your mod. To do this, append this to the end of your `main.js` script:
108
111
```javascript
109
112
var JsonLibWrapper =null;
110
113
Events.on(ClientLoadEvent, event=> {
@@ -121,44 +124,44 @@ Allows [Mindustry](https://github.com/Anuken/Mindustry) Mods to add and read cus
121
124
}
122
125
});
123
126
```
124
-
125
-
After this is executed, JsonLibWrapper will have one of two values: `null` if the CustomJsonLib is not currently installed in Mindustry, or the API object that you can use to work with the library otherwise.
126
-
Do note that only AFTER the client has loaded will JsonLibWrapper have a value, and if CustomJsonLib is not installed in Mindustry, it will not throw an error but rather it will not execute your code at all.
127
-
128
-
3.**Use the library**
129
-
130
-
Now the library is part of your project.\
131
-
To know what methods this library supports, see [Using the Library](#using-the-library) below
127
+
128
+
After this is executed, JsonLibWrapper will have one of two values: `null` if the CustomJsonLib is not currently installed in Mindustry, or the API object that you can use to work with the library otherwise.
129
+
Do note that only AFTER the client has loaded will JsonLibWrapper have a value, and if CustomJsonLib is not installed in Mindustry, it will not throw an error but rather it will not execute your code at all.
130
+
131
+
3.**Use the library**
132
+
133
+
Now the library is part of your project.\
134
+
To know what methods this library supports, see [Using the Library](#using-the-library) below
132
135
</details>
133
136
134
137
<br/>
135
138
136
139
<details>
137
140
<summary>I want to use JSON tags implemented by other Mods</summary>
138
-
141
+
139
142
<br/>
140
-
141
-
To add custom tags implemented by other Mods, follow this structure:
142
143
143
-
144
-
Let's say you want to add to a block named `weigthedBomb` a tag called `weight` from a mod whose internal name is `physics-mod`, and a tag called `explosionSize` from a mod whose internal name is `super-explosions`.
144
+
To add custom tags implemented by other Mods, follow this structure:
145
145
146
-
In your content's [h]json file you'd add the following:
147
-
148
-
JSON (weightedBomb.json):
146
+
147
+
Let's say you want to add to a block named `weigthedBomb` a tag called `weight` from a mod whose internal name is `physics-mod`, and a tag called `explosionSize` from a mod whose internal name is `super-explosions`.
148
+
149
+
In your content's [h]json file you'd add the following:
150
+
151
+
JSON (weightedBomb.json):
149
152
```json
150
153
{
151
-
"type": "Block",
154
+
"type": "Block",
152
155
153
-
...
156
+
...
154
157
155
-
"customJson": [
156
-
"physics-mod-weight": 45,
157
-
"super-explosions-explosionSize": "huge"
158
-
]
159
-
}
158
+
"customJson": [
159
+
"physics-mod-weight": 45,
160
+
"super-explosions-explosionSize": "huge"
161
+
]
162
+
}
160
163
```
161
-
HJSON (weightedBomb.hjson):
164
+
HJSON (weightedBomb.hjson):
162
165
```
163
166
type: Block
164
167
@@ -170,7 +173,7 @@ Allows [Mindustry](https://github.com/Anuken/Mindustry) Mods to add and read cus
170
173
]
171
174
```
172
175
173
-
> IMPORTANT: `type` here is added for illustration purposes only. The only part that matters is the `customJson` array.
176
+
> IMPORTANT: `type` here is added for illustration purposes only. The only part that matters is the `customJson` array.
174
177
175
178
</details>
176
179
@@ -199,6 +202,104 @@ If you want to support these in your own Mods, these are the tags implemented:
199
202
-`physics-mod-objectType`: Indicates the type of physicsObject your Block is. Options: [`sphere`, `staticMesh`, `breakableObject`] (string)
200
203
```
201
204
205
+
## Using the Library
206
+
207
+
If your mod is using Java, you're gonna have to import the CustomJsonLib package:
208
+
209
+
```java
210
+
importpyguy.jsonlib.JsonLibWrapper;
211
+
```
212
+
213
+
### What methods does this library implement (Java and JS):
214
+
215
+
Read a String custom tag named `fieldName` from a piece of content with the name `internalContentName`.
let replaceLocalized =JsonLibWrapper.GetBooleanField(block.name, "example-mod-replaceLocalized");
292
+
293
+
if (replaceLocalized)
294
+
{
295
+
block.localizedName="Replaced";
296
+
}
297
+
}
298
+
}
299
+
});
300
+
```
301
+
</details>
302
+
202
303
## Building
203
304
204
305
This mod is, in theory, cross-platform, but only Windows has been tested. The only platform not supported is iOS, just because it does not allow for Jar mods to be installed.
0 commit comments