2015-11-02 20:39:44 +00:00
|
|
|
package WayofTime.bloodmagic.api.util.helper;
|
2015-11-01 00:58:47 +00:00
|
|
|
|
2015-12-29 13:44:34 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.util.BlockPos;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraftforge.common.config.Configuration;
|
2015-12-02 05:59:07 +00:00
|
|
|
import WayofTime.bloodmagic.BloodMagic;
|
2015-11-03 01:45:11 +00:00
|
|
|
import WayofTime.bloodmagic.api.registry.ImperfectRitualRegistry;
|
2015-11-02 20:39:44 +00:00
|
|
|
import WayofTime.bloodmagic.api.registry.RitualRegistry;
|
2015-12-30 16:34:04 +00:00
|
|
|
import WayofTime.bloodmagic.api.ritual.IRitualStone;
|
2015-11-02 20:39:44 +00:00
|
|
|
import WayofTime.bloodmagic.api.ritual.Ritual;
|
2015-12-27 00:49:25 +00:00
|
|
|
import WayofTime.bloodmagic.api.ritual.RitualComponent;
|
2015-11-03 01:45:11 +00:00
|
|
|
import WayofTime.bloodmagic.api.ritual.imperfect.ImperfectRitual;
|
2015-11-01 00:58:47 +00:00
|
|
|
|
2015-12-30 20:34:40 +00:00
|
|
|
public class RitualHelper
|
|
|
|
{
|
|
|
|
public static boolean canCrystalActivate(Ritual ritual, int crystalLevel)
|
|
|
|
{
|
2015-11-01 00:58:47 +00:00
|
|
|
return ritual.getCrystalLevel() <= crystalLevel && RitualRegistry.ritualEnabled(ritual);
|
|
|
|
}
|
|
|
|
|
2015-12-30 20:34:40 +00:00
|
|
|
public static String getNextRitualKey(String currentKey)
|
|
|
|
{
|
2015-11-01 00:58:47 +00:00
|
|
|
int currentIndex = RitualRegistry.getIds().indexOf(currentKey);
|
|
|
|
int nextIndex = RitualRegistry.getRituals().listIterator(currentIndex).nextIndex();
|
|
|
|
|
|
|
|
return RitualRegistry.getIds().get(nextIndex);
|
|
|
|
}
|
|
|
|
|
2015-12-30 20:34:40 +00:00
|
|
|
public static String getPrevRitualKey(String currentKey)
|
|
|
|
{
|
2015-11-01 00:58:47 +00:00
|
|
|
int currentIndex = RitualRegistry.getIds().indexOf(currentKey);
|
|
|
|
int previousIndex = RitualRegistry.getIds().listIterator(currentIndex).previousIndex();
|
|
|
|
|
|
|
|
return RitualRegistry.getIds().get(previousIndex);
|
|
|
|
}
|
|
|
|
|
2015-12-29 13:44:34 +00:00
|
|
|
/**
|
2015-12-30 20:34:40 +00:00
|
|
|
* Checks the RitualRegistry to see if the configuration of the ritual
|
|
|
|
* stones in the world is valid for the given EnumFacing.
|
2016-01-01 18:52:42 +00:00
|
|
|
*
|
|
|
|
* @param world
|
|
|
|
* - The world
|
|
|
|
* @param pos
|
|
|
|
* - Location of the MasterRitualStone
|
|
|
|
*
|
2015-12-29 13:44:34 +00:00
|
|
|
* @return The ID of the valid ritual
|
|
|
|
*/
|
2015-12-30 20:34:40 +00:00
|
|
|
public static String getValidRitual(World world, BlockPos pos)
|
|
|
|
{
|
|
|
|
for (String key : RitualRegistry.getIds())
|
|
|
|
{
|
|
|
|
for (EnumFacing direction : EnumFacing.HORIZONTALS)
|
|
|
|
{
|
|
|
|
boolean test = checkValidRitual(world, pos, key, direction);
|
|
|
|
if (test)
|
|
|
|
{
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
2015-12-29 13:44:34 +00:00
|
|
|
}
|
2015-12-30 20:34:40 +00:00
|
|
|
|
|
|
|
public static EnumFacing getDirectionOfRitual(World world, BlockPos pos, String key)
|
|
|
|
{
|
|
|
|
for (EnumFacing direction : EnumFacing.HORIZONTALS)
|
|
|
|
{
|
|
|
|
boolean test = checkValidRitual(world, pos, key, direction);
|
|
|
|
if (test)
|
|
|
|
{
|
|
|
|
return direction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2015-12-29 14:10:03 +00:00
|
|
|
}
|
2015-12-30 20:34:40 +00:00
|
|
|
|
|
|
|
public static boolean checkValidRitual(World world, BlockPos pos, String ritualId, EnumFacing direction)
|
|
|
|
{
|
|
|
|
Ritual ritual = RitualRegistry.getRitualForId(ritualId);
|
|
|
|
if (ritual == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-29 13:44:34 +00:00
|
|
|
ArrayList<RitualComponent> components = ritual.getComponents();
|
2015-12-30 20:34:40 +00:00
|
|
|
|
2015-12-27 00:49:25 +00:00
|
|
|
if (components == null)
|
|
|
|
return false;
|
2015-12-30 20:34:40 +00:00
|
|
|
|
|
|
|
for (RitualComponent component : components)
|
|
|
|
{
|
|
|
|
BlockPos newPos = pos.add(component.getOffset(direction));
|
2015-12-29 13:44:34 +00:00
|
|
|
IBlockState worldState = world.getBlockState(newPos);
|
|
|
|
Block block = worldState.getBlock();
|
2015-12-30 20:34:40 +00:00
|
|
|
if (block instanceof IRitualStone)
|
|
|
|
{
|
|
|
|
if (!((IRitualStone) block).isRuneType(world, newPos, component.getRuneType()))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
return false;
|
2015-12-27 00:49:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-30 20:34:40 +00:00
|
|
|
public static void checkImperfectRituals(Configuration config, String packageName, String category)
|
|
|
|
{
|
2015-11-03 02:00:48 +00:00
|
|
|
checkRituals(config, packageName, category, ImperfectRitual.class, ImperfectRitualRegistry.enabledRituals);
|
2015-11-03 01:45:11 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 20:34:40 +00:00
|
|
|
public static void checkRituals(Configuration config, String packageName, String category)
|
|
|
|
{
|
2015-11-03 02:00:48 +00:00
|
|
|
checkRituals(config, packageName, category, Ritual.class, RitualRegistry.enabledRituals);
|
2015-11-03 01:45:11 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 00:58:47 +00:00
|
|
|
/**
|
2015-12-30 20:34:40 +00:00
|
|
|
* Adds your Ritual to the {@link RitualRegistry#enabledRituals} Map. This
|
|
|
|
* is used to determine whether your effect is enabled or not.
|
|
|
|
*
|
2016-01-01 18:52:42 +00:00
|
|
|
* The config option will be created as {@code B:ClassName=true}.
|
2015-12-30 20:34:40 +00:00
|
|
|
*
|
2015-11-01 00:58:47 +00:00
|
|
|
* Should be safe to modify at any point.
|
2015-12-30 20:34:40 +00:00
|
|
|
*
|
|
|
|
* @param config
|
|
|
|
* - Your mod's Forge {@link Configuration} object.
|
|
|
|
* @param packageName
|
|
|
|
* - The package your Rituals are located in.
|
|
|
|
* @param category
|
|
|
|
* - The config category to write to.
|
2015-11-01 00:58:47 +00:00
|
|
|
*/
|
2015-11-03 01:45:11 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
2015-12-30 20:34:40 +00:00
|
|
|
private static void checkRituals(Configuration config, String packageName, String category, Class ritualClass, Map enabledMap)
|
|
|
|
{
|
2015-11-01 00:58:47 +00:00
|
|
|
String name = packageName;
|
|
|
|
if (!name.startsWith("/"))
|
|
|
|
name = "/" + name;
|
|
|
|
|
|
|
|
name = name.replace('.', '/');
|
2015-12-02 05:59:07 +00:00
|
|
|
URL url = BloodMagic.class.getResource(name);
|
2015-11-01 00:58:47 +00:00
|
|
|
File directory = new File(url.getFile());
|
|
|
|
|
2015-12-30 20:34:40 +00:00
|
|
|
if (directory.exists())
|
|
|
|
{
|
2015-11-01 00:58:47 +00:00
|
|
|
String[] files = directory.list();
|
|
|
|
|
2015-12-30 20:34:40 +00:00
|
|
|
for (String file : files)
|
|
|
|
{
|
|
|
|
if (file.endsWith(".class"))
|
|
|
|
{
|
2015-11-01 00:58:47 +00:00
|
|
|
String className = file.substring(0, file.length() - 6);
|
|
|
|
|
2015-12-30 20:34:40 +00:00
|
|
|
try
|
|
|
|
{
|
2015-11-01 00:58:47 +00:00
|
|
|
Object o = Class.forName(packageName + "." + className).newInstance();
|
|
|
|
|
2015-11-03 01:45:11 +00:00
|
|
|
if (ritualClass.isInstance(o))
|
2015-11-03 15:35:14 +00:00
|
|
|
enabledMap.put(ritualClass.cast(o), config.get(category, className, true).getBoolean());
|
2015-11-01 00:58:47 +00:00
|
|
|
|
2015-12-30 20:34:40 +00:00
|
|
|
} catch (ClassNotFoundException e)
|
|
|
|
{
|
2015-11-01 00:58:47 +00:00
|
|
|
e.printStackTrace();
|
2015-12-30 20:34:40 +00:00
|
|
|
} catch (InstantiationException e)
|
|
|
|
{
|
2015-11-01 00:58:47 +00:00
|
|
|
e.printStackTrace();
|
2015-12-30 20:34:40 +00:00
|
|
|
} catch (IllegalAccessException e)
|
|
|
|
{
|
2015-11-01 00:58:47 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|