Skip to content

Commit

Permalink
Backport of TURTLE_CAN_USE (#1656)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelWilliams99 authored Dec 12, 2023
1 parent f9bb1b4 commit c1954b4
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/main/java/dan200/computercraft/api/ComputerCraftTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ protected void addTags()

tag( TURTLE_SWORD_BREAKABLE ).addTags( BlockTags.WOOL ).add( Blocks.COBWEB );

tag( TURTLE_CAN_USE )
.addTag( BlockTags.CAULDRONS )
.addTag( BlockTags.BEEHIVES )
.add( Blocks.COMPOSTER );

// Make all blocks aside from command computer mineable.
tag( BlockTags.MINEABLE_WITH_PICKAXE ).add(
Registry.ModBlocks.COMPUTER_NORMAL.get(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -227,15 +229,15 @@ 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;
}

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 )
Expand All @@ -261,26 +263,32 @@ 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 );
if( event.isCanceled() ) return event.getCancellationResult();

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,8 @@ class Turtle_Test {

/**
* Checks turtles can be cleaned in cauldrons.
*
* Currently not required as turtles can no longer right-click cauldrons.
*/
@GameTest(required = false)
@GameTest
fun Cleaned_with_cauldrons(helper: GameTestHelper) = helper.sequence {
thenOnComputer {
val details = getTurtleItemDetail(1, true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
DataVersion: 2730,
DataVersion: 3218,
size: [3, 3, 3],
data: [
{pos: [0, 0, 0], state: "minecraft:polished_andesite"},
Expand All @@ -15,7 +15,7 @@
{pos: [0, 1, 1], state: "minecraft:air"},
{pos: [0, 1, 2], state: "minecraft:air"},
{pos: [1, 1, 0], state: "computercraft:turtle_normal{facing:south,waterlogged:false}", nbt: {ComputerId: 1, Fuel: 0, Items: [{Count: 1b, Slot: 0b, id: "computercraft:turtle_normal", tag: {Color: 13388876, ComputerId: 0, display: {Name: '{"text":"Clean turtle"}'}}}], Label: "turtle_test.cleaned_with_cauldrons", On: 1b, Owner: {LowerId: -6876936588741668278L, Name: "Dev", UpperId: 4039158846114182220L}, Slot: 0, id: "computercraft:turtle_normal"}},
{pos: [1, 1, 1], state: "minecraft:cauldron{level:3}"},
{pos: [1, 1, 1], state: "minecraft:water_cauldron{level:3}"},
{pos: [1, 1, 2], state: "minecraft:air"},
{pos: [2, 1, 0], state: "minecraft:air"},
{pos: [2, 1, 1], state: "minecraft:air"},
Expand All @@ -33,8 +33,8 @@
entities: [],
palette: [
"minecraft:polished_andesite",
"computercraft:turtle_normal{facing:south,waterlogged:false}",
"minecraft:air",
"minecraft:cauldron{level:3}"
"minecraft:water_cauldron{level:3}",
"computercraft:turtle_normal{facing:south,waterlogged:false}"
]
}

0 comments on commit c1954b4

Please sign in to comment.