Skip to content

Commit

Permalink
Requested Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
UnderscoreTud committed Oct 29, 2023
1 parent ffdba78 commit 1fb2261
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 233 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/btk5h/skriptmirror/JavaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.Objects;

public class JavaType {
public final class JavaType {

private final Class<?> javaClass;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
import org.skriptlang.skript.lang.script.Script;
import org.skriptlang.skript.lang.structure.Structure;

import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.StreamSupport;
Expand Down Expand Up @@ -54,7 +58,7 @@ public class StructImport extends Structure {

@Override
public boolean init(Literal<?>[] args, int matchedPattern, ParseResult parseResult, EntryContainer entryContainer) {
this.script = SkriptUtil.getCurrentScript();
this.script = getParser().getCurrentScript();
getEntryContainer().getSource().forEach(node -> registerImport(Optional.ofNullable(node.getKey())
.map(ScriptLoader::replaceOptions)
.orElse(null), script));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -132,6 +133,7 @@ public String toString() {
}

protected List<T> whichInfo = new ArrayList<>();
private boolean hasPatterns = false;

protected abstract DataTracker<T> getDataTracker();

Expand Down Expand Up @@ -180,6 +182,7 @@ private void update() {
}

protected final void register(T data) {
hasPatterns = true;
String pattern = data.getPattern();

whichInfo.add(data);
Expand All @@ -189,8 +192,30 @@ protected final void register(T data) {
.put(pattern, data);
}

protected boolean checkHasPatterns() {
if (hasPatterns)
return true;
Skript.error("A custom syntax must have at least one pattern");
return false;
}

protected SectionNode[] getParseNode() {
SectionNode parseNode = getEntryContainer().getOptional("parse", SectionNode.class, false);
SectionNode safeParseNode = getEntryContainer().getOptional("safe parse", SectionNode.class, false);
if (parseNode != null && safeParseNode != null) {
Skript.error("A custom syntax element cannot contain both 'parse' and 'safe parse' entries");
return null;
}
if (safeParseNode != null) {
Skript.warning("The 'safe parse' entry is deprecated and will act as a regular 'parse' entry."
+ " Please use the 'parse' entry instead");
return new SectionNode[] {safeParseNode};
}
return new SectionNode[] {parseNode};
}

@SuppressWarnings("unchecked")
protected boolean handleUsableSection(SectionNode sectionNode, Map<T, List<Supplier<Boolean>>> usableSuppliers) {
protected boolean handleUsableEntry(SectionNode sectionNode, Map<T, List<Supplier<Boolean>>> usableSuppliers) {
Script currentScript = SkriptUtil.getCurrentScript();
for (Node usableNode : sectionNode) {
String usableKey = usableNode.getKey();
Expand Down Expand Up @@ -239,9 +264,9 @@ public T getFirstWhich() {

public static EntryValidator.EntryValidatorBuilder customSyntaxValidator() {
return EntryValidator.builder()
.addSection("patterns", true)
.addEntryData(new PatternsEntryData("patterns", null, true))
.addSection("parse", true)
.addSection("safe parse", true)
.addSection("safe parse", true) // Deprecated
.addSection("usable in", true);
}

Expand Down
41 changes: 41 additions & 0 deletions src/main/java/org/skriptlang/reflect/syntax/PatternsEntryData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.skriptlang.reflect.syntax;

import ch.njol.skript.ScriptLoader;
import ch.njol.skript.config.Node;
import ch.njol.skript.config.SectionNode;
import org.eclipse.jdt.annotation.Nullable;
import org.skriptlang.skript.lang.entry.EntryData;

import java.util.ArrayList;
import java.util.List;

public class PatternsEntryData extends EntryData<List<String>> {

public PatternsEntryData(String key, @Nullable List<String> defaultValue, boolean optional) {
super(key, defaultValue, optional);
}

@Override
public List<String> getValue(Node node) {
List<String> patterns = new ArrayList<>();
for (Node subNode : (SectionNode) node) {
String key = subNode.getKey();
if (key == null)
continue;
patterns.add(key);
}
return patterns;
}

@Override
public boolean canCreateWith(Node node) {
if (!(node instanceof SectionNode))
return false;
String key = node.getKey();
if (key == null)
return false;
key = ScriptLoader.replaceOptions(key);
return getKey().equalsIgnoreCase(key);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public class StructCustomCondition extends CustomSyntaxStructure<ConditionSyntax

static {
String[] syntax = {
"[(1¦local)] condition <.+>",
"[(1¦local)] condition",
"[(1¦local)] %*classinfos% property condition <.+>"
"[:local] condition <.+>",
"[:local] condition",
"[:local] %*classinfos% property condition <.+>"
};
Skript.registerStructure(StructCustomCondition.class, customSyntaxValidator()
.addSection("check", true)
.addSection("check", false)
.build(), syntax);
}

Expand All @@ -64,8 +64,7 @@ public class StructCustomCondition extends CustomSyntaxStructure<ConditionSyntax
dataTracker.addManaged(parseSectionLoaded);
}

private SectionNode parseNode, checkNode;
private Runnable register;
private SectionNode parseNode;

@Override
public DataTracker<ConditionSyntaxInfo> getDataTracker() {
Expand All @@ -78,28 +77,31 @@ public boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseRes
EntryContainer entryContainer) {
customConditionsUsed = true;

SectionNode patterns = entryContainer.getOptional("patterns", SectionNode.class, false);
Script script = (parseResult.mark & 1) == 1 ? SkriptUtil.getCurrentScript() : null;
List<String> patterns = entryContainer.getOptional("patterns", List.class, false);
Script script = parseResult.hasTag("local") ? SkriptUtil.getCurrentScript() : null;

if (matchedPattern != 1 && patterns != null) {
Skript.error("A custom condition with an inline pattern cannot have a 'patterns' entry too");
return false;
}

switch (matchedPattern) {
case 0:
case 0: // condition with an inline pattern
String pattern = parseResult.regexes.get(0).group();
register = () -> register(ConditionSyntaxInfo.create(script, pattern, 1, false, false));
register(ConditionSyntaxInfo.create(script, pattern, 1, false, false));
break;
case 1:
case 1: // condition with a 'patterns' entry
if (patterns == null) {
Skript.error("Custom conditions without inline patterns must have a patterns section.");
Skript.error("A custom condition without an inline pattern must have a 'patterns' entry");
return false;
}

register = () -> {
int i = 1;
for (Node subNode : patterns) {
register(ConditionSyntaxInfo.create(script, subNode.getKey(), i++, false, false));
}
};
int i = 1;
for (String p : patterns) {
register(ConditionSyntaxInfo.create(script, p, i++, false, false));
}
break;
case 2:
case 2: // property condition
String property = parseResult.regexes.get(0).group();
String type = Arrays.stream(((Literal<ClassInfo<?>>) args[0]).getArray())
.map(ClassInfo::getCodeName)
Expand All @@ -113,49 +115,28 @@ public boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseRes
return codeName;
})
.collect(Collectors.joining("/"));
register = () -> {
register(ConditionSyntaxInfo.create(script, "%" + type + "% (is|are) " + property, 1, false, true));
register(
ConditionSyntaxInfo.create(script, "%" + type + "% (isn't|is not|aren't|are not) " + property, 1, true, true));
};
register(ConditionSyntaxInfo.create(script, "%" + type + "% (is|are) " + property, 1, false, true));
register(
ConditionSyntaxInfo.create(script, "%" + type + "% (isn't|is not|aren't|are not) " + property, 1, true, true));
break;
}

if (matchedPattern != 1 && patterns != null) {
Skript.error("Custom conditions with inline patterns may not have a patterns section.");
return false;
}

return true;
return checkHasPatterns();
}

@Override
public boolean preLoad() {
super.preLoad();
register.run();
EntryContainer entryContainer = getEntryContainer();
parseNode = entryContainer.getOptional("parse", SectionNode.class, false);
SectionNode safeParseNode = entryContainer.getOptional("safe parse", SectionNode.class, false);
if (parseNode != null) {
if (safeParseNode != null) {
Skript.error("You can't have two parse sections");
return false;
}
whichInfo.forEach(which -> parseSectionLoaded.put(which, false));
} else if (safeParseNode != null) {
SyntaxParseEvent.register(safeParseNode, whichInfo, parserHandlers);
whichInfo.forEach(which -> parseSectionLoaded.put(which, true));
}

SectionNode usableInNode = entryContainer.getOptional("usable in", SectionNode.class, false);
if (usableInNode != null && !handleUsableSection(usableInNode, usableSuppliers))
SectionNode[] parseNode = getParseNode();
if (parseNode == null)
return false;
this.parseNode = parseNode[0];
whichInfo.forEach(which -> parseSectionLoaded.put(which, false));

checkNode = entryContainer.getOptional("check", SectionNode.class, false);
if (checkNode == null)
Skript.warning("Custom conditions are useless without a check section");

return true;
SectionNode usableInNode = entryContainer.getOptional("usable in", SectionNode.class, false);
return usableInNode == null || handleUsableEntry(usableInNode, usableSuppliers);
}

@Override
Expand All @@ -167,13 +148,12 @@ public boolean load() {
whichInfo.forEach(which -> parseSectionLoaded.put(which, true));
}

if (checkNode != null) {
SkriptLogger.setNode(checkNode);
getParser().setCurrentEvent("custom condition check", ConditionCheckEvent.class);
List<TriggerItem> items = SkriptUtil.getItemsFromNode(checkNode);
whichInfo.forEach(which -> conditionHandlers.put(which,
new Trigger(getParser().getCurrentScript(), "condition " + which, new SimpleEvent(), items)));
}
SectionNode checkNode = getEntryContainer().get("check", SectionNode.class, false);
SkriptLogger.setNode(checkNode);
getParser().setCurrentEvent("custom condition check", ConditionCheckEvent.class);
List<TriggerItem> items = SkriptUtil.getItemsFromNode(checkNode);
whichInfo.forEach(which -> conditionHandlers.put(which,
new Trigger(getParser().getCurrentScript(), "condition " + which, new SimpleEvent(), items)));
SkriptLogger.setNode(null);

return true;
Expand Down
Loading

0 comments on commit 1fb2261

Please sign in to comment.