Skip to content

Commit 06e9889

Browse files
Expand AddonModule documentation
1 parent 7d7abc2 commit 06e9889

3 files changed

Lines changed: 103 additions & 3 deletions

File tree

src/content/docs/api/localization/language-files.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ Language entries are defined in a tree-like structure:
2424
error messages:
2525
unknown error: An unknown error has occurred.
2626
```
27-
This entry would then be available under the key `error messages.unknown error`.
27+
This entry would be available under the key `error messages.unknown error`.

src/content/docs/api/skript/addons.md

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public class MyAddon {
5151
```
5252

5353
## Addon modules
54-
SkriptAddon instances can also be used to load [AddonModules](https://github.com/SkriptLang/Skript/blob/master/src/main/java/org/skriptlang/skript/addon/AddonModule.java), which allow you to modularize (organize) the features you are registering. This makes it much easier to control which features of your addon are enabled and which are not.
54+
SkriptAddon instances can also be used to load [AddonModules](https://github.com/SkriptLang/Skript/blob/master/src/main/java/org/skriptlang/skript/addon/AddonModule.java), which allow you to modularize (organize) the features you are registering.
55+
This makes it much easier to control which features of your addon are enabled and which are not.
5556

5657
### Creating an addon module
5758
Addon modules are defined by implementing the AddonModule interface on a class.
@@ -70,18 +71,41 @@ This method is intended to be implemented by modules that register components th
7071
The `load(SkriptAddon)` method is the second and final stage of module initialization.
7172
This method is intended to be implemented by modules that register components that are generally only used within the module itself, such as [syntax](../../syntax).
7273

74+
#### name
75+
The `name` method, which is required to be implemented, should just return a name that describes the module's purpose.
76+
For example, if you were creating a module covering math syntax, you could call it `math`.
77+
The module name can really be anything, though, as it does not affect functionality.
78+
It is primarily used for documentation and logging purposes.
79+
7380
#### Example module
7481
The following is a simple example of a module that prints a message when it loads:
7582
```java
83+
// MyModule.java
7684
import org.skriptlang.skript.addon.AddonModule;
7785
public class MyModule implements AddonModule {
7886
@Override
87+
public void init(SkriptAddon addon) {
88+
System.out.println("MyModule has just been initialized by " + addon.name());
89+
}
90+
91+
@Override
7992
public void load(SkriptAddon addon) {
8093
System.out.println("MyModule has just been loaded by " + addon.name());
8194
}
95+
96+
@Override
97+
public String name() {
98+
return "MyModule";
99+
}
82100
}
83101
```
84102

103+
When `MyModule` is loaded, `init` is run before `load`, so the output will look something like:
104+
```bash title="My Skript Process"
105+
MyModule has just been initialized by ...
106+
MyModule has just been loaded by ...
107+
```
108+
85109
### Loading an addon module
86110
Defined on SkriptAddon is a utility method, `loadModules(AddonModule...)` that loads method.
87111
That is, it first calls `canLoad` on each provided module, compiling a collection of those that are permitted to load.
@@ -101,13 +125,86 @@ public class MyAddon {
101125
}
102126
}
103127
```
128+
### Hierarchical addon modules
129+
We also provide an implementation of AddonModule, [HierarchicalAddonModule](https://github.com/SkriptLang/Skript/blob/master/src/main/java/org/skriptlang/skript/addon/HierarchicalAddonModule.java), which allows defining a module with children (submodules).
130+
This implementation overrides the standard module methods to manage loading the hierarchy.
131+
Instead, implementors can override methods such as `canLoadSelf`, `initSelf` and `loadSelf` which function the same as their regular versions.
132+
133+
The key method of this implementation is `children`, which can be overridden.
134+
The method should return an iterable of the instances of the children modules.
135+
136+
#### Example hierarchical modules
137+
Here is an example of defining a module with a child module:
138+
```java
139+
// MyParentModule.java
140+
import org.skriptlang.skript.addon.HierarchicalAddonModule;
141+
public class MyParentModule implements HierarchicalAddonModule {
142+
// No special constructor needed since this module will not have a parent (it is the root)
143+
144+
@Override
145+
public Iterable<AddonModule> children() {
146+
// We initialize a new instance of MyChildModule with this module as the parent
147+
return List.of(new MyChildModule(this));
148+
}
149+
150+
@Override
151+
public void initSelf(SkriptAddon addon) {
152+
System.out.println("MyParentModule has just been initialized by " + addon.name());
153+
}
154+
155+
@Override
156+
public void loadSelf(SkriptAddon addon) {
157+
System.out.println("MyParentModule has just been loaded by " + addon.name());
158+
}
159+
160+
@Override
161+
public String name() {
162+
return "MyParentModule";
163+
}
164+
}
165+
```
166+
```java
167+
// MyChildModule.java
168+
import org.skriptlang.skript.addon.HierarchicalAddonModule;
169+
public class MyChildModule implements HierarchicalAddonModule {
170+
// We define a constructor for setting this module's parent
171+
public MyChildModule(AddonModule parentModule) {
172+
super(parentModule);
173+
}
174+
175+
// MyChildModule has no children, so no need to override 'children'
176+
177+
@Override
178+
public void initSelf(SkriptAddon addon) {
179+
System.out.println("MyChildModule has just been initialized by " + addon.name());
180+
}
181+
182+
@Override
183+
public void loadSelf(SkriptAddon addon) {
184+
System.out.println("MyChildModule has just been loaded by " + addon.name());
185+
}
186+
187+
@Override
188+
public String name() {
189+
return "MyChildModule";
190+
}
191+
}
192+
```
193+
194+
When `MyParentModule` is loaded, its `...Self` methods will always run before the equivalent for its children, meaning the output will look something like:
195+
```bash title="My Skript Process"
196+
MyParentModule has just been initialized by ...
197+
MyChildModule has just been initialized by ...
198+
MyParentModule has just been loaded by ...
199+
MyChildModule has just been loaded by ...
200+
```
104201

105202
## Using registries
106203
SkriptAddon instances provide access to Skript's registries through multiple methods defined on the interface.
107204

108205
:::note
109206
This section does not cover any of the registries defined and used by the API.
110-
For more information about what registries are available, please review the [registries](../../registries) section.
207+
For more information about what registries are available, see the [registries](../../registries) section.
111208
:::
112209

113210
### Obtaining registries

src/content/docs/api/syntax/writing-syntax.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
---
22
title: Writing Syntax
3+
tableOfContents:
4+
minHeadingLevel: 1
5+
maxHeadingLevel: 4
36
sidebar:
47
order: 5
58
---

0 commit comments

Comments
 (0)