Wrong attempt at Tag lifecycle
This commit is contained in:
parent
6a315742d9
commit
227caac7f0
2 changed files with 125 additions and 67 deletions
|
@ -1,428 +0,0 @@
|
|||
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..c6fd2e8a570ac474dd1751929137280c52d79e78
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/io/papermc/paper/world/flag/PaperFeatureDependent.java
|
||||
@@ -0,0 +1,17 @@
|
||||
+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..d58b650fcafd04d00a92005ca8576d314535ce32
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/io/papermc/paper/world/flag/PaperFeatureFlagProviderImpl.java
|
||||
@@ -0,0 +1,53 @@
|
||||
+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 da90e5c84a0cc4505a5fff0ed278a905978c6ce2..0000000000000000000000000000000000000000
|
||||
--- a/src/main/java/org/bukkit/craftbukkit/CraftFeatureFlag.java
|
||||
+++ /dev/null
|
||||
@@ -1,51 +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 284234fcdd15c4c7a4567c7c887d47bf0b7967f4..c9ecec5da937bc5458f69736b68ff6ae50aa5ebc 100644
|
||||
--- a/src/main/java/org/bukkit/craftbukkit/CraftRegionAccessor.java
|
||||
+++ b/src/main/java/org/bukkit/craftbukkit/CraftRegionAccessor.java
|
||||
@@ -558,4 +558,11 @@ 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 74feb45c3199308652448c8448eb87fb8fbf6f11..7fc52f9f4f2e2cd8ea3abdf6c6f5d1f679779c47 100644
|
||||
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
|
||||
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
|
||||
@@ -2374,10 +2374,7 @@ 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 785d3fe4929e008d4150f3ecab258fd5b6d7cdaf..978e602c38d66c1ac219ffdfe82f9fc777a6ad26 100644
|
||||
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlockType.java
|
||||
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlockType.java
|
||||
@@ -33,7 +33,7 @@ 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 c81455a4ee9a3185f125ebf8cec325f4ed2e501d..8d962b055bd6574df7eb5510dd0efd67f579d84a 100644
|
||||
--- a/src/main/java/org/bukkit/craftbukkit/generator/CraftWorldInfo.java
|
||||
+++ b/src/main/java/org/bukkit/craftbukkit/generator/CraftWorldInfo.java
|
||||
@@ -17,6 +17,7 @@ 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;
|
||||
@@ -31,6 +32,7 @@ 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
|
||||
@@ -92,4 +94,11 @@ 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 450c63c31d2f5d056d989aa00452231f50c8224d..b9d9f1df2720c301915c8b07c0bdc12970128324 100644
|
||||
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemType.java
|
||||
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemType.java
|
||||
@@ -36,7 +36,7 @@ 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/inventory/CraftMenuType.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMenuType.java
|
||||
index d658634ea4468c9dbfb29bc12282441c96358566..fbde94b72063da69cc1a1f7934e069c6c8c0f804 100644
|
||||
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMenuType.java
|
||||
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMenuType.java
|
||||
@@ -18,7 +18,7 @@ import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.inventory.InventoryView;
|
||||
import org.bukkit.inventory.MenuType;
|
||||
|
||||
-public class CraftMenuType<V extends InventoryView> implements MenuType.Typed<V>, Handleable<net.minecraft.world.inventory.MenuType<?>> {
|
||||
+public class CraftMenuType<V extends InventoryView> implements MenuType.Typed<V>, Handleable<net.minecraft.world.inventory.MenuType<?>>, io.papermc.paper.world.flag.PaperFeatureDependent { // Paper - make FeatureDependant
|
||||
|
||||
private final NamespacedKey key;
|
||||
private final net.minecraft.world.inventory.MenuType<?> handle;
|
||||
diff --git a/src/main/java/org/bukkit/craftbukkit/packs/CraftDataPack.java b/src/main/java/org/bukkit/craftbukkit/packs/CraftDataPack.java
|
||||
index 9525a7d4602b9b98dcc5e5e60dbcb628a656ae53..a0c0ad832726dcf9c8c25c1cfce2a6ddf770bf0f 100644
|
||||
--- a/src/main/java/org/bukkit/craftbukkit/packs/CraftDataPack.java
|
||||
+++ b/src/main/java/org/bukkit/craftbukkit/packs/CraftDataPack.java
|
||||
@@ -11,7 +11,7 @@ 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;
|
||||
@@ -98,7 +98,7 @@ 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 6cf790c9fa23ea313423fdaeb7c181bf530828c6..0bcb9df1103050441f8922a688b163dc97c04591 100644
|
||||
--- a/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionEffectType.java
|
||||
+++ b/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionEffectType.java
|
||||
@@ -13,7 +13,7 @@ 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 6adc18c40d5d62e2ebc8deec197cec630a366937..8b2dbdfcdc4e98602f6bfd48d2c53840730f4691 100644
|
||||
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
||||
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
||||
@@ -47,7 +47,7 @@ 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;
|
||||
@@ -461,11 +461,7 @@ 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..c3e6b96013f6dd0b784bd867196552d97f3d8685
|
||||
--- /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..c3e6b96013f6dd0b784bd867196552d97f3d8685
|
||||
--- /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..2e07ff04faa5999d14d29e44377deb4e483044a0
|
||||
--- /dev/null
|
||||
+++ b/src/test/java/io/papermc/paper/world/flag/FeatureFlagTest.java
|
||||
@@ -0,0 +1,99 @@
|
||||
+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 io.papermc.paper.registry.entry.RegistryEntry;
|
||||
+import java.util.HashSet;
|
||||
+import java.util.Set;
|
||||
+import java.util.stream.Stream;
|
||||
+import net.kyori.adventure.key.Key;
|
||||
+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.RegistryHelper;
|
||||
+import org.bukkit.support.environment.AllFeatures;
|
||||
+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.assertFalse;
|
||||
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
+import static org.junit.jupiter.api.Assertions.fail;
|
||||
+
|
||||
+@AllFeatures
|
||||
+class FeatureFlagTest {
|
||||
+
|
||||
+ @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) {
|
||||
+ registryKeys.add(PaperRegistries.registryFromNms(filteredRegistry));
|
||||
+ }
|
||||
+ return registryKeys;
|
||||
+ }
|
||||
+
|
||||
+ @MethodSource("featureFilteredRegistries")
|
||||
+ @ParameterizedTest
|
||||
+ <T extends Keyed> void testApiImplementsFeatureDependant(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);
|
||||
+ }
|
||||
+
|
||||
+ static Stream<RegistryKey<?>> nonFeatureFilteredRegistries() {
|
||||
+ return RegistryHelper.getRegistry().registries().filter(r -> {
|
||||
+ final RegistryEntry<?, ?> entry = PaperRegistries.getEntry(r.key());
|
||||
+ // has an API registry and isn't a filtered registry
|
||||
+ return entry != null && !FeatureElement.FILTERED_REGISTRIES.contains(r.key());
|
||||
+ }).map(r -> PaperRegistries.getEntry(r.key()).apiKey());
|
||||
+ }
|
||||
+
|
||||
+
|
||||
+ @MethodSource("nonFeatureFilteredRegistries")
|
||||
+ @ParameterizedTest
|
||||
+ <T extends Keyed> void testApiDoesntImplementFeatureDependant(final RegistryKey<T> registryKey) {
|
||||
+ final org.bukkit.Registry<T> registry = RegistryAccess.registryAccess().getRegistry(registryKey);
|
||||
+ final T anyElement = registry.iterator().next();
|
||||
+ assertFalse(anyElement instanceof FeatureDependant, "Registry " + registryKey + " has feature dependent elements");
|
||||
+ }
|
||||
+}
|
|
@ -1,537 +0,0 @@
|
|||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||
From: Jake Potrebic <jake.m.potrebic@gmail.com>
|
||||
Date: Thu, 20 Jun 2024 09:40:57 -0700
|
||||
Subject: [PATCH] Tag Lifecycle Events
|
||||
|
||||
== AT ==
|
||||
public net/minecraft/tags/TagEntry id
|
||||
public net/minecraft/tags/TagEntry tag
|
||||
public net/minecraft/tags/TagEntry required
|
||||
|
||||
diff --git a/src/main/java/io/papermc/paper/plugin/lifecycle/event/types/LifecycleEventTypeProviderImpl.java b/src/main/java/io/papermc/paper/plugin/lifecycle/event/types/LifecycleEventTypeProviderImpl.java
|
||||
index b11346e04bb16c3238f32deb87dbd680e261d4d2..996bac2fab8fb927d184fb62e043454e877727a6 100644
|
||||
--- a/src/main/java/io/papermc/paper/plugin/lifecycle/event/types/LifecycleEventTypeProviderImpl.java
|
||||
+++ b/src/main/java/io/papermc/paper/plugin/lifecycle/event/types/LifecycleEventTypeProviderImpl.java
|
||||
@@ -13,6 +13,8 @@ public final class LifecycleEventTypeProviderImpl implements LifecycleEventTypeP
|
||||
return (LifecycleEventTypeProviderImpl) LifecycleEventTypeProvider.provider();
|
||||
}
|
||||
|
||||
+ private final PaperTagEventTypeProvider provider = new PaperTagEventTypeProvider();
|
||||
+
|
||||
@Override
|
||||
public <O extends LifecycleEventOwner, E extends LifecycleEvent> LifecycleEventType.Monitorable<O, E> monitor(final String name, final Class<? extends O> ownerType) {
|
||||
return new MonitorableLifecycleEventType<>(name, ownerType);
|
||||
@@ -22,4 +24,9 @@ public final class LifecycleEventTypeProviderImpl implements LifecycleEventTypeP
|
||||
public <O extends LifecycleEventOwner, E extends LifecycleEvent> LifecycleEventType.Prioritizable<O, E> prioritized(final String name, final Class<? extends O> ownerType) {
|
||||
return new PrioritizableLifecycleEventType.Simple<>(name, ownerType);
|
||||
}
|
||||
+
|
||||
+ @Override
|
||||
+ public PaperTagEventTypeProvider tagProvider() {
|
||||
+ return this.provider;
|
||||
+ }
|
||||
}
|
||||
diff --git a/src/main/java/io/papermc/paper/plugin/lifecycle/event/types/PaperTagEventTypeProvider.java b/src/main/java/io/papermc/paper/plugin/lifecycle/event/types/PaperTagEventTypeProvider.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..1cf4a3caa8d630e95eb569eef2cd577bba1a97a9
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/io/papermc/paper/plugin/lifecycle/event/types/PaperTagEventTypeProvider.java
|
||||
@@ -0,0 +1,24 @@
|
||||
+package io.papermc.paper.plugin.lifecycle.event.types;
|
||||
+
|
||||
+import io.papermc.paper.plugin.bootstrap.BootstrapContext;
|
||||
+import io.papermc.paper.plugin.lifecycle.event.registrar.ReloadableRegistrarEvent;
|
||||
+import io.papermc.paper.registry.RegistryKey;
|
||||
+import io.papermc.paper.tag.PaperTagListenerManager;
|
||||
+import io.papermc.paper.tag.PostFlattenTagRegistrar;
|
||||
+import io.papermc.paper.tag.PreFlattenTagRegistrar;
|
||||
+import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
+import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
+
|
||||
+@DefaultQualifier(NonNull.class)
|
||||
+public class PaperTagEventTypeProvider implements TagEventTypeProvider {
|
||||
+
|
||||
+ @Override
|
||||
+ public <T> PrioritizableLifecycleEventType.Simple<BootstrapContext, ReloadableRegistrarEvent<PreFlattenTagRegistrar<T>>> preFlatten(final RegistryKey<T> registryKey) {
|
||||
+ return PaperTagListenerManager.INSTANCE.getPreFlattenType(registryKey);
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public <T> PrioritizableLifecycleEventType.Simple<BootstrapContext, ReloadableRegistrarEvent<PostFlattenTagRegistrar<T>>> postFlatten(final RegistryKey<T> registryKey) {
|
||||
+ return PaperTagListenerManager.INSTANCE.getPostFlattenType(registryKey);
|
||||
+ }
|
||||
+}
|
||||
diff --git a/src/main/java/io/papermc/paper/tag/PaperPostFlattenTagRegistrar.java b/src/main/java/io/papermc/paper/tag/PaperPostFlattenTagRegistrar.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..cc37618cbbad7f21d65c7753f4dd4416042d2146
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/io/papermc/paper/tag/PaperPostFlattenTagRegistrar.java
|
||||
@@ -0,0 +1,118 @@
|
||||
+package io.papermc.paper.tag;
|
||||
+
|
||||
+import com.google.common.collect.Collections2;
|
||||
+import com.google.common.collect.ImmutableMap;
|
||||
+import io.papermc.paper.adventure.PaperAdventure;
|
||||
+import io.papermc.paper.plugin.bootstrap.BootstrapContext;
|
||||
+import io.papermc.paper.plugin.lifecycle.event.registrar.PaperRegistrar;
|
||||
+import io.papermc.paper.registry.RegistryKey;
|
||||
+import io.papermc.paper.registry.TypedKey;
|
||||
+import io.papermc.paper.registry.tag.TagKey;
|
||||
+import java.util.ArrayList;
|
||||
+import java.util.Collection;
|
||||
+import java.util.Collections;
|
||||
+import java.util.List;
|
||||
+import java.util.Map;
|
||||
+import java.util.NoSuchElementException;
|
||||
+import java.util.Optional;
|
||||
+import java.util.function.Function;
|
||||
+import net.minecraft.resources.ResourceLocation;
|
||||
+import org.bukkit.craftbukkit.util.CraftNamespacedKey;
|
||||
+import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
+import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
+import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
+
|
||||
+@SuppressWarnings("BoundedWildcard")
|
||||
+@DefaultQualifier(NonNull.class)
|
||||
+public class PaperPostFlattenTagRegistrar<M, T> implements PaperRegistrar<BootstrapContext>, PostFlattenTagRegistrar<T> {
|
||||
+
|
||||
+ public final Map<ResourceLocation, Collection<M>> tags;
|
||||
+ private final Function<ResourceLocation, Optional<? extends M>> fromIdConverter;
|
||||
+ private final Function<M, ResourceLocation> toIdConverter;
|
||||
+ private final RegistryKey<T> registryKey;
|
||||
+
|
||||
+ public PaperPostFlattenTagRegistrar(
|
||||
+ final Map<ResourceLocation, Collection<M>> tags,
|
||||
+ final TagEventConfig<M, T> config
|
||||
+ ) {
|
||||
+ this.tags = tags;
|
||||
+ this.fromIdConverter = config.fromIdConverter();
|
||||
+ this.toIdConverter = config.toIdConverter();
|
||||
+ this.registryKey = config.apiRegistryKey();
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public void setCurrentContext(final @Nullable BootstrapContext owner) {
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public RegistryKey<T> registryKey() {
|
||||
+ return this.registryKey;
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public Map<TagKey<T>, Collection<TypedKey<T>>> getAllTags() {
|
||||
+ final ImmutableMap.Builder<TagKey<T>, Collection<TypedKey<T>>> tags = ImmutableMap.builderWithExpectedSize(this.tags.size());
|
||||
+ for (final Map.Entry<ResourceLocation, Collection<M>> entry : this.tags.entrySet()) {
|
||||
+ final TagKey<T> key = TagKey.create(this.registryKey, CraftNamespacedKey.fromMinecraft(entry.getKey()));
|
||||
+ tags.put(key, this.convert(entry.getValue()));
|
||||
+ }
|
||||
+ return tags.build();
|
||||
+ }
|
||||
+
|
||||
+ private Collection<TypedKey<T>> convert(final Collection<M> nms) {
|
||||
+ return Collections.unmodifiableCollection(
|
||||
+ Collections2.transform(nms, m -> this.convert(this.toIdConverter.apply(m)))
|
||||
+ );
|
||||
+ }
|
||||
+
|
||||
+ private TypedKey<T> convert(final ResourceLocation location) {
|
||||
+ return TypedKey.create(this.registryKey, CraftNamespacedKey.fromMinecraft(location));
|
||||
+ }
|
||||
+
|
||||
+ private M convert(final TypedKey<T> key) {
|
||||
+ final Optional<? extends M> optional = this.fromIdConverter.apply(PaperAdventure.asVanilla(key.key()));
|
||||
+ if (optional.isEmpty()) {
|
||||
+ throw new IllegalArgumentException(key + " doesn't exist");
|
||||
+ }
|
||||
+ return optional.get();
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public boolean hasTag(final TagKey<T> tagKey) {
|
||||
+ return this.tags.containsKey(PaperAdventure.asVanilla(tagKey.key()));
|
||||
+ }
|
||||
+
|
||||
+ private Collection<M> getNmsTag(final TagKey<T> tagKey, final boolean create) {
|
||||
+ final ResourceLocation vanillaKey = PaperAdventure.asVanilla(tagKey.key());
|
||||
+ Collection<M> tag = this.tags.get(vanillaKey);
|
||||
+ if (tag == null) {
|
||||
+ if (create) {
|
||||
+ tag = this.tags.computeIfAbsent(vanillaKey, k -> new ArrayList<>());
|
||||
+ } else {
|
||||
+ throw new NoSuchElementException("Tag " + tagKey + " is not present");
|
||||
+ }
|
||||
+ }
|
||||
+ return tag;
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public Collection<TypedKey<T>> getTag(final TagKey<T> tagKey) {
|
||||
+ return this.convert(this.getNmsTag(tagKey, false));
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public void addToTag(final TagKey<T> tagKey, final Collection<TypedKey<T>> values) {
|
||||
+ final Collection<M> nmsTag = new ArrayList<>(this.getNmsTag(tagKey, true));
|
||||
+ for (final TypedKey<T> key : values) {
|
||||
+ nmsTag.add(this.convert(key));
|
||||
+ }
|
||||
+ this.tags.put(PaperAdventure.asVanilla(tagKey.key()), nmsTag);
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public void setTag(final TagKey<T> tagKey, final Collection<TypedKey<T>> values) {
|
||||
+ final List<M> newList = List.copyOf(Collections2.transform(values, this::convert));
|
||||
+ this.tags.put(PaperAdventure.asVanilla(tagKey.key()), newList);
|
||||
+ }
|
||||
+}
|
||||
diff --git a/src/main/java/io/papermc/paper/tag/PaperPreFlattenTagRegistrar.java b/src/main/java/io/papermc/paper/tag/PaperPreFlattenTagRegistrar.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..44111b55eaa6d1cc93e2c556b23bb5c97953caac
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/io/papermc/paper/tag/PaperPreFlattenTagRegistrar.java
|
||||
@@ -0,0 +1,128 @@
|
||||
+package io.papermc.paper.tag;
|
||||
+
|
||||
+import com.google.common.base.Preconditions;
|
||||
+import com.google.common.collect.Collections2;
|
||||
+import com.google.common.collect.ImmutableMap;
|
||||
+import com.google.common.collect.Lists;
|
||||
+import io.papermc.paper.adventure.PaperAdventure;
|
||||
+import io.papermc.paper.plugin.bootstrap.BootstrapContext;
|
||||
+import io.papermc.paper.plugin.lifecycle.event.registrar.PaperRegistrar;
|
||||
+import io.papermc.paper.registry.RegistryKey;
|
||||
+import io.papermc.paper.registry.tag.TagKey;
|
||||
+import java.util.ArrayList;
|
||||
+import java.util.Collection;
|
||||
+import java.util.Collections;
|
||||
+import java.util.HashMap;
|
||||
+import java.util.List;
|
||||
+import java.util.Map;
|
||||
+import java.util.NoSuchElementException;
|
||||
+import net.minecraft.resources.ResourceLocation;
|
||||
+import net.minecraft.tags.TagLoader;
|
||||
+import org.bukkit.craftbukkit.util.CraftNamespacedKey;
|
||||
+import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
+import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
+import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
+
|
||||
+@SuppressWarnings("BoundedWildcard")
|
||||
+@DefaultQualifier(NonNull.class)
|
||||
+public class PaperPreFlattenTagRegistrar<T> implements PaperRegistrar<BootstrapContext>, PreFlattenTagRegistrar<T> {
|
||||
+
|
||||
+ public final Map<ResourceLocation, List<TagLoader.EntryWithSource>> tags;
|
||||
+ private final RegistryKey<T> registryKey;
|
||||
+
|
||||
+ private @Nullable BootstrapContext owner;
|
||||
+
|
||||
+ public PaperPreFlattenTagRegistrar(
|
||||
+ final Map<ResourceLocation, List<TagLoader.EntryWithSource>> tags,
|
||||
+ final TagEventConfig<?, T> config
|
||||
+ ) {
|
||||
+ this.tags = new HashMap<>(tags);
|
||||
+ this.registryKey = config.apiRegistryKey();
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public void setCurrentContext(final @Nullable BootstrapContext owner) {
|
||||
+ this.owner = owner;
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public RegistryKey<T> registryKey() {
|
||||
+ return this.registryKey;
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public Map<TagKey<T>, Collection<TagEntry<T>>> getAllTags() {
|
||||
+ final ImmutableMap.Builder<TagKey<T>, Collection<io.papermc.paper.tag.TagEntry<T>>> builder = ImmutableMap.builderWithExpectedSize(this.tags.size());
|
||||
+ for (final Map.Entry<ResourceLocation, List<TagLoader.EntryWithSource>> entry : this.tags.entrySet()) {
|
||||
+ final TagKey<T> key = TagKey.create(this.registryKey, CraftNamespacedKey.fromMinecraft(entry.getKey()));
|
||||
+ builder.put(key, convert(entry.getValue()));
|
||||
+ }
|
||||
+ return builder.build();
|
||||
+ }
|
||||
+
|
||||
+ private static <T> List<io.papermc.paper.tag.TagEntry<T>> convert(final List<TagLoader.EntryWithSource> nmsEntries) {
|
||||
+ return Collections.unmodifiableList(Lists.transform(nmsEntries, PaperPreFlattenTagRegistrar::convert));
|
||||
+ }
|
||||
+
|
||||
+ private static <T> io.papermc.paper.tag.TagEntry<T> convert(final TagLoader.EntryWithSource nmsEntry) {
|
||||
+ return new TagEntryImpl<>(CraftNamespacedKey.fromMinecraft(nmsEntry.entry().id), nmsEntry.entry().tag, nmsEntry.entry().required);
|
||||
+ }
|
||||
+
|
||||
+ private TagLoader.EntryWithSource convert(final TagEntry<T> entry) {
|
||||
+ Preconditions.checkState(this.owner != null, "Owner is not set");
|
||||
+ final ResourceLocation vanilla = PaperAdventure.asVanilla(entry.key());
|
||||
+ final net.minecraft.tags.TagEntry nmsEntry;
|
||||
+ if (entry.isTag()) {
|
||||
+ if (entry.isRequired()) {
|
||||
+ nmsEntry = net.minecraft.tags.TagEntry.tag(vanilla);
|
||||
+ } else {
|
||||
+ nmsEntry = net.minecraft.tags.TagEntry.optionalTag(vanilla);
|
||||
+ }
|
||||
+ } else {
|
||||
+ if (entry.isRequired()) {
|
||||
+ nmsEntry = net.minecraft.tags.TagEntry.element(vanilla);
|
||||
+ } else {
|
||||
+ nmsEntry = net.minecraft.tags.TagEntry.optionalElement(vanilla);
|
||||
+ }
|
||||
+ }
|
||||
+ return new TagLoader.EntryWithSource(nmsEntry, this.owner.getPluginMeta().getDisplayName());
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public boolean hasTag(final TagKey<T> tagKey) {
|
||||
+ return this.tags.containsKey(PaperAdventure.asVanilla(tagKey.key()));
|
||||
+ }
|
||||
+
|
||||
+ private List<TagLoader.EntryWithSource> getNmsTag(final TagKey<T> tagKey, boolean create) {
|
||||
+ final ResourceLocation vanillaKey = PaperAdventure.asVanilla(tagKey.key());
|
||||
+ List<TagLoader.EntryWithSource> tag = this.tags.get(vanillaKey);
|
||||
+ if (tag == null) {
|
||||
+ if (create) {
|
||||
+ tag = this.tags.computeIfAbsent(vanillaKey, k -> new ArrayList<>());
|
||||
+ } else {
|
||||
+ throw new NoSuchElementException("Tag " + tagKey + " is not present");
|
||||
+ }
|
||||
+ }
|
||||
+ return tag;
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public List<TagEntry<T>> getTag(final TagKey<T> tagKey) {
|
||||
+ return convert(this.getNmsTag(tagKey, false));
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public void addToTag(final TagKey<T> tagKey, final Collection<TagEntry<T>> entries) {
|
||||
+ final List<TagLoader.EntryWithSource> nmsTag = new ArrayList<>(this.getNmsTag(tagKey, true));
|
||||
+ for (final TagEntry<T> tagEntry : entries) {
|
||||
+ nmsTag.add(this.convert(tagEntry));
|
||||
+ }
|
||||
+ this.tags.put(PaperAdventure.asVanilla(tagKey.key()), nmsTag);
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public void setTag(final TagKey<T> tagKey, final Collection<TagEntry<T>> entries) {
|
||||
+ final List<TagLoader.EntryWithSource> newList = List.copyOf(Collections2.transform(entries, this::convert));
|
||||
+ this.tags.put(PaperAdventure.asVanilla(tagKey.key()), newList);
|
||||
+ }
|
||||
+}
|
||||
diff --git a/src/main/java/io/papermc/paper/tag/PaperTagListenerManager.java b/src/main/java/io/papermc/paper/tag/PaperTagListenerManager.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..f3e4019f9d55d3cce34248a806bf0ccb37c8e7b1
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/io/papermc/paper/tag/PaperTagListenerManager.java
|
||||
@@ -0,0 +1,105 @@
|
||||
+package io.papermc.paper.tag;
|
||||
+
|
||||
+import io.papermc.paper.plugin.bootstrap.BootstrapContext;
|
||||
+import io.papermc.paper.plugin.lifecycle.event.LifecycleEventRunner;
|
||||
+import io.papermc.paper.plugin.lifecycle.event.registrar.ReloadableRegistrarEvent;
|
||||
+import io.papermc.paper.plugin.lifecycle.event.types.AbstractLifecycleEventType;
|
||||
+import io.papermc.paper.plugin.lifecycle.event.types.PrioritizableLifecycleEventType;
|
||||
+import io.papermc.paper.registry.PaperRegistries;
|
||||
+import io.papermc.paper.registry.RegistryKey;
|
||||
+import io.papermc.paper.registry.event.RegistryEventMap;
|
||||
+import java.util.Collection;
|
||||
+import java.util.List;
|
||||
+import java.util.Map;
|
||||
+import net.minecraft.core.Holder;
|
||||
+import net.minecraft.core.Registry;
|
||||
+import net.minecraft.resources.ResourceLocation;
|
||||
+import net.minecraft.tags.TagLoader;
|
||||
+import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
+import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
+import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
+
|
||||
+@DefaultQualifier(NonNull.class)
|
||||
+public class PaperTagListenerManager {
|
||||
+
|
||||
+ public static final String PRE_FLATTEN_EVENT_NAME = "pre-flatten";
|
||||
+ public static final String POST_FLATTEN_EVENT_NAME = "post-flatten";
|
||||
+
|
||||
+ public static final PaperTagListenerManager INSTANCE = new PaperTagListenerManager();
|
||||
+
|
||||
+ public final RegistryEventMap preFlatten = new RegistryEventMap(PRE_FLATTEN_EVENT_NAME);
|
||||
+ public final RegistryEventMap postFlatten = new RegistryEventMap(POST_FLATTEN_EVENT_NAME);
|
||||
+
|
||||
+ private PaperTagListenerManager() {
|
||||
+ }
|
||||
+
|
||||
+ public <A> Map<ResourceLocation, List<TagLoader.EntryWithSource>> firePreFlattenEvent(
|
||||
+ final Map<ResourceLocation, List<TagLoader.EntryWithSource>> initial,
|
||||
+ final @Nullable TagEventConfig<?, A> config
|
||||
+ ) {
|
||||
+ if (config == null || config.preFlatten() == null || !config.preFlatten().hasHandlers()) {
|
||||
+ return initial;
|
||||
+ }
|
||||
+ final PaperPreFlattenTagRegistrar<A> registrar = new PaperPreFlattenTagRegistrar<>(initial, config);
|
||||
+ LifecycleEventRunner.INSTANCE.callReloadableRegistrarEvent(
|
||||
+ config.preFlatten(),
|
||||
+ registrar,
|
||||
+ BootstrapContext.class,
|
||||
+ config.cause()
|
||||
+ );
|
||||
+ return Map.copyOf(registrar.tags);
|
||||
+ }
|
||||
+
|
||||
+ public <M, A> Map<ResourceLocation, Collection<M>> firePostFlattenEvent(
|
||||
+ final Map<ResourceLocation, Collection<M>> initial,
|
||||
+ final @Nullable TagEventConfig<M, A> config
|
||||
+ ) {
|
||||
+ if (config == null || config.postFlatten() == null || !config.postFlatten().hasHandlers()) {
|
||||
+ return initial;
|
||||
+ }
|
||||
+ final PaperPostFlattenTagRegistrar<M, A> registrar = new PaperPostFlattenTagRegistrar<>(initial, config);
|
||||
+ LifecycleEventRunner.INSTANCE.callReloadableRegistrarEvent(
|
||||
+ config.postFlatten(),
|
||||
+ registrar,
|
||||
+ BootstrapContext.class,
|
||||
+ config.cause()
|
||||
+ );
|
||||
+ return Map.copyOf(registrar.tags);
|
||||
+ }
|
||||
+
|
||||
+ public <M, B> @Nullable TagEventConfig<Holder<M>, B> createEventConfig(final Registry<M> registry, final ReloadableRegistrarEvent.Cause cause) {
|
||||
+ if (PaperRegistries.getEntry(registry.key()) == null) {
|
||||
+ // TODO probably should be able to modify every registry
|
||||
+ return null;
|
||||
+ }
|
||||
+ final RegistryKey<B> registryKey = PaperRegistries.registryFromNms(registry.key());
|
||||
+ @Nullable AbstractLifecycleEventType<BootstrapContext, ReloadableRegistrarEvent<PreFlattenTagRegistrar<B>>, ?> preFlatten = null;
|
||||
+ if (this.preFlatten.hasHandlers(registryKey)) {
|
||||
+ preFlatten = (AbstractLifecycleEventType<BootstrapContext, ReloadableRegistrarEvent<PreFlattenTagRegistrar<B>>, ?>) this.preFlatten.<B, ReloadableRegistrarEvent<PreFlattenTagRegistrar<B>>>getEventType(registryKey);
|
||||
+ }
|
||||
+ @Nullable AbstractLifecycleEventType<BootstrapContext, ReloadableRegistrarEvent<PostFlattenTagRegistrar<B>>, ?> postFlatten = null;
|
||||
+ if (this.postFlatten.hasHandlers(registryKey)) {
|
||||
+ postFlatten = (AbstractLifecycleEventType<BootstrapContext, ReloadableRegistrarEvent<PostFlattenTagRegistrar<B>>, ?>) this.postFlatten.<B, ReloadableRegistrarEvent<PostFlattenTagRegistrar<B>>>getEventType(registryKey);
|
||||
+ }
|
||||
+ return new TagEventConfig<>(
|
||||
+ preFlatten,
|
||||
+ postFlatten,
|
||||
+ cause,
|
||||
+ registry::getHolder,
|
||||
+ h -> ((Holder.Reference<M>) h).key().location(),
|
||||
+ PaperRegistries.registryFromNms(registry.key())
|
||||
+ );
|
||||
+ }
|
||||
+
|
||||
+ public <T> PrioritizableLifecycleEventType.Simple<BootstrapContext, ReloadableRegistrarEvent<PreFlattenTagRegistrar<T>>> getPreFlattenType(final RegistryKey<T> registryKey) {
|
||||
+ return this.preFlatten.getOrCreate(registryKey, (ignored, name) -> {
|
||||
+ return new PrioritizableLifecycleEventType.Simple<>(name, BootstrapContext.class);
|
||||
+ });
|
||||
+ }
|
||||
+
|
||||
+ public <T> PrioritizableLifecycleEventType.Simple<BootstrapContext, ReloadableRegistrarEvent<PostFlattenTagRegistrar<T>>> getPostFlattenType(final RegistryKey<T> registryKey) {
|
||||
+ return this.postFlatten.getOrCreate(registryKey, (ignored, name) -> {
|
||||
+ return new PrioritizableLifecycleEventType.Simple<>(name, BootstrapContext.class);
|
||||
+ });
|
||||
+ }
|
||||
+}
|
||||
diff --git a/src/main/java/io/papermc/paper/tag/TagEventConfig.java b/src/main/java/io/papermc/paper/tag/TagEventConfig.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..d6d4bfc6f45d646afeace422a038c670e10aaf80
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/io/papermc/paper/tag/TagEventConfig.java
|
||||
@@ -0,0 +1,23 @@
|
||||
+package io.papermc.paper.tag;
|
||||
+
|
||||
+import io.papermc.paper.plugin.bootstrap.BootstrapContext;
|
||||
+import io.papermc.paper.plugin.lifecycle.event.registrar.ReloadableRegistrarEvent;
|
||||
+import io.papermc.paper.plugin.lifecycle.event.types.AbstractLifecycleEventType;
|
||||
+import io.papermc.paper.registry.RegistryKey;
|
||||
+import java.util.Optional;
|
||||
+import java.util.function.Function;
|
||||
+import net.minecraft.resources.ResourceLocation;
|
||||
+import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
+import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
+import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
+
|
||||
+@DefaultQualifier(NonNull.class)
|
||||
+public record TagEventConfig<M, A>(
|
||||
+ @Nullable AbstractLifecycleEventType<BootstrapContext, ? extends ReloadableRegistrarEvent<PreFlattenTagRegistrar<A>>, ?> preFlatten,
|
||||
+ @Nullable AbstractLifecycleEventType<BootstrapContext, ? extends ReloadableRegistrarEvent<PostFlattenTagRegistrar<A>>, ?> postFlatten,
|
||||
+ ReloadableRegistrarEvent.Cause cause,
|
||||
+ Function<ResourceLocation, Optional<? extends M>> fromIdConverter,
|
||||
+ Function<M, ResourceLocation> toIdConverter,
|
||||
+ RegistryKey<A> apiRegistryKey
|
||||
+) {
|
||||
+}
|
||||
diff --git a/src/main/java/net/minecraft/server/ServerFunctionLibrary.java b/src/main/java/net/minecraft/server/ServerFunctionLibrary.java
|
||||
index 8ae2ca2435d84fee930d2931e45ad440245cbe0f..9fff5ca1e9707e4e6be5ecb92be936332d24bb96 100644
|
||||
--- a/src/main/java/net/minecraft/server/ServerFunctionLibrary.java
|
||||
+++ b/src/main/java/net/minecraft/server/ServerFunctionLibrary.java
|
||||
@@ -118,7 +118,7 @@ public class ServerFunctionLibrary implements PreparableReloadListener {
|
||||
return null;
|
||||
}).join());
|
||||
this.functions = builder.build();
|
||||
- this.tags = this.tagsLoader.build((Map<ResourceLocation, List<TagLoader.EntryWithSource>>)intermediate.getFirst());
|
||||
+ this.tags = this.tagsLoader.build((Map<ResourceLocation, List<TagLoader.EntryWithSource>>)intermediate.getFirst(), null); // Paper - command function tags are not implemented yet
|
||||
},
|
||||
applyExecutor
|
||||
);
|
||||
diff --git a/src/main/java/net/minecraft/tags/TagLoader.java b/src/main/java/net/minecraft/tags/TagLoader.java
|
||||
index b47afb0ce8d9517ac7ee9c651c160f99d70f8a98..0bde4f7ae7d1897c630513261d1d5fb75a762634 100644
|
||||
--- a/src/main/java/net/minecraft/tags/TagLoader.java
|
||||
+++ b/src/main/java/net/minecraft/tags/TagLoader.java
|
||||
@@ -79,7 +79,10 @@ public class TagLoader<T> {
|
||||
return list.isEmpty() ? Either.right(builder.build()) : Either.left(list);
|
||||
}
|
||||
|
||||
- public Map<ResourceLocation, Collection<T>> build(Map<ResourceLocation, List<TagLoader.EntryWithSource>> tags) {
|
||||
+ // Paper start - fire tag registrar events
|
||||
+ public Map<ResourceLocation, Collection<T>> build(Map<ResourceLocation, List<TagLoader.EntryWithSource>> tags, @Nullable io.papermc.paper.tag.TagEventConfig<T, ?> eventConfig) {
|
||||
+ tags = io.papermc.paper.tag.PaperTagListenerManager.INSTANCE.firePreFlattenEvent(tags, eventConfig);
|
||||
+ // Paper end - fire tag registrar events
|
||||
final Map<ResourceLocation, Collection<T>> map = Maps.newHashMap();
|
||||
TagEntry.Lookup<T> lookup = new TagEntry.Lookup<T>() {
|
||||
@Nullable
|
||||
@@ -107,11 +110,13 @@ public class TagLoader<T> {
|
||||
)
|
||||
.ifRight(resolvedEntries -> map.put(id, (Collection<T>)resolvedEntries))
|
||||
);
|
||||
- return map;
|
||||
+ return io.papermc.paper.tag.PaperTagListenerManager.INSTANCE.firePostFlattenEvent(map, eventConfig); // Paper - fire tag registrar events
|
||||
}
|
||||
|
||||
- public Map<ResourceLocation, Collection<T>> loadAndBuild(ResourceManager manager) {
|
||||
- return this.build(this.load(manager));
|
||||
+ // Paper start - fire tag registrar events
|
||||
+ public Map<ResourceLocation, Collection<T>> loadAndBuild(ResourceManager manager, @Nullable io.papermc.paper.tag.TagEventConfig<T, ?> eventConfig) {
|
||||
+ return this.build(this.load(manager), eventConfig);
|
||||
+ // Paper end - fire tag registrar events
|
||||
}
|
||||
|
||||
public static record EntryWithSource(TagEntry entry, String source) {
|
||||
diff --git a/src/main/java/net/minecraft/tags/TagManager.java b/src/main/java/net/minecraft/tags/TagManager.java
|
||||
index 7116362d798d7c28a4880c873a60507afa4673e6..2188274de5d1fe1aa5419be6247da6a3a2414a3b 100644
|
||||
--- a/src/main/java/net/minecraft/tags/TagManager.java
|
||||
+++ b/src/main/java/net/minecraft/tags/TagManager.java
|
||||
@@ -39,7 +39,7 @@ public class TagManager implements PreparableReloadListener {
|
||||
) {
|
||||
List<? extends CompletableFuture<? extends TagManager.LoadResult<?>>> list = this.registryAccess
|
||||
.registries()
|
||||
- .map(registry -> this.createLoader(manager, prepareExecutor, (RegistryAccess.RegistryEntry<?>)registry))
|
||||
+ .map(registry -> this.createLoader(manager, prepareExecutor, (RegistryAccess.RegistryEntry<?>)registry, applyExecutor instanceof net.minecraft.server.MinecraftServer ? io.papermc.paper.plugin.lifecycle.event.registrar.ReloadableRegistrarEvent.Cause.INITIAL : io.papermc.paper.plugin.lifecycle.event.registrar.ReloadableRegistrarEvent.Cause.RELOAD)) // Paper - add registrar event cause
|
||||
.toList();
|
||||
return CompletableFuture.allOf(list.toArray(CompletableFuture[]::new))
|
||||
.thenCompose(synchronizer::wait)
|
||||
@@ -48,11 +48,15 @@ public class TagManager implements PreparableReloadListener {
|
||||
|
||||
private <T> CompletableFuture<TagManager.LoadResult<T>> createLoader(
|
||||
ResourceManager resourceManager, Executor prepareExecutor, RegistryAccess.RegistryEntry<T> requirement
|
||||
+ , io.papermc.paper.plugin.lifecycle.event.registrar.ReloadableRegistrarEvent.Cause cause // Paper - add registrar event cause
|
||||
) {
|
||||
ResourceKey<? extends Registry<T>> resourceKey = requirement.key();
|
||||
Registry<T> registry = requirement.value();
|
||||
TagLoader<Holder<T>> tagLoader = new TagLoader<>(registry::getHolder, Registries.tagsDirPath(resourceKey));
|
||||
- return CompletableFuture.supplyAsync(() -> new TagManager.LoadResult<>(resourceKey, tagLoader.loadAndBuild(resourceManager)), prepareExecutor);
|
||||
+ // Paper start - fire tag registrar events
|
||||
+ final io.papermc.paper.tag.TagEventConfig<Holder<T>, ?> config = io.papermc.paper.tag.PaperTagListenerManager.INSTANCE.createEventConfig(registry, cause);
|
||||
+ return CompletableFuture.supplyAsync(() -> new TagManager.LoadResult<>(resourceKey, tagLoader.loadAndBuild(resourceManager, config)), prepareExecutor);
|
||||
+ // Paper end - fire tag registrar events
|
||||
}
|
||||
|
||||
public static record LoadResult<T>(ResourceKey<? extends Registry<T>> key, Map<ResourceLocation, Collection<Holder<T>>> tags) {
|
Loading…
Add table
Add a link
Reference in a new issue