Skip to content

Commit b3956c5

Browse files
committed
More internal docs
Small docs update Code reformat
1 parent 4977646 commit b3956c5

File tree

10 files changed

+164
-46
lines changed

10 files changed

+164
-46
lines changed

build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ repositories {
2525
}
2626

2727
processResources {
28-
inputs.files 'build.gradle'
29-
from sourceSets.main.resources.srcDirs
30-
filter(ReplaceTokens, tokens: [version: version])
28+
from ('src/main/resources') {
29+
filter(ReplaceTokens, tokens: ["version": version])
30+
}
3131
}
3232

3333
dependencies {
3434
implementation 'org.spigotmc:spigot-api:1.13-R0.1-SNAPSHOT'
35-
implementation 'com.github.SkriptLang:Skript:master-SNAPSHOT'
35+
implementation 'com.github.SkriptLang:Skript:2.5.3'
3636
}
3737

3838
compileJava.options.encoding = "UTF-8"

docs/advanced/experiments.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Individual features may be enabled by adding the codename of the feature on new
2323
Deferred parsing allows you to prefix any line with `(parse[d] later)` to defer parsing until the first execution of the line. This allows you to circumvent issues where custom syntaxes are used before they are defined.
2424

2525
{% hint style="danger" %}
26-
This should only be used when two custom syntaxes refer to each other. Other issues should be resolved by reordering custom syntax definitions and ensuring that libraries containing custom syntax load before other scripts.
26+
This should only be used when two custom syntaxes refer to each other. Other issues should be resolved by reordering custom syntax definitions and ensuring that libraries containing custom syntax load before other scripts, or by using the [preloading feature](#preloading).
2727
{% endhint %}
2828

2929

src/main/java/com/btk5h/skriptmirror/Null.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.btk5h.skriptmirror;
22

33
public class Null {
4-
private static Null instance = new Null();
4+
private static final Null instance = new Null();
55

66
private Null() {
77
}

src/main/java/com/btk5h/skriptmirror/ParseOrderWorkarounds.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import ch.njol.skript.Skript;
44
import ch.njol.skript.effects.EffReturn;
5-
import ch.njol.skript.lang.*;
65
import ch.njol.util.Checker;
76
import com.btk5h.skriptmirror.skript.EffExpressionStatement;
87
import com.btk5h.skriptmirror.skript.custom.ExprMatchedPattern;
@@ -11,9 +10,7 @@
1110
import com.btk5h.skriptmirror.skript.custom.expression.CustomExpression;
1211
import com.btk5h.skriptmirror.util.SkriptReflection;
1312

14-
import java.util.Arrays;
1513
import java.util.Collection;
16-
import java.util.List;
1714
import java.util.Optional;
1815

1916
/**

src/main/java/com/btk5h/skriptmirror/skript/CondParseLater.java

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ public class CondParseLater extends Condition {
2020
public boolean check(Event e) {
2121
Condition parsedCondition = getParsedCondition();
2222

23-
if (parsedCondition == null) {
23+
if (parsedCondition == null)
2424
return false;
25-
}
2625

2726
return parsedCondition.check(e);
2827
}
@@ -31,13 +30,7 @@ public boolean check(Event e) {
3130
protected TriggerItem walk(Event e) {
3231
Statement parsedStatement = getParsedStatement();
3332

34-
if (parsedStatement == null) {
35-
return null;
36-
}
37-
38-
TriggerItem.walk(parsedStatement, e);
39-
40-
return null;
33+
return parsedStatement == null ? getNext() : parsedStatement;
4134
}
4235

4336
@Override
@@ -70,12 +63,10 @@ private Statement getParsedStatement() {
7063
+ statement);
7164
previousState.applyToCurrentState();
7265

73-
if (parsedStatement == null) {
74-
return null;
66+
if (parsedStatement != null) {
67+
parsedStatement.setNext(getNext());
68+
parsedStatement.setParent(getParent());
7569
}
76-
77-
parsedStatement.setNext(getNext());
78-
parsedStatement.setParent(getParent());
7970
}
8071

8172
return parsedStatement;
@@ -84,22 +75,18 @@ private Statement getParsedStatement() {
8475
private Condition getParsedCondition() {
8576
if (parsedStatement == null) {
8677
scriptLoaderState.applyToCurrentState();
87-
parsedStatement = Condition.parse(statement,
88-
String.format("Could not parse condition at runtime: %s", statement));
78+
parsedStatement = Condition.parse(statement, "Could not parse condition at runtime: " + statement);
8979

90-
if (parsedStatement == null) {
91-
return null;
80+
if (parsedStatement != null) {
81+
parsedStatement.setNext(getNext());
82+
parsedStatement.setParent(getParent());
9283
}
93-
94-
parsedStatement.setNext(getNext());
95-
parsedStatement.setParent(getParent());
9684
}
9785

98-
if (!(parsedStatement instanceof Condition)) {
99-
throw new IllegalStateException(
100-
String.format("%s was used as a condition but was parsed as a statement", statement));
86+
if (parsedStatement != null && !(parsedStatement instanceof Condition)) {
87+
throw new IllegalStateException(statement + " was used as a condition but was parsed as a statement");
10188
}
10289

103-
return ((Condition) parsedStatement);
90+
return (Condition) parsedStatement;
10491
}
10592
}

src/main/java/com/btk5h/skriptmirror/skript/Consent.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.btk5h.skriptmirror.skript;
22

3-
import ch.njol.skript.ScriptLoader;
43
import ch.njol.skript.Skript;
54
import ch.njol.skript.config.Node;
65
import ch.njol.skript.config.SectionNode;
@@ -78,8 +77,8 @@ public void unregisterAll() {
7877

7978
@Override
8079
public boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseResult parseResult) {
81-
File currentScript = ScriptLoader.currentScript.getFile();
82-
SectionNode node = ((SectionNode) SkriptLogger.getNode());
80+
File currentScript = SkriptUtil.getCurrentScript();
81+
SectionNode node = (SectionNode) SkriptLogger.getNode();
8382

8483
if (node.getKey().toLowerCase().startsWith("on ")) {
8584
return false;

src/main/java/com/btk5h/skriptmirror/util/JavaUtil.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,40 @@ public final class JavaUtil {
4141
PRIMITIVE_CLASS_NAMES.put("short", short.class);
4242
}
4343

44+
/**
45+
* @return a {@link Stream} of {@link Field}s, with the fields declared in the given class,
46+
* and the public fields of the given class, without duplicates.
47+
*/
4448
public static Stream<Field> fields(Class<?> cls) {
4549
return Stream.concat(
4650
Arrays.stream(cls.getFields()),
4751
Arrays.stream(cls.getDeclaredFields())
4852
).distinct();
4953
}
5054

55+
/**
56+
* @return a {@link Stream} of {@link Method}s, with the methods declared in the given class
57+
* and the public methods of the given class, without duplicates.
58+
*/
5159
public static Stream<Method> methods(Class<?> cls) {
5260
return Stream.concat(
5361
Arrays.stream(cls.getMethods()),
5462
Arrays.stream(cls.getDeclaredMethods())
5563
).distinct();
5664
}
5765

66+
/**
67+
* @return a {@link Stream} of the {@link Constructor}s declared in the given class.
68+
*/
5869
public static Stream<Constructor> constructors(Class<?> cls) {
5970
return Arrays.stream(cls.getDeclaredConstructors());
6071
}
6172

73+
/**
74+
* Calls {@code toGenericString} on the given {@link Member},
75+
* returning {@code null} if the given {@link Member} is not
76+
* a {@link Field}, a {@link Method} or a {@link Constructor}.
77+
*/
6278
public static String toGenericString(Member o) {
6379
if (o instanceof Field) {
6480
return ((Field) o).toGenericString();
@@ -70,6 +86,10 @@ public static String toGenericString(Member o) {
7086
return null;
7187
}
7288

89+
/**
90+
* @return an array of the wrapper type of the component type of the given array (e.g. {@code int[] -> Integer[]}).
91+
* The contents of the array are copied over.
92+
*/
7393
public static Object boxPrimitiveArray(Object obj) {
7494
Class<?> componentType = obj.getClass().getComponentType();
7595
if (componentType != null && componentType.isPrimitive()) {
@@ -85,6 +105,9 @@ public static Object boxPrimitiveArray(Object obj) {
85105
return obj;
86106
}
87107

108+
/**
109+
* Converts a numeric array to a different numeric class. Also supports arrays with depths higher than {@code 1}.
110+
*/
88111
public static Object convertNumericArray(Object array, Class<?> to) {
89112
Class<?> componentType = array.getClass().getComponentType();
90113
int length = Array.getLength(array);
@@ -131,6 +154,11 @@ public static Object convertNumericArray(Object array, Class<?> to) {
131154
return newArray;
132155
}
133156

157+
/**
158+
* Gets the array depth of a given class, for example:<br>
159+
* Object[] -> 1<br>
160+
* Object[][][] -> 3
161+
*/
134162
public static int getArrayDepth(Class<?> cls) {
135163
int depth = 0;
136164

@@ -142,6 +170,9 @@ public static int getArrayDepth(Class<?> cls) {
142170
return depth;
143171
}
144172

173+
/**
174+
* @return the component type of the given array class, independent of the depth of the array.
175+
*/
145176
public static Class<?> getBaseComponent(Class<?> obj) {
146177
Class<?> componentType = obj.getComponentType();
147178

@@ -152,11 +183,21 @@ public static Class<?> getBaseComponent(Class<?> obj) {
152183
return componentType;
153184
}
154185

186+
/**
187+
* An functional interface with an input and output that may throw an {@link Exception}.
188+
* @param <T> the input of the function.
189+
* @param <R> the return value of the function.
190+
*/
155191
@FunctionalInterface
156192
public interface ExceptionalFunction<T, R> {
157193
R apply(T t) throws Exception;
158194
}
159195

196+
/**
197+
* Converts a {@link ExceptionalFunction} to a normal {@link Function}, by catching the exception
198+
* and logging a warning with the simple class name and message of the exception.
199+
* If Skript's verbosity is set to 'very high' or above, this will also print the stack trace.
200+
*/
160201
public static <T, R> Function<T, R> propagateErrors(ExceptionalFunction<T, R> function) {
161202
return t -> {
162203
try {
@@ -177,19 +218,31 @@ public static <T, R> Function<T, R> propagateErrors(ExceptionalFunction<T, R> fu
177218
};
178219
}
179220

221+
/**
222+
* @return whether the given class is a numeric class, albeit primitive or a wrapper of a primitive numeric class.
223+
*/
180224
public static boolean isNumericClass(Class<?> cls) {
181225
return Number.class.isAssignableFrom(cls) || JavaUtil.NUMERIC_CLASSES.contains(cls);
182226
}
183227

228+
/**
229+
* {@return} a new array with the given class as base component type, and the given length as the array length.
230+
*/
184231
@SuppressWarnings("unchecked")
185232
public static <T> T[] newArray(Class<? extends T> type, int length) {
186233
return (T[]) Array.newInstance(type, length);
187234
}
188235

236+
/**
237+
* {@return} the array class of the given class.
238+
*/
189239
public static <T> Class<?> getArrayClass(Class<T> type) {
190240
return Array.newInstance(type, 0).getClass();
191241
}
192242

243+
/**
244+
* @return the array class with the given depth of the given type, using {@link #getArrayClass(Class)}.
245+
*/
193246
public static Class<?> getArrayClass(Class<?> type, int layers) {
194247
for (int i = 0; i < layers; i++) {
195248
type = getArrayClass(type);

src/main/java/com/btk5h/skriptmirror/util/SkriptMirrorUtil.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public class SkriptMirrorUtil {
2222

2323
private static final Pattern TYPE_PREFIXES = Pattern.compile("^[-*~]*");
2424

25+
/**
26+
* @return the {@link Class} of this {@link JavaType}, if it is a {@link JavaType},
27+
* otherwise it returns {@link #getClass(Object)} of this object.
28+
*/
2529
public static Class<?> toClassUnwrapJavaTypes(Object o) {
2630
if (o instanceof JavaType) {
2731
return ((JavaType) o).getJavaClass();
@@ -34,6 +38,10 @@ public static String getDebugName(Class<?> cls) {
3438
return Skript.logVeryHigh() ? cls.getName() : cls.getSimpleName();
3539
}
3640

41+
/**
42+
* The given object is first possibly unwrapped using {@link ObjectWrapper#unwrapIfNecessary(Object)}.
43+
* Then returns the class of the object, or {@code Object.class}, if the object is {@code null}.
44+
*/
3745
public static Class<?> getClass(Object o) {
3846
o = ObjectWrapper.unwrapIfNecessary(o);
3947

@@ -44,6 +52,10 @@ public static Class<?> getClass(Object o) {
4452
return o.getClass();
4553
}
4654

55+
/**
56+
* Returns the given pattern, with all types starting with an underscore
57+
* replaced with {@code javaobject(s)}, as those types are just context indicators.
58+
*/
4759
public static String preprocessPattern(String pattern) {
4860
StringBuilder newPattern = new StringBuilder(pattern.length());
4961
String[] parts = pattern.split("%");
@@ -68,6 +80,10 @@ public static String preprocessPattern(String pattern) {
6880
return newPattern.toString();
6981
}
7082

83+
/**
84+
* Returns the given type string, using {@link SkriptUtil#replaceUserInputPatterns(String)}
85+
* on each type in the given string.
86+
*/
7187
public static String processTypes(String part) {
7288
if (part.length() > 0) {
7389
// copy all prefixes
@@ -88,14 +104,18 @@ public static String processTypes(String part) {
88104

89105
// replace user input patterns
90106
String types = Arrays.stream(part.split("/"))
91-
.map(SkriptUtil::replaceUserInputPatterns)
92-
.collect(Collectors.joining("/"));
107+
.map(SkriptUtil::replaceUserInputPatterns)
108+
.collect(Collectors.joining("/"));
93109

94110
return prefixes + types + suffixes;
95111
}
96112
return part;
97113
}
98114

115+
/**
116+
* @return {@link Null#getInstance()} if the given object is {@code null},
117+
* otherwise returns the given object itself.
118+
*/
99119
public static Object reifyIfNull(Object o) {
100120
return o == null ? Null.getInstance() : o;
101121
}

src/main/java/com/btk5h/skriptmirror/util/SkriptReflection.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ public static Parameter<?>[] getParameters(Function<?> function) {
187187
}
188188
}
189189

190+
/**
191+
* Prints the log stored in the given {@link RetainingLogHandler} after closing
192+
* all active {@link LogHandler}s, except the {@link ParseLogHandler}s.
193+
*/
190194
public static void printLog(RetainingLogHandler logger) {
191195
logger.stop();
192196
HandlerList handler;
@@ -214,6 +218,9 @@ public static void printLog(RetainingLogHandler logger) {
214218
SkriptLogger.logAll(logger.getLog());
215219
}
216220

221+
/**
222+
* @return a {@link Map} of the options currently being loaded by {@link ScriptLoader}.
223+
*/
217224
@SuppressWarnings("unchecked")
218225
public static Map<String, String> getCurrentOptions() {
219226
try {
@@ -364,6 +371,9 @@ public static void disableAndOrWarnings() {
364371
}
365372
}
366373

374+
/**
375+
* Executes {@link SkriptParser}'s {@code parse_i} method with the given arguments.
376+
*/
367377
public static SkriptParser.ParseResult parse_i(SkriptParser skriptParser, String pattern, int i, int j) {
368378
if (PARSE_I == null)
369379
return null;
@@ -376,6 +386,9 @@ public static SkriptParser.ParseResult parse_i(SkriptParser skriptParser, String
376386
return null;
377387
}
378388

389+
/**
390+
* {@return} a list of all of Skript's registered {@link ch.njol.skript.lang.Expression}s.
391+
*/
379392
public static List<ExpressionInfo<?, ?>> getExpressions() {
380393
try {
381394
return (List<ExpressionInfo<?, ?>>) EXPRESSIONS.get(null);

0 commit comments

Comments
 (0)