1
0
Fork 0
mirror of https://github.com/PaperMC/Paper.git synced 2025-02-17 02:34:30 +01:00

Tag Lifecycle Events ()

* wip tags

* use generics in tag registrars

* comment out varargs methods for now

* split up patch

* cache loaded service provider

* finish renames

* use builderWithExpectedSize

* finalize
This commit is contained in:
Jake Potrebic 2024-09-08 11:56:09 -07:00
parent a21216b976
commit 969432263f
8 changed files with 1013 additions and 48 deletions

View file

@ -455,15 +455,19 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+
+import io.papermc.paper.plugin.lifecycle.event.LifecycleEvent;
+import io.papermc.paper.plugin.lifecycle.event.LifecycleEventOwner;
+import java.util.Optional;
+import java.util.ServiceLoader;
+import org.jetbrains.annotations.ApiStatus;
+
+@ApiStatus.Internal
+interface LifecycleEventTypeProvider {
+
+ LifecycleEventTypeProvider PROVIDER = ServiceLoader.load(LifecycleEventTypeProvider.class)
+ .findFirst()
+ .orElseThrow();
+ Optional<LifecycleEventTypeProvider> INSTANCE = ServiceLoader.load(LifecycleEventTypeProvider.class)
+ .findFirst();
+
+ static LifecycleEventTypeProvider provider() {
+ return INSTANCE.orElseThrow();
+ }
+
+ <O extends LifecycleEventOwner, E extends LifecycleEvent> LifecycleEventType.Monitorable<O, E> monitor(String name, Class<? extends O> ownerType);
+
@ -494,33 +498,33 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+
+ //<editor-fold desc="helper methods" defaultstate="collapsed">
+ @ApiStatus.Internal
+ private static <E extends LifecycleEvent> LifecycleEventType.Monitorable<Plugin, E> plugin(final String name) {
+ static <E extends LifecycleEvent> LifecycleEventType.Monitorable<Plugin, E> plugin(final String name) {
+ return monitor(name, Plugin.class);
+ }
+
+ @ApiStatus.Internal
+ private static <E extends LifecycleEvent> LifecycleEventType.Prioritizable<Plugin, E> pluginPrioritized(final String name) {
+ static <E extends LifecycleEvent> LifecycleEventType.Prioritizable<Plugin, E> pluginPrioritized(final String name) {
+ return prioritized(name, Plugin.class);
+ }
+
+ @ApiStatus.Internal
+ private static <E extends LifecycleEvent> LifecycleEventType.Monitorable<BootstrapContext, E> bootstrap(final String name) {
+ static <E extends LifecycleEvent> LifecycleEventType.Monitorable<BootstrapContext, E> bootstrap(final String name) {
+ return monitor(name, BootstrapContext.class);
+ }
+
+ @ApiStatus.Internal
+ private static <E extends LifecycleEvent> LifecycleEventType.Prioritizable<BootstrapContext, E> bootstrapPrioritized(final String name) {
+ static <E extends LifecycleEvent> LifecycleEventType.Prioritizable<BootstrapContext, E> bootstrapPrioritized(final String name) {
+ return prioritized(name, BootstrapContext.class);
+ }
+
+ @ApiStatus.Internal
+ private static <O extends LifecycleEventOwner, E extends LifecycleEvent, O2 extends O> LifecycleEventType.Monitorable<O, E> monitor(final String name, final Class<O2> ownerType) {
+ return LifecycleEventTypeProvider.PROVIDER.monitor(name, ownerType);
+ static <O extends LifecycleEventOwner, E extends LifecycleEvent, O2 extends O> LifecycleEventType.Monitorable<O, E> monitor(final String name, final Class<O2> ownerType) {
+ return LifecycleEventTypeProvider.provider().monitor(name, ownerType);
+ }
+
+ @ApiStatus.Internal
+ private static <O extends LifecycleEventOwner, E extends LifecycleEvent> LifecycleEventType.Prioritizable<O, E> prioritized(final String name, final Class<? extends O> ownerType) {
+ return LifecycleEventTypeProvider.PROVIDER.prioritized(name, ownerType);
+ static <O extends LifecycleEventOwner, E extends LifecycleEvent> LifecycleEventType.Prioritizable<O, E> prioritized(final String name, final Class<? extends O> ownerType) {
+ return LifecycleEventTypeProvider.provider().prioritized(name, ownerType);
+ }
+ //</editor-fold>
+

View file

@ -1587,8 +1587,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ .findFirst();
+
+ static VanillaArgumentProvider provider() {
+ return PROVIDER.orElseThrow();
+ }
+ return PROVIDER.orElseThrow();
+ }
+
+ ArgumentType<EntitySelectorArgumentResolver> entity();
+
@ -1942,7 +1942,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+
//<editor-fold desc="helper methods" defaultstate="collapsed">
@ApiStatus.Internal
private static <E extends LifecycleEvent> LifecycleEventType.Monitorable<Plugin, E> plugin(final String name) {
static <E extends LifecycleEvent> LifecycleEventType.Monitorable<Plugin, E> plugin(final String name) {
diff --git a/src/main/java/org/bukkit/command/Command.java b/src/main/java/org/bukkit/command/Command.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/command/Command.java

View file

@ -788,6 +788,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+@ApiStatus.Internal
+@DefaultQualifier(NonNull.class)
+record TagKeyImpl<T>(RegistryKey<T> registryKey, Key key) implements TagKey<T> {
+
+ @Override
+ public String toString() {
+ return "#" + this.key + " (in " + this.registryKey + ")";
+ }
+}
diff --git a/src/main/java/org/bukkit/Registry.java b/src/main/java/org/bukkit/Registry.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644

View file

@ -0,0 +1,406 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Thu, 20 Jun 2024 09:40:53 -0700
Subject: [PATCH] Tag Lifecycle Events
diff --git a/src/main/java/io/papermc/paper/plugin/lifecycle/event/types/LifecycleEventTypeProvider.java b/src/main/java/io/papermc/paper/plugin/lifecycle/event/types/LifecycleEventTypeProvider.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/io/papermc/paper/plugin/lifecycle/event/types/LifecycleEventTypeProvider.java
+++ b/src/main/java/io/papermc/paper/plugin/lifecycle/event/types/LifecycleEventTypeProvider.java
@@ -0,0 +0,0 @@ interface LifecycleEventTypeProvider {
<O extends LifecycleEventOwner, E extends LifecycleEvent> LifecycleEventType.Monitorable<O, E> monitor(String name, Class<? extends O> ownerType);
<O extends LifecycleEventOwner, E extends LifecycleEvent> LifecycleEventType.Prioritizable<O, E> prioritized(String name, Class<? extends O> ownerType);
+
+ TagEventTypeProvider tagProvider();
}
diff --git a/src/main/java/io/papermc/paper/plugin/lifecycle/event/types/LifecycleEvents.java b/src/main/java/io/papermc/paper/plugin/lifecycle/event/types/LifecycleEvents.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/io/papermc/paper/plugin/lifecycle/event/types/LifecycleEvents.java
+++ b/src/main/java/io/papermc/paper/plugin/lifecycle/event/types/LifecycleEvents.java
@@ -0,0 +0,0 @@ public final class LifecycleEvents {
*/
public static final LifecycleEventType.Prioritizable<LifecycleEventOwner, ReloadableRegistrarEvent<Commands>> COMMANDS = prioritized("commands", LifecycleEventOwner.class);
+ /**
+ * These events are for registering tags to the server's tag system. You can register a handler for these events
+ * only in {@link io.papermc.paper.plugin.bootstrap.PluginBootstrap#bootstrap(BootstrapContext)}.
+ */
+ public static final TagEventTypeProvider TAGS = LifecycleEventTypeProvider.provider().tagProvider();
+
//<editor-fold desc="helper methods" defaultstate="collapsed">
@ApiStatus.Internal
static <E extends LifecycleEvent> LifecycleEventType.Monitorable<Plugin, E> plugin(final String name) {
diff --git a/src/main/java/io/papermc/paper/plugin/lifecycle/event/types/TagEventTypeProvider.java b/src/main/java/io/papermc/paper/plugin/lifecycle/event/types/TagEventTypeProvider.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/plugin/lifecycle/event/types/TagEventTypeProvider.java
@@ -0,0 +0,0 @@
+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.PostFlattenTagRegistrar;
+import io.papermc.paper.tag.PreFlattenTagRegistrar;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.jetbrains.annotations.ApiStatus;
+
+/**
+ * Provides event types for tag registration.
+ *
+ * @see PreFlattenTagRegistrar
+ * @see PostFlattenTagRegistrar
+ */
+@ApiStatus.Experimental
+@ApiStatus.NonExtendable
+public interface TagEventTypeProvider {
+
+ /**
+ * Get a prioritizable, reloadable registrar event for tags before they are flattened.
+ *
+ * @param registryKey the registry key for the tag type
+ * @return the registry event type
+ * @param <T> the type of value in the tag
+ * @see PreFlattenTagRegistrar
+ */
+ <T> LifecycleEventType.@NonNull Prioritizable<BootstrapContext, ReloadableRegistrarEvent<PreFlattenTagRegistrar<T>>> preFlatten(@NonNull RegistryKey<T> registryKey);
+
+ /**
+ * Get a prioritizable, reloadable registrar event for tags after they are flattened.
+ *
+ * @param registryKey the registry key for the tag type
+ * @return the registry event type
+ * @param <T> the type of value in the tag
+ * @see PostFlattenTagRegistrar
+ */
+ <T> LifecycleEventType.@NonNull Prioritizable<BootstrapContext, ReloadableRegistrarEvent<PostFlattenTagRegistrar<T>>> postFlatten(@NonNull RegistryKey<T> registryKey);
+}
diff --git a/src/main/java/io/papermc/paper/tag/PostFlattenTagRegistrar.java b/src/main/java/io/papermc/paper/tag/PostFlattenTagRegistrar.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/tag/PostFlattenTagRegistrar.java
@@ -0,0 +0,0 @@
+package io.papermc.paper.tag;
+
+import io.papermc.paper.plugin.lifecycle.event.registrar.Registrar;
+import io.papermc.paper.registry.RegistryKey;
+import io.papermc.paper.registry.TypedKey;
+import io.papermc.paper.registry.tag.TagKey;
+import java.util.Collection;
+import java.util.Map;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.Contract;
+import org.jetbrains.annotations.Unmodifiable;
+
+/**
+ * Registrar for tags after they have been flattened. Flattened
+ * tags are tags which have any nested tags resolved to the tagged
+ * values the nested tags point to. This registrar, being a post-flatten
+ * registrar, allows for modification after that flattening has happened, when
+ * tags only point to individual entries and not other nested tags.
+ * <p>
+ * An example of a custom enchant being registered to the vanilla
+ * {@code #minecraft:in_enchanting_table} tag.
+ * <pre>{@code
+ * class YourBootstrapClass implements PluginBootstrap {
+ *
+ * @Override
+ * public void bootstrap(@NotNull BootstrapContext context) {
+ * LifecycleEventManager<BootstrapContext> manager = context.getLifecycleManager();
+ * manager.registerEventHandler(LifecycleEvents.TAGS.postFlatten(RegistryKey.ENCHANTMENT), event -> {
+ * final PostFlattenTagRegistrar<Enchantment> registrar = event.registrar();
+ * registrar.addToTag(
+ * EnchantmentTagKeys.IN_ENCHANTING_TABLE,
+ * Set.of(CUSTOM_ENCHANT)
+ * );
+ * });
+ * }
+ * }
+ * }</pre>
+ *
+ * @param <T> the type of value in the tag
+ * @see PreFlattenTagRegistrar
+ */
+@ApiStatus.Experimental
+@ApiStatus.NonExtendable
+public interface PostFlattenTagRegistrar<T> extends Registrar {
+
+ /**
+ * Get the registry key for this tag registrar.
+ *
+ * @return the registry key
+ */
+ @NonNull RegistryKey<T> registryKey();
+
+ /**
+ * Get a copy of all tags currently held in this registrar.
+ *
+ * @return an immutable map of all tags
+ */
+ @Contract(value = "-> new", pure = true)
+ @Unmodifiable @NonNull Map<TagKey<T>, Collection<TypedKey<T>>> getAllTags();
+
+ /**
+ * Checks if this registrar has a tag with the given key.
+ *
+ * @param tagKey the key to check for
+ * @return true if the tag exists, false otherwise
+ */
+ @Contract(pure = true)
+ boolean hasTag(@NonNull TagKey<T> tagKey);
+
+ /**
+ * Get the tag with the given key. Use {@link #hasTag(TagKey)} to check
+ * if a tag exists first.
+ *
+ * @param tagKey the key of the tag to get
+ * @return an immutable list of tag entries
+ * @throws java.util.NoSuchElementException if the tag does not exist
+ * @see #hasTag(TagKey)
+ */
+ @Contract(value = "_ -> new", pure = true)
+ @Unmodifiable @NonNull Collection<TypedKey<T>> getTag(@NonNull TagKey<T> tagKey);
+
+ /**
+ * Adds values to the given tag. If the tag does not exist, it will be created.
+ *
+ * @param tagKey the key of the tag to add to
+ * @param values the values to add
+ * @see #setTag(TagKey, Collection)
+ */
+ @Contract(mutates = "this")
+ void addToTag(@NonNull TagKey<T> tagKey, @NonNull Collection<TypedKey<T>> values);
+
+ /**
+ * Sets the values of the given tag. If the tag does not exist, it will be created.
+ * If the tag does exist, it will be overwritten.
+ *
+ * @param tagKey the key of the tag to set
+ * @param values the values to set
+ * @see #addToTag(TagKey, Collection)
+ */
+ @Contract(mutates = "this")
+ void setTag(@NonNull TagKey<T> tagKey, @NonNull Collection<TypedKey<T>> values);
+}
diff --git a/src/main/java/io/papermc/paper/tag/PreFlattenTagRegistrar.java b/src/main/java/io/papermc/paper/tag/PreFlattenTagRegistrar.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/tag/PreFlattenTagRegistrar.java
@@ -0,0 +0,0 @@
+package io.papermc.paper.tag;
+
+import io.papermc.paper.plugin.lifecycle.event.registrar.Registrar;
+import io.papermc.paper.registry.RegistryKey;
+import io.papermc.paper.registry.tag.TagKey;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.Contract;
+import org.jetbrains.annotations.Unmodifiable;
+
+/**
+ * Registrar for tags before they are flattened. Flattened
+ * tags are tags which have any nested tags resolved to the tagged
+ * values the nested tags point to. This registrar, being a pre-flatten
+ * registrar, allows for modification before that flattening has happened, when
+ * tags both point to individual entries and other nested tags.
+ * <p>
+ * An example of a tag being created in a pre-flatten registrar:
+ * <pre>{@code
+ * class YourBootstrapClass implements PluginBootstrap {
+ *
+ * @Override
+ * public void bootstrap(@NotNull BootstrapContext context) {
+ * LifecycleEventManager<BootstrapContext> manager = context.getLifecycleManager();
+ * manager.registerEventHandler(LifecycleEvents.TAGS.preFlatten(RegistryKey.ITEM), event -> {
+ * final PreFlattenTagRegistrar<ItemType> registrar = event.registrar();
+ * registrar.setTag(AXE_PICKAXE, Set.of(
+ * TagEntry.tagEntry(ItemTypeTagKeys.PICKAXES),
+ * TagEntry.tagEntry(ItemTypeTagKeys.AXES)
+ * ));
+ * });
+ * }
+ * }
+ * }</pre>
+ *
+ * @param <T> the type of value in the tag
+ * @see PostFlattenTagRegistrar
+ */
+@ApiStatus.Experimental
+@ApiStatus.NonExtendable
+public interface PreFlattenTagRegistrar<T> extends Registrar {
+
+ /**
+ * Get the registry key for this tag registrar.
+ *
+ * @return the registry key
+ */
+ @NonNull RegistryKey<T> registryKey();
+
+ /**
+ * Get a copy of all tags currently held in this registrar.
+ *
+ * @return an immutable map of all tags
+ */
+ @Contract(value = "-> new", pure = true)
+ @Unmodifiable @NonNull Map<TagKey<T>, Collection<TagEntry<T>>> getAllTags();
+
+ /**
+ * Checks if this registrar has a tag with the given key.
+ *
+ * @param tagKey the key to check for
+ * @return true if the tag exists, false otherwise
+ */
+ @Contract(pure = true)
+ boolean hasTag(@NonNull TagKey<T> tagKey);
+
+ /**
+ * Get the tag with the given key. Use {@link #hasTag(TagKey)} to check
+ * if a tag exists first.
+ *
+ * @param tagKey the key of the tag to get
+ * @return an immutable list of tag entries
+ * @throws java.util.NoSuchElementException if the tag does not exist
+ * @see #hasTag(TagKey)
+ */
+ @Contract(value = "_ -> new", pure = true)
+ @Unmodifiable @NonNull List<TagEntry<T>> getTag(@NonNull TagKey<T> tagKey);
+
+ /**
+ * Adds entries to the given tag. If the tag does not exist, it will be created.
+ *
+ * @param tagKey the key of the tag to add to
+ * @param entries the entries to add
+ * @see #setTag(TagKey, Collection)
+ */
+ @Contract(mutates = "this")
+ void addToTag(@NonNull TagKey<T> tagKey, @NonNull Collection<TagEntry<T>> entries);
+
+ /**
+ * Sets the entries of the given tag. If the tag does not exist, it will be created.
+ * If the tag does exist, it will be overwritten.
+ *
+ * @param tagKey the key of the tag to set
+ * @param entries the entries to set
+ * @see #addToTag(TagKey, Collection)
+ */
+ @Contract(mutates = "this")
+ void setTag(@NonNull TagKey<T> tagKey, @NonNull Collection<TagEntry<T>> entries);
+}
diff --git a/src/main/java/io/papermc/paper/tag/TagEntry.java b/src/main/java/io/papermc/paper/tag/TagEntry.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/tag/TagEntry.java
@@ -0,0 +0,0 @@
+package io.papermc.paper.tag;
+
+import io.papermc.paper.registry.TypedKey;
+import io.papermc.paper.registry.tag.TagKey;
+import net.kyori.adventure.key.Keyed;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.Contract;
+
+/**
+ * An entry is a pre-flattened tag. Represents
+ * either an individual registry entry or a whole tag.
+ *
+ * @param <T> the type of value in the tag
+ * @see PreFlattenTagRegistrar
+ */
+@ApiStatus.Experimental
+@ApiStatus.NonExtendable
+public interface TagEntry<T> extends Keyed {
+
+ /**
+ * Create required tag entry for a single value.
+ *
+ * @param entryKey the key of the entry
+ * @return a new tag entry for a value
+ * @param <T> the type of value
+ */
+ @Contract(value = "_ -> new", pure = true)
+ static <T> @NonNull TagEntry<T> valueEntry(final @NonNull TypedKey<T> entryKey) {
+ return valueEntry(entryKey, true);
+ }
+
+ /**
+ * Create tag entry for a single value.
+ *
+ * @param entryKey the key of the entry
+ * @param isRequired if this entry is required (see {@link #isRequired()})
+ * @return a new tag entry for a value
+ * @param <T> the type of value
+ */
+ @Contract(value = "_, _ -> new", pure = true)
+ static <T> @NonNull TagEntry<T> valueEntry(final @NonNull TypedKey<T> entryKey, final boolean isRequired) {
+ return new TagEntryImpl<>(entryKey.key(), false, isRequired);
+ }
+
+ /**
+ * Create a required tag entry for a nested tag.
+ *
+ * @param tagKey they key for the tag
+ * @return a new tag entry for a tag
+ * @param <T> the type of value
+ */
+ @Contract(value = "_ -> new", pure = true)
+ static <T> @NonNull TagEntry<T> tagEntry(final @NonNull TagKey<T> tagKey) {
+ return tagEntry(tagKey, true);
+ }
+
+ /**
+ * Create a tag entry for a nested tag.
+ *
+ * @param tagKey they key for the tag
+ * @param isRequired if this entry is required (see {@link #isRequired()})
+ * @return a new tag entry for a tag
+ * @param <T> the type of value
+ */
+ @Contract(value = "_, _ -> new", pure = true)
+ static <T> @NonNull TagEntry<T> tagEntry(final @NonNull TagKey<T> tagKey, final boolean isRequired) {
+ return new TagEntryImpl<>(tagKey.key(), true, isRequired);
+ }
+
+ /**
+ * Returns if this entry represents a tag.
+ *
+ * @return true if this entry is a tag, false if it is an individual entry
+ */
+ @Contract(pure = true)
+ boolean isTag();
+
+ /**
+ * Returns if this entry is required. If an entry is required,
+ * the value or tag must exist on the server in order for the tag
+ * to load correctly. A missing value will prevent the tag holding
+ * that missing value from being created.
+ *
+ * @return true if this entry is required, false if it is optional
+ */
+ @Contract(pure = true)
+ boolean isRequired();
+}
diff --git a/src/main/java/io/papermc/paper/tag/TagEntryImpl.java b/src/main/java/io/papermc/paper/tag/TagEntryImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/tag/TagEntryImpl.java
@@ -0,0 +0,0 @@
+package io.papermc.paper.tag;
+
+import net.kyori.adventure.key.Key;
+import org.jetbrains.annotations.ApiStatus;
+
+@ApiStatus.Internal
+record TagEntryImpl<T>(Key key, boolean isTag, boolean isRequired) implements TagEntry<T> {
+}

View file

@ -468,6 +468,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+ }
+
+ public abstract boolean hasHandlers();
+
+ public abstract void forEachHandler(E event, Consumer<RegisteredHandler<O, E>> consumer, Predicate<RegisteredHandler<O, E>> predicate);
+
+ public abstract void removeMatching(Predicate<RegisteredHandler<O, E>> predicate);
@ -505,7 +507,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+public final class LifecycleEventTypeProviderImpl implements LifecycleEventTypeProvider {
+
+ public static LifecycleEventTypeProviderImpl instance() {
+ return (LifecycleEventTypeProviderImpl) LifecycleEventTypeProvider.PROVIDER;
+ return (LifecycleEventTypeProviderImpl) LifecycleEventTypeProvider.provider();
+ }
+
+ @Override
@ -550,6 +552,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+
+ @Override
+ public boolean hasHandlers() {
+ return !this.handlers.isEmpty();
+ }
+
+ @Override
+ public MonitorLifecycleEventHandlerConfigurationImpl<O, E> newHandler(final LifecycleEventHandler<? super E> handler) {
+ return new MonitorLifecycleEventHandlerConfigurationImpl<>(handler, this);
+ }
@ -652,6 +659,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+
+ @Override
+ public boolean hasHandlers() {
+ return !this.handlers.isEmpty();
+ }
+
+ @Override
+ protected void register(final O owner, final AbstractLifecycleEventHandlerConfiguration<O, E> config) {
+ Preconditions.checkArgument(config instanceof PrioritizedLifecycleEventHandlerConfigurationImpl<?, ?>, "Configuration must be a PrioritizedLifecycleEventHandlerConfiguration");
+ this.handlers.add(new RegisteredHandler<>(owner, config));

View file

@ -96,7 +96,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ protected HolderSet<Enchantment> exclusiveWith = HolderSet.empty(); // Paper added default to empty.
+
+ // Effects
+ protected final DataComponentMap effects;
+ protected DataComponentMap effects;
+
+ protected final Conversions conversions;
+

View file

@ -134,12 +134,12 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.intellij.lang.annotations.Subst;
+
+public final class PaperRegistryListenerManager {
+public class PaperRegistryListenerManager {
+
+ public static final PaperRegistryListenerManager INSTANCE = new PaperRegistryListenerManager();
+
+ public final RegistryEventMap valueAddHooks = new RegistryEventMap("value add");
+ public final RegistryEventMap freezeHooks = new RegistryEventMap("freeze");
+ public final RegistryEventMap valueAddEventTypes = new RegistryEventMap("value add");
+ public final RegistryEventMap freezeEventTypes = new RegistryEventMap("freeze");
+
+ private PaperRegistryListenerManager() {
+ }
@ -200,7 +200,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ ) {
+ Preconditions.checkState(LaunchEntryPointHandler.INSTANCE.hasEntered(Entrypoint.BOOTSTRAPPER), registry.key() + " tried to run modification listeners before bootstrappers have been called"); // verify that bootstrappers have been called
+ final @Nullable RegistryEntryInfo<M, T> entry = PaperRegistries.getEntry(registry.key());
+ if (!RegistryEntry.Modifiable.isModifiable(entry) || !this.valueAddHooks.hasHooks(entry.apiKey())) {
+ if (!RegistryEntry.Modifiable.isModifiable(entry) || !this.valueAddEventTypes.hasHandlers(entry.apiKey())) {
+ return registerMethod.register((WritableRegistry<M>) registry, key, nms, registrationInfo);
+ }
+ final RegistryEntry.Modifiable<M, T, B> modifiableEntry = RegistryEntry.Modifiable.asModifiable(entry);
@ -217,7 +217,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ final RegistrationInfo registrationInfo,
+ final Conversions conversions
+ ) {
+ if (!RegistryEntry.Modifiable.isModifiable(entry) || !this.valueAddHooks.hasHooks(entry.apiKey())) {
+ if (!RegistryEntry.Modifiable.isModifiable(entry) || !this.valueAddEventTypes.hasHandlers(entry.apiKey())) {
+ registry.register(key, builder.build(), registrationInfo);
+ return;
+ }
@ -237,7 +237,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ @Subst("namespace:key") final ResourceLocation beingAdded = key.location();
+ @SuppressWarnings("PatternValidation") final TypedKey<T> typedKey = TypedKey.create(entry.apiKey(), Key.key(beingAdded.getNamespace(), beingAdded.getPath()));
+ final RegistryEntryAddEventImpl<T, B> event = entry.createEntryAddEvent(typedKey, builder, conversions);
+ LifecycleEventRunner.INSTANCE.callEvent(this.valueAddHooks.getHook(entry.apiKey()), event);
+ LifecycleEventRunner.INSTANCE.callEvent(this.valueAddEventTypes.getEventType(entry.apiKey()), event);
+ if (oldNms != null) {
+ ((MappedRegistry<M>) registry).clearIntrusiveHolder(oldNms);
+ }
@ -261,27 +261,27 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+
+ public <M, T extends org.bukkit.Keyed, B extends PaperRegistryBuilder<M, T>> void runFreezeListeners(final ResourceKey<? extends Registry<M>> resourceKey, final Conversions conversions) {
+ final @Nullable RegistryEntryInfo<M, T> entry = PaperRegistries.getEntry(resourceKey);
+ if (!RegistryEntry.Addable.isAddable(entry) || !this.freezeHooks.hasHooks(entry.apiKey())) {
+ if (!RegistryEntry.Addable.isAddable(entry) || !this.freezeEventTypes.hasHandlers(entry.apiKey())) {
+ return;
+ }
+ final RegistryEntry.Addable<M, T, B> writableEntry = RegistryEntry.Addable.asAddable(entry);
+ final WritableCraftRegistry<M, T, B> writableRegistry = PaperRegistryAccess.instance().getWritableRegistry(entry.apiKey());
+ final RegistryFreezeEventImpl<T, B> event = writableEntry.createFreezeEvent(writableRegistry, conversions);
+ LifecycleEventRunner.INSTANCE.callEvent(this.freezeHooks.getHook(entry.apiKey()), event);
+ LifecycleEventRunner.INSTANCE.callEvent(this.freezeEventTypes.getEventType(entry.apiKey()), event);
+ }
+
+ public <T, B extends RegistryBuilder<T>> RegistryEntryAddEventType<T, B> getRegistryValueAddEventType(final RegistryEventProvider<T, B> type) {
+ if (!RegistryEntry.Modifiable.isModifiable(PaperRegistries.getEntry(type.registryKey()))) {
+ throw new IllegalArgumentException(type.registryKey() + " does not support RegistryEntryAddEvent");
+ }
+ return this.valueAddHooks.getOrCreate(type, RegistryEntryAddEventTypeImpl::new);
+ return this.valueAddEventTypes.getOrCreate(type.registryKey(), RegistryEntryAddEventTypeImpl::new);
+ }
+
+ public <T, B extends RegistryBuilder<T>> LifecycleEventType.Prioritizable<BootstrapContext, RegistryFreezeEvent<T, B>> getRegistryFreezeEventType(final RegistryEventProvider<T, B> type) {
+ if (!RegistryEntry.Addable.isAddable(PaperRegistries.getEntry(type.registryKey()))) {
+ throw new IllegalArgumentException(type.registryKey() + " does not support RegistryFreezeEvent");
+ }
+ return this.freezeHooks.getOrCreate(type, RegistryLifecycleEventType::new);
+ return this.freezeEventTypes.getOrCreate(type.registryKey(), RegistryLifecycleEventType::new);
+ }
+}
diff --git a/src/main/java/io/papermc/paper/registry/WritableCraftRegistry.java b/src/main/java/io/papermc/paper/registry/WritableCraftRegistry.java
@ -702,9 +702,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+package io.papermc.paper.registry.event;
+
+import io.papermc.paper.plugin.bootstrap.BootstrapContext;
+import io.papermc.paper.plugin.lifecycle.event.LifecycleEvent;
+import io.papermc.paper.plugin.lifecycle.event.LifecycleEventRunner;
+import io.papermc.paper.plugin.lifecycle.event.types.AbstractLifecycleEventType;
+import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEventType;
+import io.papermc.paper.registry.RegistryBuilder;
+import io.papermc.paper.registry.RegistryKey;
+import java.util.HashMap;
+import java.util.Map;
@ -713,7 +714,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+
+public final class RegistryEventMap {
+
+ private final Map<RegistryKey<?>, LifecycleEventType<BootstrapContext, ? extends RegistryEvent<?>, ?>> hooks = new HashMap<>();
+ private final Map<RegistryKey<?>, LifecycleEventType<BootstrapContext, ? extends LifecycleEvent, ?>> eventTypes = new HashMap<>();
+ private final String name;
+
+ public RegistryEventMap(final String name) {
@ -721,25 +722,26 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T, B extends RegistryBuilder<T>, E extends RegistryEvent<T>, ET extends LifecycleEventType<BootstrapContext, E, ?>> ET getOrCreate(final RegistryEventProvider<T, B> type, final BiFunction<? super RegistryEventProvider<T, B>, ? super String, ET> eventTypeCreator) {
+ final ET registerHook;
+ if (this.hooks.containsKey(type.registryKey())) {
+ registerHook = (ET) this.hooks.get(type.registryKey());
+ public <T, E extends LifecycleEvent, ET extends LifecycleEventType<BootstrapContext, E, ?>> ET getOrCreate(final RegistryKey<T> registryKey, final BiFunction<? super RegistryKey<T>, ? super String, ET> eventTypeCreator) {
+ final ET eventType;
+ if (this.eventTypes.containsKey(registryKey)) {
+ eventType = (ET) this.eventTypes.get(registryKey);
+ } else {
+ registerHook = eventTypeCreator.apply(type, this.name);
+ LifecycleEventRunner.INSTANCE.addEventType(registerHook);
+ this.hooks.put(type.registryKey(), registerHook);
+ eventType = eventTypeCreator.apply(registryKey, this.name);
+ LifecycleEventRunner.INSTANCE.addEventType(eventType);
+ this.eventTypes.put(registryKey, eventType);
+ }
+ return registerHook;
+ return eventType;
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T, E extends RegistryEvent<T>> LifecycleEventType<BootstrapContext, E, ?> getHook(final RegistryKey<T> registryKey) {
+ return (LifecycleEventType<BootstrapContext, E, ?>) Objects.requireNonNull(this.hooks.get(registryKey), "No hook for " + registryKey);
+ public <T, E extends LifecycleEvent> LifecycleEventType<BootstrapContext, E, ?> getEventType(final RegistryKey<T> registryKey) {
+ return (LifecycleEventType<BootstrapContext, E, ?>) Objects.requireNonNull(this.eventTypes.get(registryKey), "No hook for " + registryKey);
+ }
+
+ public boolean hasHooks(final RegistryKey<?> registryKey) {
+ return this.hooks.containsKey(registryKey);
+ public boolean hasHandlers(final RegistryKey<?> registryKey) {
+ final AbstractLifecycleEventType<?, ?, ?> type = ((AbstractLifecycleEventType<?, ?, ?>) this.eventTypes.get(registryKey));
+ return type != null && type.hasHandlers();
+ }
+
+}
@ -830,15 +832,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+import io.papermc.paper.plugin.lifecycle.event.handler.LifecycleEventHandler;
+import io.papermc.paper.plugin.lifecycle.event.types.PrioritizableLifecycleEventType;
+import io.papermc.paper.registry.RegistryBuilder;
+import io.papermc.paper.registry.RegistryKey;
+import io.papermc.paper.registry.event.RegistryEntryAddEvent;
+import io.papermc.paper.registry.event.RegistryEventProvider;
+import java.util.function.Consumer;
+import java.util.function.Predicate;
+
+public class RegistryEntryAddEventTypeImpl<T, B extends RegistryBuilder<T>> extends PrioritizableLifecycleEventType<BootstrapContext, RegistryEntryAddEvent<T, B>, RegistryEntryAddConfiguration<T>> implements RegistryEntryAddEventType<T, B> {
+
+ public RegistryEntryAddEventTypeImpl(final RegistryEventProvider<T, B> type, final String eventName) {
+ super(type.registryKey() + " / " + eventName, BootstrapContext.class);
+ public RegistryEntryAddEventTypeImpl(final RegistryKey<T> registryKey, final String eventName) {
+ super(registryKey + " / " + eventName, BootstrapContext.class);
+ }
+
+ @Override
@ -914,14 +916,13 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+
+import io.papermc.paper.plugin.bootstrap.BootstrapContext;
+import io.papermc.paper.plugin.lifecycle.event.types.PrioritizableLifecycleEventType;
+import io.papermc.paper.registry.RegistryBuilder;
+import io.papermc.paper.registry.RegistryKey;
+import io.papermc.paper.registry.event.RegistryEvent;
+import io.papermc.paper.registry.event.RegistryEventProvider;
+
+public final class RegistryLifecycleEventType<T, B extends RegistryBuilder<T>, E extends RegistryEvent<T>> extends PrioritizableLifecycleEventType.Simple<BootstrapContext, E> {
+public final class RegistryLifecycleEventType<T, E extends RegistryEvent<T>> extends PrioritizableLifecycleEventType.Simple<BootstrapContext, E> {
+
+ public RegistryLifecycleEventType(final RegistryEventProvider<T, B> type, final String eventName) {
+ super(type.registryKey() + " / " + eventName, BootstrapContext.class);
+ public RegistryLifecycleEventType(final RegistryKey<T> registryKey, final String eventName) {
+ super(registryKey + " / " + eventName, BootstrapContext.class);
+ }
+}
diff --git a/src/main/java/io/papermc/paper/registry/legacy/DelayedRegistry.java b/src/main/java/io/papermc/paper/registry/legacy/DelayedRegistry.java

View file

@ -0,0 +1,537 @@
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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 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
@@ -0,0 +0,0 @@ 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 LifecycleEventRunner.INSTANCE.addEventType(new MonitorableLifecycleEventType<>(name, ownerType));
@@ -0,0 +0,0 @@ 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 LifecycleEventRunner.INSTANCE.addEventType(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..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/plugin/lifecycle/event/types/PaperTagEventTypeProvider.java
@@ -0,0 +0,0 @@
+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..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/tag/PaperPostFlattenTagRegistrar.java
@@ -0,0 +0,0 @@
+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..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/tag/PaperPreFlattenTagRegistrar.java
@@ -0,0 +0,0 @@
+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..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/tag/PaperTagListenerManager.java
@@ -0,0 +0,0 @@
+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..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/tag/TagEventConfig.java
@@ -0,0 +0,0 @@
+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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/ServerFunctionLibrary.java
+++ b/src/main/java/net/minecraft/server/ServerFunctionLibrary.java
@@ -0,0 +0,0 @@ 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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/tags/TagLoader.java
+++ b/src/main/java/net/minecraft/tags/TagLoader.java
@@ -0,0 +0,0 @@ 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
@@ -0,0 +0,0 @@ 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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/tags/TagManager.java
+++ b/src/main/java/net/minecraft/tags/TagManager.java
@@ -0,0 +0,0 @@ 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)
@@ -0,0 +0,0 @@ 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) {