This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
SorceryCraft/src/main/java/com/thebrokenrail/sorcerycraft/spell/SpellRegistry.java

62 lines
2.0 KiB
Java

package com.thebrokenrail.sorcerycraft.spell;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SpellRegistry {
private static final Map<String, Class<?>> spells = new HashMap<>();
public static Spell getSpell(String id, int level) {
if (!spells.containsKey(id)) {
return null;
}
try {
return (Spell) spells.get(id).getConstructor(String.class, int.class).newInstance(id, level);
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
return null;
}
}
public static int getMaxLevel(String id) {
Spell tempSpell = getSpell(id, 0);
if (tempSpell == null) {
return -1;
}
return tempSpell.getMaxLevel();
}
public static Spell[] getSpells() {
List<Spell> out = new ArrayList<>();
for (Map.Entry<String, Class<?>> entry : spells.entrySet()) {
int maxLevel = getMaxLevel(entry.getKey());
if (maxLevel == -1) {
continue;
}
for (int i = 0; i < maxLevel; i++) {
Spell spell = getSpell(entry.getKey(), i);
if (spell != null) {
out.add(spell);
}
}
}
return out.toArray(new Spell[0]);
}
private static void registerSpell(String id, Class<?> spell) {
spells.put(id, spell);
}
public static void init() {
SpellRegistry.registerSpell("heal_spell", HealSpell.class);
SpellRegistry.registerSpell("damage_spell", DamageSpell.class);
SpellRegistry.registerSpell("dissolve_spell", DissolveSpell.class);
SpellRegistry.registerSpell("steadfast_spell", SteadfastSpell.class);
SpellRegistry.registerSpell("flame_spell", FlameSpell.class);
SpellRegistry.registerSpell("levitate_spell", LevitateSpell.class);
}
}