Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sync-only entry in custom events (attempt #2) #95

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.skriptlang.reflect.syntax.event;

import ch.njol.skript.ScriptLoader;
import ch.njol.skript.Skript;
import ch.njol.skript.config.Node;
import ch.njol.skript.config.SimpleNode;
import org.eclipse.jdt.annotation.Nullable;
import org.skriptlang.skript.lang.entry.KeyValueEntryData;

public class SyncOnlyEntryData extends KeyValueEntryData<Boolean> {

public SyncOnlyEntryData(String key, @Nullable Boolean defaultValue, boolean optional) {
super(key, defaultValue, optional);
}

@Override
@Nullable
protected Boolean getValue(String value) {
if (!(value.equalsIgnoreCase("false") || value.equalsIgnoreCase("true"))) {
Skript.error("Sync only entry can be only true or false.");
return null;
}
return Boolean.valueOf(value);
}

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

protected boolean canCreateWith(String node) {
return node.startsWith(getKey() + getSeparator());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ public class CustomEvent extends SkriptEvent {
private Expression<?>[] exprs;
private SkriptParser.ParseResult parseResult;
private Object variablesMap;

private boolean syncOnly;

@Override
public boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseResult parseResult) {
which = StructCustomEvent.lookup(SkriptUtil.getCurrentScript(), matchedPattern);

if (which == null) {
return false;
}

this.exprs = Arrays.stream(args)
.map(SkriptUtil::defendExpression)
.toArray(Expression[]::new);
Expand All @@ -48,7 +49,9 @@ public boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseRes
if (!SkriptUtil.canInitSafely(this.exprs)) {
return false;
}


syncOnly = StructCustomEvent.syncOnly.get(which);

Boolean bool = StructCustomEvent.parseSectionLoaded.get(which);
if (bool != null && !bool) {
Skript.error("You can't use custom events with parse sections before they're loaded.");
Expand Down Expand Up @@ -83,7 +86,12 @@ public boolean load() {
CustomEvent.setLastWhich(null);
return parsed;
}


@Override
public boolean canExecuteAsynchronously() {
return !syncOnly;
}

@Override
public boolean check(Event e) {
BukkitCustomEvent bukkitCustomEvent = (BukkitCustomEvent) e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.skriptlang.reflect.syntax.event.EventSyntaxInfo;
import org.skriptlang.reflect.syntax.event.EventTriggerEvent;
import org.skriptlang.reflect.syntax.event.EventValuesEntryData;
import org.skriptlang.reflect.syntax.event.SyncOnlyEntryData;
import org.skriptlang.skript.lang.entry.EntryContainer;
import org.skriptlang.skript.lang.script.Script;

Expand All @@ -34,6 +35,11 @@ public class StructCustomEvent extends CustomSyntaxStructure<EventSyntaxInfo> {
static {
Skript.registerStructure(StructCustomEvent.class, customSyntaxValidator()
.addEntry("pattern", null, true)
.addEntryData(new SyncOnlyEntryData("sync only", false, true) {
public boolean canCreateWith(String node) {
return super.canCreateWith(node) || node.startsWith(getKey().replace(' ', '-') + getSeparator());
}
})
.addEntryData(new EventValuesEntryData("event values", null, true) {
@Override
public boolean canCreateWith(String node) {
Expand All @@ -47,13 +53,14 @@ public boolean canCreateWith(String node) {
}

private static final DataTracker<EventSyntaxInfo> dataTracker = new DataTracker<>();

static final Map<EventSyntaxInfo, String> nameValues = new HashMap<>();
static final Map<EventSyntaxInfo, List<ClassInfo<?>>> eventValueTypes = new HashMap<>();
static final Map<EventSyntaxInfo, Trigger> parserHandlers = new HashMap<>();
static final Map<EventSyntaxInfo, Trigger> eventHandlers = new HashMap<>();
static final Map<EventSyntaxInfo, Boolean> parseSectionLoaded = new HashMap<>();

static final Map<EventSyntaxInfo, Boolean> syncOnly = new HashMap<>();

static {
Skript.registerEvent("custom event", CustomEvent.class, BukkitCustomEvent.class);
Optional<SkriptEventInfo<?>> info = Skript.getEvents().stream()
Expand All @@ -66,6 +73,7 @@ public boolean canCreateWith(String node) {
dataTracker.addManaged(parserHandlers);
dataTracker.addManaged(eventHandlers);
dataTracker.addManaged(parseSectionLoaded);
dataTracker.addManaged(syncOnly);
}

private SectionNode parseNode;
Expand All @@ -90,25 +98,29 @@ public boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseRes
} else if (patternNode != null) {
patterns = Collections.singletonList(patternNode);
}

if (patterns == null || patterns.isEmpty()) {
// Always false. Used for the error
return checkHasPatterns();
}


boolean syncOnlyEntry = entryContainer.getOptional("sync only", Boolean.class, true);

int i = 1;
for (String pattern : patterns) {
register(EventSyntaxInfo.create(script, pattern, i++));
}

String name = (String) args[0].getSingle();
if (nameValues.values().stream().anyMatch(name::equalsIgnoreCase)) {
Skript.error("There is already a custom event with that name");
return false;
}


whichInfo.forEach(which -> syncOnly.put(which, syncOnlyEntry));

whichInfo.forEach(which -> nameValues.put(which, name));

// Register the custom events during #init, rather than #preLoad
super.preLoad();
return true;
Expand All @@ -134,7 +146,7 @@ public boolean load() {
SkriptReflection.replaceEventValues(classInfoList);
whichInfo.forEach(which -> eventValueTypes.put(which, classInfoList));
}

if (parseNode != null) {
SkriptLogger.setNode(parseNode);
SyntaxParseEvent.register(parseNode, whichInfo, parserHandlers);
Expand Down