BloodMagic/src/main/java/WayofTime/bloodmagic/ritual/RitualMagnetic.java

191 lines
5.9 KiB
Java
Raw Normal View History

2016-01-09 16:37:20 -05:00
package WayofTime.bloodmagic.ritual;
import WayofTime.bloodmagic.BloodMagic;
import WayofTime.bloodmagic.apibutnotreally.BlockStack;
import WayofTime.bloodmagic.apibutnotreally.ritual.*;
2017-08-15 21:30:48 -07:00
import WayofTime.bloodmagic.util.Utils;
2016-01-09 16:37:20 -05:00
import net.minecraft.block.Block;
import net.minecraft.block.BlockOre;
import net.minecraft.block.BlockRedstoneOre;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
2016-12-12 19:56:36 -08:00
import net.minecraft.init.Items;
2016-01-09 16:37:20 -05:00
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
2016-01-09 16:37:20 -05:00
import net.minecraft.world.World;
import net.minecraftforge.oredict.OreDictionary;
2017-08-15 21:30:48 -07:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
2016-01-09 16:37:20 -05:00
2017-08-15 21:30:48 -07:00
public class RitualMagnetic extends Ritual {
2016-01-09 16:37:20 -05:00
public static final String PLACEMENT_RANGE = "placementRange";
2017-08-15 21:30:48 -07:00
private static final Map<BlockStack, Boolean> oreBlockCache = new HashMap<BlockStack, Boolean>();
2016-01-09 16:37:20 -05:00
// public static final String SEARCH_RANGE = "searchRange";
public BlockPos lastPos; // An offset
2017-08-15 21:30:48 -07:00
public RitualMagnetic() {
super("ritualMagnetic", 0, 5000, "ritual." + BloodMagic.MODID + ".magneticRitual");
2016-01-09 16:37:20 -05:00
addBlockRange(PLACEMENT_RANGE, new AreaDescriptor.Rectangle(new BlockPos(-1, 1, -1), 3));
setMaximumVolumeAndDistanceOfRange(PLACEMENT_RANGE, 50, 4, 4);
2016-01-09 16:37:20 -05:00
}
@Override
2017-08-15 21:30:48 -07:00
public void performRitual(IMasterRitualStone masterRitualStone) {
2016-01-09 16:37:20 -05:00
World world = masterRitualStone.getWorldObj();
int currentEssence = masterRitualStone.getOwnerNetwork().getCurrentEssence();
2016-01-09 16:37:20 -05:00
2017-08-15 21:30:48 -07:00
if (currentEssence < getRefreshCost()) {
masterRitualStone.getOwnerNetwork().causeNausea();
2016-01-09 16:37:20 -05:00
return;
}
BlockPos pos = masterRitualStone.getBlockPos();
AreaDescriptor placementRange = getBlockRange(PLACEMENT_RANGE);
BlockPos replacement = pos;
boolean replace = false;
2017-08-15 21:30:48 -07:00
for (BlockPos offset : placementRange.getContainedPositions(pos)) {
if (world.isAirBlock(offset)) {
2016-01-09 16:37:20 -05:00
replacement = offset;
replace = true;
break;
}
}
IBlockState downState = world.getBlockState(pos.down());
Block downBlock = downState.getBlock();
int radius = getRadius(downBlock);
2017-08-15 21:30:48 -07:00
if (replace) {
2016-01-09 16:37:20 -05:00
int j = -1;
int i = -radius;
int k = -radius;
2017-08-15 21:30:48 -07:00
if (lastPos != null) {
2016-01-09 16:37:20 -05:00
j = lastPos.getY();
i = Math.min(radius, Math.max(-radius, lastPos.getX()));
k = Math.min(radius, Math.max(-radius, lastPos.getZ()));
}
2017-08-15 21:30:48 -07:00
while (j + pos.getY() >= 0) {
while (i <= radius) {
while (k <= radius) {
2016-01-09 16:37:20 -05:00
BlockPos newPos = pos.add(i, j, k);
IBlockState state = world.getBlockState(newPos);
Block block = state.getBlock();
2016-07-03 07:40:15 -04:00
ItemStack checkStack = block.getItem(world, newPos, state);
// int meta = block.getMetaFromState(state);
2016-01-09 16:37:20 -05:00
2017-08-15 21:30:48 -07:00
if (isBlockOre(checkStack)) {
Utils.swapLocations(world, newPos, world, replacement);
masterRitualStone.getOwnerNetwork().syphon(getRefreshCost());
2016-01-09 16:37:20 -05:00
k++;
this.lastPos = new BlockPos(i, j, k);
return;
2017-08-15 21:30:48 -07:00
} else {
2016-01-09 16:37:20 -05:00
k++;
}
}
i++;
k = -radius;
}
j--;
i = -radius;
this.lastPos = new BlockPos(i, j, k);
return;
}
j = -1;
this.lastPos = new BlockPos(i, j, k);
return;
}
}
2017-08-15 21:30:48 -07:00
public int getRadius(Block block) {
if (block == Blocks.IRON_BLOCK) {
2016-01-09 16:37:20 -05:00
return 7;
}
2017-08-15 21:30:48 -07:00
if (block == Blocks.GOLD_BLOCK) {
2016-01-09 16:37:20 -05:00
return 15;
}
2017-08-15 21:30:48 -07:00
if (block == Blocks.DIAMOND_BLOCK) {
2016-01-09 16:37:20 -05:00
return 31;
}
return 3;
}
@Override
2017-08-15 21:30:48 -07:00
public int getRefreshTime() {
2016-01-09 16:37:20 -05:00
return 40;
}
@Override
2017-08-15 21:30:48 -07:00
public int getRefreshCost() {
2016-01-09 18:17:38 -05:00
return 50;
2016-01-09 16:37:20 -05:00
}
@Override
2017-08-15 21:30:48 -07:00
public ArrayList<RitualComponent> getComponents() {
2016-01-09 16:37:20 -05:00
ArrayList<RitualComponent> components = new ArrayList<RitualComponent>();
this.addCornerRunes(components, 1, 0, EnumRuneType.EARTH);
this.addParallelRunes(components, 2, 1, EnumRuneType.EARTH);
this.addCornerRunes(components, 2, 1, EnumRuneType.AIR);
this.addParallelRunes(components, 2, 2, EnumRuneType.FIRE);
return components;
}
@Override
2017-08-15 21:30:48 -07:00
public Ritual getNewCopy() {
2016-01-09 16:37:20 -05:00
return new RitualMagnetic();
}
2017-08-15 21:30:48 -07:00
public static boolean isBlockOre(Block block, int meta) {
if (block == null)
return false;
if (block instanceof BlockOre || block instanceof BlockRedstoneOre)
return true;
if (Item.getItemFromBlock(block) == Items.AIR)
return false;
BlockStack type = new BlockStack(block, meta);
Boolean result = oreBlockCache.get(type);
if (result == null) {
result = computeIsItemOre(type);
oreBlockCache.put(type, result);
}
return result;
}
private static boolean computeIsItemOre(BlockStack type) {
ItemStack stack = new ItemStack(type.getBlock(), type.getMeta());
return isBlockOre(stack);
}
public static boolean isBlockOre(ItemStack stack) {
if (stack.isEmpty()) {
return false;
}
for (int id : OreDictionary.getOreIDs(stack)) {
String oreName = OreDictionary.getOreName(id);
if (oreName.contains("ore"))
return true;
}
return false;
}
2016-01-09 16:37:20 -05:00
}