From 011b91b1da4ecf5c08483deb960d6f71a5e43fbd Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Mon, 23 Dec 2024 08:02:02 -0800 Subject: [PATCH] class load bukkit.Registry super early --- .editorconfig | 2 ++ .../paper/registry/PaperRegistryAccess.java | 13 +++---------- .../java/org/bukkit/craftbukkit/CraftRegistry.java | 14 ++++++++++---- .../bukkit/registry/RegistryConversionTest.java | 1 + .../org/bukkit/registry/RegistryLoadOrderTest.java | 3 ++- 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/.editorconfig b/.editorconfig index ac14754a32..65898b5181 100644 --- a/.editorconfig +++ b/.editorconfig @@ -31,6 +31,8 @@ ij_java_generate_final_locals = true ij_java_generate_final_parameters = true ij_java_method_parameters_new_line_after_left_paren = true ij_java_method_parameters_right_paren_on_new_line = true +ij_java_use_fq_class_names = false +ij_java_class_names_in_javadoc = 1 [paper-server/src/minecraft/java/**/*.java] ij_java_use_fq_class_names = true diff --git a/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistryAccess.java b/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistryAccess.java index 6400df6ea1..ab8fba3442 100644 --- a/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistryAccess.java +++ b/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistryAccess.java @@ -1,6 +1,5 @@ package io.papermc.paper.registry; -import com.google.common.base.Preconditions; import io.papermc.paper.registry.entry.RegistryEntry; import io.papermc.paper.registry.entry.RegistryEntryMeta; import io.papermc.paper.registry.legacy.DelayedRegistry; @@ -74,7 +73,7 @@ public class PaperRegistryAccess implements RegistryAccess { if (PaperRegistries.getEntry(key) == null) { throw new NoSuchElementException(key + " is not a valid registry key"); } - final @Nullable RegistryHolder registryHolder = (RegistryHolder) this.registries.get(key); + final RegistryHolder registryHolder = (RegistryHolder) this.registries.get(key); if (registryHolder == null) { throw new IllegalArgumentException(key + " points to a registry that is not available yet"); } @@ -112,22 +111,16 @@ public class PaperRegistryAccess implements RegistryAccess { return; } final CraftRegistry registry = (CraftRegistry) this.getRegistry(entry.apiKey()); - Preconditions.checkState(registry.isUnloaded(), "Registry %s is already loaded", resourceKey); - try { - Class.forName(serverSide.classToPreload().getName()); // this should always trigger the initialization of the class - } catch (final ClassNotFoundException e) { - throw new IllegalStateException("Failed to load class " + serverSide.classToPreload().getName(), e); - } registry.lockReferenceHolders(); } @SuppressWarnings("unchecked") // this method should be called right after any new MappedRegistry instances are created to later be used by the server. private > void registerRegistry(final ResourceKey> resourceKey, final net.minecraft.core.Registry registry, final boolean replace) { - final @Nullable RegistryEntry entry = PaperRegistries.getEntry(resourceKey); + final RegistryEntry entry = PaperRegistries.getEntry(resourceKey); if (entry == null) { // skip registries that don't have API entries return; } - final @Nullable RegistryHolder registryHolder = (RegistryHolder) this.registries.get(entry.apiKey()); + final RegistryHolder registryHolder = (RegistryHolder) this.registries.get(entry.apiKey()); if (registryHolder == null || replace) { // if the holder doesn't exist yet, or is marked as "replaceable", put it in the map. this.registries.put(entry.apiKey(), entry.createRegistryHolder(registry)); diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java index ab0a1eed5c..946beee658 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java @@ -181,11 +181,17 @@ public class CraftRegistry implements Registry { this.lockReferenceHolders = !this.minecraftToBukkit.supportsDirectHolders(); } - public boolean isUnloaded() { - return this.cache.isEmpty(); - } - public void lockReferenceHolders() { + Preconditions.checkState(this.cache.isEmpty(), "Registry %s is already loaded", this.minecraftRegistry.key()); + + try { + Class.forName(this.bukkitClass.getName()); // this should always trigger the initialization of the class + } catch (final ClassNotFoundException e) { + throw new IllegalStateException("Failed to load class " + this.bukkitClass.getSimpleName(), e); + } + if (!this.minecraftToBukkit.supportsDirectHolders()) { + return; + } Preconditions.checkState(!this.lockReferenceHolders, "Reference holders are already locked"); this.lockReferenceHolders = true; } diff --git a/paper-server/src/test/java/org/bukkit/registry/RegistryConversionTest.java b/paper-server/src/test/java/org/bukkit/registry/RegistryConversionTest.java index 293af3511c..f640c0c252 100644 --- a/paper-server/src/test/java/org/bukkit/registry/RegistryConversionTest.java +++ b/paper-server/src/test/java/org/bukkit/registry/RegistryConversionTest.java @@ -16,6 +16,7 @@ import net.minecraft.resources.ResourceKey; import org.bukkit.Bukkit; import org.bukkit.Keyed; import org.bukkit.Registry; +import org.bukkit.craftbukkit.CraftRegistry; import org.bukkit.craftbukkit.util.Handleable; import org.bukkit.support.environment.AllFeatures; import org.bukkit.support.provider.RegistryArgumentProvider; diff --git a/paper-server/src/test/java/org/bukkit/registry/RegistryLoadOrderTest.java b/paper-server/src/test/java/org/bukkit/registry/RegistryLoadOrderTest.java index 0c704fb9e7..d548ff4a96 100644 --- a/paper-server/src/test/java/org/bukkit/registry/RegistryLoadOrderTest.java +++ b/paper-server/src/test/java/org/bukkit/registry/RegistryLoadOrderTest.java @@ -25,7 +25,7 @@ public class RegistryLoadOrderTest { private static boolean initInterface = false; private static boolean initAbstract = false; - private static org.bukkit.Registry registry; // Paper - remap fix + private static Registry registry; public static Stream data() { return Stream.of( @@ -60,6 +60,7 @@ public class RegistryLoadOrderTest { RegistryLoadOrderTest.registry = new CraftRegistry<>(keyedClass, minecraftRegistry, minecraftToBukkit, (namespacedKey, apiVersion) -> namespacedKey); this.testClassNotLoaded(init.get()); + ((CraftRegistry) RegistryLoadOrderTest.registry).lockReferenceHolders(); Object testOne = RegistryLoadOrderTest.registry.get(new NamespacedKey("bukkit", "test-one")); Object otherTestOne = RegistryLoadOrderTest.registry.get(new NamespacedKey("bukkit", "test-one"));