package com.minelittlepony.unicopia.spell; import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.concurrent.Callable; public class SpellRegistry { private static final SpellRegistry instance = new SpellRegistry(); public static SpellRegistry instance() { return instance; } private final Map> factories = new HashMap<>(); private SpellRegistry() { registerSpell("shield", SpellShield::new); } public Optional getSpellFromName(String name) { try { if (factories.containsKey(name)) { return Optional.ofNullable(factories.get(name).call()); } } catch (Exception e) { e.printStackTrace(); } return Optional.empty(); } public void registerSpell(String key, Callable factory) { factories.put(key, factory); } }