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;
|
2019-01-22 17:39:30 +01:00
|
|
|
import javax.annotation.Nullable;
|
2019-01-10 18:46:40 +01:00
|
|
|
|
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-22 17:39:30 +01:00
|
|
|
registerSpell(SpellShield::new);
|
|
|
|
registerSpell(SpellCharge::new);
|
|
|
|
registerSpell(SpellFire::new);
|
|
|
|
registerSpell(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;
|
|
|
|
}
|
|
|
|
|
2019-01-22 17:39:30 +01:00
|
|
|
@Nullable
|
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;
|
|
|
|
}
|
|
|
|
|
2019-01-22 17:39:30 +01:00
|
|
|
@Nullable
|
|
|
|
public IUseAction getUseActionFrom(ItemStack stack) {
|
|
|
|
String key = getKeyFromStack(stack);
|
|
|
|
|
|
|
|
if (entries.containsKey(key)) {
|
|
|
|
Entry entry = entries.get(key);
|
|
|
|
if (entry.canUse) {
|
|
|
|
return entry.create();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-09-20 22:49:10 +02:00
|
|
|
public IMagicEffect getSpellFromItemStack(ItemStack stack) {
|
|
|
|
return getSpellFromName(getKeyFromStack(stack));
|
|
|
|
}
|
|
|
|
|
2019-01-22 17:39:30 +01:00
|
|
|
public void registerSpell(Callable<IMagicEffect> factory) {
|
2018-09-20 22:49:10 +02:00
|
|
|
try {
|
2019-01-22 17:39:30 +01:00
|
|
|
new Entry(factory);
|
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;
|
2019-01-22 17:39:30 +01:00
|
|
|
boolean canUse;
|
|
|
|
|
|
|
|
Entry(Callable<IMagicEffect> factory) throws Exception {
|
|
|
|
IMagicEffect inst = factory.call();
|
2018-09-20 22:49:10 +02:00
|
|
|
|
|
|
|
this.factory = factory;
|
2019-01-22 17:39:30 +01:00
|
|
|
this.color = inst.getTint();
|
|
|
|
this.canDispense = inst instanceof IDispenceable;
|
|
|
|
this.canUse = inst instanceof IUseAction;
|
|
|
|
|
|
|
|
entries.put(inst.getName(), this);
|
2018-09-20 22:49:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
}
|
|
|
|
}
|