Skip to content

Commit

Permalink
Deprecate Structure#getEntryContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
APickledWalrus committed Apr 11, 2024
1 parent a52c7d5 commit 213b007
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
7 changes: 5 additions & 2 deletions src/main/java/ch/njol/skript/lang/SkriptEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public abstract class SkriptEvent extends Structure {
public static final Priority PRIORITY = new Priority(600);

private String expr;
private SectionNode source;
@Nullable
protected EventPriority eventPriority;
@Nullable
Expand All @@ -67,7 +68,7 @@ public abstract class SkriptEvent extends Structure {
protected Trigger trigger;

@Override
public final boolean init(Literal<?>[] args, int matchedPattern, ParseResult parseResult, EntryContainer entryContainer) {
public final boolean init(Literal<?>[] args, int matchedPattern, ParseResult parseResult, @Nullable EntryContainer entryContainer) {
this.expr = parseResult.expr;

EventData eventData = getParser().getData(EventData.class);
Expand Down Expand Up @@ -101,6 +102,9 @@ public final boolean init(Literal<?>[] args, int matchedPattern, ParseResult par
return false;
}

assert entryContainer != null; // cannot be null for non-simple structures
this.source = entryContainer.getSource();

return init(args, matchedPattern, parseResult);
}

Expand Down Expand Up @@ -129,7 +133,6 @@ public boolean load() {
return false;

// noinspection ConstantConditions - entry container cannot be null as this structure is not simple
SectionNode source = getEntryContainer().getSource();
if (Skript.debug() || source.debug())
Skript.debug(expr + " (" + this + "):");

Expand Down
8 changes: 5 additions & 3 deletions src/main/java/ch/njol/skript/structures/StructCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,23 @@ protected Integer getValue(String value) {
);
}

@SuppressWarnings("NotNullFieldNotInitialized")
private EntryContainer entryContainer;

@Nullable
private ScriptCommand scriptCommand;

@Override
public boolean init(Literal<?>[] args, int matchedPattern, ParseResult parseResult, @Nullable EntryContainer entryContainer) {
assert entryContainer != null; // cannot be null for non-simple structures
this.entryContainer = entryContainer;
return true;
}

@Override
public boolean load() {
getParser().setCurrentEvent("command", ScriptCommandEvent.class);

EntryContainer entryContainer = getEntryContainer();
assert entryContainer != null;

String fullCommand = entryContainer.getSource().getKey();
assert fullCommand != null;
fullCommand = ScriptLoader.replaceOptions(fullCommand);
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/ch/njol/skript/structures/StructFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import ch.njol.skript.ScriptLoader;
import ch.njol.skript.Skript;
import ch.njol.skript.config.SectionNode;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
Expand Down Expand Up @@ -70,12 +71,16 @@ public class StructFunction extends Structure {
);
}

@SuppressWarnings("NotNullFieldNotInitialized")
private SectionNode source;
@Nullable
private Signature<?> signature;
private boolean local;

@Override
public boolean init(Literal<?>[] literals, int matchedPattern, ParseResult parseResult, @Nullable EntryContainer entryContainer) {
assert entryContainer != null; // cannot be null for non-simple structures
this.source = entryContainer.getSource();
local = parseResult.hasTag("local");
return true;
}
Expand All @@ -84,7 +89,7 @@ public boolean init(Literal<?>[] literals, int matchedPattern, ParseResult parse
public boolean preLoad() {
// match signature against pattern
// noinspection ConstantConditions - entry container cannot be null as this structure is not simple
String rawSignature = getEntryContainer().getSource().getKey();
String rawSignature = source.getKey();
assert rawSignature != null;
rawSignature = ScriptLoader.replaceOptions(rawSignature);
Matcher matcher = SIGNATURE_PATTERN.matcher(rawSignature);
Expand Down Expand Up @@ -112,7 +117,7 @@ public boolean load() {

assert signature != null;
// noinspection ConstantConditions - entry container cannot be null as this structure is not simple
Functions.loadFunction(parser.getCurrentScript(), getEntryContainer().getSource(), signature);
Functions.loadFunction(parser.getCurrentScript(), source, signature);

parser.deleteCurrentEvent();

Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/skriptlang/skript/lang/structure/Structure.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import ch.njol.util.Kleenean;
import ch.njol.util.coll.iterator.CheckedIterator;
import ch.njol.util.coll.iterator.ConsumingIterator;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.entry.EntryContainer;
Expand Down Expand Up @@ -89,10 +90,17 @@ public int compareTo(@NotNull Structure.Priority o) {

/**
* @return An EntryContainer containing this Structure's {@link EntryData} and {@link Node} parse results.
* This method will return null if the Structure has not yet been initialized or if it is simple.
* Please note that this Structure <b>MUST</b> have been initialized for this to work.
* This method is not usable for simple structures.
* @deprecated This method will be removed in a future version.
* If the EntryContainer is needed outside of {@link #init(Literal[], int, ParseResult, EntryContainer)},
* the Structure should keep a reference to it.
*/
@Nullable
@Deprecated
@ApiStatus.ScheduledForRemoval
public final EntryContainer getEntryContainer() {
if (entryContainer == null)
throw new IllegalStateException("This Structure hasn't been initialized!");
return entryContainer;
}

Expand Down

0 comments on commit 213b007

Please sign in to comment.