2015-11-02 12:39:44 -08:00
|
|
|
package WayofTime.bloodmagic.api.registry;
|
2015-10-31 17:58:47 -07:00
|
|
|
|
2015-11-02 12:39:44 -08:00
|
|
|
import WayofTime.bloodmagic.api.BloodMagicAPI;
|
|
|
|
import WayofTime.bloodmagic.api.ritual.Ritual;
|
2015-10-31 17:58:47 -07:00
|
|
|
import com.google.common.collect.BiMap;
|
|
|
|
import com.google.common.collect.HashBiMap;
|
2015-11-28 18:25:46 -08:00
|
|
|
|
2016-12-12 19:56:36 -08:00
|
|
|
import javax.annotation.Nullable;
|
2017-02-27 17:37:30 -08:00
|
|
|
import java.util.*;
|
2016-03-17 13:00:44 -07:00
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
public class RitualRegistry
|
|
|
|
{
|
2015-11-03 07:35:14 -08:00
|
|
|
public static final Map<Ritual, Boolean> enabledRituals = new HashMap<Ritual, Boolean>();
|
2015-10-31 17:58:47 -07:00
|
|
|
private static final BiMap<String, Ritual> registry = HashBiMap.create();
|
2017-02-27 17:37:30 -08:00
|
|
|
private static final List<String> lookupList = new ArrayList<String>();
|
2016-01-01 10:34:17 +01:00
|
|
|
/**
|
|
|
|
* Ordered list for actions that depend on the order that the rituals were
|
|
|
|
* registered in
|
|
|
|
*/
|
2015-12-30 15:34:40 -05:00
|
|
|
private static final ArrayList<String> orderedIdList = new ArrayList<String>();
|
2015-10-31 17:58:47 -07:00
|
|
|
|
2017-02-27 17:37:30 -08:00
|
|
|
private static boolean locked;
|
|
|
|
|
2015-10-31 17:58:47 -07:00
|
|
|
/**
|
|
|
|
* The safe way to register a new Ritual.
|
2015-12-30 15:34:40 -05:00
|
|
|
*
|
|
|
|
* @param ritual
|
2016-01-02 17:56:37 -05:00
|
|
|
* - The ritual to register.
|
2015-12-30 15:34:40 -05:00
|
|
|
* @param id
|
2016-01-02 17:56:37 -05:00
|
|
|
* - The ID for the ritual. Cannot be duplicated.
|
2015-10-31 17:58:47 -07:00
|
|
|
*/
|
2016-02-01 17:05:39 -08:00
|
|
|
public static void registerRitual(Ritual ritual, String id, boolean enabled)
|
2015-12-30 15:34:40 -05:00
|
|
|
{
|
2017-02-27 17:37:30 -08:00
|
|
|
if (locked)
|
|
|
|
{
|
2017-08-15 20:21:54 -07:00
|
|
|
BloodMagicAPI.logger.error("This registry has been locked. Please register your ritual earlier.");
|
|
|
|
BloodMagicAPI.logger.error("If you reflect this, I will hunt you down. - TehNut");
|
2017-02-27 17:37:30 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
if (ritual != null)
|
|
|
|
{
|
2015-10-31 17:58:47 -07:00
|
|
|
if (registry.containsKey(id))
|
2017-08-15 20:21:54 -07:00
|
|
|
BloodMagicAPI.logger.error("Duplicate ritual id: %s", id);
|
2015-12-30 15:34:40 -05:00
|
|
|
else
|
|
|
|
{
|
2015-10-31 17:58:47 -07:00
|
|
|
registry.put(id, ritual);
|
2016-02-01 17:05:39 -08:00
|
|
|
enabledRituals.put(ritual, enabled);
|
2015-12-30 11:34:04 -05:00
|
|
|
orderedIdList.add(id);
|
|
|
|
}
|
2015-10-31 17:58:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 17:05:39 -08:00
|
|
|
public static void registerRitual(Ritual ritual, boolean enabled)
|
|
|
|
{
|
|
|
|
registerRitual(ritual, ritual.getName(), enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void registerRitual(Ritual ritual, String id)
|
|
|
|
{
|
|
|
|
registerRitual(ritual, id, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void registerRitual(Ritual ritual)
|
|
|
|
{
|
|
|
|
registerRitual(ritual, ritual.getName());
|
|
|
|
}
|
|
|
|
|
2016-12-12 19:56:36 -08:00
|
|
|
@Nullable
|
2015-12-30 15:34:40 -05:00
|
|
|
public static Ritual getRitualForId(String id)
|
|
|
|
{
|
2015-12-31 08:01:39 -05:00
|
|
|
Ritual ritual = registry.get(id);
|
|
|
|
return ritual != null ? ritual.getNewCopy() : null;
|
2015-10-31 17:58:47 -07:00
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
public static String getIdForRitual(Ritual ritual)
|
|
|
|
{
|
2015-10-31 17:58:47 -07:00
|
|
|
return registry.inverse().get(ritual);
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
public static boolean isMapEmpty()
|
|
|
|
{
|
2015-10-31 17:58:47 -07:00
|
|
|
return registry.isEmpty();
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
public static int getMapSize()
|
|
|
|
{
|
2015-10-31 17:58:47 -07:00
|
|
|
return registry.size();
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
public static boolean ritualEnabled(Ritual ritual)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2016-02-01 17:05:39 -08:00
|
|
|
return enabledRituals.get(ritual);
|
2015-12-30 15:34:40 -05:00
|
|
|
} catch (NullPointerException e)
|
|
|
|
{
|
2017-08-15 20:21:54 -07:00
|
|
|
BloodMagicAPI.logger.error("Invalid Ritual was called");
|
2015-11-02 17:45:11 -08:00
|
|
|
return false;
|
|
|
|
}
|
2015-10-31 17:58:47 -07:00
|
|
|
}
|
|
|
|
|
2016-03-16 18:41:06 -04:00
|
|
|
public static boolean ritualEnabled(String id)
|
|
|
|
{
|
2016-02-01 17:05:39 -08:00
|
|
|
return ritualEnabled(getRitualForId(id));
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
public static BiMap<String, Ritual> getRegistry()
|
|
|
|
{
|
2015-10-31 17:58:47 -07:00
|
|
|
return HashBiMap.create(registry);
|
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
public static Map<Ritual, Boolean> getEnabledMap()
|
|
|
|
{
|
2015-11-03 07:35:14 -08:00
|
|
|
return new HashMap<Ritual, Boolean>(enabledRituals);
|
2015-11-02 17:45:11 -08:00
|
|
|
}
|
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
public static ArrayList<String> getIds()
|
|
|
|
{
|
2017-02-27 17:37:30 -08:00
|
|
|
return new ArrayList<String>(lookupList);
|
2015-10-31 17:58:47 -07:00
|
|
|
}
|
2015-12-30 15:34:40 -05:00
|
|
|
|
|
|
|
public static ArrayList<String> getOrderedIds()
|
|
|
|
{
|
|
|
|
return orderedIdList;
|
2015-12-30 11:34:04 -05:00
|
|
|
}
|
2015-10-31 17:58:47 -07:00
|
|
|
|
2015-12-30 15:34:40 -05:00
|
|
|
public static ArrayList<Ritual> getRituals()
|
|
|
|
{
|
2015-10-31 17:58:47 -07:00
|
|
|
return new ArrayList<Ritual>(registry.values());
|
|
|
|
}
|
2017-02-27 17:37:30 -08:00
|
|
|
|
|
|
|
public static void orderLookupList()
|
|
|
|
{
|
|
|
|
locked = true; // Lock registry so no no rituals can be registered
|
|
|
|
lookupList.clear(); // Make sure it's empty
|
|
|
|
lookupList.addAll(registry.keySet());
|
|
|
|
Collections.sort(lookupList, new Comparator<String>()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public int compare(String o1, String o2)
|
|
|
|
{
|
|
|
|
Ritual ritual1 = registry.get(o1);
|
|
|
|
Ritual ritual2 = registry.get(o2);
|
|
|
|
return ritual1.getComponents().size() > ritual2.getComponents().size() ? -1 : 0; // Put earlier if bigger
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-10-31 17:58:47 -07:00
|
|
|
}
|