2016-02-18 17:25:11 +01:00
|
|
|
package WayofTime.bloodmagic.ritual;
|
|
|
|
|
2017-08-14 20:53:42 -07:00
|
|
|
import WayofTime.bloodmagic.BloodMagic;
|
2016-03-17 13:00:44 -07:00
|
|
|
import WayofTime.bloodmagic.api.ritual.*;
|
|
|
|
import WayofTime.bloodmagic.util.Utils;
|
2016-02-18 17:25:11 +01:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.entity.item.EntityItem;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
2017-08-15 21:30:48 -07:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-02-18 17:25:11 +01:00
|
|
|
import net.minecraft.world.World;
|
2017-03-11 16:01:00 -08:00
|
|
|
import net.minecraftforge.items.IItemHandler;
|
|
|
|
import net.minecraftforge.items.ItemHandlerHelper;
|
2016-03-17 13:00:44 -07:00
|
|
|
|
2017-03-11 16:01:00 -08:00
|
|
|
import javax.annotation.Nullable;
|
2016-03-17 13:00:44 -07:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
2016-02-18 17:25:11 +01:00
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public class RitualFelling extends Ritual {
|
2016-02-18 17:25:11 +01:00
|
|
|
public static final String FELLING_RANGE = "fellingRange";
|
2016-04-11 09:57:57 -04:00
|
|
|
public static final String CHEST_RANGE = "chest";
|
2016-02-18 17:25:11 +01:00
|
|
|
|
|
|
|
private ArrayList<BlockPos> treePartsCache;
|
|
|
|
private Iterator<BlockPos> blockPosIterator;
|
|
|
|
|
|
|
|
private boolean cached = false;
|
|
|
|
private BlockPos currentPos;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public RitualFelling() {
|
2017-08-14 20:53:42 -07:00
|
|
|
super("ritualFelling", 0, 20000, "ritual." + BloodMagic.MODID + ".fellingRitual");
|
2016-02-18 17:25:11 +01:00
|
|
|
addBlockRange(FELLING_RANGE, new AreaDescriptor.Rectangle(new BlockPos(-10, -3, -10), new BlockPos(11, 27, 11)));
|
2016-04-11 09:57:57 -04: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 17:25:11 +01:00
|
|
|
|
|
|
|
treePartsCache = new ArrayList<BlockPos>();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public void performRitual(IMasterRitualStone masterRitualStone) {
|
2016-02-18 17:25:11 +01:00
|
|
|
World world = masterRitualStone.getWorldObj();
|
2017-02-12 17:00:18 -08:00
|
|
|
int currentEssence = masterRitualStone.getOwnerNetwork().getCurrentEssence();
|
2016-02-18 17:25:11 +01:00
|
|
|
|
2016-04-11 09:57:57 -04:00
|
|
|
BlockPos masterPos = masterRitualStone.getBlockPos();
|
|
|
|
AreaDescriptor chestRange = getBlockRange(CHEST_RANGE);
|
|
|
|
TileEntity tileInventory = world.getTileEntity(chestRange.getContainedPositions(masterPos).get(0));
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (currentEssence < getRefreshCost()) {
|
2017-02-12 17:00:18 -08:00
|
|
|
masterRitualStone.getOwnerNetwork().causeNausea();
|
2016-02-18 17:25:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (!cached || treePartsCache.isEmpty()) {
|
|
|
|
for (BlockPos blockPos : getBlockRange(FELLING_RANGE).getContainedPositions(masterRitualStone.getBlockPos())) {
|
2016-02-18 17:25:11 +01:00
|
|
|
if (!treePartsCache.contains(blockPos))
|
2017-08-15 21:30:48 -07: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 17:25:11 +01:00
|
|
|
treePartsCache.add(blockPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cached = true;
|
|
|
|
blockPosIterator = treePartsCache.iterator();
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (blockPosIterator.hasNext() && tileInventory != null) {
|
2017-02-12 17:00:18 -08:00
|
|
|
masterRitualStone.getOwnerNetwork().syphon(getRefreshCost());
|
2016-02-18 17:25:11 +01:00
|
|
|
currentPos = blockPosIterator.next();
|
2017-03-11 16:01:00 -08:00
|
|
|
IItemHandler inventory = Utils.getInventory(tileInventory, EnumFacing.DOWN);
|
|
|
|
placeInInventory(world.getBlockState(currentPos), world, currentPos, inventory);
|
2016-02-18 17:25:11 +01:00
|
|
|
world.setBlockToAir(currentPos);
|
|
|
|
blockPosIterator.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public int getRefreshCost() {
|
2016-02-19 20:11:29 -05:00
|
|
|
return 10;
|
2016-02-18 17:25:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public int getRefreshTime() {
|
2016-02-18 17:25:11 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public ArrayList<RitualComponent> getComponents() {
|
2016-02-18 17:25:11 +01:00
|
|
|
ArrayList<RitualComponent> components = new ArrayList<RitualComponent>();
|
|
|
|
|
|
|
|
addCornerRunes(components, 1, 0, EnumRuneType.EARTH);
|
|
|
|
addCornerRunes(components, 1, 1, EnumRuneType.EARTH);
|
|
|
|
|
|
|
|
return components;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public Ritual getNewCopy() {
|
2016-02-18 17:25:11 +01:00
|
|
|
return new RitualFelling();
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
private void placeInInventory(IBlockState choppedState, World world, BlockPos choppedPos, @Nullable IItemHandler inventory) {
|
2017-03-11 16:01:00 -08:00
|
|
|
if (inventory == null)
|
|
|
|
return;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
for (ItemStack stack : choppedState.getBlock().getDrops(world, choppedPos, world.getBlockState(choppedPos), 0)) {
|
2017-03-11 16:01:00 -08:00
|
|
|
ItemStack remainder = ItemHandlerHelper.insertItem(inventory, stack, false);
|
|
|
|
if (!remainder.isEmpty())
|
|
|
|
world.spawnEntity(new EntityItem(world, choppedPos.getX() + 0.4, choppedPos.getY() + 2, choppedPos.getZ() + 0.4, remainder));
|
2016-02-18 17:25:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|