Search in sources :

Example 1 with FutureBreak

use of com.ferreusveritas.dynamictrees.event.FutureBreak in project DynamicTrees by DynamicTreesTeam.

the class BlockBranch method futureBreak.

/*
	1.10.2 Simplified Block Harvesting Logic Flow(for no silk touch)
	
	tryHarvestBlock {
		canHarvest = canHarvestBlock() <- (ForgeHooks.canHarvestBlock occurs in here)
		removed = removeBlock(canHarvest) {
			removedByPlayer() {
				onBlockHarvested()
				world.setBlockState() <- block is set to air here
			}
		}
		
		if (removed) harvestBlock() {
			fortune = getEnchantmentLevel(FORTUNE)
			dropBlockAsItem(fortune) {
				dropBlockAsItemWithChance(fortune) {
					items = getDrops(fortune) {
						getItemDropped(fortune) {
							Item.getItemFromBlock(this) <- (Standard block behavior)
						}
					}
					ForgeEventFactory.fireBlockHarvesting(items) <- (BlockEvent.HarvestDropsEvent)
					(for all items) -> spawnAsEntity(item)
				}
			}
		}
	}
	 */
@Override
public void futureBreak(IBlockState state, World world, BlockPos cutPos, EntityLivingBase entity) {
    // Try to get the face being pounded on
    final double reachDistance = entity instanceof EntityPlayerMP ? entity.getEntityAttribute(EntityPlayer.REACH_DISTANCE).getAttributeValue() : 5.0D;
    RayTraceResult rtResult = playerRayTrace(entity, reachDistance, 1.0F);
    EnumFacing toolDir = rtResult != null ? (entity.isSneaking() ? rtResult.sideHit.getOpposite() : rtResult.sideHit) : EnumFacing.DOWN;
    if (toolDir == null) {
        // Some rayTracing results can theoretically produce a face hit with no side designation.
        // Make everything better
        toolDir = EnumFacing.DOWN;
    }
    // Do the actual destruction
    BranchDestructionData destroyData = destroyBranchFromNode(world, cutPos, toolDir, false);
    // Get all of the wood drops
    ItemStack heldItem = entity.getHeldItemMainhand();
    int fortune = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, heldItem);
    float fortuneFactor = 1.0f + 0.25f * fortune;
    // The amount of wood calculated from the body of the tree network
    float woodVolume = destroyData.woodVolume;
    List<ItemStack> woodItems = getLogDrops(world, cutPos, destroyData.species, woodVolume * fortuneFactor);
    if (entity.getActiveHand() == null) {
        // What the hell man? I trusted you!
        // Players do things with hands.
        entity.setActiveHand(EnumHand.MAIN_HAND);
    }
    float chance = 1.0f;
    // Fire the block harvesting event.  For An-Sar's PrimalCore mod :)
    if (entity instanceof EntityPlayer) {
        chance = net.minecraftforge.event.ForgeEventFactory.fireBlockHarvesting(woodItems, world, cutPos, state, fortune, chance, false, (EntityPlayer) entity);
    }
    final float finalChance = chance;
    // Build the final wood drop list taking chance into consideration
    List<ItemStack> woodDropList = woodItems.stream().filter(i -> world.rand.nextFloat() <= finalChance).collect(Collectors.toList());
    // This will drop the EntityFallingTree into the world
    EntityFallingTree.dropTree(world, destroyData, woodDropList, DestroyType.HARVEST);
    // Damage the axe by a prescribed amount
    damageAxe(entity, heldItem, getRadius(state), woodVolume);
}
Also used : ModConfigs(com.ferreusveritas.dynamictrees.ModConfigs) Explosion(net.minecraft.world.Explosion) FutureBreak(com.ferreusveritas.dynamictrees.event.FutureBreak) Item(net.minecraft.item.Item) EnumHand(net.minecraft.util.EnumHand) DestroyType(com.ferreusveritas.dynamictrees.entities.EntityFallingTree.DestroyType) TreeFamily(com.ferreusveritas.dynamictrees.trees.TreeFamily) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) IProperty(net.minecraft.block.properties.IProperty) Block(net.minecraft.block.Block) Vec3d(net.minecraft.util.math.Vec3d) EnumPushReaction(net.minecraft.block.material.EnumPushReaction) ITreePart(com.ferreusveritas.dynamictrees.api.treedata.ITreePart) BlockFaceShape(net.minecraft.block.state.BlockFaceShape) ItemAxe(net.minecraft.item.ItemAxe) Enchantments(net.minecraft.init.Enchantments) Collectors(java.util.stream.Collectors) BlockBounds(com.ferreusveritas.dynamictrees.util.BlockBounds) EnchantmentHelper(net.minecraft.enchantment.EnchantmentHelper) BranchDestructionData(com.ferreusveritas.dynamictrees.util.BranchDestructionData) EntityPlayer(net.minecraft.entity.player.EntityPlayer) NodeDestroyer(com.ferreusveritas.dynamictrees.systems.nodemappers.NodeDestroyer) NodeSpecies(com.ferreusveritas.dynamictrees.systems.nodemappers.NodeSpecies) SimpleVoxmap(com.ferreusveritas.dynamictrees.util.SimpleVoxmap) java.util(java.util) Blocks(net.minecraft.init.Blocks) EntityCreeper(net.minecraft.entity.monster.EntityCreeper) Species(com.ferreusveritas.dynamictrees.trees.Species) MapSignal(com.ferreusveritas.dynamictrees.api.network.MapSignal) ItemStack(net.minecraft.item.ItemStack) RayTraceResult(net.minecraft.util.math.RayTraceResult) EntityFallingTree(com.ferreusveritas.dynamictrees.entities.EntityFallingTree) NodeNetVolume(com.ferreusveritas.dynamictrees.systems.nodemappers.NodeNetVolume) ModBlocks(com.ferreusveritas.dynamictrees.ModBlocks) IUnlistedProperty(net.minecraftforge.common.property.IUnlistedProperty) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos) Cell(com.ferreusveritas.dynamictrees.util.SimpleVoxmap.Cell) IBlockAccess(net.minecraft.world.IBlockAccess) Nullable(javax.annotation.Nullable) IFutureBreakable(com.ferreusveritas.dynamictrees.api.IFutureBreakable) Properties(net.minecraftforge.common.property.Properties) World(net.minecraft.world.World) PropertyInteger(net.minecraft.block.properties.PropertyInteger) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) TreeHelper(com.ferreusveritas.dynamictrees.api.TreeHelper) IBlockState(net.minecraft.block.state.IBlockState) Material(net.minecraft.block.material.Material) EntityLivingBase(net.minecraft.entity.EntityLivingBase) NodeExtState(com.ferreusveritas.dynamictrees.systems.nodemappers.NodeExtState) EnumFacing(net.minecraft.util.EnumFacing) RayTraceResult(net.minecraft.util.math.RayTraceResult) BranchDestructionData(com.ferreusveritas.dynamictrees.util.BranchDestructionData) EntityPlayer(net.minecraft.entity.player.EntityPlayer) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) ItemStack(net.minecraft.item.ItemStack)

Aggregations

ModBlocks (com.ferreusveritas.dynamictrees.ModBlocks)1 ModConfigs (com.ferreusveritas.dynamictrees.ModConfigs)1 IFutureBreakable (com.ferreusveritas.dynamictrees.api.IFutureBreakable)1 TreeHelper (com.ferreusveritas.dynamictrees.api.TreeHelper)1 MapSignal (com.ferreusveritas.dynamictrees.api.network.MapSignal)1 ITreePart (com.ferreusveritas.dynamictrees.api.treedata.ITreePart)1 EntityFallingTree (com.ferreusveritas.dynamictrees.entities.EntityFallingTree)1 DestroyType (com.ferreusveritas.dynamictrees.entities.EntityFallingTree.DestroyType)1 FutureBreak (com.ferreusveritas.dynamictrees.event.FutureBreak)1 NodeDestroyer (com.ferreusveritas.dynamictrees.systems.nodemappers.NodeDestroyer)1 NodeExtState (com.ferreusveritas.dynamictrees.systems.nodemappers.NodeExtState)1 NodeNetVolume (com.ferreusveritas.dynamictrees.systems.nodemappers.NodeNetVolume)1 NodeSpecies (com.ferreusveritas.dynamictrees.systems.nodemappers.NodeSpecies)1 Species (com.ferreusveritas.dynamictrees.trees.Species)1 TreeFamily (com.ferreusveritas.dynamictrees.trees.TreeFamily)1 BlockBounds (com.ferreusveritas.dynamictrees.util.BlockBounds)1 BranchDestructionData (com.ferreusveritas.dynamictrees.util.BranchDestructionData)1 SimpleVoxmap (com.ferreusveritas.dynamictrees.util.SimpleVoxmap)1 Cell (com.ferreusveritas.dynamictrees.util.SimpleVoxmap.Cell)1 java.util (java.util)1