BloodMagic/src/main/java/WayofTime/bloodmagic/apibutnotreally/util/helper/RitualHelper.java

144 lines
5.2 KiB
Java
Raw Normal View History

package WayofTime.bloodmagic.apibutnotreally.util.helper;
import WayofTime.bloodmagic.apibutnotreally.registry.RitualRegistry;
import WayofTime.bloodmagic.apibutnotreally.ritual.EnumRuneType;
import WayofTime.bloodmagic.apibutnotreally.ritual.IRitualStone;
import WayofTime.bloodmagic.apibutnotreally.ritual.Ritual;
import WayofTime.bloodmagic.apibutnotreally.ritual.RitualComponent;
import net.minecraft.block.Block;
2017-01-02 05:43:34 +00:00
import net.minecraft.block.state.IBlockState;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import java.util.ArrayList;
2017-08-16 04:30:48 +00:00
public class RitualHelper {
@CapabilityInject(IRitualStone.Tile.class)
static Capability<IRitualStone.Tile> RUNE_CAPABILITY = null;
2017-08-16 04:30:48 +00:00
public static boolean canCrystalActivate(Ritual ritual, int crystalLevel) {
return ritual.getCrystalLevel() <= crystalLevel && RitualRegistry.ritualEnabled(ritual);
}
2017-08-16 04:30:48 +00:00
public static String getNextRitualKey(String currentKey) {
int currentIndex = RitualRegistry.getIds().indexOf(currentKey);
int nextIndex = RitualRegistry.getRituals().listIterator(currentIndex).nextIndex();
return RitualRegistry.getIds().get(nextIndex);
}
2017-08-16 04:30:48 +00:00
public static String getPrevRitualKey(String currentKey) {
int currentIndex = RitualRegistry.getIds().indexOf(currentKey);
int previousIndex = RitualRegistry.getIds().listIterator(currentIndex).previousIndex();
return RitualRegistry.getIds().get(previousIndex);
}
/**
* Checks the RitualRegistry to see if the configuration of the ritual
* stones in the world is valid for the given EnumFacing.
2017-08-16 04:30:48 +00:00
*
* @param world - The world
* @param pos - Location of the MasterRitualStone
* @return The ID of the valid ritual
*/
2017-08-16 04:30:48 +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);
2017-08-16 04:30:48 +00:00
if (test) {
return key;
}
}
}
return "";
}
2017-08-16 04:30:48 +00:00
public static EnumFacing getDirectionOfRitual(World world, BlockPos pos, String key) {
for (EnumFacing direction : EnumFacing.HORIZONTALS) {
boolean test = checkValidRitual(world, pos, key, direction);
2017-08-16 04:30:48 +00:00
if (test) {
return direction;
}
}
return null;
2015-12-29 14:10:03 +00:00
}
2017-08-16 04:30:48 +00:00
public static boolean checkValidRitual(World world, BlockPos pos, String ritualId, EnumFacing direction) {
Ritual ritual = RitualRegistry.getRitualForId(ritualId);
2017-08-16 04:30:48 +00:00
if (ritual == null) {
return false;
}
ArrayList<RitualComponent> components = ritual.getComponents();
if (components == null)
return false;
2017-08-16 04:30:48 +00:00
for (RitualComponent component : components) {
BlockPos newPos = pos.add(component.getOffset(direction));
2017-08-16 04:30:48 +00:00
if (isRuneType(world, newPos, component.getRuneType())) {
continue;
2017-08-16 04:30:48 +00:00
} else {
return false;
}
}
return true;
}
2017-08-16 04:30:48 +00:00
public static boolean isRuneType(World world, BlockPos pos, EnumRuneType type) {
if (world == null)
return false;
Block block = world.getBlockState(pos).getBlock();
TileEntity tile = world.getTileEntity(pos);
if (block instanceof IRitualStone)
return ((IRitualStone) block).isRuneType(world, pos, type);
2016-03-16 22:41:06 +00:00
else if (tile instanceof IRitualStone.Tile)
return ((IRitualStone.Tile) tile).isRuneType(type);
else if (tile != null && tile.hasCapability(RUNE_CAPABILITY, null))
return tile.getCapability(RUNE_CAPABILITY, null).isRuneType(type);
return false;
}
2017-08-16 04:30:48 +00:00
public static boolean isRune(World world, BlockPos pos) {
if (world == null)
return false;
Block block = world.getBlockState(pos).getBlock();
TileEntity tile = world.getTileEntity(pos);
if (block instanceof IRitualStone)
return true;
2016-03-16 22:41:06 +00:00
else if (tile instanceof IRitualStone.Tile)
return true;
else if (tile != null && tile.hasCapability(RUNE_CAPABILITY, null))
return true;
return false;
}
2017-08-16 04:30:48 +00:00
public static void setRuneType(World world, BlockPos pos, EnumRuneType type) {
if (world == null)
return;
2017-01-02 05:43:34 +00:00
IBlockState state = world.getBlockState(pos);
TileEntity tile = world.getTileEntity(pos);
2017-01-02 05:43:34 +00:00
if (state.getBlock() instanceof IRitualStone)
((IRitualStone) state.getBlock()).setRuneType(world, pos, type);
2016-03-16 22:41:06 +00:00
else if (tile instanceof IRitualStone.Tile)
((IRitualStone.Tile) tile).setRuneType(type);
2017-08-16 04:30:48 +00:00
else if (tile != null && tile.hasCapability(RUNE_CAPABILITY, null)) {
tile.getCapability(RUNE_CAPABILITY, null).setRuneType(type);
2017-01-02 05:43:34 +00:00
world.notifyBlockUpdate(pos, state, state, 3);
}
}
}