1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-07-01 01:23:12 +00:00
Jonathan Coates 9142ccfc93
Rewrite turtle placing logic
- Simplify how the turtle's inventory is processed. We now copy all
   items into the player inventory, attempt to place, and then copy the
   items back.

   This also fixes the problem where turtle.place() wouldn't (always)
   update the item which was placed.

   I'm also hoping this is more "correct" in how we process drops from
   entities and whatnot. Though I've not had any reports of issues, so
   it's probably fine.

 - Replace the "error message" string array with an actual object. It'd
   be nicer all these functions returned a TurtleCommandResult, but
   merging error messages from that is a little harder.

Fun facts: the test suite was actually helpful here, and caught the fact
that hoeing was broken!
2021-05-29 15:18:26 +01:00

61 lines
2.2 KiB
Java

/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.turtle.core;
import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleCommand;
import dan200.computercraft.api.turtle.TurtleCommandResult;
import dan200.computercraft.api.turtle.event.TurtleBlockEvent;
import dan200.computercraft.shared.peripheral.generic.data.BlockData;
import net.minecraft.block.BlockState;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;
public class TurtleInspectCommand implements ITurtleCommand
{
private final InteractDirection direction;
public TurtleInspectCommand( InteractDirection direction )
{
this.direction = direction;
}
@Nonnull
@Override
public TurtleCommandResult execute( @Nonnull ITurtleAccess turtle )
{
// Get world direction from direction
Direction direction = this.direction.toWorldDir( turtle );
// Check if thing in front is air or not
World world = turtle.getWorld();
BlockPos oldPosition = turtle.getPosition();
BlockPos newPosition = oldPosition.relative( direction );
BlockState state = world.getBlockState( newPosition );
if( state.getBlock().isAir( state, world, newPosition ) )
{
return TurtleCommandResult.failure( "No block to inspect" );
}
Map<String, Object> table = BlockData.fill( new HashMap<>(), state );
// Fire the event, exiting if it is cancelled
TurtlePlayer turtlePlayer = TurtlePlayer.getWithPosition( turtle, oldPosition, direction );
TurtleBlockEvent.Inspect event = new TurtleBlockEvent.Inspect( turtle, turtlePlayer, world, newPosition, state, table );
if( MinecraftForge.EVENT_BUS.post( event ) ) return TurtleCommandResult.failure( event.getFailureMessage() );
return TurtleCommandResult.success( new Object[] { table } );
}
}