Skip to content

Commit aaf44be

Browse files
committed
Bug fixes for alpha 1
1 parent f10f9f5 commit aaf44be

4 files changed

Lines changed: 30 additions & 7 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ Note that this means default values need to be re-evaluated and therefore will n
3434
Removing a template will 'orphan' existing structs, who will function as if the template still existed, though it may be impossible to access their information as the field access expression may not recognize the field names. If a new template is added with the same name, these existing structs will be updated to match the new template.
3535
If you remove a template, you should take care to remove structs that depended on it.
3636

37+
**I recommend putting your templates in a separate script file, so you can limit the chances of them being accidentally modified or disabled.**
38+
3739
## Roadmap
3840
Alpha 2:
3941
- `constant` modifier for fields

src/main/java/com/sovdee/oopsk/elements/expressions/ExprFieldAccess.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.lang.reflect.Array;
2222
import java.util.Arrays;
2323
import java.util.Collections;
24+
import java.util.Locale;
2425
import java.util.Map;
2526
import java.util.Set;
2627
import java.util.WeakHashMap;
@@ -37,9 +38,9 @@ public static void updateAll() {
3738

3839
static {
3940
Skript.registerExpression(ExprFieldAccess.class, Object.class, ExpressionType.PROPERTY,
40-
"[the] field <\\w+> [of] %struct%",
41-
"%struct%'[s] <\\w+> field",
42-
"%struct%[ ]->[ ]<\\w+>");
41+
"[the] field <[\\w ]+> [of] %struct%",
42+
"%struct%'[s] <[\\w ]+> field",
43+
"%struct%[ ]->[ ]<[\\w ]+>");
4344
}
4445

4546
String fieldName;
@@ -57,6 +58,11 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean is
5758
setExpr((Expression<Struct>) expressions[0]);
5859
node = getParser().getNode();
5960
fieldName = parseResult.regexes.get(0).group(0);
61+
if (fieldName == null) {
62+
Skript.error("Field name cannot be null.");
63+
return false;
64+
}
65+
fieldName = fieldName.trim().toLowerCase(Locale.ENGLISH);
6066
if (!updateFieldGuesses()) {
6167
Skript.error("No field with name '" + fieldName + "' found.");
6268
return false;

src/main/java/com/sovdee/oopsk/elements/expressions/ExprStructInstance.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
import org.jetbrains.annotations.Nullable;
1515
import org.skriptlang.skript.log.runtime.SyntaxRuntimeErrorProducer;
1616

17+
import java.util.Locale;
18+
1719
public class ExprStructInstance extends SimpleExpression<Struct> implements SyntaxRuntimeErrorProducer {
1820

1921
static {
2022
Skript.registerExpression(ExprStructInstance.class, Struct.class, ExpressionType.SIMPLE,
21-
"[a[n]] <(\\w+)> struct [instance]");
23+
"[a[n]] <([\\w ]+)> struct [instance]");
2224
}
2325

2426
private String name;
@@ -29,6 +31,7 @@ public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean is
2931
name = parseResult.regexes.get(0).group(1);
3032
if (name == null)
3133
return false;
34+
name = name.trim().toLowerCase(Locale.ENGLISH);
3235
if (Oopsk.getTemplateManager().getTemplate(name) == null) {
3336
Skript.error("A struct by the name of '" + name + "' does not exist.");
3437
return false;

src/main/java/com/sovdee/oopsk/elements/structures/StructStructTemplate.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.util.ArrayList;
2626
import java.util.List;
27+
import java.util.Locale;
2728
import java.util.regex.MatchResult;
2829
import java.util.regex.Matcher;
2930
import java.util.regex.Pattern;
@@ -41,7 +42,7 @@ public class StructStructTemplate extends Structure {
4142
@Override
4243
public boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseResult parseResult, @Nullable EntryContainer entryContainer) {
4344
MatchResult regex = parseResult.regexes.get(0);
44-
name = regex.group(1);
45+
name = regex.group(1).trim().toLowerCase(Locale.ENGLISH);
4546
this.entryContainer = entryContainer;
4647

4748
return entryContainer != null;
@@ -67,7 +68,7 @@ public boolean preLoad() {
6768
}
6869

6970

70-
private static final Pattern fieldPattern = Pattern.compile("(\\w+): ([\\w ]+?)(?: ?= ?(.+))?");
71+
private static final Pattern fieldPattern = Pattern.compile("([\\w ]+): ([\\w ]+?)(?: ?= ?(.+))?");
7172

7273
private List<Field<?>> getFields(@NotNull SectionNode node) {
7374
List<Field<?>> fields = new ArrayList<>();
@@ -80,8 +81,19 @@ private List<Field<?>> getFields(@NotNull SectionNode node) {
8081
Skript.error("invalid field: " + key);
8182
return null;
8283
}
84+
// parse the field name
85+
String fieldName = matcher.group(1).trim().toLowerCase(Locale.ENGLISH);
86+
if (fieldName.isEmpty()) {
87+
Skript.error("Field name cannot be empty.");
88+
return null;
89+
}
90+
// check if the field name is already taken
91+
if (fields.stream().anyMatch(field -> field.name().equalsIgnoreCase(fieldName))) {
92+
Skript.error("Field name '" + fieldName + "' is already taken.");
93+
return null;
94+
}
95+
8396
// parse the field type
84-
String fieldName = matcher.group(1);
8597
var pair = Utils.getEnglishPlural(matcher.group(2).trim());
8698
boolean isPlural = pair.getValue();
8799
ClassInfo<?> fieldType = Classes.getClassInfoFromUserInput(pair.getKey());

0 commit comments

Comments
 (0)