Skip to content

Commit 8f2e8fa

Browse files
authored
Update Vanilla Registry Documentation (#36)
* fix examples with java * fix oredict description * comment info about map key bug * add examples of multiple ways to count * name some code blocks * update the modifications text * add the new vanilla stuff * add forge registry methods to mods
1 parent 9dcf14f commit 8f2e8fa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1111
-69
lines changed

docs/groovy-script/getting_started/classes.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,32 @@ The default package classes are located in is `classes`, so you would run `impor
2929

3030
## Example
3131

32-
::: info Basic Example {id="example"}
32+
:::: info Basic Example {id="example"}
3333

34-
With this file, named `DemoClass.groovy`, placed inside the `classes` folder, and the folder being added to the `classes` element in `runConfig.json`:
34+
With a named `DemoClass.groovy` that is registered by the `runConfig.json` to the `classes` element,
35+
you can access that class from any script file or another class.
3536

36-
```groovy
37+
::: code-group
38+
39+
```groovy [classes/DemoClass.groovy]
3740
class DemoClass {
3841
final static def iron = item 'minecraft:iron_ingot'
3942
final static def gold = item 'minecraft:gold_ingot'
4043
}
4144
```
4245

43-
You are able to import and use it in other script files:
46+
```json [runConfig.json]
47+
"classes": [
48+
"classes/" // targets either the file or a folder that the file is nested within
49+
]
50+
```
4451

45-
```groovy
52+
```groovy [postInit/CheckIron.groovy]
4653
import classes.DemoClass
4754
48-
log.info DemoClass.iron in ore('ingotIron')
55+
log.info(DemoClass.iron in ore('ingotIron'))
4956
```
5057

51-
5258
:::
59+
60+
::::

docs/groovy-script/getting_started/editors.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ This language support will work with any variation of Emacs, but will presume yo
9090

9191
1. Open Emacs and install [lsp-mode](https://emacs-lsp.github.io/lsp-mode/page/installation/), and follow further installation instructions for lsp-mode.
9292
2. Install the GroovyScript LSP file here:
93-
::: details GroovyScript LSP {id="example"}
94-
```lisp title="lsp-groovyscript.el"
93+
:::: details GroovyScript LSP {id="example"}
94+
::: code-group
95+
```lisp [lsp-groovyscript.el]
9596
;;; lsp-groovyscript.el --- GroovyScript LSP support for lsp-mode -*- lexical-binding: t; -*-
9697
9798
;; Version: 1.0.0
@@ -143,6 +144,7 @@ This language support will work with any variation of Emacs, but will presume yo
143144
;;; lsp-groovyscript.el ends here
144145
```
145146
:::
147+
::::
146148
3. [Start the Language Server](#start-the-language-server) via the instructions above
147149
4. The port in the `groovyscript.cfg` config file must match the port setting.
148150
5. Done

docs/groovy-script/getting_started/groovy_modifications.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,17 @@ import static net.minecraft.util.math.MathHelper.*
6060
## Maps
6161

6262

63-
GroovyScript replaces the use of `LinkedHashMap` with uses `Object2ObjectLinkedOpenHashMap` as the default map used in Groovy.
64-
This is done because it is slightly more memory efficient and keeps the element order.
63+
GroovyScript replaces uses of `LinkedHashMap` with `Object2ObjectLinkedOpenHashMap` as the default map used in Groovy.
64+
This is done because it is slightly more memory efficient while still keeping the element order.
6565

66+
If you want to create a `LinkedHashMap` anyways, you can simply import it and create a new instance of it as normal.
67+
This only changes the shorthand method of creating maps (`[:]`).
6668

6769

6870
## Security Restrictions
6971

7072

71-
A number of packages or classes are banned, and not able to be used by Groovy.
73+
A number of packages, classes, and methods are banned, and not able to be used by Groovy.
7274

73-
When interacting with a File object through Groovy, the File can only refer to a file path that targets inside the minecraft directory.
75+
In particular, when interacting with a File object through Groovy,
76+
the File can only refer to a file path that targets inside the minecraft directory.

docs/groovy-script/getting_started/reloading.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ However, you can add your own custom class and implement reloading for your reci
4343
To do so, first make a new file in `classes` with the class containing an `add`, `remove`, and an `onReload` method.
4444
It should have an instance created inside it for access purposes.
4545

46-
::: info DemoRegistry {id="example"}
47-
```groovy
46+
:::: info Example {id="example"}
47+
::: code-group {id="example"}
48+
49+
```groovy [classes/DemoRegistry.groovy]
4850
import example.DemoHolder
4951
import example.DemoRecipe
5052
@@ -74,11 +76,14 @@ class DemoRegistry {
7476
}
7577
```
7678
:::
79+
::::
7780

7881
Then, add an event listener that is listening for `GroovyReloadEvent`.
7982
Inside of this event listener, call the `onReload` method.
8083

81-
```groovy
84+
::: code-group
85+
86+
```groovy [postInit/ReloadHelper.groovy]
8287
import classes.DemoRegistry
8388
import com.cleanroommc.groovyscript.event.GroovyReloadEvent
8489
@@ -87,5 +92,7 @@ eventManager.listen(GroovyReloadEvent) {
8792
}
8893
```
8994

95+
:::
96+
9097
Finally, any time you would add a recipe to or remove a recipe from `DemoHolder.DemoRecipeList`,
9198
ensure it is manipulated via `DemoRegistry.instance`.

docs/groovy-script/getting_started/run_config.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ Scripts with stuff like Item Creation go in `groovy/preInit`.
1313

1414
Let's see what the file can look like.
1515

16-
::: info Example {id="example"}
17-
```json
16+
:::: info Example {id="example"}
17+
::: code-group
18+
```json [groovy/runConfig.json]
1819
{
1920
"packName": "",
2021
"packId": "",
@@ -42,6 +43,7 @@ Let's see what the file can look like.
4243
}
4344
```
4445
:::
46+
::::
4547

4648
Let's go through it bit by bit:
4749

docs/groovy-script/groovy/differences_from_java.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ Parentheses around parameters are optional when there is no ambiguity.
3838
::: code-group
3939

4040
```java
41-
Code.run('example');
42-
Code.run(Code.nested('example'));
43-
println('hello');
41+
Code.run("example");
42+
Code.run(Code.nested("example"));
43+
System.out.println("hello");
4444
```
4545

4646
```groovy
@@ -105,7 +105,7 @@ In Groovy, you can easily create a list using square braces `[]`, using commas `
105105
::: code-group
106106

107107
```java
108-
List<Integer> list = new ArrayList<>();
108+
List<String> list = new ArrayList<>();
109109
list.add("example");
110110
list.add("demo");
111111
```
@@ -131,7 +131,11 @@ map.put(3, "demo");
131131
```
132132

133133
```groovy
134-
def map = [1: "example", 3: "demo"]
134+
def map0 = [:]
135+
map0[1] = "example"
136+
map0[3] = "demo"
137+
def map1 = [1: "example", 3: "demo"] // shorthand
138+
def map2 = [(1): "example", (3): "demo"] // if the key is an expression, wrapping it in parentheses may be needed
135139
```
136140

137141
:::
@@ -166,6 +170,7 @@ If the field is final, only a *getter* method will be created.
166170

167171
```java
168172
public class DemoClass {
173+
private final String internal = "internal";
169174
private final String value = "value";
170175
private String name = "example";
171176

@@ -185,8 +190,9 @@ public class DemoClass {
185190

186191
```groovy
187192
class DemoClass {
188-
final def value = "value"
189-
def name = "example"
193+
private final def internal = 'internal'
194+
final def value = 'value'
195+
def name = 'example'
190196
}
191197
```
192198

@@ -244,7 +250,7 @@ public class DemoClass {
244250

245251
```groovy
246252
class DemoClass {
247-
DemoClass foo() {
253+
def foo() {
248254
this
249255
}
250256
@@ -334,9 +340,9 @@ Groovy has multiple ways of simplifying interacting with multiple methods or fie
334340
DemoClass obj = new DemoClass();
335341
obj.sayHi();
336342
obj.value = 1;
337-
obj.name = 'hello';
343+
obj.name = "hello";
338344
obj.total = 100;
339-
obj.clay = 'clay';
345+
obj.clay = "clay";
340346
obj.current = 5;
341347
obj.sayGoodbye();
342348
```

docs/groovy-script/groovy/loops.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,60 @@ for (entry in elements) {
9090

9191
`break` can be used at any time inside a loop to abort the current and all following runs.
9292
`continue` will only abort the current run and _continues_ with the next run (if the condition is still true)
93+
94+
95+
## Count
96+
97+
There are multiple different strategies to iterate through a loop a specific number of times.
98+
99+
::: code-group
100+
101+
102+
```groovy:no-line-numbers [while]
103+
def x = 0
104+
while (x < 10) {
105+
log.info x++
106+
}
107+
```
108+
109+
```groovy:no-line-numbers [do-while]
110+
def x = 0
111+
do {
112+
log.info x++ // this would execute at least once, even if x was greater than 10
113+
} while (x < 10)
114+
```
115+
116+
```groovy:no-line-numbers [for]
117+
for (def x = 0; x < 10; x++) {
118+
log.info x
119+
}
120+
```
121+
122+
```groovy:no-line-numbers [enhanced list]
123+
for (def x in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) {
124+
log.info x
125+
}
126+
for (def x : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) { // "in" and ":" are the same here
127+
log.info x
128+
}
129+
```
130+
131+
```groovy:no-line-numbers [each list]
132+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].each {
133+
log.info x
134+
}
135+
```
136+
137+
```groovy:no-line-numbers [for in range]
138+
for (def x : 0..<10) { // creates a range
139+
log.info x
140+
}
141+
```
142+
143+
```groovy:no-line-numbers [each range]
144+
(0..<10).each {
145+
log.info x
146+
}
147+
```
148+
149+
:::

docs/groovy-script/groovy/maps.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,37 @@ We are using the map we created above here.
3434
```groovy:no-line-numbers
3535
println(elements['Pb']) // Lead
3636
println(elements['Ag']) // Silver
37-
println(elements['B']) // null since there is no key 'B'
37+
println(elements['B']) // null since there is no key 'B'
3838
```
3939

4040
## Modifying maps
4141

4242
We are using the map we created above here.
4343

4444
```groovy:no-line-numbers
45-
elements['Au'] = 'Copper' // Au is know mapped to copper
46-
elements.remove('H') // removes H: Hydrogen
45+
elements['Au'] = 'Copper' // Au is now mapped to copper
46+
elements.remove('H') // removes H: Hydrogen
4747
```
48+
49+
## Variable keys
50+
51+
Groovy can have the keys of a map be set via variables.
52+
However, in order to do this, the key might need to be surrounded in parentheses
53+
to be properly handled.
54+
55+
This is particularly relevant with GroovyScript [Object Mappers](../getting_started/object_mappers.md).
56+
57+
```groovy:no-line-numbers
58+
def itemStackToValue = [
59+
(item('minecraft:dirt')): -1,
60+
(item('minecraft:stone')): 5,
61+
(item('minecraft:clay')): 1000,
62+
]
63+
```
64+
65+
::: info Warning {id="danger"}
66+
67+
Failing to properly surround a key in parentheses if it requires them may result in the script file
68+
throwing a `MultipleCompilationErrorsException` due to being "unable to resolve class".
69+
70+
:::

0 commit comments

Comments
 (0)