Registry Modification API

== AT ==
public net.minecraft.core.MappedRegistry validateWrite(Lnet/minecraft/resources/ResourceKey;)V
public net.minecraft.resources.RegistryOps lookupProvider
public net.minecraft.resources.RegistryOps$HolderLookupAdapter
This commit is contained in:
Jake Potrebic
2023-02-27 18:28:39 -08:00
parent 463f952cd4
commit a1b891dd88
35 changed files with 1109 additions and 11 deletions

View File

@@ -0,0 +1,44 @@
package io.papermc.paper.registry;
import io.papermc.paper.registry.data.util.Conversions;
import io.papermc.paper.registry.entry.RegistryEntry;
import io.papermc.paper.registry.entry.RegistryEntryInfo;
import io.papermc.paper.registry.legacy.DelayedRegistryEntry;
import java.util.Map;
import java.util.stream.Stream;
import net.minecraft.core.Registry;
import net.minecraft.resources.RegistryOps;
import net.minecraft.resources.ResourceKey;
import org.bukkit.Keyed;
import org.bukkit.support.RegistryHelper;
import org.bukkit.support.environment.AllFeatures;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
@AllFeatures
class RegistryBuilderTest {
static Stream<Arguments> registries() {
return PaperRegistries.REGISTRY_ENTRIES.stream()
.map(RegistryBuilderTest::possiblyUnwrap)
.filter(RegistryEntry.BuilderHolder.class::isInstance)
.map(Arguments::arguments);
}
private static <M, B extends Keyed> RegistryEntryInfo<M, B> possiblyUnwrap(final RegistryEntryInfo<M, B> entry) {
return entry instanceof final DelayedRegistryEntry<M, B> delayed ? delayed.delegate() : entry;
}
@ParameterizedTest
@MethodSource("registries")
<M, T> void testEquality(final RegistryEntry.BuilderHolder<M, T, ?> registryEntry) {
final Registry<M> registry = RegistryHelper.getRegistry().lookupOrThrow(registryEntry.mcKey());
for (final Map.Entry<ResourceKey<M>, M> entry : registry.entrySet()) {
final M built = registryEntry.fillBuilder(new Conversions(new RegistryOps.HolderLookupAdapter(RegistryHelper.getRegistry())), entry.getValue()).build();
assertEquals(entry.getValue(), built);
}
}
}

View File

@@ -111,7 +111,7 @@ public class RegistryClassTest {
outsideRequest.clear();
MockUtil.resetMock(spyRegistry);
doAnswer(invocation -> {
Keyed item = realRegistry.get(invocation.getArgument(0));
Keyed item = realRegistry.get((NamespacedKey) invocation.getArgument(0)); // Paper - registry modification api - specifically call namespaced key overload
if (item == null) {
nullValue.add(invocation.getArgument(0));
@@ -120,10 +120,10 @@ public class RegistryClassTest {
nullAble.add(invocation.getArgument(0));
return item;
}).when(spyRegistry).get(any());
}).when(spyRegistry).get((NamespacedKey) any()); // Paper - registry modification api - specifically call namespaced key overload
doAnswer(invocation -> {
Keyed item = realRegistry.get(invocation.getArgument(0));
Keyed item = realRegistry.get((NamespacedKey) invocation.getArgument(0)); // Paper - registry modification api - specifically call namespaced key overload
if (item == null) {
nullValue.add(invocation.getArgument(0));
@@ -138,7 +138,7 @@ public class RegistryClassTest {
notNullAble.add(invocation.getArgument(0));
return item;
}).when(spyRegistry).getOrThrow(any());
}).when(spyRegistry).getOrThrow((NamespacedKey) any()); // Paper - registry modification api - specifically call namespaced key overload
// Load class
try {
@@ -171,13 +171,13 @@ public class RegistryClassTest {
outsideRequest
.computeIfAbsent(type, ty -> new ArrayList<>()).add(invocation.getArgument(0));
return mock(type);
}).when(spyRegistry).get(any());
}).when(spyRegistry).get((NamespacedKey) any()); // Paper - registry modification api - specifically call namespaced key overload
doAnswer(invocation -> {
outsideRequest
.computeIfAbsent(type, ty -> new ArrayList<>()).add(invocation.getArgument(0));
return mock(type);
}).when(spyRegistry).getOrThrow(any());
}).when(spyRegistry).getOrThrow((NamespacedKey) any()); // Paper - registry modification api - specifically call namespaced key overload
}
private static void initFieldDataCache() {

View File

@@ -62,7 +62,7 @@ public class NormalExtension extends BaseExtension {
doAnswer(invocation ->
mocks.computeIfAbsent(invocation.getArgument(0), k -> mock(RegistryHelper.updateClass(keyed, invocation.getArgument(0)), withSettings().stubOnly().defaultAnswer(DEFAULT_ANSWER)))
).when(registry).get(any()); // Allow static classes to fill there fields, so that it does not error out, just by loading them
).when(registry).get((NamespacedKey) any()); // Allow static classes to fill there fields, so that it does not error out, just by loading them // Paper - registry modification api - specifically call namespaced key overload
return registry;
}