Unicopia/src/main/java/com/minelittlepony/unicopia/spell/SpellRegistry.java

165 lines
4.3 KiB
Java
Raw Normal View History

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;
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;
}
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;
}
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
}
public ItemStack disenchantStack(ItemStack stack) {
if (stackHasEnchantment(stack)) {
stack.getTagCompound().removeTag("spell");
if (stack.getTagCompound().isEmpty()) {
stack.setTagCompound(null);
}
}
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;
}
@Nonnull
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
}
}