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;
|
|
|
|
|
2019-01-10 18:46:40 +01:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
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() {
|
2019-01-20 00:07:59 +01:00
|
|
|
registerSpell("shield", 0x66CDAA, SpellShield::new);
|
|
|
|
registerSpell("charge", 0x0000AA, SpellCharge::new);
|
|
|
|
registerSpell("fire", 0xFF0000, SpellFire::new);
|
|
|
|
registerSpell("ice", 0xADD8E6, SpellIce::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;
|
|
|
|
}
|
|
|
|
|
2018-09-21 17:53:33 +02:00
|
|
|
public IMagicEffect createEffectFromNBT(NBTTagCompound compound) {
|
|
|
|
if (compound.hasKey("effect_id")) {
|
2018-09-20 22:49:10 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-09-21 17:53:33 +02:00
|
|
|
public NBTTagCompound serializeEffectToNBT(IMagicEffect effect) {
|
|
|
|
NBTTagCompound compound = effect.toNBT();
|
|
|
|
|
|
|
|
compound.setString("effect_id", effect.getName());
|
|
|
|
|
|
|
|
return compound;
|
|
|
|
}
|
|
|
|
|
2018-09-20 22:49:10 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-01-10 18:46:40 +01:00
|
|
|
public ItemStack disenchantStack(ItemStack stack) {
|
|
|
|
if (stackHasEnchantment(stack)) {
|
|
|
|
stack.getTagCompound().removeTag("spell");
|
2019-01-13 21:06:54 +01:00
|
|
|
|
|
|
|
if (stack.getTagCompound().isEmpty()) {
|
|
|
|
stack.setTagCompound(null);
|
|
|
|
}
|
2019-01-10 18:46:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemStack enchantStack(ItemStack stack, ItemStack from) {
|
|
|
|
stack.setTagCompound(new NBTTagCompound());
|
|
|
|
stack.getTagCompound().setString("spell", getKeyFromStack(from));
|
|
|
|
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-01-10 18:46:40 +01:00
|
|
|
@Nonnull
|
2018-09-21 17:53:33 +02:00
|
|
|
public static String getKeyFromStack(ItemStack stack) {
|
2018-09-20 22:49:10 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|