2022-03-04 08:09:43 +00:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
|
From: Jake Potrebic <jake.m.potrebic@gmail.com>
|
|
|
|
Date: Wed, 2 Mar 2022 13:33:08 -0800
|
|
|
|
Subject: [PATCH] Add PaperRegistry
|
|
|
|
|
|
|
|
PaperRegistry is a server-backed impl of bukkit's Registry interface
|
|
|
|
|
|
|
|
diff --git a/src/main/java/io/papermc/paper/registry/PaperRegistry.java b/src/main/java/io/papermc/paper/registry/PaperRegistry.java
|
|
|
|
new file mode 100644
|
2022-06-08 19:00:19 +00:00
|
|
|
index 0000000000000000000000000000000000000000..7c265d27da034986be73921d35bf08ae250b42f3
|
2022-03-04 08:09:43 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/src/main/java/io/papermc/paper/registry/PaperRegistry.java
|
2022-06-08 19:00:19 +00:00
|
|
|
@@ -0,0 +1,167 @@
|
2022-03-04 08:09:43 +00:00
|
|
|
+package io.papermc.paper.registry;
|
|
|
|
+
|
|
|
|
+import com.google.common.base.Preconditions;
|
|
|
|
+import com.google.common.base.Suppliers;
|
|
|
|
+import net.minecraft.core.Holder;
|
|
|
|
+import net.minecraft.core.Registry;
|
|
|
|
+import net.minecraft.core.RegistryAccess;
|
|
|
|
+import net.minecraft.resources.ResourceKey;
|
|
|
|
+import net.minecraft.resources.ResourceLocation;
|
|
|
|
+import net.minecraft.server.MinecraftServer;
|
|
|
|
+import org.bukkit.Keyed;
|
|
|
|
+import org.bukkit.NamespacedKey;
|
|
|
|
+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;
|
|
|
|
+
|
|
|
|
+import java.util.Collections;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Iterator;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.Objects;
|
|
|
|
+import java.util.Optional;
|
2022-04-03 01:10:13 +00:00
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
2022-06-08 19:00:19 +00:00
|
|
|
+import java.util.function.Consumer;
|
2022-03-04 08:09:43 +00:00
|
|
|
+import java.util.function.Supplier;
|
|
|
|
+
|
|
|
|
+@DefaultQualifier(NonNull.class)
|
|
|
|
+public abstract class PaperRegistry<API extends Keyed, MINECRAFT> implements org.bukkit.Registry<API> {
|
|
|
|
+
|
|
|
|
+ @SuppressWarnings("FieldMayBeFinal") // non-final for testing
|
|
|
|
+ private static Supplier<RegistryAccess> REGISTRY_ACCESS = Suppliers.memoize(() -> MinecraftServer.getServer().registryAccess());
|
|
|
|
+ private static final Map<RegistryKey<?, ?>, PaperRegistry<?, ?>> INTERNAL_REGISTRIES = new HashMap<>();
|
|
|
|
+ public static final Map<RegistryKey<?, ?>, PaperRegistry<?, ?>> REGISTRIES = Collections.unmodifiableMap(INTERNAL_REGISTRIES);
|
|
|
|
+ private static final Map<Class<?>, PaperRegistry<?, ?>> REGISTRY_BY_API_CLASS = new HashMap<>();
|
|
|
|
+ private static final Map<ResourceKey<? extends Registry<?>>, PaperRegistry<?, ?>> REGISTRY_BY_RES_KEY = new HashMap<>();
|
|
|
|
+
|
|
|
|
+ private boolean registered;
|
|
|
|
+ private final RegistryKey<API, MINECRAFT> registryKey;
|
|
|
|
+ private final Supplier<Registry<MINECRAFT>> registry;
|
2022-04-03 01:10:13 +00:00
|
|
|
+ private final Map<NamespacedKey, API> cache = new ConcurrentHashMap<>();
|
|
|
|
+ private final Map<NamespacedKey, ResourceKey<MINECRAFT>> resourceKeyCache = new ConcurrentHashMap<>();
|
2022-03-04 08:09:43 +00:00
|
|
|
+
|
|
|
|
+ public PaperRegistry(RegistryKey<API, MINECRAFT> registryKey) {
|
|
|
|
+ this.registryKey = registryKey;
|
|
|
|
+ this.registry = Suppliers.memoize(() -> REGISTRY_ACCESS.get().registryOrThrow(this.registryKey.resourceKey()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public @Nullable API get(NamespacedKey key) {
|
|
|
|
+ return this.cache.computeIfAbsent(key, k -> {
|
|
|
|
+ final @Nullable MINECRAFT nms = this.registry.get().get(CraftNamespacedKey.toMinecraft(k));
|
|
|
|
+ if (nms != null) {
|
|
|
|
+ return this.convertToApi(k, nms);
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
2022-06-08 19:00:19 +00:00
|
|
|
+ public abstract @Nullable API convertToApi(NamespacedKey key, MINECRAFT nms);
|
2022-03-04 08:09:43 +00:00
|
|
|
+
|
2022-06-08 19:00:19 +00:00
|
|
|
+ public API convertToApiOrThrow(ResourceLocation resourceLocation, MINECRAFT nms) {
|
|
|
|
+ return Objects.requireNonNull(this.convertToApi(resourceLocation, nms), resourceLocation + " has a null api representation");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public @Nullable API convertToApi(ResourceLocation resourceLocation, MINECRAFT nms) {
|
2022-03-04 08:09:43 +00:00
|
|
|
+ return this.convertToApi(CraftNamespacedKey.fromMinecraft(resourceLocation), nms);
|
|
|
|
+ }
|
|
|
|
+
|
2022-06-08 19:00:19 +00:00
|
|
|
+ public API convertToApiOrThrow(Holder<MINECRAFT> nmsHolder) {
|
|
|
|
+ return Objects.requireNonNull(this.convertToApi(nmsHolder), nmsHolder + " has a null api representation");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public @Nullable API convertToApi(Holder<MINECRAFT> nmsHolder) {
|
2022-03-04 08:09:43 +00:00
|
|
|
+ final Optional<ResourceKey<MINECRAFT>> key = nmsHolder.unwrapKey();
|
|
|
|
+ if (nmsHolder.isBound() && key.isPresent()) {
|
|
|
|
+ return this.convertToApi(key.get().location(), nmsHolder.value());
|
|
|
|
+ } else if (!nmsHolder.isBound() && key.isPresent()) {
|
|
|
|
+ return this.convertToApi(key.get().location(), this.registry.get().getOrThrow(key.get()));
|
|
|
|
+ } else if (nmsHolder.isBound() && key.isEmpty()) {
|
|
|
|
+ final @Nullable ResourceLocation loc = this.registry.get().getKey(nmsHolder.value());
|
|
|
|
+ if (loc != null) {
|
|
|
|
+ return this.convertToApi(loc, nmsHolder.value());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ throw new IllegalStateException("Cannot convert " + nmsHolder + " to an API type in: " + this.registryKey);
|
|
|
|
+ }
|
|
|
|
+
|
2022-06-08 19:00:19 +00:00
|
|
|
+ public void convertToApi(Iterable<Holder<MINECRAFT>> holders, Consumer<API> apiConsumer, boolean throwOnNull) {
|
|
|
|
+ for (Holder<MINECRAFT> holder : holders) {
|
|
|
|
+ final @Nullable API api = this.convertToApi(holder);
|
|
|
|
+ if (api == null && throwOnNull) {
|
|
|
|
+ throw new NullPointerException(holder + " has a null api representation");
|
|
|
|
+ } else if (api != null) {
|
|
|
|
+ apiConsumer.accept(api);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
2022-03-04 08:09:43 +00:00
|
|
|
+ public MINECRAFT getMinecraftValue(API apiValue) {
|
|
|
|
+ return this.registry.get().getOptional(CraftNamespacedKey.toMinecraft(apiValue.getKey())).orElseThrow();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Holder<MINECRAFT> getMinecraftHolder(API apiValue) {
|
2022-04-03 01:10:13 +00:00
|
|
|
+ return this.registry.get().getHolderOrThrow(this.resourceKeyCache.computeIfAbsent(apiValue.getKey(), key -> ResourceKey.create(this.registryKey.resourceKey(), CraftNamespacedKey.toMinecraft(key))));
|
2022-03-04 08:09:43 +00:00
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Iterator<API> iterator() {
|
|
|
|
+ return this.registry.get().keySet().stream().map(key -> this.get(CraftNamespacedKey.fromMinecraft(key))).iterator();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void clearCache() {
|
|
|
|
+ this.cache.clear();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void register() {
|
|
|
|
+ if (this.registered) {
|
|
|
|
+ throw new IllegalStateException("Already registered: " + this.registryKey.apiClass());
|
|
|
|
+ }
|
|
|
|
+ INTERNAL_REGISTRIES.put(this.registryKey, this);
|
|
|
|
+ REGISTRY_BY_API_CLASS.put(this.registryKey.apiClass(), this);
|
|
|
|
+ REGISTRY_BY_RES_KEY.put(this.registryKey.resourceKey(), this);
|
|
|
|
+ this.registered = true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean equals(@Nullable Object o) {
|
|
|
|
+ if (this == o) return true;
|
|
|
|
+ if (o == null || !PaperRegistry.class.isAssignableFrom(o.getClass())) return false;
|
|
|
|
+ PaperRegistry<?, ?> that = (PaperRegistry<?, ?>) o;
|
|
|
|
+ return this.registryKey.equals(that.registryKey);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public int hashCode() {
|
|
|
|
+ return Objects.hash(this.registryKey);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ protected static <T> Supplier<Registry<T>> registryFor(ResourceKey<? extends Registry<T>> registryKey) {
|
|
|
|
+ return Suppliers.memoize(() -> REGISTRY_ACCESS.get().registryOrThrow(registryKey));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void clearCaches() {
|
|
|
|
+ for (PaperRegistry<?, ?> registry : INTERNAL_REGISTRIES.values()) {
|
|
|
|
+ registry.clearCache();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
+ public static <T extends Keyed> PaperRegistry<T, ?> getRegistry(Class<T> classOfT) {
|
|
|
|
+ Preconditions.checkArgument(REGISTRY_BY_API_CLASS.containsKey(classOfT), "No registry for that type");
|
|
|
|
+ return (PaperRegistry<T, ?>) REGISTRY_BY_API_CLASS.get(classOfT);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
+ public static <T> PaperRegistry<?, T> getRegistry(ResourceKey<? extends Registry<T>> resourceKey) {
|
|
|
|
+ Preconditions.checkArgument(REGISTRY_BY_RES_KEY.containsKey(resourceKey));
|
|
|
|
+ return (PaperRegistry<?, T>) REGISTRY_BY_RES_KEY.get(resourceKey);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
+ public static <A extends Keyed, M> PaperRegistry<A, M> getRegistry(RegistryKey<A, M> registryKey) {
|
|
|
|
+ Preconditions.checkArgument(INTERNAL_REGISTRIES.containsKey(registryKey));
|
|
|
|
+ return (PaperRegistry<A, M>) INTERNAL_REGISTRIES.get(registryKey);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
diff --git a/src/main/java/io/papermc/paper/registry/RegistryKey.java b/src/main/java/io/papermc/paper/registry/RegistryKey.java
|
|
|
|
new file mode 100644
|
|
|
|
index 0000000000000000000000000000000000000000..6f39e343147803e15e7681c993b8797a629702e7
|
|
|
|
--- /dev/null
|
|
|
|
+++ b/src/main/java/io/papermc/paper/registry/RegistryKey.java
|
|
|
|
@@ -0,0 +1,8 @@
|
|
|
|
+package io.papermc.paper.registry;
|
|
|
|
+
|
|
|
|
+import net.minecraft.core.Registry;
|
|
|
|
+import net.minecraft.resources.ResourceKey;
|
|
|
|
+import org.bukkit.Keyed;
|
|
|
|
+
|
|
|
|
+public record RegistryKey<API extends Keyed, MINECRAFT>(Class<API> apiClass, ResourceKey<? extends Registry<MINECRAFT>> resourceKey) {
|
|
|
|
+}
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
|
2022-11-03 21:03:31 +00:00
|
|
|
index 17e0425a520bd95074bf34c262c36c5603266ea7..f05f5d13f23599dbb95a7062ebbb41334ec21f14 100644
|
2022-03-04 08:09:43 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
|
2022-09-26 08:02:51 +00:00
|
|
|
@@ -2020,6 +2020,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
2022-03-04 08:09:43 +00:00
|
|
|
this.packRepository.setSelected(dataPacks);
|
|
|
|
this.worldData.setDataPackConfig(MinecraftServer.getSelectedPacks(this.packRepository));
|
|
|
|
this.resources.managers.updateRegistryTags(this.registryAccess());
|
2022-06-08 07:30:41 +00:00
|
|
|
+ io.papermc.paper.registry.PaperRegistry.clearCaches(); // Paper
|
2022-03-04 08:09:43 +00:00
|
|
|
new io.papermc.paper.event.server.ServerResourcesReloadedEvent(cause).callEvent(); // Paper
|
2022-06-16 20:59:53 +00:00
|
|
|
// Paper start
|
|
|
|
if (Thread.currentThread() != this.serverThread) {
|
2022-03-04 08:09:43 +00:00
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
2022-11-23 04:53:50 +00:00
|
|
|
index 5656b0380c8a74084f5ff366c09419171b66f172..bd6a65a15e581e0f342d7dd9572651fa21ce3961 100644
|
2022-03-04 08:09:43 +00:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
2022-11-23 04:53:50 +00:00
|
|
|
@@ -517,6 +517,11 @@ public final class CraftMagicNumbers implements UnsafeValues {
|
2022-03-04 08:09:43 +00:00
|
|
|
public int nextEntityId() {
|
|
|
|
return net.minecraft.world.entity.Entity.nextEntityId();
|
|
|
|
}
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public <T extends org.bukkit.Keyed> Registry<T> registryFor(Class<T> classOfT) {
|
|
|
|
+ return io.papermc.paper.registry.PaperRegistry.getRegistry(classOfT);
|
|
|
|
+ }
|
|
|
|
// Paper end
|
|
|
|
|
|
|
|
/**
|
|
|
|
diff --git a/src/test/java/org/bukkit/support/AbstractTestingBase.java b/src/test/java/org/bukkit/support/AbstractTestingBase.java
|
2022-07-04 14:38:06 +00:00
|
|
|
index e73a9a957cd55bf838e301ed531295162f2cfb89..23435a5914a29ac4f87ad0f34ce83842422df49f 100644
|
2022-03-04 08:09:43 +00:00
|
|
|
--- a/src/test/java/org/bukkit/support/AbstractTestingBase.java
|
|
|
|
+++ b/src/test/java/org/bukkit/support/AbstractTestingBase.java
|
2022-07-04 14:38:06 +00:00
|
|
|
@@ -39,6 +39,15 @@ public abstract class AbstractTestingBase {
|
2022-03-04 08:09:43 +00:00
|
|
|
MultiPackResourceManager resourceManager = new MultiPackResourceManager(PackType.SERVER_DATA, Collections.singletonList(new VanillaPackResources(ServerPacksSource.BUILT_IN_METADATA, "minecraft")));
|
|
|
|
// add tags and loot tables for unit tests
|
2022-07-04 14:38:06 +00:00
|
|
|
REGISTRY_CUSTOM = RegistryAccess.builtinCopy().freeze();
|
2022-03-04 08:09:43 +00:00
|
|
|
+ // Paper start
|
|
|
|
+ try {
|
|
|
|
+ java.lang.reflect.Field field = io.papermc.paper.registry.PaperRegistry.class.getDeclaredField("REGISTRY_ACCESS");
|
|
|
|
+ field.trySetAccessible();
|
2022-07-04 14:38:06 +00:00
|
|
|
+ field.set(null, com.google.common.base.Suppliers.ofInstance(REGISTRY_CUSTOM));
|
2022-03-04 08:09:43 +00:00
|
|
|
+ } catch (ReflectiveOperationException ex) {
|
|
|
|
+ throw new IllegalStateException("Could not reflectively set RegistryAccess in PaperRegistry", ex);
|
|
|
|
+ }
|
|
|
|
+ // Paper end
|
|
|
|
// Register vanilla pack
|
2022-07-04 14:38:06 +00:00
|
|
|
DATA_PACK = ReloadableServerResources.loadResources(resourceManager, REGISTRY_CUSTOM, Commands.CommandSelection.DEDICATED, 0, MoreExecutors.directExecutor(), MoreExecutors.directExecutor()).join();
|
2022-03-04 08:09:43 +00:00
|
|
|
// Bind tags
|