New IForgeBlock#getToolModifiedState hook allow better control over tools interacting with blocks. (#7176)

This commit is contained in:
ChampionAsh5357 authored and GitHub committed 2020-08-03 16:49:26 -07:00
1 parent 97a2b9a448
commit 0984777c98
10 files changed
+214 -4

No files matched your search

@@ -42,8 +42,11 @@ import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.WitherSkullEntity;
import net.minecraft.fluid.FluidState;
import net.minecraft.potion.Effects;
import net.minecraft.item.AxeItem;
import net.minecraft.item.DyeColor;
import net.minecraft.item.HoeItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ShovelItem;
import net.minecraft.pathfinding.PathNodeType;
import net.minecraft.state.Property;
import net.minecraft.state.properties.BedPart;
@@ -887,4 +890,24 @@ public interface IForgeBlock
{
return state.getBlock() instanceof BreakableBlock || state.getBlock() instanceof LeavesBlock;
}
/**
* Returns the state that this block should transform into when right clicked by a tool.
* For example: Used to determine if an axe can strip, a shovel can path, or a hoe can till.
* Return null if vanilla behavior should be disabled.
*
* @param state The current state
* @param world The world
* @param pos The block position in world
* @param player The player clicking the block
* @param stack The stack being used by the player
* @return The resulting state after the action has been performed
*/
@Nullable
default BlockState getToolModifiedState(BlockState state, World world, BlockPos pos, PlayerEntity player, ItemStack stack, ToolType toolType)
{
if (toolType == ToolType.AXE) return AxeItem.getAxeStrippingState(state);
else if(toolType == ToolType.HOE) return HoeItem.getHoeTillingState(state);
else return toolType == ToolType.SHOVEL ? ShovelItem.getShovelPathingState(state) : null;
}
}
@@ -778,4 +778,23 @@ public interface IForgeBlockState
{
return getBlockState().getBlock().shouldDisplayFluidOverlay(getBlockState(), world, pos, fluidState);
}
/**
* Returns the state that this block should transform into when right clicked by a tool.
* For example: Used to determine if an axe can strip, a shovel can path, or a hoe can till.
* Return null if vanilla behavior should be disabled.
*
* @param world The world
* @param pos The block position in world
* @param player The player clicking the block
* @param stack The stack being used by the player
* @param toolTypes The tool types to be considered when performing the action
* @return The resulting state after the action has been performed
*/
@Nullable
default BlockState getToolModifiedState(World world, BlockPos pos, PlayerEntity player, ItemStack stack, ToolType toolType)
{
BlockState eventState = net.minecraftforge.event.ForgeEventFactory.onToolUse(getBlockState(), world, pos, player, stack, toolType);
return eventState != getBlockState() ? eventState : getBlockState().getBlock().getToolModifiedState(getBlockState(), world, pos, player, stack, toolType);
}
}