Backport addition of TURTLE_CAN_USE

This commit is contained in:
Samuel Williams 2023-12-11 20:23:54 +00:00
parent f9bb1b4979
commit 1b6ee53f86
4 changed files with 29 additions and 11 deletions

View File

@ -0,0 +1 @@
{"values": ["#minecraft:cauldrons", "#minecraft:beehives"]}

View File

@ -12,6 +12,8 @@
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
/**
* Tags provided by ComputerCraft.
@ -58,6 +60,12 @@ public static class Blocks
*/
public static final TagKey<Block> TURTLE_HOE_BREAKABLE = make( "turtle_hoe_harvestable" );
/**
* Block which can be {@linkplain BlockState#use(Level, Player, InteractionHand, BlockHitResult) used} when
* calling {@code turtle.place()}.
*/
public static final TagKey<Block> TURTLE_CAN_USE = make( "turtle_can_use" );
private static TagKey<Block> make( String name )
{
return BlockTags.create( new ResourceLocation( ComputerCraft.MOD_ID, name ) );

View File

@ -60,6 +60,8 @@ protected void addTags()
tag( TURTLE_SWORD_BREAKABLE ).addTags( BlockTags.WOOL ).add( Blocks.COBWEB );
tag( TURTLE_CAN_USE ).addTag( BlockTags.CAULDRONS ).addTag( BlockTags.BEEHIVES );
// Make all blocks aside from command computer mineable.
tag( BlockTags.MINEABLE_WITH_PICKAXE ).add(
Registry.ModBlocks.COMPUTER_NORMAL.get(),

View File

@ -43,6 +43,8 @@
import javax.annotation.Nonnull;
import static dan200.computercraft.api.ComputerCraftTags.Blocks.TURTLE_CAN_USE;
public class TurtlePlaceCommand implements ITurtleCommand
{
private final InteractDirection direction;
@ -210,7 +212,7 @@ private static boolean canDeployOnBlock(
private static boolean deployOnBlock(
@Nonnull ItemStack stack, ITurtleAccess turtle, TurtlePlayer turtlePlayer, BlockPos position, Direction side,
Object[] extraArguments, boolean allowReplace, ErrorMessage outErrorMessage
Object[] extraArguments, boolean adjacent, ErrorMessage outErrorMessage
)
{
// Re-orient the fake player
@ -227,7 +229,7 @@ private static boolean deployOnBlock(
// Check if there's something suitable to place onto
BlockHitResult hit = new BlockHitResult( new Vec3( hitX, hitY, hitZ ), side, position, false );
UseOnContext context = new UseOnContext( turtlePlayer, InteractionHand.MAIN_HAND, hit );
if( !canDeployOnBlock( new BlockPlaceContext( context ), turtle, turtlePlayer, position, side, allowReplace, outErrorMessage ) )
if( !canDeployOnBlock( new BlockPlaceContext( context ), turtle, turtlePlayer, position, side, adjacent, outErrorMessage ) )
{
return false;
}
@ -235,7 +237,7 @@ private static boolean deployOnBlock(
Item item = stack.getItem();
BlockEntity existingTile = turtle.getLevel().getBlockEntity( position );
boolean placed = doDeployOnBlock( stack, turtlePlayer, position, context, hit ).consumesAction();
boolean placed = doDeployOnBlock( stack, turtlePlayer, position, context, hit, adjacent ).consumesAction();
// Set text on signs
if( placed && item instanceof SignItem && extraArguments != null && extraArguments.length >= 1 && extraArguments[0] instanceof String message )
@ -261,11 +263,13 @@ private static boolean deployOnBlock(
* @param position The block we're deploying against's position.
* @param context The context of this place action.
* @param hit Where the block we're placing against was clicked.
* @param adjacent If the block is directly adjacent to the turtle, and so can be interacted with via
* {@link BlockState#use(Level, Player, InteractionHand, BlockHitResult)}.
* @return If this item was deployed.
* @see net.minecraft.server.level.ServerPlayerGameMode#useItemOn For the original implementation.
*/
private static InteractionResult doDeployOnBlock(
@Nonnull ItemStack stack, TurtlePlayer turtlePlayer, BlockPos position, UseOnContext context, BlockHitResult hit
@Nonnull ItemStack stack, TurtlePlayer turtlePlayer, BlockPos position, UseOnContext context, BlockHitResult hit, boolean adjacent
)
{
PlayerInteractEvent.RightClickBlock event = ForgeHooks.onRightClickBlock( turtlePlayer, InteractionHand.MAIN_HAND, position, hit );
@ -273,14 +277,17 @@ private static InteractionResult doDeployOnBlock(
if( event.getUseItem() != Result.DENY )
{
InteractionResult result = stack.onItemUseFirst( context );
if( result != InteractionResult.PASS ) return result;
}
InteractionResult resultUseFirst = stack.onItemUseFirst( context );
if( resultUseFirst != InteractionResult.PASS ) return resultUseFirst;
if( event.getUseItem() != Result.DENY )
{
InteractionResult result = stack.useOn( context );
if( result != InteractionResult.PASS ) return result;
var block = turtlePlayer.level.getBlockState(hit.getBlockPos());
if (event.getUseBlock() != Result.DENY && !block.isAir() && adjacent && block.is( TURTLE_CAN_USE )) {
var useResult = block.use(turtlePlayer.level, turtlePlayer, InteractionHand.MAIN_HAND, hit);
if (useResult.consumesAction()) return useResult;
}
InteractionResult resultUseOn = stack.useOn( context );
if( resultUseOn != InteractionResult.PASS ) return resultUseOn;
}
Item item = stack.getItem();