Skip to content

Commit

Permalink
use RegistryBuilderFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Machine-Maker committed Jan 5, 2025
1 parent b24dba9 commit d4e8d54
Show file tree
Hide file tree
Showing 22 changed files with 223 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import io.papermc.paper.registry.RegistryBuilder;
import io.papermc.paper.registry.RegistryBuilderFactory;
import io.papermc.paper.registry.TypedKey;
import io.papermc.paper.util.Either;
import java.util.function.Consumer;
Expand Down Expand Up @@ -90,7 +91,7 @@ interface Builder extends JukeboxSongRegistryEntry, RegistryBuilder<JukeboxSong>
* @see #soundEvent(TypedKey)
*/
@Contract(value = "_ -> this", mutates = "this")
Builder soundEvent(Consumer<? super SoundEventRegistryEntry.Builder> soundEvent);
Builder soundEvent(Consumer<RegistryBuilderFactory<Sound, ? extends SoundEventRegistryEntry.Builder>> soundEvent);

/**
* Sets the description for this song.
Expand Down
58 changes: 58 additions & 0 deletions paper-api/src/main/java/io/papermc/paper/util/Either.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,69 @@
import org.jetbrains.annotations.Contract;
import org.jspecify.annotations.NullMarked;

/**
* A type that can be either a left value or a right value.
*
* @param <L> the type of the left value
* @param <R> the type of the right value
*/
@NullMarked
public sealed interface Either<L, R> permits Either.Left, Either.Right {

/**
* Create a new Either with a left value.
*
* @param value the left value
* @return a new Either with a left value
* @param <L> the type of the left value
* @param <R> the type of the right value
*/
@Contract(value = "_ -> new", pure = true)
static <L, R> Either.Left<L, R> left(final L value) {
return new EitherLeft<>(value);
}

/**
* Create a new Either with a right value.
*
* @param value the right value
* @return a new Either with a right value
* @param <L> the type of the left value
* @param <R> the type of the right value
*/
@Contract(value = "_ -> new", pure = true)
static <L, R> Either.Right<L, R> right(final R value) {
return new EitherRight<>(value);
}

/**
* Get an optional of the left value.
*
* @return an optional of the left value
*/
Optional<L> left();

/**
* Get an optional of the right value.
*
* @return an optional of the right value
*/
Optional<R> right();

/**
* A left value.
*
* @param <L> the type of the left value
* @param <R> the type of the right value
*/
sealed interface Left<L, R> extends Either<L, R> permits EitherLeft {

/**
* Get the left value.
*
* @return the left value
*/
@Contract(pure = true)
L value();

@Override
Expand All @@ -35,7 +80,20 @@ default Optional<R> right() {
}
}

/**
* A right value.
*
* @param <L> the type of the left value
* @param <R> the type of the right value
*/
sealed interface Right<L, R> extends Either<L, R> permits EitherRight {

/**
* Get the right value.
*
* @return the right value
*/
@Contract(pure = true)
R value();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29346,10 +29346,10 @@ index 0000000000000000000000000000000000000000..62c0f4073aff301bf5b3187e0d4446fd
+}
diff --git a/ca/spottedleaf/dataconverter/util/CommandArgumentUpgrader.java b/ca/spottedleaf/dataconverter/util/CommandArgumentUpgrader.java
new file mode 100644
index 0000000000000000000000000000000000000000..40da70d5cf584a9730f9fe81c355cf8513fba475
index 0000000000000000000000000000000000000000..9fab2371790596ab57298fe28b8983d85210c6d9
--- /dev/null
+++ b/ca/spottedleaf/dataconverter/util/CommandArgumentUpgrader.java
@@ -0,0 +1,592 @@
@@ -0,0 +1,597 @@
+package ca.spottedleaf.dataconverter.util;
+
+import ca.spottedleaf.dataconverter.minecraft.MCDataConverter;
Expand Down Expand Up @@ -29908,6 +29908,11 @@ index 0000000000000000000000000000000000000000..40da70d5cf584a9730f9fe81c355cf85
+ ) {
+ return Optional.of(new HolderLookup.RegistryLookup<T>() {
+ @Override
+ public Optional<T> getValueForCopying(final ResourceKey<T> resourceKey) {
+ return Optional.empty();
+ }
+
+ @Override
+ public ResourceKey<? extends Registry<? extends T>> key() {
+ return registryRef;
+ }
Expand Down Expand Up @@ -30621,7 +30626,7 @@ index 1110ca4075a1bbaa46b66686435dab91b275c945..c2218630c3074c8b3f82364e37503b12
return structureTemplate.save(new CompoundTag());
}
diff --git a/net/minecraft/server/MinecraftServer.java b/net/minecraft/server/MinecraftServer.java
index 0d65bf24f515b80701150fdc430f324a533cb478..b92a3da5c325e69f5601416d4205fb33429742b3 100644
index 4b9df7d4aab3f7dae2d3b8ced4663162334cdbe6..5ef7a016a0c7bc09d5cd24f14124b15b99d5e18d 100644
--- a/net/minecraft/server/MinecraftServer.java
+++ b/net/minecraft/server/MinecraftServer.java
@@ -305,6 +305,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23508,10 +23508,10 @@ index 3d3eec1db91cb47395f40c4f47aa77164ad42175..216f97207dac88cc1dc3df59c6ee8a62
+ // Paper end - optimise collisions
}
diff --git a/net/minecraft/core/MappedRegistry.java b/net/minecraft/core/MappedRegistry.java
index 452c358c2cfa0c39e0b09853cd4a9a12c6ced65d..5f752603aa5611ce9d3dd44cc5b70c27ac46a86e 100644
index 450a5d78d2b1885bc5c2db7e969f87a3db440bf0..5d560486d0765d3f1bc1d51ae6f7f676e8f2383f 100644
--- a/net/minecraft/core/MappedRegistry.java
+++ b/net/minecraft/core/MappedRegistry.java
@@ -51,6 +51,19 @@ public class MappedRegistry<T> implements WritableRegistry<T> {
@@ -58,6 +58,19 @@ public class MappedRegistry<T> implements WritableRegistry<T> {
return this.getTags();
}

Expand All @@ -23531,7 +23531,7 @@ index 452c358c2cfa0c39e0b09853cd4a9a12c6ced65d..5f752603aa5611ce9d3dd44cc5b70c27
public MappedRegistry(ResourceKey<? extends Registry<T>> key, Lifecycle registryLifecycle) {
this(key, registryLifecycle, false);
}
@@ -116,6 +129,7 @@ public class MappedRegistry<T> implements WritableRegistry<T> {
@@ -123,6 +136,7 @@ public class MappedRegistry<T> implements WritableRegistry<T> {
this.registrationInfos.put(key, registrationInfo);
this.registryLifecycle = this.registryLifecycle.add(registrationInfo.lifecycle());
this.temporaryUnfrozenMap.put(key.location(), value); // Paper - support pre-filling in registry mod API
Expand All @@ -23552,7 +23552,7 @@ index 2b46ca9a2a046063cad422bec00d76107537b091..9aa664537cc37e44db46d5a2a64ae311
thread1 -> {
DedicatedServer dedicatedServer1 = new DedicatedServer(
diff --git a/net/minecraft/server/MinecraftServer.java b/net/minecraft/server/MinecraftServer.java
index b92a3da5c325e69f5601416d4205fb33429742b3..d967d605c2e4227ae980c30f1c8b86edbc680d6d 100644
index 5ef7a016a0c7bc09d5cd24f14124b15b99d5e18d..2769b3cb17b8101dd90fb95d3853d6083fd0c3e2 100644
--- a/net/minecraft/server/MinecraftServer.java
+++ b/net/minecraft/server/MinecraftServer.java
@@ -173,7 +173,7 @@ import net.minecraft.world.phys.Vec2;
Expand Down Expand Up @@ -27496,7 +27496,7 @@ index 192977dd661ee795ada13db895db770293e9b402..95a4e37a3c93f9b3c56c7a7376ed521c
}

diff --git a/net/minecraft/server/level/ServerPlayer.java b/net/minecraft/server/level/ServerPlayer.java
index 097ec55166b9e9269142be58992c29687122fe28..aeabb79512aabd7a9e8af1be72e1745f0e7eefe4 100644
index 472693a7d10dc67b116db02e3e4472adbc5a4d62..422db52e8a0a08350542670bfc9ba94ad9481d0c 100644
--- a/net/minecraft/server/level/ServerPlayer.java
+++ b/net/minecraft/server/level/ServerPlayer.java
@@ -178,7 +178,7 @@ import net.minecraft.world.scores.Team;
Expand Down Expand Up @@ -28372,7 +28372,7 @@ index 8cc5c0716392ba06501542ff5cbe71ee43979e5d..09fd99c9cbd23b5f3c899bfb00c9b896
+ // Paper end - block counting
}
diff --git a/net/minecraft/world/entity/Entity.java b/net/minecraft/world/entity/Entity.java
index 189385600b9094291152035b17df869eaccc0428..25a1089a7376f0cbd96bb43b5c203640c88fc282 100644
index 8d7c1ee6c1cd4419621b11029eb06bf8135e59aa..1d0151a042ed5de4e235ef0bdac1a0e8240e85e7 100644
--- a/net/minecraft/world/entity/Entity.java
+++ b/net/minecraft/world/entity/Entity.java
@@ -135,7 +135,7 @@ import net.minecraft.world.scores.ScoreHolder;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--- a/net/minecraft/core/HolderLookup.java
+++ b/net/minecraft/core/HolderLookup.java
@@ -68,6 +_,9 @@
}

public interface RegistryLookup<T> extends HolderLookup<T>, HolderOwner<T> {
+
+ Optional<T> getValueForCopying(ResourceKey<T> resourceKey); // Paper - add method to get the value for pre-filling builders in the reg mod API
+
ResourceKey<? extends Registry<? extends T>> key();

Lifecycle registryLifecycle();
@@ -81,6 +_,11 @@
default HolderLookup.RegistryLookup<T> filterElements(final Predicate<T> predicate) {
return new HolderLookup.RegistryLookup.Delegate<T>() {
@Override
+ public Optional<T> getValueForCopying(final ResourceKey<T> resourceKey) {
+ return this.parent().getValueForCopying(resourceKey).filter(predicate);
+ }
+
+ @Override
public HolderLookup.RegistryLookup<T> parent() {
return RegistryLookup.this;
}
@@ -99,6 +_,11 @@

public interface Delegate<T> extends HolderLookup.RegistryLookup<T> {
HolderLookup.RegistryLookup<T> parent();
+
+ @Override
+ default Optional<T> getValueForCopying(ResourceKey<T> resourceKey) {
+ return this.parent().getValueForCopying(resourceKey);
+ }

@Override
default ResourceKey<? extends Registry<? extends T>> key() {
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--- a/net/minecraft/core/MappedRegistry.java
+++ b/net/minecraft/core/MappedRegistry.java
@@ -33,17 +_,18 @@
@@ -33,17 +_,25 @@
public class MappedRegistry<T> implements WritableRegistry<T> {
private final ResourceKey<? extends Registry<T>> key;
private final ObjectList<Holder.Reference<T>> byId = new ObjectArrayList<>(256);
Expand All @@ -20,7 +20,14 @@
private boolean frozen;
@Nullable
private Map<T, Holder.Reference<T>> unregisteredIntrusiveHolders;
+ public final Map<ResourceLocation, T> temporaryUnfrozenMap = new HashMap<>(); // Paper - support pre-filling in registry mod API
+ // Paper start - support pre-filling in registry mod API
+ private final Map<ResourceLocation, T> temporaryUnfrozenMap = new HashMap<>();
+
+ @Override
+ public Optional<T> getValueForCopying(final ResourceKey<T> resourceKey) {
+ return this.frozen ? this.getOptional(resourceKey) : Optional.ofNullable(this.temporaryUnfrozenMap.get(resourceKey.location()));
+ }
+ // Paper end - support pre-filling in registry mod API

@Override
public Stream<HolderSet.Named<T>> listTags() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--- a/net/minecraft/core/RegistrySetBuilder.java
+++ b/net/minecraft/core/RegistrySetBuilder.java
@@ -42,6 +_,11 @@
) {
return new RegistrySetBuilder.EmptyTagRegistryLookup<T>(owner) {
@Override
+ public Optional<T> getValueForCopying(final ResourceKey<T> resourceKey) {
+ return this.get(resourceKey).map(Holder.Reference::value);
+ }
+
+ @Override
public ResourceKey<? extends Registry<? extends T>> key() {
return registryKey;
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
--- a/net/minecraft/core/registries/BuiltInRegistries.java
+++ b/net/minecraft/core/registries/BuiltInRegistries.java
@@ -296,6 +_,17 @@
@@ -296,6 +_,11 @@
public static final Registry<SlotDisplay.Type<?>> SLOT_DISPLAY = registerSimple(Registries.SLOT_DISPLAY, SlotDisplays::bootstrap);
public static final Registry<RecipeBookCategory> RECIPE_BOOK_CATEGORY = registerSimple(Registries.RECIPE_BOOK_CATEGORY, RecipeBookCategories::bootstrap);
public static final Registry<? extends Registry<?>> REGISTRY = WRITABLE_REGISTRY;
+ // Paper start - add built-in registry conversions
+ public static final io.papermc.paper.registry.data.util.Conversions BUILT_IN_CONVERSIONS = new io.papermc.paper.registry.data.util.Conversions(new net.minecraft.resources.RegistryOps.RegistryInfoLookup() {
+ @Override
+ public <T> java.util.Optional<net.minecraft.resources.RegistryOps.RegistryInfo<T>> lookup(final ResourceKey<? extends Registry<? extends T>> registryRef) {
+ final Registry<T> registry = net.minecraft.server.RegistryLayer.STATIC_ACCESS.lookupOrThrow(registryRef);
+ return java.util.Optional.of(
+ new net.minecraft.resources.RegistryOps.RegistryInfo<>(registry, registry, Lifecycle.experimental())
+ );
+ }
+ });
+ public static final io.papermc.paper.registry.data.util.Conversions STATIC_ACCESS_CONVERSIONS = new io.papermc.paper.registry.data.util.Conversions(
+ new net.minecraft.resources.RegistryOps.HolderLookupAdapter(net.minecraft.server.RegistryLayer.STATIC_ACCESS)
+ );
+ // Paper end - add built-in registry conversions

private static <T> Registry<T> registerSimple(ResourceKey<? extends Registry<T>> key, BuiltInRegistries.RegistryBootstrap<T> bootstrap) {
Expand Down Expand Up @@ -65,7 +59,7 @@

for (Registry<?> registry : REGISTRY) {
bindBootstrappedTagsToEmpty(registry);
+ io.papermc.paper.registry.PaperRegistryListenerManager.INSTANCE.runFreezeListeners(registry.key(), BUILT_IN_CONVERSIONS); // Paper
+ io.papermc.paper.registry.PaperRegistryListenerManager.INSTANCE.runFreezeListeners(registry.key(), STATIC_ACCESS_CONVERSIONS); // Paper
registry.freeze();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import com.destroystokyo.paper.profile.PlayerProfile;
import com.google.common.base.Preconditions;
import io.papermc.paper.registry.PaperRegistries;
import io.papermc.paper.registry.data.util.Conversions;
import io.papermc.paper.registry.set.PaperRegistrySets;
import io.papermc.paper.registry.set.RegistryKeySet;
import io.papermc.paper.registry.tag.TagKey;
import io.papermc.paper.text.Filtered;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.util.TriState;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.item.component.OminousBottleAmplifier;
import org.bukkit.JukeboxSong;
Expand Down Expand Up @@ -208,7 +208,7 @@ public Enchantable enchantable(final int level) {
@Override
public Repairable repairable(final RegistryKeySet<ItemType> types) {
return new PaperRepairable(new net.minecraft.world.item.enchantment.Repairable(
PaperRegistrySets.convertToNms(Registries.ITEM, BuiltInRegistries.BUILT_IN_CONVERSIONS.lookup(), types)
PaperRegistrySets.convertToNms(Registries.ITEM, Conversions.global().lookup(), types)
));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package io.papermc.paper.datacomponent.item;

import io.papermc.paper.adventure.PaperAdventure;
import io.papermc.paper.registry.PaperRegistries;
import io.papermc.paper.registry.RegistryKey;
import io.papermc.paper.registry.data.util.Conversions;
import io.papermc.paper.registry.set.PaperRegistrySets;
import io.papermc.paper.registry.set.RegistryKeySet;
import java.util.Optional;
import java.util.function.Function;
import net.kyori.adventure.key.Key;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.util.datafix.fixes.EquippableAssetRenameFix;
import net.minecraft.world.item.equipment.EquipmentAsset;
import net.minecraft.world.item.equipment.EquipmentAssets;
import org.bukkit.craftbukkit.CraftEquipmentSlot;
Expand Down Expand Up @@ -133,7 +130,7 @@ public Builder cameraOverlay(@Nullable final Key cameraOverlay) {
@Override
public Builder allowedEntities(final @Nullable RegistryKeySet<EntityType> allowedEntities) {
this.allowedEntities = Optional.ofNullable(allowedEntities)
.map((set) -> PaperRegistrySets.convertToNms(Registries.ENTITY_TYPE, BuiltInRegistries.BUILT_IN_CONVERSIONS.lookup(), set));
.map((set) -> PaperRegistrySets.convertToNms(Registries.ENTITY_TYPE, Conversions.global().lookup(), set));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import io.papermc.paper.block.BlockPredicate;
import io.papermc.paper.registry.RegistryKey;
import io.papermc.paper.registry.data.util.Conversions;
import io.papermc.paper.registry.set.PaperRegistrySets;
import io.papermc.paper.util.MCUtil;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.util.List;
import java.util.Optional;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import org.bukkit.craftbukkit.util.Handleable;

Expand Down Expand Up @@ -48,7 +48,7 @@ static final class BuilderImpl implements ItemAdventurePredicate.Builder {
@Override
public ItemAdventurePredicate.Builder addPredicate(final BlockPredicate predicate) {
this.predicates.add(new net.minecraft.advancements.critereon.BlockPredicate(Optional.ofNullable(predicate.blocks()).map(
blocks -> PaperRegistrySets.convertToNms(Registries.BLOCK, BuiltInRegistries.BUILT_IN_CONVERSIONS.lookup(), blocks)
blocks -> PaperRegistrySets.convertToNms(Registries.BLOCK, Conversions.global().lookup(), blocks)
), Optional.empty(), Optional.empty()));
return this;
}
Expand Down
Loading

0 comments on commit d4e8d54

Please sign in to comment.