From 213b007ab02060066a02638bccdaf5b03f431a6a Mon Sep 17 00:00:00 2001 From: APickledWalrus Date: Thu, 11 Apr 2024 14:45:13 -0400 Subject: [PATCH] Deprecate Structure#getEntryContainer --- src/main/java/ch/njol/skript/lang/SkriptEvent.java | 7 +++++-- .../ch/njol/skript/structures/StructCommand.java | 8 +++++--- .../ch/njol/skript/structures/StructFunction.java | 9 +++++++-- .../skriptlang/skript/lang/structure/Structure.java | 12 ++++++++++-- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/ch/njol/skript/lang/SkriptEvent.java b/src/main/java/ch/njol/skript/lang/SkriptEvent.java index 67614f469f8..145115a4460 100644 --- a/src/main/java/ch/njol/skript/lang/SkriptEvent.java +++ b/src/main/java/ch/njol/skript/lang/SkriptEvent.java @@ -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 @@ -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); @@ -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); } @@ -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 + "):"); diff --git a/src/main/java/ch/njol/skript/structures/StructCommand.java b/src/main/java/ch/njol/skript/structures/StructCommand.java index a3331d2d9a3..cf2dc234276 100644 --- a/src/main/java/ch/njol/skript/structures/StructCommand.java +++ b/src/main/java/ch/njol/skript/structures/StructCommand.java @@ -143,11 +143,16 @@ 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; } @@ -155,9 +160,6 @@ public boolean init(Literal[] args, int matchedPattern, ParseResult parseResu 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); diff --git a/src/main/java/ch/njol/skript/structures/StructFunction.java b/src/main/java/ch/njol/skript/structures/StructFunction.java index 06f8cb4c1b5..ec9feba732a 100644 --- a/src/main/java/ch/njol/skript/structures/StructFunction.java +++ b/src/main/java/ch/njol/skript/structures/StructFunction.java @@ -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; @@ -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; } @@ -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); @@ -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(); diff --git a/src/main/java/org/skriptlang/skript/lang/structure/Structure.java b/src/main/java/org/skriptlang/skript/lang/structure/Structure.java index c87de7bc0d3..743d049ef59 100644 --- a/src/main/java/org/skriptlang/skript/lang/structure/Structure.java +++ b/src/main/java/org/skriptlang/skript/lang/structure/Structure.java @@ -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; @@ -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 MUST 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; }