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/registry/SpellRegistry.java

71 lines
2.1 KiB
Java

package com.thebrokenrail.sorcerycraft.spell.registry;
import com.thebrokenrail.sorcerycraft.spell.api.Spell;
import net.minecraft.util.Identifier;
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<Identifier, Class<?>> spells = new HashMap<>();
public static Spell getSpell(Map.Entry<Identifier, Integer> entry) {
return getSpell(entry.getKey(), entry.getValue());
}
public static Spell getSpell(Identifier id, int level) {
if (!spells.containsKey(id)) {
return null;
}
try {
return (Spell) spells.get(id).getConstructor(Identifier.class, int.class).newInstance(id, level);
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
return null;
}
}
public static int getMaxLevel(Identifier 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<Identifier, 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]);
}
/**
* Register a Spell
* @param id The Spell ID
* @param spell The Spell Class
* @return The Spell ID
*/
public static Identifier register(Identifier id, Class<?> spell) {
spells.put(id, spell);
return id;
}
public static Identifier[] getSpellsID() {
return spells.keySet().toArray(new Identifier[0]);
}
}