2018-09-12 01:29:49 +02:00
|
|
|
package com.minelittlepony.unicopia.spell;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2018-09-20 22:49:10 +02:00
|
|
|
import java.util.Set;
|
2018-09-12 01:29:49 +02:00
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
|
2018-09-20 22:49:10 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
2018-09-12 01:29:49 +02:00
|
|
|
public class SpellRegistry {
|
|
|
|
|
|
|
|
private static final SpellRegistry instance = new SpellRegistry();
|
|
|
|
|
|
|
|
public static SpellRegistry instance() {
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2018-09-20 22:49:10 +02:00
|
|
|
public static boolean stackHasEnchantment(ItemStack stack) {
|
|
|
|
return stack.hasTagCompound() && stack.getTagCompound().hasKey("spell");
|
|
|
|
}
|
|
|
|
|
|
|
|
private final Map<String, Entry> entries = new HashMap<>();
|
2018-09-12 01:29:49 +02:00
|
|
|
|
|
|
|
private SpellRegistry() {
|
2018-09-20 22:49:10 +02:00
|
|
|
registerSpell("shield", 0xffff00, SpellShield::new);
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 22:49:10 +02:00
|
|
|
public IMagicEffect getSpellFromName(String name) {
|
|
|
|
if (entries.containsKey(name)) {
|
|
|
|
return entries.get(name).create();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IMagicEffect createEffectFroNBT(NBTTagCompound compound) {
|
|
|
|
if (compound.hasKey("effect_id") && compound.hasKey("effect")) {
|
|
|
|
IMagicEffect effect = getSpellFromName(compound.getString("effect_id"));
|
|
|
|
|
|
|
|
if (effect != null) {
|
|
|
|
effect.readFromNBT(compound.getCompoundTag("effect"));
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
2018-09-20 22:49:10 +02:00
|
|
|
|
|
|
|
return effect;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IDispenceable getDispenseActionFrom(ItemStack stack) {
|
|
|
|
String key = getKeyFromStack(stack);
|
|
|
|
|
|
|
|
if (entries.containsKey(key)) {
|
|
|
|
Entry entry = entries.get(key);
|
|
|
|
if (entry.canDispense) {
|
|
|
|
return entry.create();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IMagicEffect getSpellFromItemStack(ItemStack stack) {
|
|
|
|
return getSpellFromName(getKeyFromStack(stack));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void registerSpell(String key, int tint, Callable<IMagicEffect> factory) {
|
|
|
|
try {
|
|
|
|
entries.put(key, new Entry(factory, tint));
|
2018-09-12 01:29:49 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-09-20 22:49:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public ItemStack enchantStack(ItemStack stack, String name) {
|
|
|
|
stack.setTagCompound(new NBTTagCompound());
|
|
|
|
stack.getTagCompound().setString("spell", name);
|
|
|
|
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getKeyFromStack(ItemStack stack) {
|
|
|
|
if (stack.isEmpty() || !stack.hasTagCompound() || !stack.getTagCompound().hasKey("spell")) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return stack.getTagCompound().getString("spell");
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getSpellTintFromStack(ItemStack stack) {
|
|
|
|
return getSpellTint(getKeyFromStack(stack));
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getSpellTint(String key) {
|
|
|
|
if (entries.containsKey(key)) {
|
|
|
|
return entries.get(key).color;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0xffffff;
|
|
|
|
}
|
2018-09-12 01:29:49 +02:00
|
|
|
|
2018-09-20 22:49:10 +02:00
|
|
|
public Set<String> getAllNames() {
|
|
|
|
return entries.keySet();
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 22:49:10 +02:00
|
|
|
class Entry {
|
|
|
|
Callable<IMagicEffect> factory;
|
|
|
|
|
|
|
|
int color;
|
|
|
|
|
|
|
|
boolean canDispense;
|
|
|
|
|
|
|
|
Entry(Callable<IMagicEffect> factory, int color) throws Exception {
|
|
|
|
this.factory = factory;
|
|
|
|
this.color = color;
|
|
|
|
this.canDispense = factory.call() instanceof IDispenceable;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
<T extends IMagicEffect> T create() {
|
|
|
|
try {
|
|
|
|
return (T) factory.call();
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|
|
|
|
}
|