Skip to content

Commit b033bbe

Browse files
committed
Optimize registry access
1 parent 3b7e031 commit b033bbe

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

Common/src/main/java/mezz/jei/common/util/RegistryUtil.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,45 @@
55
import net.minecraft.core.Registry;
66
import net.minecraft.core.RegistryAccess;
77
import net.minecraft.resources.ResourceKey;
8+
import org.jetbrains.annotations.Nullable;
9+
10+
import java.util.HashMap;
11+
import java.util.Map;
812

913
public class RegistryUtil {
14+
private static final Map<ResourceKey<? extends Registry<?>>, Registry<?>> REGISTRY_CACHE = new HashMap<>();
15+
private static @Nullable RegistryAccess REGISTRY_ACCESS;
16+
1017
public static <T> Registry<T> getRegistry(ResourceKey<? extends Registry<T>> key) {
18+
Registry<?> registry = REGISTRY_CACHE.get(key);
19+
if (registry == null) {
20+
registry = getRegistryUncached(key);
21+
REGISTRY_CACHE.put(key, registry);
22+
}
23+
@SuppressWarnings("unchecked")
24+
Registry<T> castRegistry = (Registry<T>) registry;
25+
return castRegistry;
26+
}
27+
28+
private static Registry<?> getRegistryUncached(ResourceKey<? extends Registry<?>> key) {
1129
RegistryAccess registryAccess = getRegistryAccess();
1230
return registryAccess.registryOrThrow(key);
1331
}
1432

1533
public static RegistryAccess getRegistryAccess() {
16-
Minecraft minecraft = Minecraft.getInstance();
17-
ClientLevel level = minecraft.level;
18-
if (level == null) {
19-
throw new IllegalStateException("Could not get registry, registry access is unavailable because the level is currently null");
34+
if (REGISTRY_ACCESS == null) {
35+
Minecraft minecraft = Minecraft.getInstance();
36+
ClientLevel level = minecraft.level;
37+
if (level == null) {
38+
throw new IllegalStateException("Could not get registry, registry access is unavailable because the level is currently null");
39+
}
40+
REGISTRY_ACCESS = level.registryAccess();
2041
}
21-
return level.registryAccess();
42+
return REGISTRY_ACCESS;
43+
}
44+
45+
public static void setRegistryAccess(@Nullable RegistryAccess registryAccess) {
46+
REGISTRY_ACCESS = registryAccess;
47+
REGISTRY_CACHE.clear();
2248
}
2349
}

Library/src/main/java/mezz/jei/library/startup/JeiStarter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import mezz.jei.common.config.file.IConfigSchemaBuilder;
1717
import mezz.jei.common.platform.Services;
1818
import mezz.jei.common.util.ErrorUtil;
19+
import mezz.jei.common.util.RegistryUtil;
1920
import mezz.jei.core.util.LoggedTimer;
2021
import mezz.jei.library.color.ColorHelper;
2122
import mezz.jei.library.config.ColorNameConfig;
@@ -100,6 +101,7 @@ public void start() {
100101
return;
101102
}
102103
RegistryAccess registryAccess = minecraft.level.registryAccess();
104+
RegistryUtil.setRegistryAccess(registryAccess);
103105

104106
LoggedTimer totalTime = new LoggedTimer();
105107
totalTime.start("Starting JEI");
@@ -178,5 +180,6 @@ public void stop() {
178180
List<IModPlugin> plugins = data.plugins();
179181
PluginCaller.callOnPlugins("Sending Runtime Unavailable", plugins, IModPlugin::onRuntimeUnavailable);
180182
Internal.setRuntime(null);
183+
RegistryUtil.setRegistryAccess(null);
181184
}
182185
}

0 commit comments

Comments
 (0)