2016-02-18 16:25:11 +00:00
|
|
|
package WayofTime.bloodmagic.ritual;
|
|
|
|
|
2016-03-17 20:00:44 +00:00
|
|
|
import WayofTime.bloodmagic.api.Constants;
|
2016-06-12 20:41:02 +00:00
|
|
|
import WayofTime.bloodmagic.api.saving.SoulNetwork;
|
2016-03-17 20:00:44 +00:00
|
|
|
import WayofTime.bloodmagic.api.ritual.*;
|
|
|
|
import WayofTime.bloodmagic.api.util.helper.NetworkHelper;
|
|
|
|
import WayofTime.bloodmagic.util.Utils;
|
2016-02-18 16:25:11 +00:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.entity.item.EntityItem;
|
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2016-03-17 20:00:44 +00:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-02-18 16:25:11 +00:00
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.world.World;
|
2016-03-17 20:00:44 +00:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
2016-02-18 16:25:11 +00:00
|
|
|
|
|
|
|
public class RitualFelling extends Ritual
|
|
|
|
{
|
|
|
|
public static final String FELLING_RANGE = "fellingRange";
|
2016-04-11 13:57:57 +00:00
|
|
|
public static final String CHEST_RANGE = "chest";
|
2016-02-18 16:25:11 +00:00
|
|
|
|
|
|
|
private ArrayList<BlockPos> treePartsCache;
|
|
|
|
private Iterator<BlockPos> blockPosIterator;
|
|
|
|
|
|
|
|
private boolean cached = false;
|
|
|
|
private BlockPos currentPos;
|
|
|
|
|
|
|
|
public RitualFelling()
|
|
|
|
{
|
2016-04-11 13:57:57 +00:00
|
|
|
super("ritualFelling", 0, 20000, "ritual." + Constants.Mod.MODID + ".fellingRitual");
|
2016-02-18 16:25:11 +00:00
|
|
|
addBlockRange(FELLING_RANGE, new AreaDescriptor.Rectangle(new BlockPos(-10, -3, -10), new BlockPos(11, 27, 11)));
|
2016-04-11 13:57:57 +00:00
|
|
|
addBlockRange(CHEST_RANGE, new AreaDescriptor.Rectangle(new BlockPos(0, 1, 0), 1));
|
|
|
|
|
|
|
|
setMaximumVolumeAndDistanceOfRange(FELLING_RANGE, 14000, 15, 30);
|
|
|
|
setMaximumVolumeAndDistanceOfRange(CHEST_RANGE, 1, 3, 3);
|
2016-02-18 16:25:11 +00:00
|
|
|
|
|
|
|
treePartsCache = new ArrayList<BlockPos>();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void performRitual(IMasterRitualStone masterRitualStone)
|
|
|
|
{
|
|
|
|
World world = masterRitualStone.getWorldObj();
|
|
|
|
SoulNetwork network = NetworkHelper.getSoulNetwork(masterRitualStone.getOwner());
|
|
|
|
int currentEssence = network.getCurrentEssence();
|
|
|
|
|
2016-04-11 13:57:57 +00:00
|
|
|
BlockPos masterPos = masterRitualStone.getBlockPos();
|
|
|
|
AreaDescriptor chestRange = getBlockRange(CHEST_RANGE);
|
|
|
|
TileEntity tileInventory = world.getTileEntity(chestRange.getContainedPositions(masterPos).get(0));
|
|
|
|
|
2016-02-18 16:25:11 +00:00
|
|
|
if (currentEssence < getRefreshCost())
|
|
|
|
{
|
2016-06-12 20:41:02 +00:00
|
|
|
network.causeNausea();
|
2016-02-18 16:25:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cached || treePartsCache.isEmpty())
|
|
|
|
{
|
|
|
|
for (BlockPos blockPos : getBlockRange(FELLING_RANGE).getContainedPositions(masterRitualStone.getBlockPos()))
|
|
|
|
{
|
|
|
|
if (!treePartsCache.contains(blockPos))
|
2016-03-17 20:00:44 +00:00
|
|
|
if (!world.isAirBlock(blockPos) && (world.getBlockState(blockPos).getBlock().isWood(world, blockPos) || world.getBlockState(blockPos).getBlock().isLeaves(world.getBlockState(blockPos), world, blockPos)))
|
2016-02-18 16:25:11 +00:00
|
|
|
{
|
|
|
|
treePartsCache.add(blockPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cached = true;
|
|
|
|
blockPosIterator = treePartsCache.iterator();
|
|
|
|
}
|
|
|
|
|
2016-04-11 13:57:57 +00:00
|
|
|
if (blockPosIterator.hasNext() && tileInventory != null && tileInventory instanceof IInventory)
|
2016-02-18 16:25:11 +00:00
|
|
|
{
|
|
|
|
network.syphon(getRefreshCost());
|
|
|
|
currentPos = blockPosIterator.next();
|
2016-04-11 13:57:57 +00:00
|
|
|
placeInInventory(world.getBlockState(currentPos), world, currentPos, chestRange.getContainedPositions(masterPos).get(0));
|
2016-02-18 16:25:11 +00:00
|
|
|
world.setBlockToAir(currentPos);
|
|
|
|
blockPosIterator.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getRefreshCost()
|
|
|
|
{
|
2016-02-20 01:11:29 +00:00
|
|
|
return 10;
|
2016-02-18 16:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getRefreshTime()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ArrayList<RitualComponent> getComponents()
|
|
|
|
{
|
|
|
|
ArrayList<RitualComponent> components = new ArrayList<RitualComponent>();
|
|
|
|
|
|
|
|
addCornerRunes(components, 1, 0, EnumRuneType.EARTH);
|
|
|
|
addCornerRunes(components, 1, 1, EnumRuneType.EARTH);
|
|
|
|
|
|
|
|
return components;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Ritual getNewCopy()
|
|
|
|
{
|
|
|
|
return new RitualFelling();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void placeInInventory(IBlockState blockState, World world, BlockPos blockPos, BlockPos tileEntityPos)
|
|
|
|
{
|
|
|
|
TileEntity tile = world.getTileEntity(tileEntityPos);
|
|
|
|
if (tile != null && blockState.getBlock().getDrops(world, blockPos, world.getBlockState(blockPos), 0) != null)
|
|
|
|
{
|
|
|
|
if (tile instanceof IInventory)
|
|
|
|
{
|
|
|
|
for (ItemStack stack : blockState.getBlock().getDrops(world, blockPos, world.getBlockState(blockPos), 0))
|
|
|
|
{
|
|
|
|
ItemStack copyStack = stack.copy();
|
|
|
|
Utils.insertStackIntoInventory(copyStack, (IInventory) tile, EnumFacing.DOWN);
|
|
|
|
if (copyStack.stackSize > 0)
|
|
|
|
{
|
|
|
|
world.spawnEntityInWorld(new EntityItem(world, blockPos.getX() + 0.4, blockPos.getY() + 2, blockPos.getZ() + 0.4, copyStack));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|