Implemented built in animations for abilities

This commit is contained in:
Vidal committed 2026-01-27 01:33:12 -03:00
1 parent 563b6f7a26
commit c858f24e67
2 files changed
+147 -5

No files matched your search

@@ -0,0 +1,79 @@
package kamkeel.npcs.controllers.data.ability;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import static kamkeel.npcs.controllers.data.ability.AbilityPhase.*;
public enum AbilityAnimation {;
private final HashMap<AbilityPhase, String> animations = new HashMap<>();
AbilityAnimation(String idle, String windUp, String active, String recovery) {
animations.put(IDLE, idle);
animations.put(WINDUP, windUp);
animations.put(ACTIVE, active);
animations.put(RECOVERY, recovery);
}
AbilityAnimation(String idle, String windUp, String active) {
animations.put(IDLE, idle);
animations.put(WINDUP, windUp);
animations.put(ACTIVE, active);
}
AbilityAnimation(String windUp, String active) {
animations.put(WINDUP, windUp);
animations.put(ACTIVE, active);
}
AbilityAnimation(String anim, AbilityPhase phase) {
animations.put(phase, anim);
}
public String fileName(AbilityPhase phase) {
return animations.get(phase);
}
public File get(AbilityPhase phase) {
if (animations.get(phase).isEmpty() || animations.get(phase).equals("N/A"))
return null;
return getFile(animations.get(phase));
}
public File idle() {
return get(IDLE);
}
public File windUp() {
return get(WINDUP);
}
public File active() {
return get(ACTIVE);
}
public File recovery() {
return get(RECOVERY);
}
private File getFile(String fileName) {
try (InputStream stream = CustomNpcs.class.getResourceAsStream("/internal/data/animations/" + name() + "/" + fileName + ".json")) {
File file = new File("temp");
FileUtils.copyInputStreamToFile(stream, file);
return file;
} catch (Exception i) {
LogWriter.except(i);
}
return null;
}
}
@@ -22,14 +22,13 @@ import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import noppes.npcs.api.handler.IAbilityHandler;
import noppes.npcs.api.handler.data.IAnimation;
import noppes.npcs.controllers.AnimationController;
import noppes.npcs.controllers.data.Animation;
import noppes.npcs.util.NBTJsonUtil;
import java.io.File;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.function.Supplier;
/**
@@ -49,6 +48,7 @@ public class AbilityController implements IAbilityHandler {
public AbilityController() {
Instance = this;
registerBuiltins();
new Animations();
}
/**
@@ -321,4 +321,67 @@ public class AbilityController implements IAbilityHandler {
registerType("cnpc:hazard", AbilityHazard::new);
registerType("cnpc:trap", AbilityTrap::new);
}
public static class Animations {
public HashMap<Integer, Map<AbilityPhase, Animation>> animations;
public static Animations Instance;
public Animations() {
Instance = this;
animations = new HashMap<>();
load();
}
public void load() {
registerBuiltins();
}
private void registerBuiltins() {
animations.clear();
try {
for (AbilityPhase phase : AbilityPhase.values()) {
for (AbilityAnimation anim : AbilityAnimation.values()) {
File file = anim.get(phase);
if (file == null) {
// LogWriter.info("AnimationController: Ability Animations for " + anim.name() + " not found.");
continue;
}
Animation animation = new Animation();
animation.readFromNBT(NBTJsonUtil.LoadFile(file));
animations
.computeIfAbsent(anim.ordinal(), k -> new EnumMap<>(AbilityPhase.class))
.put(phase, animation);
}
}
} catch (Exception e) {
LogWriter.except(e);
}
}
public IAnimation get(AbilityAnimation ability, AbilityPhase phase) {
return get(ability.ordinal(), phase);
}
public IAnimation get(int id, AbilityPhase phase) {
return animations.get(id).get(phase);
}
public boolean has(AbilityAnimation ability, AbilityPhase phase) {
return get(ability.ordinal(), phase) != null;
}
public boolean has(int id, AbilityPhase phase) {
return get(id, phase) != null;
}
public IAnimation[] getAnimations(AbilityAnimation ability) {
ArrayList<IAnimation> animations = new ArrayList<>(this.animations.get(ability.ordinal()).values());
return animations.toArray(new IAnimation[0]);
}
}
}