mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-12 16:27:47 +00:00
New IForgeBlock#getToolModifiedState hook allow better control over tools interacting with blocks. (#7176)
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
@@ -88,6 +88,7 @@ import net.minecraftforge.client.event.ClientChatReceivedEvent;
|
||||
import net.minecraftforge.client.event.RenderBlockOverlayEvent;
|
||||
import net.minecraftforge.client.event.RenderBlockOverlayEvent.OverlayType;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.common.ToolType;
|
||||
import net.minecraftforge.common.capabilities.CapabilityDispatcher;
|
||||
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
||||
import net.minecraftforge.common.util.BlockSnapshot;
|
||||
@@ -126,6 +127,7 @@ import net.minecraftforge.event.entity.player.SleepingTimeCheckEvent;
|
||||
import net.minecraftforge.event.entity.player.UseHoeEvent;
|
||||
import net.minecraftforge.event.furnace.FurnaceFuelBurnTimeEvent;
|
||||
import net.minecraftforge.event.world.BlockEvent;
|
||||
import net.minecraftforge.event.world.BlockEvent.BlockToolInteractEvent;
|
||||
import net.minecraftforge.event.world.BlockEvent.CreateFluidSourceEvent;
|
||||
import net.minecraftforge.event.world.BlockEvent.EntityMultiPlaceEvent;
|
||||
import net.minecraftforge.event.world.BlockEvent.EntityPlaceEvent;
|
||||
@@ -348,6 +350,8 @@ public class ForgeEventFactory
|
||||
return MinecraftForge.EVENT_BUS.post(event) ? "" : event.getMessage();
|
||||
}
|
||||
|
||||
//TODO: 1.17 Remove
|
||||
@Deprecated
|
||||
public static int onHoeUse(ItemUseContext context)
|
||||
{
|
||||
UseHoeEvent event = new UseHoeEvent(context);
|
||||
@@ -359,6 +363,13 @@ public class ForgeEventFactory
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static BlockState onToolUse(BlockState originalState, World world, BlockPos pos, PlayerEntity player, ItemStack stack, ToolType toolType)
|
||||
{
|
||||
BlockToolInteractEvent event = new BlockToolInteractEvent(world, pos, originalState, player, stack, toolType);
|
||||
return MinecraftForge.EVENT_BUS.post(event) ? null : event.getFinalState();
|
||||
}
|
||||
|
||||
public static int onApplyBonemeal(@Nonnull PlayerEntity player, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull BlockState state, @Nonnull ItemStack stack)
|
||||
{
|
||||
|
||||
@@ -33,9 +33,12 @@ import javax.annotation.Nonnull;
|
||||
* and damage the hoe.
|
||||
*
|
||||
* setResult(ALLOW) is the same as the old setHandled();
|
||||
*
|
||||
* TODO: 1.17 Remove
|
||||
*/
|
||||
@Cancelable
|
||||
@HasResult
|
||||
@Deprecated
|
||||
public class UseHoeEvent extends PlayerEvent
|
||||
{
|
||||
private final ItemUseContext context;;
|
||||
|
||||
@@ -35,6 +35,7 @@ import net.minecraft.util.Direction;
|
||||
import net.minecraft.world.IWorld;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.ForgeHooks;
|
||||
import net.minecraftforge.common.ToolType;
|
||||
import net.minecraftforge.common.util.BlockSnapshot;
|
||||
import net.minecraftforge.eventbus.api.Cancelable;
|
||||
import net.minecraftforge.eventbus.api.Event;
|
||||
@@ -444,4 +445,68 @@ public class BlockEvent extends Event
|
||||
return size;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fired when when this block is right clicked by a tool to change its state.
|
||||
* For example: Used to determine if an axe can strip, a shovel can path, or a hoe can till.
|
||||
*
|
||||
* This event is {@link Cancelable}. If canceled, this will prevent the tool
|
||||
* from changing the block's state.
|
||||
*/
|
||||
@Cancelable
|
||||
public static class BlockToolInteractEvent extends BlockEvent
|
||||
{
|
||||
|
||||
private final PlayerEntity player;
|
||||
private final ItemStack stack;
|
||||
private final ToolType toolType;
|
||||
private BlockState state;
|
||||
|
||||
public BlockToolInteractEvent(IWorld world, BlockPos pos, BlockState originalState, PlayerEntity player, ItemStack stack, ToolType toolType)
|
||||
{
|
||||
super(world, pos, originalState);
|
||||
this.player = player;
|
||||
this.stack = stack;
|
||||
this.state = originalState;
|
||||
this.toolType = toolType;
|
||||
}
|
||||
|
||||
/**Gets the player using the tool.*/
|
||||
public PlayerEntity getPlayer()
|
||||
{
|
||||
return player;
|
||||
}
|
||||
|
||||
/**Gets the tool being used.*/
|
||||
public ItemStack getHeldItemStack()
|
||||
{
|
||||
return stack;
|
||||
}
|
||||
|
||||
/**Gets the current type of the tool being compared against.*/
|
||||
public ToolType getToolType()
|
||||
{
|
||||
return toolType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the transformed state after tool use.
|
||||
* If not set, will return the original state.
|
||||
* This will be bypassed if canceled returning null instead.
|
||||
* */
|
||||
public void setFinalState(BlockState finalState)
|
||||
{
|
||||
this.state = finalState;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the transformed state after tool use.
|
||||
* If setFinalState not called, will return the original state.
|
||||
* This will be bypassed if canceled returning null instead.
|
||||
* */
|
||||
public BlockState getFinalState()
|
||||
{
|
||||
return state;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user