diff --git a/src/main/java/ch/njol/skript/Skript.java b/src/main/java/ch/njol/skript/Skript.java index 216bc6687d8..457c85b9712 100644 --- a/src/main/java/ch/njol/skript/Skript.java +++ b/src/main/java/ch/njol/skript/Skript.java @@ -203,8 +203,6 @@ public static void updateMinecraftVersion() { @Nullable private static Version version = null; - @Deprecated(since = "2.9.0", forRemoval = true) // TODO this field will be replaced by a proper registry later - private static @UnknownNullability ExperimentRegistry experimentRegistry; public static Version getVersion() { final Version v = version; @@ -339,9 +337,13 @@ public static void disableHookRegistration(Class>... hooks) { /** * @return The manager for experimental, optional features. + * @deprecated {@link ExperimentRegistry} is now a regular registry, and should be accessed as such. + * See {@link org.skriptlang.skript.addon.SkriptAddon#registry(Class)}. */ + @Deprecated(since = "INSERT VERSION", forRemoval = true) public static ExperimentRegistry experiments() { - return experimentRegistry; + // intentionally returning the modifiable view + return skript.registry(ExperimentRegistry.class); } /** @@ -469,8 +471,8 @@ public void onEnable() { // initialize the old Skript SkriptAddon instance getAddonInstance(); - experimentRegistry = new ExperimentRegistry(this); - Feature.registerAll(getAddonInstance(), experimentRegistry); + skript.storeRegistry(ExperimentRegistry.class, new ExperimentRegistry(skript)); + Feature.registerAll(skript, skript.registry(ExperimentRegistry.class)); skript.storeRegistry(PropertyRegistry.class, new PropertyRegistry(this)); Property.registerDefaultProperties(); @@ -1286,8 +1288,6 @@ public void onDisable() { Skript.exception(e, "An error occurred while shutting down.", "This might or might not cause any issues."); } } - - this.experimentRegistry = null; } // ================ CONSTANTS, OPTIONS & OTHER ================ diff --git a/src/main/java/ch/njol/skript/registrations/Feature.java b/src/main/java/ch/njol/skript/registrations/Feature.java index e1e57512645..6d175deef81 100644 --- a/src/main/java/ch/njol/skript/registrations/Feature.java +++ b/src/main/java/ch/njol/skript/registrations/Feature.java @@ -1,17 +1,17 @@ package ch.njol.skript.registrations; -import ch.njol.skript.SkriptAddon; import ch.njol.skript.doc.Documentable; import ch.njol.skript.patterns.PatternCompiler; import ch.njol.skript.patterns.SkriptPattern; import com.google.common.base.Preconditions; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Unmodifiable; +import org.skriptlang.skript.addon.SkriptAddon; import org.skriptlang.skript.lang.experiment.Experiment; import org.skriptlang.skript.lang.experiment.ExperimentRegistry; import org.skriptlang.skript.lang.experiment.LifeCycle; -import java.util.Collection; import java.util.List; /** @@ -209,6 +209,7 @@ set the damage location to location(0, 0, 10) }; } + @ApiStatus.Internal public static void registerAll(SkriptAddon addon, ExperimentRegistry manager) { for (Feature value : values()) { manager.register(addon, value); diff --git a/src/main/java/org/skriptlang/skript/lang/experiment/ExperimentRegistry.java b/src/main/java/org/skriptlang/skript/lang/experiment/ExperimentRegistry.java index ec6a5ae5157..3d0b5ee33dd 100644 --- a/src/main/java/org/skriptlang/skript/lang/experiment/ExperimentRegistry.java +++ b/src/main/java/org/skriptlang/skript/lang/experiment/ExperimentRegistry.java @@ -1,10 +1,13 @@ package org.skriptlang.skript.lang.experiment; -import ch.njol.skript.Skript; -import ch.njol.skript.SkriptAddon; import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Unmodifiable; +import org.skriptlang.skript.Skript; +import org.skriptlang.skript.addon.SkriptAddon; import org.skriptlang.skript.lang.script.Script; +import org.skriptlang.skript.util.Registry; +import org.skriptlang.skript.util.ViewProvider; import java.util.LinkedHashSet; import java.util.Set; @@ -12,21 +15,41 @@ /** * A manager for registering (and identifying) experimental feature flags. */ -/* -* TODO -* This is designed to be (replaced by|refactored into) a proper registry when the registries rework PR -* is completed. The overall skeleton is designed to remain, so that there should be no breaking changes -* for anything using it. I.e. you will still be able to use Skript#experiments() and obtain 'this' class -* although these will just become helper methods for the proper registry behaviour. -* */ -public class ExperimentRegistry implements Experimented { +public class ExperimentRegistry implements Registry, ViewProvider, Experimented { private final Skript skript; private final Set experiments; + private final @Nullable ExperimentRegistry source; public ExperimentRegistry(Skript skript) { this.skript = skript; this.experiments = new LinkedHashSet<>(); + this.source = null; + } + + /** + * Internal constructor for creating an unmodifiable view of an experiment registry. + */ + private ExperimentRegistry(ExperimentRegistry source) { + this.skript = source.skript; + this.experiments = source.experiments; + this.source = source; + } + + /** + * @deprecated Use {@link #ExperimentRegistry(Skript)}. + */ + @Deprecated(since = "INSERT VERSION", forRemoval = true) + public ExperimentRegistry(ch.njol.skript.Skript ignored) { + this(ch.njol.skript.Skript.instance()); + } + + /** + * @return An unmodifiable view of this experiment registry. + */ + @Override + public ExperimentRegistry unmodifiableView() { + return new ExperimentRegistry(this); } /** @@ -52,6 +75,14 @@ public Experiment[] registered() { return experiments.toArray(new Experiment[0]); } + /** + * @return An unmodifiable set containing all currently-registered experiments. + */ + @Override + public @Unmodifiable Set elements() { + return Set.copyOf(experiments); + } + /** * Registers a new experimental feature flag, which will be available to scripts * with the {@code using %name%} structure. @@ -60,6 +91,9 @@ public Experiment[] registered() { * @param experiment The experimental feature flag. */ public void register(SkriptAddon addon, Experiment experiment) { + if (source != null) { + throw new UnsupportedOperationException("Cannot register experiments using an unmodifiable registry."); + } // the addon instance is requested for now in case we need it in future (for error triage) this.experiments.add(experiment); } @@ -81,6 +115,9 @@ public void registerAll(SkriptAddon addon, Experiment... experiments) { * @param experiment The experimental feature flag. */ public void unregister(SkriptAddon addon, Experiment experiment) { + if (source != null) { + throw new UnsupportedOperationException("Cannot unregister experiments using an unmodifiable registry."); + } // the addon instance is requested for now in case we need it in future (for error triage) this.experiments.remove(experiment); }