Skip to content

Commit 80c153e

Browse files
authored
update docs for GroovyScript 1.2.4 (#38)
* update docs for GroovyScript 1.2.4 * remove duplicate method invalid documentation * add equality difference from java * update runconfig for classes removal * fix custom reloading instructions * update class information * include metaclass information * update main page description info * warn about file name and method size
1 parent c2c2aff commit 80c153e

File tree

393 files changed

+9526
-538
lines changed

Some content is hidden

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

393 files changed

+9526
-538
lines changed

docs/groovy-script/getting_started/classes.md

Lines changed: 96 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,114 @@
22
# Classes
33

44

5-
While all Groovy files are technically class files,
6-
this refers specifically to files interacting with the [`runConfig.json`](./run_config.md#classes) classes section.
5+
All groovy files are classes.
6+
However, if no specific class declaration is used, the file will be parsed as a script file.
77

88

9-
## Class Declaration
9+
## Groovy File Extensions
1010

1111

12-
::: info Bug {id="bug"}
12+
A Groovy file is a file with the extension `groovy`, `gy`, `gvy`, or `gsh`.
1313

14-
Starting from version `1.0.0` and getting fixed in version `1.1.0`
15-
there was a bug which caused class files require a package declaration
16-
in the form of `package classes` at the top of the script to be functional.
14+
In almost all places on the wiki, only `groovy` will be used.
1715

16+
Groovy file names must follow the [Java class naming specification](https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.8),
17+
meaning valid names match the regex pattern of `[a-zA-Z$_][a-zA-Z0-9$_]*`.
18+
To spell it out: a file name must begin with a letter, dollar sign, or underscore,
19+
and every character afterwards is must be either a letter, number, dollar sign, or underscore.
20+
21+
::: warning Invalid Name {id="danger"}
22+
23+
If the file does not have a valid name it will not be loaded!
24+
25+
:::
26+
27+
::: warning Global Object Confusion {id="warning"}
28+
29+
As the file name will create a class with the same name, naming your file with the
30+
same name as a global object such as `item`, `mods`, `eventManager`, or otherwise
31+
may cause unexpected issues due to accessing the class instead of the desired global object!
32+
33+
:::
34+
35+
36+
## Script Files
37+
38+
39+
Script files must be placed in a specific [loader](./run_config.md#loaders) to be run,
40+
and will be executed in that stage.
41+
42+
::: code-group
43+
44+
```groovy [postInit/Example.groovy]
45+
log.info('A normal script file')
46+
```
47+
48+
```json [runConfig.json]
49+
{
50+
"loaders": {
51+
"postInit": [
52+
"postInit/Example.groovy"
53+
]
54+
},
55+
}
56+
```
1857
:::
1958

20-
Classes must be declared in the locations specified in the [`runConfig.json`](./run_config.md#classes) by the classes element.
59+
Behind the scenes, a script file will be converted like so:
60+
61+
::: code-group
62+
63+
```groovy [postInit/Example.groovy]
64+
import groovy.transform.Field
65+
66+
@Field // this annotation makes what would otherwise be a local to become a field
67+
int baseValue = 2
2168
69+
def hello = 'hello, world!' // creates a local variable
2270
23-
## Importing
71+
log.info(hello)
72+
int power(int n) { baseValue**n }
73+
log.info(power(3))
74+
```
75+
76+
```groovy [Script File]
77+
package postInit
78+
79+
import groovy.transform.Field
80+
// imports still occur before the code
2481
82+
class Example extends groovy.lang.Script {
2583
26-
You can import these classes the same way you would any package from Java.
27-
The default package classes are located in is `classes`, so you would run `import classes.DemoClass`
84+
@Field
85+
int baseValue = 2
2886
87+
int power(int n) { baseValue**n } // if baseValue was not annotated with @Field, an exception will be thrown!
2988
30-
## Example
89+
def run() {
90+
def hello = 'hello, world!'
91+
log.info(hello)
92+
log.info(power(3))
93+
}
94+
}
95+
```
96+
97+
:::
98+
99+
::: warning Maximum method size {id="danger"}
100+
101+
Java cannot load methods larger than [64kb](https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.11).
102+
Particularly large GroovyScript scripts may pass this - in this situation,
103+
you will need to refactor your scripts in some fashion to avoid this issue.
104+
105+
:::
106+
107+
108+
## Custom Classes
31109

32110
:::: info Basic Example {id="example"}
33111

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.
112+
With a named `DemoClass.groovy` from any other groovy class.
36113

37114
::: code-group
38115

@@ -44,9 +121,11 @@ class DemoClass {
44121
```
45122

46123
```json [runConfig.json]
47-
"classes": [
48-
"classes/" // targets either the file or a folder that the file is nested within
49-
]
124+
"loaders": {
125+
"preInit": [
126+
"classes/" // targets either the file or a folder that the file is nested within
127+
]
128+
}
50129
```
51130

52131
```groovy [postInit/CheckIron.groovy]

docs/groovy-script/getting_started/groovy_modifications.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,32 @@ A number of packages, classes, and methods are banned, and not able to be used b
7474

7575
In particular, when interacting with a File object through Groovy,
7676
the File can only refer to a file path that targets inside the minecraft directory.
77+
78+
79+
## MetaClass
80+
81+
82+
Groovy adds a new property to every object, `MetaClass`, which can be accessed via `object.metaClass`.
83+
This can be used for meta-programming, but in GroovyScript we have custom additions to it
84+
to allow modifying the visibility and mutability of fields and methods.
85+
86+
To strip `final` from a field, allowing it to be set, do
87+
88+
::: info Example {id="example"}
89+
90+
```groovy:no-line-numbers
91+
obj.metaClass.makeMutable('fieldName')
92+
```
93+
94+
:::
95+
96+
To make a method or field public, allowing it to be accessed, do
97+
98+
::: info Example {id="example"}
99+
100+
```groovy:no-line-numbers
101+
obj.metaClass.makePublic('fieldName')
102+
obj.metaClass.makePublic('methodName')
103+
```
104+
105+
:::

docs/groovy-script/getting_started/reloading.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,13 @@ class DemoRegistry {
7878
:::
7979
::::
8080

81-
Then, add an event listener that is listening for `GroovyReloadEvent`.
82-
Inside of this event listener, call the `onReload` method.
81+
Then, you can accomplish reloading via one of two ways:
82+
1. Create an event listener that is listening for `GroovyReloadEvent` *in the `init` stage*.
83+
2. Check `isReloading()` *in the `postInit` stage*.
8384

8485
::: code-group
8586

86-
```groovy [postInit/ReloadHelper.groovy]
87+
```groovy [init/ReloadHelper.groovy]
8788
import classes.DemoRegistry
8889
import com.cleanroommc.groovyscript.event.GroovyReloadEvent
8990
@@ -92,6 +93,15 @@ eventManager.listen(GroovyReloadEvent) {
9293
}
9394
```
9495

96+
```groovy [postInit/ReloadHelper.groovy]
97+
import classes.DemoRegistry
98+
99+
if (!isReloading()) return
100+
101+
DemoRegistry.instance.onReload()
102+
```
103+
104+
95105
:::
96106

97107
Finally, any time you would add a recipe to or remove a recipe from `DemoHolder.DemoRecipeList`,

docs/groovy-script/getting_started/run_config.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ Let's see what the file can look like.
2121
"packId": "",
2222
"version": "1.0.0",
2323
"debug": false,
24-
"classes": [
25-
"classes/"
26-
],
2724
"loaders": {
2825
"preInit": [
2926
"preInit/"
@@ -86,6 +83,21 @@ and logs the line numbers in errors.
8683

8784
## `classes`
8885

86+
This *was* an element used to control what files were loaded as class files
87+
prior to [GroovyScript 1.2.3](https://github.com/CleanroomMC/GroovyScript/tree/v1.2.3).
88+
89+
::: info Failure {id="failure"}
90+
91+
If `classes` is an element in your `runConfig.json` file, a fatal message will be logged
92+
and you **must** remove it in order to be able to launch.
93+
94+
Instead, load your classes via one of the loaders - typically [`preInit`](#preinit).
95+
To migrate in this fashion, simply add `"classes/"` to the `preInit` loader.
96+
97+
:::
98+
99+
:::: details Prior to GroovyScript 1.2.3 {id="warning"}
100+
89101
Files that contain a single class should be specified here.
90102
It makes sure classes are loaded when scripts try to access them.
91103
You can specify classes to only load in certain loaders.
@@ -113,6 +125,8 @@ This works exactly like the `loaders` property.
113125
```
114126
:::
115127

128+
::::
129+
116130
## `loaders`
117131

118132
This defines at what stage what files should be loaded.

docs/groovy-script/groovy/differences_from_java.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,30 @@ def map2 = [(1): "example", (3): "demo"] // if the key is an expression, wrappin
140140

141141
:::
142142

143+
## Equality
144+
145+
In Java, you can compare two objects by reference equality using two equal signs.
146+
Objects can override `equals` to provide a custom equal check.
147+
148+
In Groovy, comparing two objects by reference equality requires *three* equal signs,
149+
and using two defers to `equals` via [operator overloading](./operators.md#operator-overloading).
150+
151+
::: code-group
152+
153+
```java
154+
a == b // reference equality
155+
a.equals(b) // requires that a is non-null
156+
a == null ? b == null : a.equals(b) // null-safe check
157+
```
158+
159+
```groovy
160+
a === b // reference equality
161+
a == b // shorthand for a.equals(b)
162+
a.equals(b) // null-safe check
163+
```
164+
165+
:::
166+
143167
## Visibility
144168

145169
In Java, fields, classes, and methods that do not have an access specifier stated are `package-private` by default, meaning they are only visible to other classes inside the package.

docs/groovy-script/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
next:
44
text: 'Getting Started'
55
link: '/groovy-script/getting_started/'
6+
description: "GroovyScript is a comprehensive scripting sandbox for Minecraft 1.12.2 via the Groovy language."
67
---
78

89
# GroovyScript

docs/groovy-script/minecraft/helpers/crafting.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,18 @@ Just like other recipe types, the Crafting Table also uses a recipe builder.
166166

167167
Don't know what a builder is? Check [the builder info page](../../getting_started/builder.md) out.
168168

169-
:::::::::: details crafting.shapedBuilder() {open id="abstract"}
169+
:::::::::: details Recipe Builder {open id="abstract"}
170+
171+
---
172+
173+
- Create the Recipe Builder.
174+
175+
```groovy:no-line-numbers
176+
crafting.shapedBuilder()
177+
```
178+
179+
---
180+
170181
- `ResourceLocation`. Sets the Resource Location of the recipe.
171182
172183
```groovy:no-line-numbers
@@ -229,12 +240,16 @@ Don't know what a builder is? Check [the builder info page](../../getting_starte
229240
recipeAction(Closure<Void>)
230241
```
231242
243+
---
244+
232245
- First validates the builder, returning `null` and outputting errors to the log file if the validation failed, then registers the builder and returns the registered object. (returns `null` or `net.minecraft.item.crafting.IRecipe`).
233246
234247
```groovy:no-line-numbers
235248
register()
236249
```
237250
251+
---
252+
238253
::::::::: details Example {open id="example"}
239254
```groovy:no-line-numbers
240255
crafting.shapedBuilder()
@@ -331,7 +346,18 @@ crafting.shapedBuilder()
331346

332347
::::::::::
333348

334-
:::::::::: details crafting.shapelessBuilder() {open id="abstract"}
349+
:::::::::: details Recipe Builder {open id="abstract"}
350+
351+
---
352+
353+
- Create the Recipe Builder.
354+
355+
```groovy:no-line-numbers
356+
crafting.shapelessBuilder()
357+
```
358+
359+
---
360+
335361
- `ResourceLocation`. Sets the Resource Location of the recipe.
336362
337363
```groovy:no-line-numbers
@@ -372,12 +398,16 @@ crafting.shapedBuilder()
372398
recipeAction(Closure<Void>)
373399
```
374400
401+
---
402+
375403
- First validates the builder, returning `null` and outputting errors to the log file if the validation failed, then registers the builder and returns the registered object. (returns `null` or `net.minecraft.item.crafting.IRecipe`).
376404
377405
```groovy:no-line-numbers
378406
register()
379407
```
380408
409+
---
410+
381411
::::::::: details Example {open id="example"}
382412
```groovy:no-line-numbers
383413
crafting.shapelessBuilder()
@@ -462,9 +492,9 @@ crafting.shapelessBuilder()
462492
463493
:::::::::: details Example {open id="example"}
464494
```groovy:no-line-numbers
465-
crafting.removeByOutput(item('minecraft:gold_ingot'))
466-
crafting.remove('minecraft:mossy_stonebrick')
467495
crafting.remove(resource('minecraft:stonebrick'))
496+
crafting.remove('minecraft:mossy_stonebrick')
497+
crafting.removeByOutput(item('minecraft:gold_ingot'))
468498
crafting.removeAll()
469499
```
470500

0 commit comments

Comments
 (0)