Skip to content

Commit

Permalink
add player.doActionWithRot instead of writing ton of overload methods
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Apr 26, 2024
1 parent 8116c68 commit 6d7d52a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean, String> result = owner.withPlayer(apFakePlayer -> apFakePlayer.digBlock(yaw, pitch));
Pair<Boolean, String> result = owner.withPlayer(apFakePlayer -> apFakePlayer.doActionWithRot(yaw, pitch, APFakePlayer::digBlock));
if (!result.getLeft()) {
return MethodResult.of(null, result.getRight());
}
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ public AutomataEntityHandPlugin(AutomataCorePeripheral automataCore, Predicate<E
}

@LuaFunction(mainThread = true)
public final MethodResult useOnAnimal() throws LuaException {
public final MethodResult useOnAnimal(@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_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);

Expand All @@ -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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,30 +119,22 @@ 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) {
public T doActionWithRot<T>(float yaw, float pitch, Function<APFakePlayer, T> 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<Boolean, String> digBlockAction() {
@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() {
Level world = getLevel();
HitResult hit = findHit(true, false);
if (hit.getType() == HitResult.Type.MISS)
Expand Down Expand Up @@ -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<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 @@ -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<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

0 comments on commit 6d7d52a

Please sign in to comment.