Skip to content

Commit

Permalink
add pitch & yaw argument to automata core
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Apr 26, 2024
1 parent 4acaa1c commit 8116c68
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package de.srendi.advancedperipherals.common.addons.computercraft.peripheral.plugins;

import dan200.computercraft.api.lua.IArguments;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.core.apis.TableHelper;
import de.srendi.advancedperipherals.common.addons.computercraft.owner.TurtlePeripheralOwner;
import de.srendi.advancedperipherals.common.util.Pair;
import de.srendi.advancedperipherals.common.util.fakeplayer.APFakePlayer;
import de.srendi.advancedperipherals.lib.peripherals.AutomataCorePeripheral;
import de.srendi.advancedperipherals.lib.peripherals.IPeripheralOperation;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;

import static de.srendi.advancedperipherals.common.addons.computercraft.operations.SingleOperation.DIG;
import static de.srendi.advancedperipherals.common.addons.computercraft.operations.SingleOperation.USE_ON_BLOCK;

Expand All @@ -27,12 +32,15 @@ public AutomataBlockHandPlugin(AutomataCorePeripheral automataCore) {
}

@LuaFunction(mainThread = true)
public final MethodResult digBlock() throws LuaException {
public final MethodResult digBlock(@NotNull IArguments arguments) throws LuaException {
Map<?, ?> opts = arguments.count() > 0 ? arguments.getTable(0) : null;
float yaw = opts != null ? (float) TableHelper.optNumberField(opts, "yaw", 0) : 0;
float pitch = opts != null ? (float) TableHelper.optNumberField(opts, "pitch", 0) : 0;
return automataCore.withOperation(DIG, context -> {
TurtlePeripheralOwner owner = automataCore.getPeripheralOwner();
ItemStack selectedTool = owner.getToolInMainHand();
int previousDamageValue = selectedTool.getDamageValue();
Pair<Boolean, String> result = owner.withPlayer(apFakePlayer -> apFakePlayer.digBlock(owner.getFacing().getOpposite()));
Pair<Boolean, String> result = owner.withPlayer(apFakePlayer -> apFakePlayer.digBlock(yaw, pitch));
if (!result.getLeft()) {
return MethodResult.of(null, result.getRight());
}
Expand All @@ -43,12 +51,15 @@ public final MethodResult digBlock() throws LuaException {
}

@LuaFunction(mainThread = true)
public final MethodResult useOnBlock() throws LuaException {
public final MethodResult useOnBlock(@NotNull IArguments arguments) throws LuaException {
Map<?, ?> opts = arguments.count() > 0 ? arguments.getTable(0) : null;
float yaw = opts != null ? (float) TableHelper.optNumberField(opts, "yaw", 0) : 0;
float pitch = opts != null ? (float) TableHelper.optNumberField(opts, "pitch", 0) : 0;
return automataCore.withOperation(USE_ON_BLOCK, context -> {
TurtlePeripheralOwner owner = automataCore.getPeripheralOwner();
ItemStack selectedTool = owner.getToolInMainHand();
int previousDamageValue = selectedTool.getDamageValue();
InteractionResult result = owner.withPlayer(APFakePlayer::useOnBlock);
InteractionResult result = owner.withPlayer(apFakePlayer -> apFakePlayer.useOnBlock(yaw, pitch));
if (automataCore.hasAttribute(AutomataCorePeripheral.ATTR_STORING_TOOL_DURABILITY))
selectedTool.setDamageValue(previousDamageValue);
return MethodResult.of(true, result.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,30 @@ public float getEyeHeight(@NotNull Pose pose) {
return 0;
}

@Deprecated(forRemoval = true)
public Pair<Boolean, String> digBlock(Direction direction) {
return digBlock(direction.toYRot() - this.getYRot(), direction == Direction.DOWN ? 90 : direction == Direction.UP ? -90 : 0);
}

public Pair<Boolean, String> digBlock() {
return digBlock(0, 0);
}

/**
* @param yaw The Y axis rotation relative to turtle's heading
* @param pitch The pitch
*/
public Pair<Boolean, String> digBlock(float yaw, float pitch) {
final float oldRot = this.getYRot();
this.setRot((oldRot + yaw % 360 + 360) % 360, pitch % 360);
try {
return this.digBlockAction();
} finally {
this.setRot(oldRot, 0);
}
}

private Pair<Boolean, String> digBlockAction() {
Level world = getLevel();
HitResult hit = findHit(true, false);
if (hit.getType() == HitResult.Type.MISS)
Expand All @@ -137,6 +160,9 @@ public Pair<Boolean, String> digBlock(Direction direction) {
if (block != digBlock || !pos.equals(digPosition))
setState(block, pos);

Vec3 look = getLookAngle();
Direction direction = Direction.getNearest(look.x, look.y, look.z).getOpposite();

if (!world.isEmptyBlock(pos) && !state.getMaterial().isLiquid()) {
if (block == Blocks.BEDROCK || state.getDestroySpeed(world, pos) <= -1)
return Pair.of(false, "Unbreakable block detected");
Expand All @@ -159,7 +185,7 @@ public Pair<Boolean, String> digBlock(Direction direction) {

if (currentDamage > 9) {
world.playSound(null, pos, state.getSoundType().getHitSound(), SoundSource.NEUTRAL, .25f, 1);
manager.handleBlockBreakAction(pos, ServerboundPlayerActionPacket.Action.STOP_DESTROY_BLOCK, direction.getOpposite(), 320, 1);
manager.handleBlockBreakAction(pos, ServerboundPlayerActionPacket.Action.STOP_DESTROY_BLOCK, direction, 320, 1);
manager.destroyBlock(pos);
world.destroyBlockProgress(getId(), pos, -1);
setState(null, null);
Expand All @@ -177,14 +203,26 @@ public InteractionResult useOnBlock() {
return use(true, false);
}

public InteractionResult useOnBlock(float yaw, float pitch) {
return use(true, false, yaw, pitch);
}

public InteractionResult useOnEntity() {
return use(false, true);
}

public InteractionResult useOnEntity(float yaw, float pitch) {
return use(false, true, yaw, pitch);
}

public InteractionResult useOnFilteredEntity(Predicate<Entity> filter) {
return use(false, true, filter);
}

public InteractionResult useOnFilteredEntity(Predicate<Entity> filter, float yaw, float pitch) {
return use(false, true, filter, yaw, pitch);
}

public InteractionResult useOnSpecificEntity(@NotNull Entity entity, HitResult result) {
InteractionResult simpleInteraction = interactOn(entity, InteractionHand.MAIN_HAND);
if (simpleInteraction == InteractionResult.SUCCESS) return simpleInteraction;
Expand All @@ -198,7 +236,25 @@ public InteractionResult use(boolean skipEntity, boolean skipBlock) {
return use(skipEntity, skipBlock, null);
}

public InteractionResult use(boolean skipEntity, boolean skipBlock, float yaw, float pitch) {
return use(skipEntity, skipBlock, null, yaw, pitch);
}

public InteractionResult use(boolean skipEntity, boolean skipBlock, @Nullable Predicate<Entity> entityFilter) {
return use(skipEntity, skipBlock, entityFilter, 0, 0);
}

public InteractionResult use(boolean skipEntity, boolean skipBlock, @Nullable Predicate<Entity> entityFilter, float yaw, float pitch) {
final float oldRot = this.getYRot();
this.setRot((oldRot + yaw % 360 + 360) % 360, pitch % 360);
try {
return this.useAction(skipEntity, skipBlock, entityFilter);
} finally {
this.setRot(oldRot, 0);
}
}

private InteractionResult useAction(boolean skipEntity, boolean skipBlock, @Nullable Predicate<Entity> entityFilter) {
HitResult hit = findHit(skipEntity, skipBlock, entityFilter);

if (hit instanceof BlockHitResult blockHit) {
Expand Down Expand Up @@ -269,7 +325,7 @@ public HitResult findHit(boolean skipEntity, boolean skipBlock, @Nullable Predic
blockHit = BlockHitResult.miss(traceContext.getTo(), traceDirection, new BlockPos(traceContext.getTo()));
} else {
blockHit = BlockGetter.traverseBlocks(traceContext.getFrom(), traceContext.getTo(), traceContext, (rayTraceContext, blockPos) -> {
if (level.isEmptyBlock(blockPos)) {
if (level.isEmptyBlock(blockPos) || blockPos.equals(blockPosition())) {
return null;
}
return new BlockHitResult(new Vec3(blockPos.getX(), blockPos.getY(), blockPos.getZ()), traceDirection, blockPos, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,9 @@ public static APFakePlayer getPlayer(ITurtleAccess turtle, GameProfile profile)
public static void load(APFakePlayer player, ITurtleAccess turtle) {
Direction direction = turtle.getDirection();
player.setLevel((ServerLevel) turtle.getLevel());
BlockPos position = turtle.getPosition();
// Player position
float pitch = direction == Direction.UP ? -90 : direction == Direction.DOWN ? 90 : 0;
float yaw = direction == Direction.SOUTH ? 0 : direction == Direction.WEST ? 90 : direction == Direction.NORTH ? 180 : -90;
Vec3i sideVec = direction.getNormal();
Direction.Axis a = direction.getAxis();
Direction.AxisDirection ad = direction.getAxisDirection();
double x = a == Direction.Axis.X && ad == Direction.AxisDirection.NEGATIVE ? -.5 : .5 + sideVec.getX() / 1.9D;
double y = 0.5 + sideVec.getY() / 1.9D;
double z = a == Direction.Axis.Z && ad == Direction.AxisDirection.NEGATIVE ? -.5 : .5 + sideVec.getZ() / 1.9D;
player.moveTo(position.getX() + x, position.getY() + y, position.getZ() + z, yaw, pitch);
BlockPos position = turtle.getPosition();
player.moveTo(position.getX() + 0.5, position.getY() + 0.5, position.getZ() + 0.5, direction.toYRot(), 0);
// Player inventory
Inventory playerInventory = player.getInventory();
playerInventory.selected = 0;
Expand Down

0 comments on commit 8116c68

Please sign in to comment.