Add FeatureFlag API

This commit is contained in:
Jake Potrebic
2023-03-09 11:24:43 -08:00
parent 3fc7f9269a
commit 0f47ed5252
14 changed files with 159 additions and 24 deletions

View File

@@ -0,0 +1,28 @@
package io.papermc.paper.world.flag;
import java.util.Set;
import org.bukkit.FeatureFlag;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Unmodifiable;
import org.jspecify.annotations.NullMarked;
/**
* Implemented by types in built-in registries that are controlled by {@link FeatureFlag FeatureFlags}.
* Types in data-driven registries that are controlled by feature flags just will not exist in that registry.
* @apiNote When a type that currently implements this interface transitions to being data-drive, this
* interface will be removed from that type in the following major version.
*/
@NullMarked
@ApiStatus.NonExtendable
public interface FeatureDependant {
/**
* Gets the set of required feature flags for this
* to be enabled.
*
* @return the immutable set of feature flags
*/
default @Unmodifiable Set<FeatureFlag> requiredFeatures() {
return FeatureFlagProvider.provider().requiredFeatures(this);
}
}

View File

@@ -0,0 +1,21 @@
package io.papermc.paper.world.flag;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;
import org.bukkit.FeatureFlag;
import org.jetbrains.annotations.ApiStatus;
import org.jspecify.annotations.NullMarked;
@NullMarked
@ApiStatus.Internal
interface FeatureFlagProvider {
Optional<FeatureFlagProvider> PROVIDER = ServiceLoader.load(FeatureFlagProvider.class).findFirst();
static FeatureFlagProvider provider() {
return PROVIDER.orElseThrow();
}
Set<FeatureFlag> requiredFeatures(FeatureDependant dependant);
}

View File

@@ -0,0 +1,32 @@
package io.papermc.paper.world.flag;
import java.util.Set;
import org.bukkit.FeatureFlag;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Unmodifiable;
import org.jspecify.annotations.NullMarked;
/**
* Implemented by types that hold {@link FeatureFlag FeatureFlags} like
* {@link org.bukkit.generator.WorldInfo} and {@link org.bukkit.RegionAccessor}.
*/
@NullMarked
@ApiStatus.NonExtendable
public interface FeatureFlagSetHolder {
/**
* Checks if this is enabled based on the loaded feature flags.
*
* @return true if enabled
*/
default boolean isEnabled(final FeatureDependant featureDependant) {
return this.getFeatureFlags().containsAll(featureDependant.requiredFeatures());
}
/**
* Get all {@link FeatureFlag FeatureFlags} enabled in this world.
*
* @return all enabled {@link FeatureFlag FeatureFlags}
*/
@Unmodifiable Set<FeatureFlag> getFeatureFlags();
}