Switch Impl types to Holderable (#11662)
* Switch Impl types to Holderable * Fix compile issues * more improvements * compile fixes * remove unneeded unwrapAndConvertHolder call --------- Co-authored-by: Bjarne Koll <git@lynxplay.dev>
This commit is contained in:
@@ -83,8 +83,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
+import org.bukkit.potion.PotionEffectType;
|
||||
+import org.jspecify.annotations.Nullable;
|
||||
+
|
||||
+import static io.papermc.paper.registry.entry.RegistryEntry.apiOnly;
|
||||
+import static io.papermc.paper.registry.entry.RegistryEntry.entry;
|
||||
+import static io.papermc.paper.registry.entry.RegistryEntryBuilder.start;
|
||||
+
|
||||
+public final class PaperRegistries {
|
||||
+
|
||||
@@ -94,39 +93,39 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
+ static {
|
||||
+ REGISTRY_ENTRIES = List.of(
|
||||
+ // built-ins
|
||||
+ entry(Registries.GAME_EVENT, RegistryKey.GAME_EVENT, GameEvent.class, CraftGameEvent::new),
|
||||
+ entry(Registries.STRUCTURE_TYPE, RegistryKey.STRUCTURE_TYPE, StructureType.class, CraftStructureType::new),
|
||||
+ entry(Registries.MOB_EFFECT, RegistryKey.MOB_EFFECT, PotionEffectType.class, CraftPotionEffectType::new),
|
||||
+ entry(Registries.BLOCK, RegistryKey.BLOCK, BlockType.class, CraftBlockType::new),
|
||||
+ entry(Registries.ITEM, RegistryKey.ITEM, ItemType.class, CraftItemType::new),
|
||||
+ entry(Registries.CAT_VARIANT, RegistryKey.CAT_VARIANT, Cat.Type.class, CraftCat.CraftType::new),
|
||||
+ entry(Registries.FROG_VARIANT, RegistryKey.FROG_VARIANT, Frog.Variant.class, CraftFrog.CraftVariant::new),
|
||||
+ entry(Registries.VILLAGER_PROFESSION, RegistryKey.VILLAGER_PROFESSION, Villager.Profession.class, CraftVillager.CraftProfession::new),
|
||||
+ entry(Registries.VILLAGER_TYPE, RegistryKey.VILLAGER_TYPE, Villager.Type.class, CraftVillager.CraftType::new),
|
||||
+ entry(Registries.MAP_DECORATION_TYPE, RegistryKey.MAP_DECORATION_TYPE, MapCursor.Type.class, CraftMapCursor.CraftType::new),
|
||||
+ entry(Registries.MENU, RegistryKey.MENU, MenuType.class, CraftMenuType::new),
|
||||
+ entry(Registries.ATTRIBUTE, RegistryKey.ATTRIBUTE, Attribute.class, CraftAttribute::new),
|
||||
+ entry(Registries.FLUID, RegistryKey.FLUID, Fluid.class, CraftFluid::new),
|
||||
+ entry(Registries.SOUND_EVENT, RegistryKey.SOUND_EVENT, Sound.class, CraftSound::new),
|
||||
+ start(Registries.GAME_EVENT, RegistryKey.GAME_EVENT).craft(GameEvent.class, CraftGameEvent::new).build(),
|
||||
+ start(Registries.STRUCTURE_TYPE, RegistryKey.STRUCTURE_TYPE).craft(StructureType.class, CraftStructureType::new).build(),
|
||||
+ start(Registries.MOB_EFFECT, RegistryKey.MOB_EFFECT).craft(PotionEffectType.class, CraftPotionEffectType::new).build(),
|
||||
+ start(Registries.BLOCK, RegistryKey.BLOCK).craft(BlockType.class, CraftBlockType::new).build(),
|
||||
+ start(Registries.ITEM, RegistryKey.ITEM).craft(ItemType.class, CraftItemType::new).build(),
|
||||
+ start(Registries.CAT_VARIANT, RegistryKey.CAT_VARIANT).craft(Cat.Type.class, CraftCat.CraftType::new).build(),
|
||||
+ start(Registries.FROG_VARIANT, RegistryKey.FROG_VARIANT).craft(Frog.Variant.class, CraftFrog.CraftVariant::new).build(),
|
||||
+ start(Registries.VILLAGER_PROFESSION, RegistryKey.VILLAGER_PROFESSION).craft(Villager.Profession.class, CraftVillager.CraftProfession::new).build(),
|
||||
+ start(Registries.VILLAGER_TYPE, RegistryKey.VILLAGER_TYPE).craft(Villager.Type.class, CraftVillager.CraftType::new).build(),
|
||||
+ start(Registries.MAP_DECORATION_TYPE, RegistryKey.MAP_DECORATION_TYPE).craft(MapCursor.Type.class, CraftMapCursor.CraftType::new).build(),
|
||||
+ start(Registries.MENU, RegistryKey.MENU).craft(MenuType.class, CraftMenuType::new).build(),
|
||||
+ start(Registries.ATTRIBUTE, RegistryKey.ATTRIBUTE).craft(Attribute.class, CraftAttribute::new).build(),
|
||||
+ start(Registries.FLUID, RegistryKey.FLUID).craft(Fluid.class, CraftFluid::new).build(),
|
||||
+ start(Registries.SOUND_EVENT, RegistryKey.SOUND_EVENT).craft(Sound.class, CraftSound::new).build(),
|
||||
+
|
||||
+ // data-drivens
|
||||
+ entry(Registries.BIOME, RegistryKey.BIOME, Biome.class, CraftBiome::new).delayed(),
|
||||
+ entry(Registries.STRUCTURE, RegistryKey.STRUCTURE, Structure.class, CraftStructure::new).delayed(),
|
||||
+ entry(Registries.TRIM_MATERIAL, RegistryKey.TRIM_MATERIAL, TrimMaterial.class, CraftTrimMaterial::new).delayed(),
|
||||
+ entry(Registries.TRIM_PATTERN, RegistryKey.TRIM_PATTERN, TrimPattern.class, CraftTrimPattern::new).delayed(),
|
||||
+ entry(Registries.DAMAGE_TYPE, RegistryKey.DAMAGE_TYPE, DamageType.class, CraftDamageType::new).delayed(),
|
||||
+ entry(Registries.WOLF_VARIANT, RegistryKey.WOLF_VARIANT, Wolf.Variant.class, CraftWolf.CraftVariant::new).delayed(),
|
||||
+ entry(Registries.ENCHANTMENT, RegistryKey.ENCHANTMENT, Enchantment.class, CraftEnchantment::new).withSerializationUpdater(FieldRename.ENCHANTMENT_RENAME).delayed(),
|
||||
+ entry(Registries.JUKEBOX_SONG, RegistryKey.JUKEBOX_SONG, JukeboxSong.class, CraftJukeboxSong::new).delayed(),
|
||||
+ entry(Registries.BANNER_PATTERN, RegistryKey.BANNER_PATTERN, PatternType.class, CraftPatternType::new).delayed(),
|
||||
+ entry(Registries.PAINTING_VARIANT, RegistryKey.PAINTING_VARIANT, Art.class, CraftArt::new).delayed(),
|
||||
+ entry(Registries.INSTRUMENT, RegistryKey.INSTRUMENT, MusicInstrument.class, CraftMusicInstrument::new).delayed(),
|
||||
+ start(Registries.BIOME, RegistryKey.BIOME).craft(Biome.class, CraftBiome::new).build().delayed(),
|
||||
+ start(Registries.STRUCTURE, RegistryKey.STRUCTURE).craft(Structure.class, CraftStructure::new).build().delayed(),
|
||||
+ start(Registries.TRIM_MATERIAL, RegistryKey.TRIM_MATERIAL).craft(TrimMaterial.class, CraftTrimMaterial::new).build().delayed(),
|
||||
+ start(Registries.TRIM_PATTERN, RegistryKey.TRIM_PATTERN).craft(TrimPattern.class, CraftTrimPattern::new).build().delayed(),
|
||||
+ start(Registries.DAMAGE_TYPE, RegistryKey.DAMAGE_TYPE).craft(DamageType.class, CraftDamageType::new).build().delayed(),
|
||||
+ start(Registries.WOLF_VARIANT, RegistryKey.WOLF_VARIANT).craft(Wolf.Variant.class, CraftWolf.CraftVariant::new).build().delayed(),
|
||||
+ start(Registries.ENCHANTMENT, RegistryKey.ENCHANTMENT).craft(Enchantment.class, CraftEnchantment::new).build().withSerializationUpdater(FieldRename.ENCHANTMENT_RENAME).delayed(),
|
||||
+ start(Registries.JUKEBOX_SONG, RegistryKey.JUKEBOX_SONG).craft(JukeboxSong.class, CraftJukeboxSong::new).build().delayed(),
|
||||
+ start(Registries.BANNER_PATTERN, RegistryKey.BANNER_PATTERN).craft(PatternType.class, CraftPatternType::new).build().delayed(),
|
||||
+ start(Registries.PAINTING_VARIANT, RegistryKey.PAINTING_VARIANT).craft(Art.class, CraftArt::new).build().delayed(),
|
||||
+ start(Registries.INSTRUMENT, RegistryKey.INSTRUMENT).craft(MusicInstrument.class, CraftMusicInstrument::new).build().delayed(),
|
||||
+
|
||||
+ // api-only
|
||||
+ apiOnly(Registries.ENTITY_TYPE, RegistryKey.ENTITY_TYPE, PaperSimpleRegistry::entityType),
|
||||
+ apiOnly(Registries.PARTICLE_TYPE, RegistryKey.PARTICLE_TYPE, PaperSimpleRegistry::particleType),
|
||||
+ apiOnly(Registries.POTION, RegistryKey.POTION, PaperSimpleRegistry::potion),
|
||||
+ apiOnly(Registries.MEMORY_MODULE_TYPE, RegistryKey.MEMORY_MODULE_TYPE, () -> (org.bukkit.Registry<MemoryKey<?>>) (org.bukkit.Registry) org.bukkit.Registry.MEMORY_MODULE_TYPE)
|
||||
+ start(Registries.ENTITY_TYPE, RegistryKey.ENTITY_TYPE).apiOnly(PaperSimpleRegistry::entityType),
|
||||
+ start(Registries.PARTICLE_TYPE, RegistryKey.PARTICLE_TYPE).apiOnly(PaperSimpleRegistry::particleType),
|
||||
+ start(Registries.POTION, RegistryKey.POTION).apiOnly(PaperSimpleRegistry::potion),
|
||||
+ start(Registries.MEMORY_MODULE_TYPE, RegistryKey.MEMORY_MODULE_TYPE).apiOnly(() -> (org.bukkit.Registry<MemoryKey<?>>) (org.bukkit.Registry) org.bukkit.Registry.MEMORY_MODULE_TYPE)
|
||||
+ );
|
||||
+ final Map<RegistryKey<?>, RegistryEntry<?, ?>> byRegistryKey = new IdentityHashMap<>(REGISTRY_ENTRIES.size());
|
||||
+ final Map<ResourceKey<?>, RegistryEntry<?, ?>> byResourceKey = new IdentityHashMap<>(REGISTRY_ENTRIES.size());
|
||||
@@ -467,9 +466,12 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
+package io.papermc.paper.registry.entry;
|
||||
+
|
||||
+import com.google.common.base.Preconditions;
|
||||
+import com.mojang.datafixers.util.Either;
|
||||
+import io.papermc.paper.registry.RegistryHolder;
|
||||
+import io.papermc.paper.registry.RegistryKey;
|
||||
+import java.util.function.BiFunction;
|
||||
+import java.util.function.Function;
|
||||
+import net.minecraft.core.Holder;
|
||||
+import net.minecraft.core.Registry;
|
||||
+import net.minecraft.resources.ResourceKey;
|
||||
+import org.bukkit.Keyed;
|
||||
@@ -482,14 +484,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
+ private static final BiFunction<NamespacedKey, ApiVersion, NamespacedKey> EMPTY = (namespacedKey, apiVersion) -> namespacedKey;
|
||||
+
|
||||
+ protected final Class<?> classToPreload;
|
||||
+ protected final BiFunction<NamespacedKey, M, B> minecraftToBukkit;
|
||||
+ protected final RegistryTypeMapper<M, B> minecraftToBukkit;
|
||||
+ protected BiFunction<NamespacedKey, ApiVersion, NamespacedKey> updater = EMPTY;
|
||||
+
|
||||
+ protected CraftRegistryEntry(
|
||||
+ final ResourceKey<? extends Registry<M>> mcKey,
|
||||
+ final RegistryKey<B> apiKey,
|
||||
+ final Class<?> classToPreload,
|
||||
+ final BiFunction<NamespacedKey, M, B> minecraftToBukkit
|
||||
+ final RegistryTypeMapper<M, B> minecraftToBukkit
|
||||
+ ) {
|
||||
+ super(mcKey, apiKey);
|
||||
+ Preconditions.checkArgument(!classToPreload.getPackageName().startsWith("net.minecraft"), classToPreload + " should not be in the net.minecraft package as the class-to-preload");
|
||||
@@ -524,9 +526,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
+import io.papermc.paper.registry.RegistryKey;
|
||||
+import io.papermc.paper.registry.legacy.DelayedRegistryEntry;
|
||||
+import java.util.function.BiFunction;
|
||||
+import java.util.function.Supplier;
|
||||
+import net.minecraft.core.Registry;
|
||||
+import net.minecraft.resources.ResourceKey;
|
||||
+import org.bukkit.Keyed;
|
||||
+import org.bukkit.NamespacedKey;
|
||||
+import org.bukkit.craftbukkit.util.ApiVersion;
|
||||
@@ -548,22 +548,74 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
+ default RegistryEntry<M, B> delayed() {
|
||||
+ return new DelayedRegistryEntry<>(this);
|
||||
+ }
|
||||
+}
|
||||
diff --git a/src/main/java/io/papermc/paper/registry/entry/RegistryEntryBuilder.java b/src/main/java/io/papermc/paper/registry/entry/RegistryEntryBuilder.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/io/papermc/paper/registry/entry/RegistryEntryBuilder.java
|
||||
@@ -0,0 +0,0 @@
|
||||
+package io.papermc.paper.registry.entry;
|
||||
+
|
||||
+ static <M, B extends Keyed> RegistryEntry<M, B> entry(
|
||||
+import com.mojang.datafixers.util.Either;
|
||||
+import io.papermc.paper.registry.RegistryKey;
|
||||
+import java.util.function.BiFunction;
|
||||
+import java.util.function.Function;
|
||||
+import java.util.function.Supplier;
|
||||
+import net.minecraft.core.Holder;
|
||||
+import net.minecraft.core.Registry;
|
||||
+import net.minecraft.resources.ResourceKey;
|
||||
+import org.bukkit.Keyed;
|
||||
+import org.bukkit.NamespacedKey;
|
||||
+
|
||||
+public class RegistryEntryBuilder<M, A extends Keyed> { // TODO remove Keyed
|
||||
+
|
||||
+ public static <M, A extends Keyed> RegistryEntryBuilder<M, A> start( // TODO remove Keyed
|
||||
+ final ResourceKey<? extends Registry<M>> mcKey,
|
||||
+ final RegistryKey<B> apiKey,
|
||||
+ final Class<?> classToPreload,
|
||||
+ final BiFunction<NamespacedKey, M, B> minecraftToBukkit
|
||||
+ final RegistryKey<A> apiKey
|
||||
+ ) {
|
||||
+ return new CraftRegistryEntry<>(mcKey, apiKey, classToPreload, minecraftToBukkit);
|
||||
+ return new RegistryEntryBuilder<>(mcKey, apiKey);
|
||||
+ }
|
||||
+
|
||||
+ static <M, B extends Keyed> RegistryEntry<M, B> apiOnly(
|
||||
+ final ResourceKey<? extends Registry<M>> mcKey,
|
||||
+ final RegistryKey<B> apiKey,
|
||||
+ final Supplier<org.bukkit.Registry<B>> apiRegistrySupplier
|
||||
+ ) {
|
||||
+ return new ApiRegistryEntry<>(mcKey, apiKey, apiRegistrySupplier);
|
||||
+ protected final ResourceKey<? extends Registry<M>> mcKey;
|
||||
+ protected final RegistryKey<A> apiKey;
|
||||
+
|
||||
+ private RegistryEntryBuilder(final ResourceKey<? extends Registry<M>> mcKey, RegistryKey<A> apiKey) {
|
||||
+ this.mcKey = mcKey;
|
||||
+ this.apiKey = apiKey;
|
||||
+ }
|
||||
+
|
||||
+ public RegistryEntry<M, A> apiOnly(final Supplier<org.bukkit.Registry<A>> apiRegistrySupplier) {
|
||||
+ return new ApiRegistryEntry<>(this.mcKey, this.apiKey, apiRegistrySupplier);
|
||||
+ }
|
||||
+
|
||||
+ public CraftStage<M, A> craft(final Class<?> classToPreload, final BiFunction<? super NamespacedKey, M, ? extends A> minecraftToBukkit) {
|
||||
+ return new CraftStage<>(this.mcKey, this.apiKey, classToPreload, new RegistryTypeMapper<>(minecraftToBukkit));
|
||||
+ }
|
||||
+
|
||||
+ public CraftStage<M, A> craft(final Class<?> classToPreload, final Function<Holder<M>, ? extends A> minecraftToBukkit) {
|
||||
+ return new CraftStage<>(this.mcKey, this.apiKey, classToPreload, new RegistryTypeMapper<>(minecraftToBukkit));
|
||||
+ }
|
||||
+
|
||||
+ public static final class CraftStage<M, A extends Keyed> extends RegistryEntryBuilder<M, A> { // TODO remove Keyed
|
||||
+
|
||||
+ private final Class<?> classToPreload;
|
||||
+ private final RegistryTypeMapper<M, A> minecraftToBukkit;
|
||||
+
|
||||
+ private CraftStage(
|
||||
+ final ResourceKey<? extends Registry<M>> mcKey,
|
||||
+ final RegistryKey<A> apiKey,
|
||||
+ final Class<?> classToPreload,
|
||||
+ final RegistryTypeMapper<M, A> minecraftToBukkit
|
||||
+ ) {
|
||||
+ super(mcKey, apiKey);
|
||||
+ this.classToPreload = classToPreload;
|
||||
+ this.minecraftToBukkit = minecraftToBukkit;
|
||||
+ }
|
||||
+
|
||||
+ public RegistryEntry<M, A> build() {
|
||||
+ return new CraftRegistryEntry<>(this.mcKey, this.apiKey, this.classToPreload, this.minecraftToBukkit);
|
||||
+ }
|
||||
+ }
|
||||
+}
|
||||
diff --git a/src/main/java/io/papermc/paper/registry/entry/RegistryEntryInfo.java b/src/main/java/io/papermc/paper/registry/entry/RegistryEntryInfo.java
|
||||
@@ -584,6 +636,49 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
+
|
||||
+ RegistryKey<B> apiKey();
|
||||
+}
|
||||
diff --git a/src/main/java/io/papermc/paper/registry/entry/RegistryTypeMapper.java b/src/main/java/io/papermc/paper/registry/entry/RegistryTypeMapper.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/io/papermc/paper/registry/entry/RegistryTypeMapper.java
|
||||
@@ -0,0 +0,0 @@
|
||||
+package io.papermc.paper.registry.entry;
|
||||
+
|
||||
+import com.google.common.base.Preconditions;
|
||||
+import com.mojang.datafixers.util.Either;
|
||||
+import java.util.function.BiFunction;
|
||||
+import java.util.function.Function;
|
||||
+import net.minecraft.core.Holder;
|
||||
+import org.bukkit.NamespacedKey;
|
||||
+
|
||||
+public final class RegistryTypeMapper<M, A> {
|
||||
+
|
||||
+ final Either<BiFunction<? super NamespacedKey, M, ? extends A>, Function<Holder<M>, ? extends A>> minecraftToBukkit;
|
||||
+
|
||||
+ public RegistryTypeMapper(final BiFunction<? super NamespacedKey, M, ? extends A> byValueCreator) {
|
||||
+ this.minecraftToBukkit = Either.left(byValueCreator);
|
||||
+ }
|
||||
+
|
||||
+ public RegistryTypeMapper(final Function<Holder<M>, ? extends A> byHolderCreator) {
|
||||
+ this.minecraftToBukkit = Either.right(byHolderCreator);
|
||||
+ }
|
||||
+
|
||||
+ public A createBukkit(final NamespacedKey key, final Holder<M> minecraft) {
|
||||
+ return this.minecraftToBukkit.map(
|
||||
+ minecraftToBukkit -> minecraftToBukkit.apply(key, minecraft.value()),
|
||||
+ minecraftToBukkit -> minecraftToBukkit.apply(minecraft)
|
||||
+ );
|
||||
+ }
|
||||
+
|
||||
+ public boolean supportsDirectHolders() {
|
||||
+ return this.minecraftToBukkit.right().isPresent();
|
||||
+ }
|
||||
+
|
||||
+ public A convertDirectHolder(final Holder<M> directHolder) {
|
||||
+ Preconditions.checkArgument(this.supportsDirectHolders() && directHolder.kind() == Holder.Kind.DIRECT);
|
||||
+ return this.minecraftToBukkit.right().orElseThrow().apply(directHolder);
|
||||
+ }
|
||||
+}
|
||||
diff --git a/src/main/java/io/papermc/paper/registry/entry/package-info.java b/src/main/java/io/papermc/paper/registry/entry/package-info.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
|
||||
@@ -748,6 +843,26 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
+package io.papermc.paper.registry;
|
||||
+
|
||||
+import org.jspecify.annotations.NullMarked;
|
||||
diff --git a/src/main/java/io/papermc/paper/util/Holderable.java b/src/main/java/io/papermc/paper/util/Holderable.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/io/papermc/paper/util/Holderable.java
|
||||
@@ -0,0 +0,0 @@
|
||||
+package io.papermc.paper.util;
|
||||
+
|
||||
+import net.minecraft.core.Holder;
|
||||
+import org.bukkit.craftbukkit.util.Handleable;
|
||||
+
|
||||
+public interface Holderable<M> extends Handleable<M> {
|
||||
+
|
||||
+ Holder<M> getHolder();
|
||||
+
|
||||
+ @Override
|
||||
+ default M getHandle() {
|
||||
+ return this.getHolder().value();
|
||||
+ }
|
||||
+}
|
||||
diff --git a/src/main/java/net/minecraft/core/registries/BuiltInRegistries.java b/src/main/java/net/minecraft/core/registries/BuiltInRegistries.java
|
||||
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
||||
--- a/src/main/java/net/minecraft/core/registries/BuiltInRegistries.java
|
||||
@@ -784,10 +899,83 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
Map<ResourceLocation, T> map = new HashMap<>();
|
||||
SimpleJsonResourceReloadListener.scanDirectory(resourceManager, type.registryKey(), ops, type.codec(), map);
|
||||
map.forEach((id, value) -> writableRegistry.register(ResourceKey.create(type.registryKey(), id), (T)value, DEFAULT_REGISTRATION_INFO));
|
||||
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftParticle.java b/src/main/java/org/bukkit/craftbukkit/CraftParticle.java
|
||||
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
||||
--- a/src/main/java/org/bukkit/craftbukkit/CraftParticle.java
|
||||
+++ b/src/main/java/org/bukkit/craftbukkit/CraftParticle.java
|
||||
@@ -0,0 +0,0 @@ public abstract class CraftParticle<D> implements Keyed {
|
||||
}
|
||||
|
||||
public CraftParticleRegistry(net.minecraft.core.Registry<net.minecraft.core.particles.ParticleType<?>> minecraftRegistry) {
|
||||
- super(CraftParticle.class, minecraftRegistry, null, FieldRename.PARTICLE_TYPE_RENAME);
|
||||
+ super(CraftParticle.class, minecraftRegistry, CraftParticleRegistry::createBukkit, FieldRename.PARTICLE_TYPE_RENAME); // Paper - switch to Holder
|
||||
}
|
||||
|
||||
- @Override
|
||||
- public CraftParticle<?> createBukkit(NamespacedKey namespacedKey, net.minecraft.core.particles.ParticleType<?> particle) {
|
||||
+ public static CraftParticle<?> createBukkit(NamespacedKey namespacedKey, net.minecraft.core.particles.ParticleType<?> particle) { // Paper - idk why this is a separate implementation, just wrap the function
|
||||
if (particle == null) {
|
||||
return null;
|
||||
}
|
||||
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java b/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java
|
||||
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
||||
--- a/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java
|
||||
+++ b/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java
|
||||
@@ -0,0 +0,0 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
|
||||
Preconditions.checkArgument(minecraft != null);
|
||||
|
||||
net.minecraft.core.Registry<M> registry = CraftRegistry.getMinecraftRegistry(registryKey);
|
||||
- B bukkit = bukkitRegistry.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft)
|
||||
- .orElseThrow(() -> new IllegalStateException(String.format("Cannot convert '%s' to bukkit representation, since it is not registered.", minecraft))).location()));
|
||||
+ // Paper start - support direct Holders
|
||||
+ final java.util.Optional<ResourceKey<M>> resourceKey = registry.getResourceKey(minecraft);
|
||||
+ if (resourceKey.isEmpty() && bukkitRegistry instanceof final CraftRegistry<?, ?> craftRegistry && craftRegistry.supportsDirectHolders()) {
|
||||
+ return ((CraftRegistry<B, M>) registry).convertDirectHolder(Holder.direct(minecraft));
|
||||
+ } else if (resourceKey.isEmpty()) {
|
||||
+ throw new IllegalStateException(String.format("Cannot convert '%s' to bukkit representation, since it is not registered.", minecraft));
|
||||
+ }
|
||||
+ final B bukkit = bukkitRegistry.get(CraftNamespacedKey.fromMinecraft(resourceKey.get().location()));
|
||||
+ // Paper end - support direct Holders
|
||||
+
|
||||
+ Preconditions.checkArgument(bukkit != null);
|
||||
+
|
||||
+ return bukkit;
|
||||
+ }
|
||||
|
||||
+ // Paper start - support direct Holders
|
||||
+ public static <B extends Keyed, M> B minecraftHolderToBukkit(final Holder<M> minecraft, final Registry<B> bukkitRegistry) {
|
||||
+ Preconditions.checkArgument(minecraft != null);
|
||||
+
|
||||
+ final B bukkit = switch (minecraft) {
|
||||
+ case final Holder.Direct<M> direct -> {
|
||||
+ if (!(bukkitRegistry instanceof final CraftRegistry<?, ?> craftRegistry) || !craftRegistry.supportsDirectHolders()) {
|
||||
+ throw new IllegalArgumentException("Cannot convert direct holder to bukkit representation");
|
||||
+ }
|
||||
+ yield ((CraftRegistry<B, M>) bukkitRegistry).convertDirectHolder(direct);
|
||||
+ }
|
||||
+ case final Holder.Reference<M> reference -> bukkitRegistry.get(io.papermc.paper.util.MCUtil.fromResourceKey(reference.key()));
|
||||
+ default -> throw new IllegalArgumentException("Unknown holder: " + minecraft);
|
||||
+ };
|
||||
Preconditions.checkArgument(bukkit != null);
|
||||
|
||||
return bukkit;
|
||||
}
|
||||
+ // Paper end - support direct Holders
|
||||
|
||||
/**
|
||||
* Usage note: Only use this method to delegate the conversion methods from the individual Craft classes to here.
|
||||
@@ -0,0 +0,0 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
|
||||
|
||||
public static <B extends Keyed, M> Holder<M> bukkitToMinecraftHolder(B bukkit, ResourceKey<net.minecraft.core.Registry<M>> registryKey) {
|
||||
Preconditions.checkArgument(bukkit != null);
|
||||
+ // Paper start - support direct Holder
|
||||
+ if (bukkit instanceof io.papermc.paper.util.Holderable<?>) {
|
||||
+ return ((io.papermc.paper.util.Holderable<M>) bukkit).getHolder();
|
||||
+ }
|
||||
+ // Paper end - support direct Holder
|
||||
|
||||
net.minecraft.core.Registry<M> registry = CraftRegistry.getMinecraftRegistry(registryKey);
|
||||
|
||||
@@ -0,0 +0,0 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
|
||||
+ ", this can happen if a plugin creates its own registry entry with out properly registering it.");
|
||||
}
|
||||
@@ -896,13 +1084,19 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
+ private final Class<?> bukkitClass; // Paper - relax preload class
|
||||
private final Map<NamespacedKey, B> cache = new HashMap<>();
|
||||
private final net.minecraft.core.Registry<M> minecraftRegistry;
|
||||
private final BiFunction<NamespacedKey, M, B> minecraftToBukkit;
|
||||
- private final BiFunction<NamespacedKey, M, B> minecraftToBukkit;
|
||||
- private final BiFunction<NamespacedKey, ApiVersion, NamespacedKey> updater;
|
||||
+ private final io.papermc.paper.registry.entry.RegistryTypeMapper<M, B> minecraftToBukkit; // Paper - switch to Holder
|
||||
+ private final BiFunction<NamespacedKey, ApiVersion, NamespacedKey> serializationUpdater; // Paper - rename to make it *clear* what it is *only* for
|
||||
private boolean init;
|
||||
|
||||
- public CraftRegistry(Class<? super B> bukkitClass, net.minecraft.core.Registry<M> minecraftRegistry, BiFunction<NamespacedKey, M, B> minecraftToBukkit, BiFunction<NamespacedKey, ApiVersion, NamespacedKey> updater) {
|
||||
+ public CraftRegistry(Class<?> bukkitClass, net.minecraft.core.Registry<M> minecraftRegistry, BiFunction<NamespacedKey, M, B> minecraftToBukkit, BiFunction<NamespacedKey, ApiVersion, NamespacedKey> serializationUpdater) { // Paper - relax preload class
|
||||
+ public CraftRegistry(Class<?> bukkitClass, net.minecraft.core.Registry<M> minecraftRegistry, BiFunction<? super NamespacedKey, M, B> minecraftToBukkit, BiFunction<NamespacedKey, ApiVersion, NamespacedKey> serializationUpdater) { // Paper - relax preload class
|
||||
+ // Paper start - switch to Holder
|
||||
+ this(bukkitClass, minecraftRegistry, new io.papermc.paper.registry.entry.RegistryTypeMapper<>(minecraftToBukkit), serializationUpdater);
|
||||
+ }
|
||||
+ public CraftRegistry(Class<?> bukkitClass, net.minecraft.core.Registry<M> minecraftRegistry, io.papermc.paper.registry.entry.RegistryTypeMapper<M, B> minecraftToBukkit, BiFunction<NamespacedKey, ApiVersion, NamespacedKey> serializationUpdater) { // Paper - relax preload class
|
||||
+ // Paper end - support Holders
|
||||
this.bukkitClass = bukkitClass;
|
||||
this.minecraftRegistry = minecraftRegistry;
|
||||
this.minecraftToBukkit = minecraftToBukkit;
|
||||
@@ -917,6 +1111,39 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
|
||||
@Override
|
||||
public B get(NamespacedKey namespacedKey) {
|
||||
@@ -0,0 +0,0 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
|
||||
return this.get(namespacedKey);
|
||||
}
|
||||
|
||||
- B bukkit = this.createBukkit(namespacedKey, this.minecraftRegistry.getOptional(CraftNamespacedKey.toMinecraft(namespacedKey)).orElse(null));
|
||||
+ B bukkit = this.createBukkit(namespacedKey, this.minecraftRegistry.get(CraftNamespacedKey.toMinecraft(namespacedKey)).orElse(null)); // Paper - switch to Holder
|
||||
if (bukkit == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +0,0 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
|
||||
return this.stream().iterator();
|
||||
}
|
||||
|
||||
- public B createBukkit(NamespacedKey namespacedKey, M minecraft) {
|
||||
+ public B createBukkit(NamespacedKey namespacedKey, Holder<M> minecraft) { // Paper - switch to Holder
|
||||
if (minecraft == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
- return this.minecraftToBukkit.apply(namespacedKey, minecraft);
|
||||
+ return this.minecraftToBukkit.createBukkit(namespacedKey, minecraft); // Paper - switch to Holder
|
||||
+ }
|
||||
+
|
||||
+ // Paper start - support Direct Holders
|
||||
+ public boolean supportsDirectHolders() {
|
||||
+ return this.minecraftToBukkit.supportsDirectHolders();
|
||||
+ }
|
||||
+
|
||||
+ public B convertDirectHolder(Holder<M> holder) {
|
||||
+ return this.minecraftToBukkit.convertDirectHolder(holder);
|
||||
}
|
||||
+ // Paper end - support Direct Holders
|
||||
}
|
||||
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
||||
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
||||
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
||||
|
||||
Reference in New Issue
Block a user