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
|
|
|
|
|
|
|
public class RitualHelper {
|
|
|
|
|
|
|
|
public static boolean canCrystalActivate(Ritual ritual, int crystalLevel) {
|
|
|
|
return ritual.getCrystalLevel() <= crystalLevel && RitualRegistry.ritualEnabled(ritual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getNextRitualKey(String currentKey) {
|
|
|
|
int currentIndex = RitualRegistry.getIds().indexOf(currentKey);
|
|
|
|
int nextIndex = RitualRegistry.getRituals().listIterator(currentIndex).nextIndex();
|
|
|
|
|
|
|
|
return RitualRegistry.getIds().get(nextIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getPrevRitualKey(String currentKey) {
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Checks the RitualRegistry to see if the configuration of the ritual stones in the world is valid
|
|
|
|
* for the given EnumFacing.
|
2015-12-29 21:37:54 +00:00
|
|
|
*
|
2015-12-29 13:44:34 +00:00
|
|
|
* @return The ID of the valid ritual
|
|
|
|
*/
|
2015-12-29 14:10:03 +00:00
|
|
|
public static String getValidRitual(World world, BlockPos pos) {
|
2015-12-29 13:44:34 +00:00
|
|
|
for(String key : RitualRegistry.getIds()) {
|
2015-12-29 14:10:03 +00:00
|
|
|
for(EnumFacing direction : EnumFacing.HORIZONTALS) {
|
|
|
|
boolean test = checkValidRitual(world, pos, key, direction);
|
|
|
|
if(test) {
|
|
|
|
return key;
|
|
|
|
}
|
2015-12-29 13:44:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2015-12-29 14:10:03 +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-27 00:49:25 +00:00
|
|
|
public static boolean checkValidRitual(World world, BlockPos pos, String ritualId, EnumFacing direction) {
|
2015-12-29 13:44:34 +00:00
|
|
|
Ritual ritual = RitualRegistry.getRitualForId(ritualId);
|
|
|
|
if(ritual == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayList<RitualComponent> components = ritual.getComponents();
|
|
|
|
|
2015-12-27 00:49:25 +00:00
|
|
|
if (components == null)
|
|
|
|
return false;
|
2015-12-29 13:44:34 +00:00
|
|
|
|
2015-12-27 00:49:25 +00:00
|
|
|
for (RitualComponent component : components) {
|
2015-12-29 14:10:03 +00:00
|
|
|
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 16:34:04 +00:00
|
|
|
if (block instanceof IRitualStone) {
|
|
|
|
if(!((IRitualStone)block).isRuneType(world, newPos, component.getRuneType())) {
|
2015-12-29 13:44:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
return false;
|
2015-12-27 00:49:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-03 01:45:11 +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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Adds your Ritual to the {@link RitualRegistry#enabledRituals} Map.
|
|
|
|
* This is used to determine whether your effect is enabled or not.
|
2015-12-02 05:14:26 +00:00
|
|
|
*
|
2015-11-01 00:58:47 +00:00
|
|
|
* The config option will be created as {@code B:ClassName=true} with a comment of
|
|
|
|
* {@code Enables the ClassName ritual}.
|
2015-12-02 05:14:26 +00:00
|
|
|
*
|
2015-11-03 01:45:11 +00:00
|
|
|
* Use {@link #}
|
2015-12-02 05:14:26 +00:00
|
|
|
*
|
2015-11-01 00:58:47 +00:00
|
|
|
* Should be safe to modify at any point.
|
|
|
|
*
|
|
|
|
* @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-03 01:45:11 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
2015-11-03 15:35:14 +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());
|
|
|
|
|
|
|
|
if (directory.exists()) {
|
|
|
|
String[] files = directory.list();
|
|
|
|
|
|
|
|
for (String file : files) {
|
|
|
|
if (file.endsWith(".class")) {
|
|
|
|
String className = file.substring(0, file.length() - 6);
|
|
|
|
|
|
|
|
try {
|
|
|
|
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
|
|
|
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (InstantiationException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|