Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move forge resource loading to client package #19

Merged
merged 2 commits into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod_version=0.4.0-test0
maven_group=dev.kosmx.player-anim


fabric_loader_version=0.14.8
fabric_loader_version=0.14.9
fabric_api_version=0.58.0+1.18.2

forge_version=1.18.2-40.1.80
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,11 @@
package dev.kosmx.playerAnim.impl;

import dev.kosmx.playerAnim.core.data.gson.AnimationSerializing;
import dev.kosmx.playerAnim.minecraftApi.PlayerAnimationRegistry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManagerReloadListener;
import net.minecraftforge.client.event.RegisterClientReloadListenersEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.spongepowered.asm.mixin.Mixins;

import java.io.IOException;

@Mod("playeranimator")
public class ForgeModInterface {
public ForgeModInterface() {
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::resourceLoadingListener);
}

private void resourceLoadingListener(RegisterClientReloadListenersEvent event) {
event.registerReloadListener((ResourceManagerReloadListener) manager -> {
PlayerAnimationRegistry.clearAnimation();
for (var resource: manager.listResources("player_animation", location -> location.endsWith(".json"))) {
try (var input = manager.getResource(resource).getInputStream()) {

//Deserialize the animation json. GeckoLib animation json can contain multiple animations.
for (var animation : AnimationSerializing.deserializeAnimation(input)) {

//Save the animation for later use.
PlayerAnimationRegistry.addAnimation(new ResourceLocation(resource.getNamespace(), PlayerAnimationRegistry.serializeTextToString((String) animation.extraData.get("name"))), animation);
}
} catch(IOException e) {
throw new RuntimeException(e);//Somehow handle invalid animations
}
}
});

}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dev.kosmx.playerAnim.impl.forge;

import dev.kosmx.playerAnim.core.data.gson.AnimationSerializing;
import dev.kosmx.playerAnim.minecraftApi.PlayerAnimationRegistry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManagerReloadListener;
import net.minecraftforge.client.event.RegisterClientReloadListenersEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

@Mod.EventBusSubscriber(modid = "playeranimator", bus = Mod.EventBusSubscriber.Bus.MOD)
public class ForgeClientEvent {

@SubscribeEvent
public static void resourceLoadingListener(@NotNull RegisterClientReloadListenersEvent event) {
event.registerReloadListener((ResourceManagerReloadListener) manager -> {
PlayerAnimationRegistry.clearAnimation();
for (var resource: manager.listResources("player_animation", location -> location.getPath().endsWith(".json")).entrySet()) {
try (var input = resource.getValue().open()) {

//Deserialize the animation json. GeckoLib animation json can contain multiple animations.
for (var animation : AnimationSerializing.deserializeAnimation(input)) {

//Save the animation for later use.
PlayerAnimationRegistry.addAnimation(new ResourceLocation(resource.getKey().getNamespace(), PlayerAnimationRegistry.serializeTextToString((String) animation.extraData.get("name"))), animation);
}
} catch(IOException e) {
throw new RuntimeException(e);//Somehow handle invalid animations
}
}
});
}

}