diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataBlockHandPlugin.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataBlockHandPlugin.java index d3c3fb4c0..8ca3abe5c 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataBlockHandPlugin.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataBlockHandPlugin.java @@ -40,7 +40,7 @@ public final MethodResult digBlock(@NotNull IArguments arguments) throws LuaExce TurtlePeripheralOwner owner = automataCore.getPeripheralOwner(); ItemStack selectedTool = owner.getToolInMainHand(); int previousDamageValue = selectedTool.getDamageValue(); - Pair result = owner.withPlayer(apFakePlayer -> apFakePlayer.digBlock(yaw, pitch)); + Pair result = owner.withPlayer(apFakePlayer -> apFakePlayer.doActionWithRot(yaw, pitch, APFakePlayer::digBlock)); if (!result.getLeft()) { return MethodResult.of(null, result.getRight()); } @@ -59,7 +59,7 @@ public final MethodResult useOnBlock(@NotNull IArguments arguments) throws LuaEx TurtlePeripheralOwner owner = automataCore.getPeripheralOwner(); ItemStack selectedTool = owner.getToolInMainHand(); int previousDamageValue = selectedTool.getDamageValue(); - InteractionResult result = owner.withPlayer(apFakePlayer -> apFakePlayer.useOnBlock(yaw, pitch)); + InteractionResult result = owner.withPlayer(apFakePlayer -> apFakePlayer.doActionWithRot(yaw, pitch, APFakePlayer::useOnBlock)); if (automataCore.hasAttribute(AutomataCorePeripheral.ATTR_STORING_TOOL_DURABILITY)) selectedTool.setDamageValue(previousDamageValue); return MethodResult.of(true, result.toString()); diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataEntityHandPlugin.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataEntityHandPlugin.java index 1bc139df1..c0647b008 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataEntityHandPlugin.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataEntityHandPlugin.java @@ -39,12 +39,15 @@ public AutomataEntityHandPlugin(AutomataCorePeripheral automataCore, Predicate 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_ANIMAL, context -> { TurtlePeripheralOwner owner = automataCore.getPeripheralOwner(); ItemStack selectedTool = owner.getToolInMainHand(); int previousDamageValue = selectedTool.getDamageValue(); - InteractionResult result = owner.withPlayer(player -> player.useOnFilteredEntity(suitableEntity)); + InteractionResult result = owner.withPlayer(player -> player.doActionWithRot(yaw, pitch, player -> player.useOnFilteredEntity(suitableEntity))); if (automataCore.hasAttribute(AutomataCorePeripheral.ATTR_STORING_TOOL_DURABILITY)) selectedTool.setDamageValue(previousDamageValue); @@ -53,10 +56,14 @@ public final MethodResult useOnAnimal() throws LuaException { } @LuaFunction(mainThread = true) - public final MethodResult inspectAnimal() { + public final MethodResult inspectAnimal(@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; + automataCore.addRotationCycle(); TurtlePeripheralOwner owner = automataCore.getPeripheralOwner(); - HitResult entityHit = owner.withPlayer(player -> player.findHit(false, true, suitableEntity)); + HitResult entityHit = owner.withPlayer(player -> player.doActionWithRot(yaw, pitch, player -> player.findHit(false, true, suitableEntity))); if (entityHit.getType() == HitResult.Type.MISS) return MethodResult.of(null, "Nothing found"); diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataEntityTransferPlugin.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataEntityTransferPlugin.java index b99d53de3..0a955c868 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataEntityTransferPlugin.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataEntityTransferPlugin.java @@ -69,8 +69,12 @@ protected Entity extractEntity() { @LuaFunction(mainThread = true) - public final MethodResult captureAnimal() throws LuaException { - HitResult entityHit = automataCore.getPeripheralOwner().withPlayer(player -> player.findHit(false, true, suitableEntity)); + public final MethodResult captureAnimal(@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; + + HitResult entityHit = automataCore.getPeripheralOwner().withPlayer(player -> player.doActionWithRot(yaw, pitch, player -> player.findHit(false, true, suitableEntity))); if (entityHit.getType() == HitResult.Type.MISS) return MethodResult.of(null, "Nothing found"); return automataCore.withOperation(CAPTURE_ANIMAL, context -> { diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataLookPlugin.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataLookPlugin.java index c79019c53..329108bf6 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataLookPlugin.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataLookPlugin.java @@ -22,10 +22,14 @@ public AutomataLookPlugin(AutomataCorePeripheral automataCore) { } @LuaFunction(mainThread = true) - public final MethodResult lookAtBlock() { + public final MethodResult lookAtBlock(@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; + automataCore.addRotationCycle(); TurtlePeripheralOwner owner = automataCore.getPeripheralOwner(); - HitResult result = owner.withPlayer(apFakePlayer -> apFakePlayer.findHit(true, false)); + HitResult result = owner.withPlayer(apFakePlayer -> apFakePlayer.doActionWithRot(yaw, pitch, apFakePlayer -> apFakePlayer.findHit(true, false))); if (result.getType() == HitResult.Type.MISS) return MethodResult.of(null, "No block find"); @@ -40,9 +44,13 @@ public final MethodResult lookAtBlock() { } @LuaFunction(mainThread = true) - public final MethodResult lookAtEntity() { + public final MethodResult lookAtEntity(@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; + automataCore.addRotationCycle(); - HitResult result = automataCore.getPeripheralOwner().withPlayer(apFakePlayer -> apFakePlayer.findHit(false, true)); + HitResult result = automataCore.getPeripheralOwner().withPlayer(apFakePlayer -> apFakePlayer.doActionWithRot(yaw, pitch, apFakePlayer -> apFakePlayer.findHit(false, true))); if (result.getType() == HitResult.Type.MISS) return MethodResult.of(null, "No entity find"); diff --git a/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java b/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java index a0176a166..d6af4968f 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java +++ b/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java @@ -119,30 +119,22 @@ public float getEyeHeight(@NotNull Pose pose) { return 0; } - @Deprecated(forRemoval = true) - public Pair digBlock(Direction direction) { - return digBlock(direction.toYRot() - this.getYRot(), direction == Direction.DOWN ? 90 : direction == Direction.UP ? -90 : 0); - } - - public Pair digBlock() { - return digBlock(0, 0); - } - - /** - * @param yaw The Y axis rotation relative to turtle's heading - * @param pitch The pitch - */ - public Pair digBlock(float yaw, float pitch) { + public T doActionWithRot(float yaw, float pitch, Function action) { final float oldRot = this.getYRot(); - this.setRot((oldRot + yaw % 360 + 360) % 360, pitch % 360); + this.setRot(oldRot + yaw, pitch); try { - return this.digBlockAction(); + return action.apply(this); } finally { this.setRot(oldRot, 0); } } - private Pair digBlockAction() { + @Deprecated(forRemoval = true) + public Pair digBlock(Direction direction) { + return digBlock(direction.toYRot() - this.getYRot(), direction == Direction.DOWN ? 90 : direction == Direction.UP ? -90 : 0); + } + + public Pair digBlock() { Level world = getLevel(); HitResult hit = findHit(true, false); if (hit.getType() == HitResult.Type.MISS) @@ -203,26 +195,14 @@ 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 filter) { return use(false, true, filter); } - public InteractionResult useOnFilteredEntity(Predicate 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; @@ -236,25 +216,7 @@ 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 entityFilter) { - return use(skipEntity, skipBlock, entityFilter, 0, 0); - } - - public InteractionResult use(boolean skipEntity, boolean skipBlock, @Nullable Predicate 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 entityFilter) { HitResult hit = findHit(skipEntity, skipBlock, entityFilter); if (hit instanceof BlockHitResult blockHit) {