Skip to content

Commit 5fd230e

Browse files
committed
Fix #11
1 parent fd25327 commit 5fd230e

File tree

5 files changed

+87
-48
lines changed

5 files changed

+87
-48
lines changed

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@
77

88
public class FunctionWrapper {
99

10-
private static final Function<?> NO_OP_FUNCTION;
11-
12-
static {
13-
NO_OP_FUNCTION = new JavaFunction<Object>("$noop", new Parameter[0], Classes.getExactClassInfo(Object.class),
14-
true) {
15-
@Nullable
16-
@Override
17-
public Object[] execute(FunctionEvent e, Object[][] params) {
18-
return null;
19-
}
20-
};
21-
}
10+
private static final Function<?> NO_OP_FUNCTION = new JavaFunction<Object>("$noop", new Parameter[0],
11+
Classes.getExactClassInfo(Object.class), true) {
12+
@Nullable
13+
@Override
14+
public Object[] execute(FunctionEvent e, Object[][] params) {
15+
return null;
16+
}
17+
};
2218

2319
private final String name;
2420
private final Object[] arguments;

src/main/java/com/btk5h/skriptmirror/skript/custom/condition/CustomConditionSection.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import java.io.File;
1515
import java.util.*;
16+
import java.util.concurrent.atomic.AtomicBoolean;
1617
import java.util.stream.Collectors;
1718

1819
public class CustomConditionSection extends CustomSyntaxSection<ConditionSyntaxInfo> {
@@ -96,11 +97,12 @@ protected boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.Parse
9697
return false;
9798
}
9899

99-
100-
return handleEntriesAndSections(node,
100+
AtomicBoolean hasCheck = new AtomicBoolean();
101+
boolean nodesOkay = handleEntriesAndSections(node,
101102
entryNode -> false,
102103
sectionNode -> {
103104
String key = sectionNode.getKey();
105+
assert key != null;
104106

105107
if (key.equalsIgnoreCase("patterns")) {
106108
return true;
@@ -112,6 +114,7 @@ protected boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.Parse
112114
whichInfo.forEach(which -> conditionHandlers.put(which,
113115
new Trigger(SkriptUtil.getCurrentScript(), "condition " + which, this, items)));
114116

117+
hasCheck.set(true);
115118
return true;
116119
}
117120

@@ -122,6 +125,14 @@ protected boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.Parse
122125

123126
return false;
124127
});
128+
129+
if (!nodesOkay)
130+
return false;
131+
132+
if (!hasCheck.get())
133+
Skript.warning("Custom conditions are useless without a check section");
134+
135+
return true;
125136
}
126137

127138
public static ConditionSyntaxInfo lookup(File script, int matchedPattern) {

src/main/java/com/btk5h/skriptmirror/skript/custom/effect/CustomEffectSection.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.List;
1515
import java.util.Map;
1616
import java.util.Optional;
17+
import java.util.concurrent.atomic.AtomicBoolean;
1718

1819
public class CustomEffectSection extends CustomSyntaxSection<EffectSyntaxInfo> {
1920
static {
@@ -75,10 +76,12 @@ protected boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.Parse
7576
return false;
7677
}
7778

78-
return handleEntriesAndSections(node,
79+
AtomicBoolean hasTrigger = new AtomicBoolean();
80+
boolean nodesOkay = handleEntriesAndSections(node,
7981
entryNode -> false,
8082
sectionNode -> {
8183
String key = sectionNode.getKey();
84+
assert key != null;
8285

8386
if (key.equalsIgnoreCase("patterns")) {
8487
return true;
@@ -90,6 +93,7 @@ protected boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.Parse
9093
whichInfo.forEach(which ->
9194
effectHandlers.put(which,
9295
new Trigger(SkriptUtil.getCurrentScript(), "effect " + which, this, items)));
96+
hasTrigger.set(true);
9397
return true;
9498
}
9599

@@ -100,6 +104,14 @@ protected boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.Parse
100104

101105
return false;
102106
});
107+
108+
if (!nodesOkay)
109+
return false;
110+
111+
if (!hasTrigger.get())
112+
Skript.warning("Custom effects are useless without a trigger section");
113+
114+
return true;
103115
}
104116

105117
public static EffectSyntaxInfo lookup(File script, int matchedPattern) {

src/main/java/com/btk5h/skriptmirror/skript/custom/expression/CustomConstantSection.java

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
import java.util.Optional;
1414
import java.util.Spliterator;
1515
import java.util.Spliterators;
16+
import java.util.concurrent.atomic.AtomicBoolean;
1617
import java.util.stream.StreamSupport;
1718

1819
public class CustomConstantSection extends CustomSyntaxSection<ConstantSyntaxInfo> {
1920
static {
20-
//noinspection unchecked
2121
CustomSyntaxSection.register("Define Constant", CustomConstantSection.class,
2222
"option <.+>");
2323
// TODO add support for custom constant expressions
2424
}
2525

26-
private static DataTracker<ConstantSyntaxInfo> dataTracker = new DataTracker<>();
26+
private static final DataTracker<ConstantSyntaxInfo> dataTracker = new DataTracker<>();
2727

2828
static {
2929
dataTracker.setSyntaxType("constant");
@@ -42,33 +42,41 @@ protected DataTracker<ConstantSyntaxInfo> getDataTracker() {
4242
return dataTracker;
4343
}
4444

45-
@SuppressWarnings("unchecked")
45+
@SuppressWarnings({"unchecked", "SwitchStatementWithTooFewBranches"})
4646
@Override
47-
protected boolean init(Literal[] args, int matchedPattern, SkriptParser.ParseResult parseResult, SectionNode node) {
47+
protected boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseResult parseResult, SectionNode node) {
4848
String what;
4949

50-
5150
switch (matchedPattern) {
5251
case 0:
5352
what = parseResult.regexes.get(0).group();
54-
return handleEntriesAndSections(node,
55-
entryNode -> false,
56-
sectionNode -> {
57-
String key = sectionNode.getKey();
58-
59-
if (key.equalsIgnoreCase("get")) {
60-
ScriptLoader.setCurrentEvent("custom constant getter", ConstantGetEvent.class);
61-
List<TriggerItem> items = SkriptUtil.getItemsFromNode(sectionNode);
62-
Trigger getter =
63-
new Trigger(SkriptUtil.getCurrentScript(), "get {@" + what + "}", this, items);
64-
65-
computeOption(what, getter);
66-
67-
return true;
68-
}
69-
70-
return false;
71-
});
53+
54+
AtomicBoolean hasGetSection = new AtomicBoolean();
55+
boolean nodesOkay = handleEntriesAndSections(node,
56+
entryNode -> false,
57+
sectionNode -> {
58+
String key = sectionNode.getKey();
59+
assert key != null;
60+
61+
if (key.equalsIgnoreCase("get")) {
62+
ScriptLoader.setCurrentEvent("custom constant getter", ConstantGetEvent.class);
63+
List<TriggerItem> items = SkriptUtil.getItemsFromNode(sectionNode);
64+
Trigger getter =
65+
new Trigger(SkriptUtil.getCurrentScript(), "get {@" + what + "}", this, items);
66+
67+
computeOption(what, getter);
68+
69+
hasGetSection.set(true);
70+
return true;
71+
}
72+
73+
return false;
74+
});
75+
76+
if (!hasGetSection.get())
77+
Skript.warning("Computed options don't work without a get section");
78+
79+
return nodesOkay;
7280
}
7381

7482
return false;

src/main/java/com/btk5h/skriptmirror/skript/custom/expression/CustomExpressionSection.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@
1616

1717
import java.io.File;
1818
import java.util.*;
19+
import java.util.concurrent.atomic.AtomicBoolean;
1920
import java.util.stream.Collectors;
2021
import java.util.stream.StreamSupport;
2122

2223
public class CustomExpressionSection extends CustomSyntaxSection<ExpressionSyntaxInfo> {
2324
static {
24-
//noinspection unchecked
2525
CustomSyntaxSection.register("Define Expression", CustomExpressionSection.class,
2626
"[(2¦local)] [(1¦(plural|non(-|[ ])single))] expression <.+>",
2727
"[(2¦local)] [(1¦(plural|non(-|[ ])single))] expression",
2828
"[(2¦local)] [(1¦(plural|non(-|[ ])single))] %*classinfos% property <.+>");
2929
}
3030

31-
private static DataTracker<ExpressionSyntaxInfo> dataTracker = new DataTracker<>();
31+
private static final DataTracker<ExpressionSyntaxInfo> dataTracker = new DataTracker<>();
3232

3333
static Map<ExpressionSyntaxInfo, Class<?>> returnTypes = new HashMap<>();
3434
static Map<ExpressionSyntaxInfo, Trigger> expressionHandlers = new HashMap<>();
3535
static Map<ExpressionSyntaxInfo, Trigger> parserHandlers = new HashMap<>();
3636
static Map<ExpressionSyntaxInfo, Map<Changer.ChangeMode, Trigger>> changerHandlers = new HashMap<>();
37-
static Map<ExpressionSyntaxInfo, Map<Changer.ChangeMode, Class[]>> changerTypes = new HashMap<>();
37+
static Map<ExpressionSyntaxInfo, Map<Changer.ChangeMode, Class<?>[]>> changerTypes = new HashMap<>();
3838
static Map<ExpressionSyntaxInfo, String> loopOfs = new HashMap<>();
3939

4040
static {
@@ -63,7 +63,7 @@ protected DataTracker<ExpressionSyntaxInfo> getDataTracker() {
6363

6464
@SuppressWarnings("unchecked")
6565
@Override
66-
protected boolean init(Literal[] args, int matchedPattern, SkriptParser.ParseResult parseResult, SectionNode node) {
66+
protected boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseResult parseResult, SectionNode node) {
6767
String what;
6868
SectionNode patterns = (SectionNode) node.get("patterns");
6969
File script = (parseResult.mark & 2) == 2 ? SkriptUtil.getCurrentScript() : null;
@@ -83,12 +83,12 @@ protected boolean init(Literal[] args, int matchedPattern, SkriptParser.ParseRes
8383
int i = 1;
8484
for (Node subNode : patterns) {
8585
register(
86-
ExpressionSyntaxInfo.create(script, subNode.getKey(), i++, alwaysPlural, false, false));
86+
ExpressionSyntaxInfo.create(script, subNode.getKey(), i++, alwaysPlural, false, false));
8787
}
8888
break;
8989
case 2:
9090
what = parseResult.regexes.get(0).group();
91-
String fromType = Arrays.stream(((Literal<ClassInfo>) args[0]).getArray())
91+
String fromType = Arrays.stream(((Literal<ClassInfo<?>>) args[0]).getArray())
9292
.map(ClassInfo::getCodeName)
9393
.map(codeName -> {
9494
boolean isPlural = Utils.getEnglishPlural(codeName).getSecond();
@@ -117,14 +117,15 @@ protected boolean init(Literal[] args, int matchedPattern, SkriptParser.ParseRes
117117
return false;
118118
}
119119

120-
return handleEntriesAndSections(node,
120+
AtomicBoolean hasGetOrSet = new AtomicBoolean();
121+
boolean nodesOkay = handleEntriesAndSections(node,
121122
entryNode -> {
122123
String key = entryNode.getKey();
124+
assert key != null;
123125

124126
if (key.equalsIgnoreCase("return type")) {
125127
String userReturnType = entryNode.getValue();
126-
Class returnType =
127-
Classes.getClassFromUserInput(ScriptLoader.replaceOptions(userReturnType));
128+
Class<?> returnType = Classes.getClassFromUserInput(ScriptLoader.replaceOptions(userReturnType));
128129
whichInfo.forEach(which -> returnTypes.put(which, returnType));
129130
return true;
130131
}
@@ -139,6 +140,7 @@ protected boolean init(Literal[] args, int matchedPattern, SkriptParser.ParseRes
139140
},
140141
sectionNode -> {
141142
String key = sectionNode.getKey();
143+
assert key != null;
142144

143145
if (key.equalsIgnoreCase("patterns")) {
144146
return true;
@@ -151,6 +153,7 @@ protected boolean init(Literal[] args, int matchedPattern, SkriptParser.ParseRes
151153
expressionHandlers.put(which,
152154
new Trigger(SkriptUtil.getCurrentScript(), "get " + which.getPattern(), this, items)));
153155

156+
hasGetOrSet.set(true);
154157
return true;
155158
}
156159

@@ -176,7 +179,7 @@ protected boolean init(Literal[] args, int matchedPattern, SkriptParser.ParseRes
176179
});
177180

178181
if (rawTypes.length() > 0) {
179-
Class[] acceptedClasses = Arrays.stream(rawTypes.split(","))
182+
Class<?>[] acceptedClasses = Arrays.stream(rawTypes.split(","))
180183
.map(String::trim)
181184
.map(SkriptUtil::getUserClassInfoAndPlural)
182185
.map(meta -> {
@@ -197,12 +200,21 @@ protected boolean init(Literal[] args, int matchedPattern, SkriptParser.ParseRes
197200
});
198201
}
199202

203+
hasGetOrSet.set(true);
200204
return true;
201205
}
202206
}
203207

204208
return false;
205209
});
210+
211+
if (!nodesOkay)
212+
return false;
213+
214+
if (!hasGetOrSet.get())
215+
Skript.warning("Custom expressions are useless without a get / change section");
216+
217+
return true;
206218
}
207219

208220
public static ExpressionSyntaxInfo lookup(File script, int matchedPattern) {

0 commit comments

Comments
 (0)