Add FeatureFlag API (#8952)

* Add FeatureFlag API

* switch to index & move method

* fix test
This commit is contained in:
Jake Potrebic 2024-09-07 13:35:11 -07:00
parent b91a342aa2
commit fc2cdc7db9
4 changed files with 757 additions and 5 deletions

View file

@ -0,0 +1,333 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Thu, 9 Mar 2023 11:24:43 -0800
Subject: [PATCH] Add FeatureFlag API
diff --git a/src/main/java/io/papermc/paper/world/flag/FeatureDependant.java b/src/main/java/io/papermc/paper/world/flag/FeatureDependant.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/world/flag/FeatureDependant.java
@@ -0,0 +0,0 @@
+package io.papermc.paper.world.flag;
+
+import java.util.Set;
+import org.bukkit.FeatureFlag;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.Unmodifiable;
+
+/**
+ * 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.
+ */
+@ApiStatus.Experimental
+@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 @NonNull Set<FeatureFlag> requiredFeatures() {
+ return FeatureFlagProvider.provider().requiredFeatures(this);
+ }
+}
diff --git a/src/main/java/io/papermc/paper/world/flag/FeatureFlagProvider.java b/src/main/java/io/papermc/paper/world/flag/FeatureFlagProvider.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/world/flag/FeatureFlagProvider.java
@@ -0,0 +0,0 @@
+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;
+
+@ApiStatus.Internal
+interface FeatureFlagProvider {
+
+ Optional<FeatureFlagProvider> PROVIDER = ServiceLoader.load(FeatureFlagProvider.class).findFirst();
+
+ static FeatureFlagProvider provider() {
+ return PROVIDER.orElseThrow();
+ }
+
+ Set<FeatureFlag> requiredFeatures(FeatureDependant dependant);
+}
diff --git a/src/main/java/io/papermc/paper/world/flag/FeatureFlagSetHolder.java b/src/main/java/io/papermc/paper/world/flag/FeatureFlagSetHolder.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/world/flag/FeatureFlagSetHolder.java
@@ -0,0 +0,0 @@
+package io.papermc.paper.world.flag;
+
+import java.util.Set;
+import org.bukkit.FeatureFlag;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.Unmodifiable;
+
+/**
+ * Implemented by types that hold {@link FeatureFlag FeatureFlags} like
+ * {@link org.bukkit.generator.WorldInfo} and {@link org.bukkit.RegionAccessor}.
+ */
+@ApiStatus.Experimental
+@ApiStatus.NonExtendable
+public interface FeatureFlagSetHolder {
+
+ /**
+ * Checks if this is enabled based on the loaded feature flags.
+ *
+ * @return true if enabled
+ */
+ default boolean isEnabled(final @NonNull FeatureDependant featureDependant) {
+ return this.getFeatureFlags().containsAll(featureDependant.requiredFeatures());
+ }
+
+ /**
+ * Get all {@link FeatureFlag FeatureFlags} enabled in this world.
+ *
+ * @return all enabled {@link FeatureFlag FeatureFlags}
+ */
+ @Unmodifiable @NonNull Set<FeatureFlag> getFeatureFlags();
+}
diff --git a/src/main/java/org/bukkit/FeatureFlag.java b/src/main/java/org/bukkit/FeatureFlag.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/FeatureFlag.java
+++ b/src/main/java/org/bukkit/FeatureFlag.java
@@ -0,0 +0,0 @@
package org.bukkit;
+// Paper start - overhaul FeatureFlag API
+import com.google.common.base.Preconditions;
+import java.util.List;
+import net.kyori.adventure.key.Key;
+import net.kyori.adventure.util.Index;
+import org.intellij.lang.annotations.Subst;
+// Paper end - overhaul FeatureFlag API
import org.jetbrains.annotations.ApiStatus;
/**
* This represents a Feature Flag for a World.
- * <br>
- * Flags which are unavailable in the current version will be null and/or
- * removed.
*/
@ApiStatus.Experimental
public interface FeatureFlag extends Keyed {
- public static final FeatureFlag VANILLA = Bukkit.getUnsafe().getFeatureFlag(NamespacedKey.minecraft("vanilla"));
+ // Paper start - overhaul FeatureFlag API
+ /**
+ * The {@code vanilla} feature flag.
+ */
+ FeatureFlag VANILLA = create("vanilla");
+ /**
+ * The {@code bundle} feature flag.
+ */
@ApiStatus.Experimental // Paper - add missing annotation
- public static final FeatureFlag BUNDLE = Bukkit.getUnsafe().getFeatureFlag(NamespacedKey.minecraft("bundle"));
+ FeatureFlag BUNDLE = create("bundle");
/**
- * <strong>AVAILABLE BETWEEN VERSIONS:</strong> 1.19 - 1.19.4
- *
- * @deprecated not available since 1.20
+ * The {@code trade_rebalance} feature flag.
*/
- @Deprecated
- public static final FeatureFlag UPDATE_1_20 = Bukkit.getUnsafe().getFeatureFlag(NamespacedKey.minecraft("update_1_20"));
-
@ApiStatus.Experimental // Paper - add missing annotation
- public static final FeatureFlag TRADE_REBALANCE = Bukkit.getUnsafe().getFeatureFlag(NamespacedKey.minecraft("trade_rebalance"));
+ FeatureFlag TRADE_REBALANCE = create("trade_rebalance");
+
+ @Deprecated(since = "1.20")
+ FeatureFlag UPDATE_1_20 = deprecated("update_1_20");
+
+ @Deprecated(since = "1.21")
+ FeatureFlag UPDATE_121 = deprecated("update_1_21");
/**
- * <strong>AVAILABLE BETWEEN VERSIONS:</strong> 1.20.5 - 1.20.6
- *
- * @deprecated not available since 1.21
+ * An index of all feature flags.
*/
- @Deprecated
- public static final FeatureFlag UPDATE_121 = Bukkit.getUnsafe().getFeatureFlag(NamespacedKey.minecraft("update_1_21"));
+ Index<Key, FeatureFlag> ALL_FLAGS = Index.create(FeatureFlag::key, List.copyOf(FeatureFlagImpl.ALL_FLAGS));
+
+ private static FeatureFlag create(@Subst("vanilla") final String name) {
+ final FeatureFlag flag = new FeatureFlagImpl(NamespacedKey.minecraft(name));
+ Preconditions.checkState(FeatureFlagImpl.ALL_FLAGS.add(flag), "Tried to add duplicate feature flag: " + name);
+ return flag;
+ }
+
+ private static FeatureFlag deprecated(@Subst("vanilla") final String name) {
+ return new FeatureFlagImpl.Deprecated(NamespacedKey.minecraft(name));
+ }
+ // Paper end - overhaul FeatureFlag API
}
diff --git a/src/main/java/org/bukkit/FeatureFlagImpl.java b/src/main/java/org/bukkit/FeatureFlagImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/org/bukkit/FeatureFlagImpl.java
@@ -0,0 +0,0 @@
+package org.bukkit;
+
+import java.util.HashSet;
+import java.util.Set;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.framework.qual.DefaultQualifier;
+import org.jetbrains.annotations.ApiStatus;
+
+@ApiStatus.Internal
+@DefaultQualifier(NonNull.class)
+record FeatureFlagImpl(NamespacedKey key) implements FeatureFlag {
+
+ static final Set<FeatureFlag> ALL_FLAGS = new HashSet<>();
+
+ @Override
+ public NamespacedKey getKey() {
+ return this.key;
+ }
+
+ @ApiStatus.Internal
+ @DefaultQualifier(NonNull.class)
+ record Deprecated(NamespacedKey key) implements FeatureFlag {
+
+ @Override
+ public NamespacedKey getKey() {
+ return this.key;
+ }
+ }
+}
diff --git a/src/main/java/org/bukkit/RegionAccessor.java b/src/main/java/org/bukkit/RegionAccessor.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/RegionAccessor.java
+++ b/src/main/java/org/bukkit/RegionAccessor.java
@@ -0,0 +0,0 @@ import org.jetbrains.annotations.Nullable;
* A RegionAccessor gives access to getting, modifying and spawning {@link Biome}, {@link BlockState} and {@link Entity},
* as well as generating some basic structures.
*/
-public interface RegionAccessor extends Keyed { // Paper
+public interface RegionAccessor extends Keyed, io.papermc.paper.world.flag.FeatureFlagSetHolder { // Paper - feature flag API
/**
* Gets the {@link Biome} at the given {@link Location}.
diff --git a/src/main/java/org/bukkit/UnsafeValues.java b/src/main/java/org/bukkit/UnsafeValues.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/UnsafeValues.java
+++ b/src/main/java/org/bukkit/UnsafeValues.java
@@ -0,0 +0,0 @@ public interface UnsafeValues {
String getTranslationKey(Attribute attribute);
- @Nullable
- FeatureFlag getFeatureFlag(@NotNull NamespacedKey key);
+ // Paper - replace with better system
/**
* Do not use, method will get removed, and the plugin won't run
diff --git a/src/main/java/org/bukkit/block/BlockType.java b/src/main/java/org/bukkit/block/BlockType.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/block/BlockType.java
+++ b/src/main/java/org/bukkit/block/BlockType.java
@@ -0,0 +0,0 @@ import org.jetbrains.annotations.Nullable;
* changes may occur. Do not use this API in plugins.
*/
@ApiStatus.Internal
-public interface BlockType extends Keyed, Translatable, net.kyori.adventure.translation.Translatable { // Paper - add translatable
+public interface BlockType extends Keyed, Translatable, net.kyori.adventure.translation.Translatable, io.papermc.paper.world.flag.FeatureDependant { // Paper - add translatable & feature flag API
/**
* Typed represents a subtype of {@link BlockType}s that have a known block
diff --git a/src/main/java/org/bukkit/enchantments/Enchantment.java b/src/main/java/org/bukkit/enchantments/Enchantment.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/enchantments/Enchantment.java
+++ b/src/main/java/org/bukkit/enchantments/Enchantment.java
@@ -0,0 +0,0 @@ import org.jetbrains.annotations.Nullable;
/**
* The various type of enchantments that may be added to armour or weapons
*/
-public abstract class Enchantment implements Keyed, Translatable, net.kyori.adventure.translation.Translatable { // Paper - Adventure translations
+public abstract class Enchantment implements Keyed, Translatable, net.kyori.adventure.translation.Translatable, io.papermc.paper.world.flag.FeatureDependant { // Paper - Adventure translations & feature flag API
/**
* Provides protection against environmental damage
*/
diff --git a/src/main/java/org/bukkit/entity/EntityType.java b/src/main/java/org/bukkit/entity/EntityType.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/entity/EntityType.java
+++ b/src/main/java/org/bukkit/entity/EntityType.java
@@ -0,0 +0,0 @@ import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
-public enum EntityType implements Keyed, Translatable, net.kyori.adventure.translation.Translatable { // Paper - translatable
+public enum EntityType implements Keyed, Translatable, net.kyori.adventure.translation.Translatable, io.papermc.paper.world.flag.FeatureDependant { // Paper - translatable
// These strings MUST match the strings in nms.EntityTypes and are case sensitive.
/**
diff --git a/src/main/java/org/bukkit/generator/WorldInfo.java b/src/main/java/org/bukkit/generator/WorldInfo.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/generator/WorldInfo.java
+++ b/src/main/java/org/bukkit/generator/WorldInfo.java
@@ -0,0 +0,0 @@ import org.jetbrains.annotations.NotNull;
/**
* Holds various information of a World
*/
-public interface WorldInfo {
+public interface WorldInfo extends io.papermc.paper.world.flag.FeatureFlagSetHolder { // Paper - feature flag API
/**
* Gets the unique name of this world
diff --git a/src/main/java/org/bukkit/inventory/ItemType.java b/src/main/java/org/bukkit/inventory/ItemType.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/inventory/ItemType.java
+++ b/src/main/java/org/bukkit/inventory/ItemType.java
@@ -0,0 +0,0 @@ import org.jetbrains.annotations.Nullable;
* changes may occur. Do not use this API in plugins.
*/
@ApiStatus.Experimental // Paper - already required for registry builders
-public interface ItemType extends Keyed, Translatable, net.kyori.adventure.translation.Translatable { // Paper - add Translatable
+public interface ItemType extends Keyed, Translatable, net.kyori.adventure.translation.Translatable, io.papermc.paper.world.flag.FeatureDependant { // Paper - add Translatable & feature flag API
/**
* Typed represents a subtype of {@link ItemType}s that have a known item meta type
diff --git a/src/main/java/org/bukkit/potion/PotionEffectType.java b/src/main/java/org/bukkit/potion/PotionEffectType.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/potion/PotionEffectType.java
+++ b/src/main/java/org/bukkit/potion/PotionEffectType.java
@@ -0,0 +0,0 @@ import org.jetbrains.annotations.Nullable;
/**
* Represents a type of potion and its effect on an entity.
*/
-public abstract class PotionEffectType implements Keyed, Translatable, net.kyori.adventure.translation.Translatable { // Paper - implement Translatable
+public abstract class PotionEffectType implements Keyed, Translatable, net.kyori.adventure.translation.Translatable, io.papermc.paper.world.flag.FeatureDependant { // Paper - implement Translatable & feature flag API
private static final BiMap<Integer, PotionEffectType> ID_MAP = HashBiMap.create();
/**
diff --git a/src/main/java/org/bukkit/potion/PotionType.java b/src/main/java/org/bukkit/potion/PotionType.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/potion/PotionType.java
+++ b/src/main/java/org/bukkit/potion/PotionType.java
@@ -0,0 +0,0 @@ import org.jetbrains.annotations.Nullable;
* This enum reflects and matches each potion state that can be obtained from
* the Creative mode inventory
*/
-public enum PotionType implements Keyed {
+public enum PotionType implements Keyed, io.papermc.paper.world.flag.FeatureDependant { // Paper - feature flag API
WATER("water"),
MUNDANE("mundane"),
THICK("thick"),

View file

@ -81,7 +81,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
* @return the manager
*/
@NotNull
+ @Deprecated(forRemoval = true)
+ @Deprecated(forRemoval = true, since = "1.20")
public static DataPackManager getDataPackManager() {
return server.getDataPackManager();
}
@ -100,6 +100,18 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
// Paper end
@NotNull
diff --git a/src/main/java/org/bukkit/Material.java b/src/main/java/org/bukkit/Material.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/Material.java
+++ b/src/main/java/org/bukkit/Material.java
@@ -0,0 +0,0 @@ public enum Material implements Keyed, Translatable, net.kyori.adventure.transla
* @param world the world to check
* @return true if this material can be used in this World.
*/
+ @Deprecated(forRemoval = true, since = "1.20") // Paper
public boolean isEnabledByFeature(@NotNull World world) {
if (isItem()) {
return Bukkit.getDataPackManager().isEnabledByFeature(asItemType(), world);
diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/Server.java
@ -112,7 +124,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
* @return the manager
*/
@NotNull
+ @Deprecated(forRemoval = true) // Paper
+ @Deprecated(forRemoval = true, since = "1.20") // Paper
public DataPackManager getDataPackManager();
/**
@ -128,6 +140,18 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ io.papermc.paper.datapack.DatapackManager getDatapackManager();
// Paper end
}
diff --git a/src/main/java/org/bukkit/entity/EntityType.java b/src/main/java/org/bukkit/entity/EntityType.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/entity/EntityType.java
+++ b/src/main/java/org/bukkit/entity/EntityType.java
@@ -0,0 +0,0 @@ public enum EntityType implements Keyed, Translatable, net.kyori.adventure.trans
* @param world the world to check
* @return true if this EntityType can be used to spawn an Entity for this World.
*/
+ @Deprecated(forRemoval = true, since = "1.20") // Paper
public boolean isEnabledByFeature(@NotNull World world) {
return Bukkit.getDataPackManager().isEnabledByFeature(this, world);
}
diff --git a/src/main/java/org/bukkit/packs/DataPack.java b/src/main/java/org/bukkit/packs/DataPack.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/packs/DataPack.java
@ -138,7 +162,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
* @see <a href="https://minecraft.wiki/w/Data_pack">Minecraft wiki</a>
+ * @deprecated use {@link io.papermc.paper.datapack.Datapack}
*/
+@Deprecated(forRemoval = true) // Paper
+@Deprecated(forRemoval = true, since = "1.20") // Paper
public interface DataPack extends Keyed {
/**
@ -152,7 +176,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
* Manager of data packs.
+ * @deprecated use {@link io.papermc.paper.datapack.DatapackManager}
*/
+@Deprecated(forRemoval = true) // Paper
+@Deprecated(forRemoval = true, since = "1.20") // Paper
public interface DataPackManager {
/**

View file

@ -0,0 +1,395 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Wed, 29 May 2024 19:50:21 -0700
Subject: [PATCH] Add FeatureFlag API
diff --git a/src/main/java/io/papermc/paper/world/flag/PaperFeatureDependent.java b/src/main/java/io/papermc/paper/world/flag/PaperFeatureDependent.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/world/flag/PaperFeatureDependent.java
@@ -0,0 +0,0 @@
+package io.papermc.paper.world.flag;
+
+import java.util.Set;
+import net.minecraft.world.flag.FeatureElement;
+import org.bukkit.FeatureFlag;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.jetbrains.annotations.Unmodifiable;
+
+public interface PaperFeatureDependent extends FeatureDependant {
+
+ <M extends FeatureElement> M getHandle();
+
+ @Override
+ default @Unmodifiable @NonNull Set<FeatureFlag> requiredFeatures() {
+ return PaperFeatureFlagProviderImpl.fromNms(this.getHandle().requiredFeatures());
+ }
+}
diff --git a/src/main/java/io/papermc/paper/world/flag/PaperFeatureFlagProviderImpl.java b/src/main/java/io/papermc/paper/world/flag/PaperFeatureFlagProviderImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/world/flag/PaperFeatureFlagProviderImpl.java
@@ -0,0 +0,0 @@
+package io.papermc.paper.world.flag;
+
+import com.google.common.collect.BiMap;
+import com.google.common.collect.ImmutableBiMap;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+import net.minecraft.world.flag.FeatureElement;
+import net.minecraft.world.flag.FeatureFlagSet;
+import net.minecraft.world.flag.FeatureFlags;
+import org.bukkit.FeatureFlag;
+import org.bukkit.craftbukkit.entity.CraftEntityType;
+import org.bukkit.craftbukkit.entity.CraftEntityTypes;
+import org.bukkit.craftbukkit.potion.CraftPotionType;
+import org.bukkit.craftbukkit.util.CraftMagicNumbers;
+import org.bukkit.entity.EntityType;
+import org.bukkit.potion.PotionType;
+
+public class PaperFeatureFlagProviderImpl implements FeatureFlagProvider {
+
+ public static final BiMap<FeatureFlag, net.minecraft.world.flag.FeatureFlag> FLAGS = ImmutableBiMap.of(
+ FeatureFlag.VANILLA, FeatureFlags.VANILLA,
+ FeatureFlag.BUNDLE, FeatureFlags.BUNDLE,
+ FeatureFlag.TRADE_REBALANCE, FeatureFlags.TRADE_REBALANCE
+ );
+
+ @Override
+ public Set<FeatureFlag> requiredFeatures(final FeatureDependant dependant) {
+ final FeatureFlagSet requiredFeatures = getFeatureElement(dependant).requiredFeatures();
+ return fromNms(requiredFeatures);
+ }
+
+ public static Set<FeatureFlag> fromNms(final FeatureFlagSet flagSet) {
+ final Set<FeatureFlag> flags = new HashSet<>();
+ for (final net.minecraft.world.flag.FeatureFlag nmsFlag : FeatureFlags.REGISTRY.names.values()) {
+ if (flagSet.contains(nmsFlag)) {
+ flags.add(FLAGS.inverse().get(nmsFlag));
+ }
+ }
+ return Collections.unmodifiableSet(flags);
+ }
+
+ static FeatureElement getFeatureElement(final FeatureDependant dependant) {
+ if (dependant instanceof final EntityType entityType) {
+ // TODO remove when EntityType is server-backed
+ return CraftEntityType.bukkitToMinecraft(entityType);
+ } else if (dependant instanceof final PotionType potionType) {
+ return CraftPotionType.bukkitToMinecraft(potionType);
+ } else {
+ throw new IllegalArgumentException(dependant + " is not a valid feature dependant");
+ }
+ }
+}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftFeatureFlag.java b/src/main/java/org/bukkit/craftbukkit/CraftFeatureFlag.java
deleted file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- a/src/main/java/org/bukkit/craftbukkit/CraftFeatureFlag.java
+++ /dev/null
@@ -0,0 +0,0 @@
-package org.bukkit.craftbukkit;
-
-import java.util.HashSet;
-import java.util.Set;
-import net.minecraft.resources.ResourceLocation;
-import net.minecraft.world.flag.FeatureFlagSet;
-import net.minecraft.world.flag.FeatureFlags;
-import org.bukkit.FeatureFlag;
-import org.bukkit.NamespacedKey;
-import org.bukkit.craftbukkit.util.CraftNamespacedKey;
-import org.jetbrains.annotations.NotNull;
-
-public class CraftFeatureFlag implements FeatureFlag {
-
- private final NamespacedKey namespacedKey;
- private final net.minecraft.world.flag.FeatureFlag featureFlag;
-
- public CraftFeatureFlag(ResourceLocation minecraftKey, net.minecraft.world.flag.FeatureFlag featureFlag) {
- this.namespacedKey = CraftNamespacedKey.fromMinecraft(minecraftKey);
- this.featureFlag = featureFlag;
- }
-
- public net.minecraft.world.flag.FeatureFlag getHandle() {
- return this.featureFlag;
- }
-
- @NotNull
- @Override
- public NamespacedKey getKey() {
- return this.namespacedKey;
- }
-
- @Override
- public String toString() {
- return "CraftFeatureFlag{key=" + this.getKey() + ",keyUniverse=" + this.getHandle().universe.toString() + "}";
- }
-
- public static Set<CraftFeatureFlag> getFromNMS(FeatureFlagSet featureFlagSet) {
- Set<CraftFeatureFlag> set = new HashSet<>();
- FeatureFlags.REGISTRY.names.forEach((minecraftkey, featureflag) -> {
- if (featureFlagSet.contains(featureflag)) {
- set.add(new CraftFeatureFlag(minecraftkey, featureflag));
- }
- });
- return set;
- }
-
- public static CraftFeatureFlag getFromNMS(NamespacedKey namespacedKey) {
- return FeatureFlags.REGISTRY.names.entrySet().stream().filter(entry -> CraftNamespacedKey.fromMinecraft(entry.getKey()).equals(namespacedKey)).findFirst().map(entry -> new CraftFeatureFlag(entry.getKey(), entry.getValue())).orElse(null);
- }
-}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftRegionAccessor.java b/src/main/java/org/bukkit/craftbukkit/CraftRegionAccessor.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftRegionAccessor.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftRegionAccessor.java
@@ -0,0 +0,0 @@ public abstract class CraftRegionAccessor implements RegionAccessor {
return !this.getHandle().noCollision(aabb);
}
// Paper end
+
+ // Paper start - feature flag API
+ @Override
+ public java.util.Set<org.bukkit.FeatureFlag> getFeatureFlags() {
+ return io.papermc.paper.world.flag.PaperFeatureFlagProviderImpl.fromNms(this.getHandle().enabledFeatures());
+ }
+ // Paper end - feature flag API
}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
@@ -0,0 +0,0 @@ public class CraftWorld extends CraftRegionAccessor implements World {
return this.persistentDataContainer;
}
- @Override
- public Set<FeatureFlag> getFeatureFlags() {
- return CraftFeatureFlag.getFromNMS(this.getHandle().enabledFeatures()).stream().map(FeatureFlag.class::cast).collect(Collectors.toUnmodifiableSet());
- }
+ // Paper - replace feature flag API
public void storeBukkitValues(CompoundTag c) {
if (!this.persistentDataContainer.isEmpty()) {
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBlockType.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBlockType.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlockType.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlockType.java
@@ -0,0 +0,0 @@ import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.inventory.ItemType;
import org.jetbrains.annotations.NotNull;
-public class CraftBlockType<B extends BlockData> implements BlockType.Typed<B>, Handleable<Block> {
+public class CraftBlockType<B extends BlockData> implements BlockType.Typed<B>, Handleable<Block>, io.papermc.paper.world.flag.PaperFeatureDependent { // Paper - feature flag API
private final NamespacedKey key;
private final Block block;
diff --git a/src/main/java/org/bukkit/craftbukkit/generator/CraftWorldInfo.java b/src/main/java/org/bukkit/craftbukkit/generator/CraftWorldInfo.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/generator/CraftWorldInfo.java
+++ b/src/main/java/org/bukkit/craftbukkit/generator/CraftWorldInfo.java
@@ -0,0 +0,0 @@ public class CraftWorldInfo implements WorldInfo {
private final long seed;
private final int minHeight;
private final int maxHeight;
+ private final net.minecraft.world.flag.FeatureFlagSet enabledFeatures; // Paper - feature flag API
// Paper start
private final net.minecraft.world.level.chunk.ChunkGenerator vanillaChunkGenerator;
private final net.minecraft.core.RegistryAccess.Frozen registryAccess;
@@ -0,0 +0,0 @@ public class CraftWorldInfo implements WorldInfo {
this.seed = ((PrimaryLevelData) worldDataServer).worldGenOptions().seed();
this.minHeight = dimensionManager.minY();
this.maxHeight = dimensionManager.minY() + dimensionManager.height();
+ this.enabledFeatures = worldDataServer.enabledFeatures(); // Paper - feature flag API
}
@Override
@@ -0,0 +0,0 @@ public class CraftWorldInfo implements WorldInfo {
};
}
// Paper end
+
+ // Paper start - feature flag API
+ @Override
+ public java.util.Set<org.bukkit.FeatureFlag> getFeatureFlags() {
+ return io.papermc.paper.world.flag.PaperFeatureFlagProviderImpl.fromNms(this.enabledFeatures);
+ }
+ // Paper end - feature flag API
}
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemType.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemType.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemType.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemType.java
@@ -0,0 +0,0 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
-public class CraftItemType<M extends ItemMeta> implements ItemType.Typed<M>, Handleable<Item> {
+public class CraftItemType<M extends ItemMeta> implements ItemType.Typed<M>, Handleable<Item>, io.papermc.paper.world.flag.PaperFeatureDependent { // Paper - feature flag API
private final NamespacedKey key;
private final Item item;
diff --git a/src/main/java/org/bukkit/craftbukkit/packs/CraftDataPack.java b/src/main/java/org/bukkit/craftbukkit/packs/CraftDataPack.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/packs/CraftDataPack.java
+++ b/src/main/java/org/bukkit/craftbukkit/packs/CraftDataPack.java
@@ -0,0 +0,0 @@ import net.minecraft.util.InclusiveRange;
import org.bukkit.Bukkit;
import org.bukkit.FeatureFlag;
import org.bukkit.NamespacedKey;
-import org.bukkit.craftbukkit.CraftFeatureFlag;
+// import org.bukkit.craftbukkit.CraftFeatureFlag; // Paper - replace feature flag API
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.util.CraftChatMessage;
import org.bukkit.packs.DataPack;
@@ -0,0 +0,0 @@ public class CraftDataPack implements DataPack {
@Override
public Set<FeatureFlag> getRequestedFeatures() {
- return CraftFeatureFlag.getFromNMS(this.getHandle().getRequestedFeatures()).stream().map(FeatureFlag.class::cast).collect(Collectors.toUnmodifiableSet());
+ return io.papermc.paper.world.flag.PaperFeatureFlagProviderImpl.fromNms(this.getHandle().getRequestedFeatures()); // Paper - replace feature flag API
}
@Override
diff --git a/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionEffectType.java b/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionEffectType.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionEffectType.java
+++ b/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionEffectType.java
@@ -0,0 +0,0 @@ import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionEffectTypeCategory;
import org.jetbrains.annotations.NotNull;
-public class CraftPotionEffectType extends PotionEffectType implements Handleable<MobEffect> {
+public class CraftPotionEffectType extends PotionEffectType implements Handleable<MobEffect>, io.papermc.paper.world.flag.PaperFeatureDependent { // Paper - feature flag API
public static PotionEffectType minecraftHolderToBukkit(Holder<MobEffect> minecraft) {
return CraftPotionEffectType.minecraftToBukkit(minecraft.value());
diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
@@ -0,0 +0,0 @@ import org.bukkit.advancement.Advancement;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.block.data.BlockData;
-import org.bukkit.craftbukkit.CraftFeatureFlag;
+// import org.bukkit.craftbukkit.CraftFeatureFlag; // Paper
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.attribute.CraftAttribute;
@@ -0,0 +0,0 @@ public final class CraftMagicNumbers implements UnsafeValues {
return CraftAttribute.bukkitToMinecraft(attribute).getDescriptionId();
}
- @Override
- public FeatureFlag getFeatureFlag(NamespacedKey namespacedKey) {
- Preconditions.checkArgument(namespacedKey != null, "NamespaceKey cannot be null");
- return CraftFeatureFlag.getFromNMS(namespacedKey);
- }
+ // Paper - replace feature flag API
@Override
public PotionType.InternalPotionData getInternalPotionData(NamespacedKey namespacedKey) {
diff --git a/src/main/resources/META-INF/services/io.papermc.paper.world.flag.FeatureFlagProvider b/src/main/resources/META-INF/services/io.papermc.paper.world.flag.FeatureFlagProvider
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/resources/META-INF/services/io.papermc.paper.world.flag.FeatureFlagProvider
@@ -0,0 +1 @@
+io.papermc.paper.world.flag.PaperFeatureFlagProviderImpl
diff --git a/src/main/resources/META-INF/services/io.papermc.paper.world.flag.FeatureFlags$FeatureFlagProvider b/src/main/resources/META-INF/services/io.papermc.paper.world.flag.FeatureFlags$FeatureFlagProvider
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/resources/META-INF/services/io.papermc.paper.world.flag.FeatureFlags$FeatureFlagProvider
@@ -0,0 +1 @@
+io.papermc.paper.world.flag.PaperFeatureFlagProviderImpl
diff --git a/src/test/java/io/papermc/paper/world/flag/FeatureFlagTest.java b/src/test/java/io/papermc/paper/world/flag/FeatureFlagTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/test/java/io/papermc/paper/world/flag/FeatureFlagTest.java
@@ -0,0 +0,0 @@
+package io.papermc.paper.world.flag;
+
+import io.papermc.paper.adventure.PaperAdventure;
+import io.papermc.paper.registry.PaperRegistries;
+import io.papermc.paper.registry.RegistryAccess;
+import io.papermc.paper.registry.RegistryKey;
+import java.util.HashSet;
+import java.util.Set;
+import net.kyori.adventure.key.Key;
+import net.minecraft.core.registries.Registries;
+import net.minecraft.resources.ResourceKey;
+import net.minecraft.resources.ResourceLocation;
+import net.minecraft.world.flag.FeatureElement;
+import net.minecraft.world.flag.FeatureFlagSet;
+import net.minecraft.world.flag.FeatureFlags;
+import org.bukkit.FeatureFlag;
+import org.bukkit.Keyed;
+import org.bukkit.support.AbstractTestingBase;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.fail;
+
+class FeatureFlagTest extends AbstractTestingBase {
+
+ @Test
+ void testFeatureFlagParity() {
+ final Set<ResourceLocation> locations = new HashSet<>();
+ for (final FeatureFlag flag : FeatureFlag.ALL_FLAGS.values()) {
+ locations.add(PaperAdventure.asVanilla(flag.getKey()));
+ }
+ FeatureFlags.REGISTRY.fromNames(locations, unknown -> {
+ fail("Unknown api feature flag: " + unknown);
+ });
+
+ for (final ResourceLocation nmsFlag : allNames()) {
+ assertNotNull(FeatureFlag.ALL_FLAGS.value(Key.key(nmsFlag.toString())), "can't find api flag for " + nmsFlag);
+ }
+ }
+
+ @Test
+ void testFeatureFlagConversion() {
+ assertEquals(allNames().size(), PaperFeatureFlagProviderImpl.FLAGS.size());
+ for (final FeatureFlag featureFlag : PaperFeatureFlagProviderImpl.FLAGS.keySet()) {
+ final net.minecraft.world.flag.FeatureFlag nmsFlag = PaperFeatureFlagProviderImpl.FLAGS.get(featureFlag);
+ final ResourceLocation nmsFlagName = FeatureFlags.REGISTRY.toNames(FeatureFlagSet.of(nmsFlag)).iterator().next();
+ assertEquals(nmsFlagName.toString(), featureFlag.key().asString());
+ }
+ }
+
+ static Set<ResourceLocation> allNames() {
+ return FeatureFlags.REGISTRY.toNames(FeatureFlags.REGISTRY.allFlags());
+ }
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ static Set<RegistryKey<?>> featureFilteredRegistries() {
+ final Set<RegistryKey<?>> registryKeys = new HashSet<>();
+ for (final ResourceKey filteredRegistry : FeatureElement.FILTERED_REGISTRIES) {
+ if (filteredRegistry == Registries.MENU) continue; // not applicable atm
+ registryKeys.add(PaperRegistries.registryFromNms(filteredRegistry));
+ }
+ return registryKeys;
+ }
+
+ @MethodSource("featureFilteredRegistries")
+ @ParameterizedTest
+ <T extends Keyed> void testFeatureDependent(final RegistryKey<T> registryKey) {
+ final org.bukkit.Registry<T> registry = RegistryAccess.registryAccess().getRegistry(registryKey);
+ final T anyElement = registry.iterator().next();
+ assertInstanceOf(FeatureDependant.class, anyElement, "Registry " + registryKey + " doesn't have feature dependent elements");
+ final FeatureDependant dependant = ((FeatureDependant) anyElement);
+ assertDoesNotThrow(dependant::requiredFeatures, "Failed to get required features for " + anyElement + " in " + registryKey);
+ }
+}

View file

@ -100,7 +100,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ private final net.minecraft.core.RegistryAccess.Frozen registryAccess;
- public CraftWorldInfo(ServerLevelData worldDataServer, LevelStorageSource.LevelStorageAccess session, World.Environment environment, DimensionType dimensionManager) {
+ public CraftWorldInfo(ServerLevelData worldDataServer, LevelStorageSource.LevelStorageAccess session, World.Environment environment, DimensionType dimensionManager, net.minecraft.world.level.chunk.ChunkGenerator chunkGenerator, net.minecraft.core.RegistryAccess.Frozen registryAccess) {
+ public CraftWorldInfo(PrimaryLevelData worldDataServer, LevelStorageSource.LevelStorageAccess session, World.Environment environment, DimensionType dimensionManager, net.minecraft.world.level.chunk.ChunkGenerator chunkGenerator, net.minecraft.core.RegistryAccess.Frozen registryAccess) {
+ this.registryAccess = registryAccess;
+ this.vanillaChunkGenerator = chunkGenerator;
+ // Paper end