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

Fix events in StructParse #7067

Merged
merged 9 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
39 changes: 14 additions & 25 deletions src/main/java/ch/njol/skript/test/runner/StructParse.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.test.runner;

import ch.njol.skript.ScriptLoader;
import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.classes.Changer.ChangerUtils;
import ch.njol.skript.config.Node;
Expand All @@ -30,6 +11,7 @@
import ch.njol.skript.doc.NoDoc;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.ContextlessEvent;
import ch.njol.skript.log.LogEntry;
import ch.njol.skript.log.RetainingLogHandler;
Expand All @@ -42,9 +24,6 @@
import org.skriptlang.skript.lang.entry.util.ExpressionEntryData;
import org.skriptlang.skript.lang.structure.Structure;

import static ch.njol.skript.lang.SkriptParser.ParseResult;


@Name("Parse Structure")
@Description("Parses the code inside this structure as a structure and use 'parse logs' to grab any logs from it.")
@NoDoc
Expand All @@ -54,7 +33,7 @@ public class StructParse extends Structure {
Skript.registerStructure(StructParse.class, "parse");
}

private static EntryValidator validator = EntryValidator.builder()
private static final EntryValidator validator = EntryValidator.builder()
.addEntryData(new ExpressionEntryData<>("results", null, false, Object.class))
.addSection("code", false)
.build();
Expand All @@ -64,26 +43,31 @@ public class StructParse extends Structure {
private Expression<?> resultsExpression;

@Override
public boolean init(Literal<?>[] args, int matchedPattern, ParseResult parseResult, EntryContainer entryContainer) {
public boolean init(Literal<?>[] args, int matchedPattern,
ParseResult parseResult, EntryContainer entryContainer) {
SectionNode parseStructureSectionNode = entryContainer.getSource();

Class<? extends Event>[] originalEvents = getParser().getCurrentEvents();
getParser().setCurrentEvent("parse", ContextlessEvent.class);
EntryContainer validatedEntries = validator.validate(parseStructureSectionNode);
getParser().setCurrentEvents(originalEvents);

if (validatedEntries == null) {
Skript.error("A parse structure must have a result entry and a code section");
return false;
}

Expression<?> maybeResultsExpression = (Expression<?>) validatedEntries.get("results", false);
if (!ChangerUtils.acceptsChange(maybeResultsExpression, ChangeMode.SET, String[].class)) {
Skript.error(maybeResultsExpression.toString(null, false) + " cannot be set to strings");
}

SectionNode codeSectionNode = (SectionNode) validatedEntries.get("code", false);
Node maybeStructureSectionNodeToParse = Iterables.getFirst(codeSectionNode, null);
if (Iterables.size(codeSectionNode) != 1 || !(maybeStructureSectionNodeToParse instanceof SectionNode)) {
Skript.error("The code section must contain a single section to parse as a structure");
}

resultsExpression = maybeResultsExpression;
structureSectionNodeToParse = (SectionNode) maybeStructureSectionNodeToParse;
return true;
Expand All @@ -100,7 +84,12 @@ public boolean load() {
try (RetainingLogHandler handler = SkriptLogger.startRetainingLog()) {
String structureSectionNodeKey = ScriptLoader.replaceOptions(structureSectionNodeToParse.getKey());
String error = "Can't understand this structure: " + structureSectionNodeKey;
Structure.parse(structureSectionNodeKey, structureSectionNodeToParse, error);
Structure structure = Structure.parse(structureSectionNodeKey, structureSectionNodeToParse, error);

if (structure != null) {
structure.load();
}
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
Efnilite marked this conversation as resolved.
Show resolved Hide resolved

logs = handler.getLog().stream()
.map(LogEntry::getMessage)
.toArray(String[]::new);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parse:
results: {StructEvent::parse::*}
code:
on script load:
broadcast "test"

test "events in structs":
assert {StructEvent::parse::*} is not set with "using event in struct caused an error"
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
Loading