Removed static class from AbilityController and made animations built into the enum

This commit is contained in:
Vidal committed 2026-01-27 01:49:08 -03:00
1 parent 39b6304bed
commit 246baa9f55
3 files changed
+34 -77

No files matched your search

@@ -2,6 +2,9 @@ package kamkeel.npcs.controllers.data.ability;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import noppes.npcs.api.handler.data.IAnimation;
import noppes.npcs.controllers.data.Animation;
import noppes.npcs.util.NBTJsonUtil;
import org.apache.commons.io.FileUtils;
import java.io.File;
@@ -11,7 +14,6 @@ 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) {
@@ -40,30 +42,49 @@ public enum AbilityAnimation {;
return animations.get(phase);
}
public File get(AbilityPhase phase) {
public File getFile(AbilityPhase phase) {
if (animations.get(phase).isEmpty() || animations.get(phase).equals("N/A"))
return null;
return getFile(animations.get(phase));
return getFileFromName(animations.get(phase));
}
public File idle() {
return get(IDLE);
public IAnimation getAnimation(AbilityPhase phase) {
try {
File file = getFile(phase);
if (file == null) {
return null;
}
Animation animation = new Animation();
animation.readFromNBT(NBTJsonUtil.LoadFile(file));
return animation;
} catch (Exception e) {
LogWriter.except(e);
}
return null;
}
public File windUp() {
return get(WINDUP);
public IAnimation idle() {
return getAnimation(IDLE);
}
public File active() {
return get(ACTIVE);
public IAnimation windUp() {
return getAnimation(WINDUP);
}
public File recovery() {
return get(RECOVERY);
public IAnimation active() {
return getAnimation(ACTIVE);
}
private File getFile(String fileName) {
public IAnimation recovery() {
return getAnimation(RECOVERY);
}
private File getFileFromName(String fileName) {
try (InputStream stream = CustomNpcs.class.getResourceAsStream("/internal/data/animations/" + name() + "/" + fileName + ".json")) {
File file = new File("temp");
@@ -47,7 +47,6 @@ public class AbilityController implements IAbilityHandler {
public AbilityController() {
Instance = this;
registerBuiltins();
new Animations();
}
/**
@@ -320,67 +319,4 @@ 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]);
}
}
}