From 7c5c7a956030cca21b31be72758e663c5a6f74ab Mon Sep 17 00:00:00 2001 From: StavWasPlayZ Date: Sun, 27 Aug 2023 23:50:37 +0300 Subject: [PATCH] Fix crash on version rollback (reload resources) --- .../partial/InstrumentThemeLoader.java | 52 ++++++++++++------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/partial/InstrumentThemeLoader.java b/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/partial/InstrumentThemeLoader.java index 4793b732..9bae9c8f 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/partial/InstrumentThemeLoader.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/partial/InstrumentThemeLoader.java @@ -5,6 +5,7 @@ import java.util.HashMap; import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.Supplier; import org.slf4j.Logger; @@ -14,6 +15,7 @@ import com.google.gson.JsonParser; import com.mojang.logging.LogUtils; +import net.minecraft.client.Minecraft; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.server.packs.resources.ResourceManagerReloadListener; @@ -153,25 +155,29 @@ public static void registerReloadEvent(final RegisterClientReloadListenersEvent @Override public void onResourceManagerReload(ResourceManager resourceManager) { - // Handle global resource packs - isGlobalThemed = false; + InstrumentThemeLoader.reload(resourceManager); + } + + }); + } - try { - isGlobalThemed = JsonParser.parseReader(resourceManager.getResource(INSTRUMENTS_META_LOC).get().openAsReader()) - .getAsJsonObject().get("is_global_pack").getAsBoolean(); + private static void reload(final ResourceManager resourceManager) { + // Handle global resource packs + isGlobalThemed = false; - if (isGlobalThemed) - LOGGER.info("Instrument global themes enabled; loading all instrument resources from "+GLOBAL_LOC); - } catch (Exception e) {} + try { + isGlobalThemed = JsonParser.parseReader(resourceManager.getResource(INSTRUMENTS_META_LOC).get().openAsReader()) + .getAsJsonObject().get("is_global_pack").getAsBoolean(); + if (isGlobalThemed) + LOGGER.info("Instrument global themes enabled; loading all instrument resources from "+GLOBAL_LOC); + } catch (Exception e) {} - for (final InstrumentThemeLoader instrumentLoader : LOADERS) - instrumentLoader.performReload(resourceManager); - CACHES.clear(); - } - - }); + for (final InstrumentThemeLoader instrumentLoader : LOADERS) + instrumentLoader.performReload(resourceManager); + + CACHES.clear(); } private void performReload(final ResourceManager resourceManager) { @@ -225,31 +231,41 @@ public ResourceLocation getStylerLocation() { public Color getNoteTheme() { - return noteTheme; + return getTheme(() -> noteTheme); } public void setNoteTheme(Color noteTheme) { this.noteTheme = noteTheme; } public Color getPressedNoteTheme() { - return pressedNoteTheme; + return getTheme(() -> pressedNoteTheme); } public void setPressedNoteTheme(Color pressedNoteTheme) { this.pressedNoteTheme = pressedNoteTheme; } public Color getLabelTheme() { - return labelTheme; + return getTheme(() -> labelTheme); } public void setLabelTheme(Color labelTheme) { this.labelTheme = labelTheme; } public Color getNoteRingTheme() { - return noteRingTheme; + return getTheme(() -> noteRingTheme); } public void setNoteRingTheme(Color noteRingTheme) { this.noteRingTheme = noteRingTheme; } + + protected T getTheme(final Supplier theme) { + if (theme.get() == null) { + LOGGER.warn("Requested theme not found, performing reload!"); + performReload(Minecraft.getInstance().getResourceManager()); + } + + return theme.get(); + } + } \ No newline at end of file