Skip to content
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
14 changes: 7 additions & 7 deletions src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -339,9 +337,13 @@ public static void disableHookRegistration(Class<? extends Hook<?>>... 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);
}

/**
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 ================
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/ch/njol/skript/registrations/Feature.java
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,55 @@
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;

/**
* 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<Experiment>, ViewProvider<ExperimentRegistry>, Experimented {

private final Skript skript;
private final Set<Experiment> 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);
}

/**
Expand All @@ -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<Experiment> elements() {
return Set.copyOf(experiments);
}

/**
* Registers a new experimental feature flag, which will be available to scripts
* with the {@code using %name%} structure.
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
Loading