Skip to content

Commit

Permalink
Merge pull request #19 from KosmX/port_18
Browse files Browse the repository at this point in the history
Move forge resource loading to client package
  • Loading branch information
KosmX authored Sep 10, 2022
2 parents 560ade7 + 3e94b4b commit 0d5ca98
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
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
}
}
});
}

}

0 comments on commit 0d5ca98

Please sign in to comment.