Skip to content

Commit

Permalink
Migrate to pre-existing Fabric resources loader event
Browse files Browse the repository at this point in the history
  • Loading branch information
StavWasPlayZ committed Apr 23, 2024
1 parent 41989c4 commit 2373b6e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.cstav.genshinstrument.client.gui.screen.instrument.partial;

import java.awt.Color;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.function.Consumer;
import java.util.function.Function;

import com.cstav.genshinstrument.GInstrumentMod;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
import net.minecraft.server.packs.PackType;
import org.slf4j.Logger;

import com.cstav.genshinstrument.event.impl.EventArgs;
Expand All @@ -29,7 +34,7 @@
* This class must be initialized during mod setup.
*/
@Environment(EnvType.CLIENT)
public class InstrumentThemeLoader {
public class InstrumentThemeLoader {
private static final Logger LOGGER = LogUtils.getLogger();
public static final String JSON_STYLER_NAME = "instrument_style.json";

Expand All @@ -38,6 +43,10 @@ public class InstrumentThemeLoader {
GLOBAL_LOC = InstrumentScreen.getInternalResourceFromGlob("instrument/global")
;

static {
subscribeResourcesReloadEvent();
}

private static boolean isGlobalThemed;
public static boolean isGlobalThemed() {
return isGlobalThemed;
Expand Down Expand Up @@ -183,17 +192,30 @@ protected <T, J extends JsonElement> T tryGetProperty(J element, Function<J, T>
}


public static void onResourcesReload(final EventArgs.Empty args) {
InstrumentThemeLoader.reload(Minecraft.getInstance().getResourceManager());
private static void subscribeResourcesReloadEvent() {
ResourceManagerHelper.get(PackType.CLIENT_RESOURCES).registerReloadListener(new SimpleSynchronousResourceReloadListener() {
@Override
public ResourceLocation getFabricId() {
return new ResourceLocation(GInstrumentMod.MODID, "instrument_theme_loaders");
}

@Override
public void onResourceManagerReload(ResourceManager resourceManager) {
InstrumentThemeLoader.reload(resourceManager);
}
});
}


private static void reload(final ResourceManager resourceManager) {
// Handle global resource packs
isGlobalThemed = false;

try {
isGlobalThemed = JsonParser.parseReader(resourceManager.getResource(INSTRUMENTS_META_LOC).get().openAsReader())
.getAsJsonObject().get("is_global_pack").getAsBoolean();
try (final BufferedReader reader = resourceManager.getResource(INSTRUMENTS_META_LOC).get().openAsReader()) {
isGlobalThemed = JsonParser.parseReader(reader)
.getAsJsonObject()
.get("is_global_pack")
.getAsBoolean();

if (isGlobalThemed)
LOGGER.info("Instrument global themes enabled; loading all instrument resources from "+GLOBAL_LOC);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.minecraft.client.Minecraft;
import net.minecraft.server.packs.PackType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
Expand All @@ -37,7 +39,6 @@ public static void register() {
PosePlayerArmEvent.EVENT.register(ClientEvents::posePlayerArmEvent);

ClientLifecycleEvents.CLIENT_STOPPING.register(ClientEvents::onGameShutdown);
ResourcesLoadedEvent.EVENT.register(InstrumentThemeLoader::onResourcesReload);
MidiEvent.EVENT.register(ClientEvents::onMidiEvent);
}

Expand Down

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/resources/genshinstrument.required.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"package": "com.cstav.genshinstrument.mixin.required",
"compatibilityLevel": "JAVA_17",
"client": [
"ResourcesLoaderInjector"

],
"mixins": [
"EntityNBTInjector"
Expand Down

0 comments on commit 2373b6e

Please sign in to comment.