Skip to content

Commit

Permalink
allow limiting click inputs to only blocks or entities
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Jan 30, 2025
1 parent 22f36bf commit bddaea1
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 54 deletions.
20 changes: 15 additions & 5 deletions src/main/java/com/zenith/command/impl/ClickCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public CommandUsage commandUsage() {
"right hold",
"right hold <mainHand/offHand/alternate>",
"right hold interval <ticks>",
"reach add <float>",
"addedBlockReach <float>",
"addedEntityReach <float>",
"hold forceRotation on/off",
"hold forceRotation <yaw> <pitch>",
"hold sneak on/off",
Expand Down Expand Up @@ -133,14 +134,22 @@ public LiteralArgumentBuilder<CommandContext> register() {
.primaryColor();
return OK;
})))))
.then(literal("reach").then(literal("add").then(argument("reach", floatArg(0, 10)).executes(c -> {
.then(literal("addedBlockReach").then(argument("reach", floatArg(0, 10)).executes(c -> {
float f = getFloat(c, "reach");
CONFIG.client.extra.click.additionalBlockReach = f;
c.getSource().getEmbed()
.title("Additional Reach Set")
.title("Additional Block Reach Set")
.primaryColor();
return OK;
}))))
})))
.then(literal("addedEntityReach").then(argument("reach", floatArg(0, 10)).executes(c -> {
float f = getFloat(c, "reach");
CONFIG.client.extra.click.additionalEntityReach = f;
c.getSource().getEmbed()
.title("Additional Entity Reach Set")
.primaryColor();
return OK;
})))
.then(literal("hold")
.then(literal("forceRotation")
.then(argument("toggle", toggle()).executes(c -> {
Expand Down Expand Up @@ -183,7 +192,8 @@ public void postPopulate(Embed embed) {
.addField("Click Hold Sneak", toggleStr(CONFIG.client.extra.click.holdSneak), false)
.addField("Right Click Hold Mode", rightClickHoldModeToString(CONFIG.client.extra.click.holdRightClickMode), false)
.addField("Right Click Hold Interval", CONFIG.client.extra.click.holdRightClickInterval + " ticks", false)
.addField("Additional Reach", CONFIG.client.extra.click.additionalBlockReach, false)
.addField("Added Block Reach", CONFIG.client.extra.click.additionalBlockReach, false)
.addField("Added Entity Reach", CONFIG.client.extra.click.additionalEntityReach, false)
.primaryColor();
}

Expand Down
40 changes: 21 additions & 19 deletions src/main/java/com/zenith/command/impl/RaycastCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,26 @@ public CommandUsage commandUsage() {
@Override
public LiteralArgumentBuilder<CommandContext> register() {
return command("raycast").executes(c -> {
var result = RaycastHelper.playerBlockOrEntityRaycast(4.5);
var embed = c.getSource().getEmbed();
embed.title("Raycast Result")
.addField("Hit", result.hit(), false)
.primaryColor();
if (result.isBlock()) {
embed.addField("Block", result.block().block().toString(), false)
.addField("Pos", result.block().x() + ", " + result.block().y() + ", " + result.block().z(), false)
.addField("Direction", result.block().direction().name(), false);
if (result.hit()) {
embed.addField("State", World.getBlockState(result.block().x(), result.block().y(), result.block().z()).toString(), false);
}
} else if (result.isEntity()) {
var type = result.entity().entityType();
embed.addField("Entity", type != null ? type : "N/A", false);
}
})
var sim = MODULE.get(PlayerSimulation.class);
var result = RaycastHelper.playerBlockOrEntityRaycast(sim.getBlockReachDistance(), sim.getEntityInteractDistance());
var embed = c.getSource().getEmbed();
embed.title("Raycast Result")
.addField("Hit", result.hit(), false)
.primaryColor();
if (result.isBlock()) {
embed.addField("Block", result.block().block().toString(), false)
.addField("Pos", result.block().x() + ", " + result.block().y() + ", " + result.block().z(), false)
.addField("Direction", result.block().direction().name(), false);
if (result.hit()) {
embed.addField("State", World.getBlockState(result.block().x(), result.block().y(), result.block().z()).toString(), false);
}
} else if (result.isEntity()) {
var type = result.entity().entityType();
embed.addField("Entity", type != null ? type : "N/A", false);
}})
.then(literal("e").executes(c -> {
var result = RaycastHelper.playerEntityRaycast(4.5);
var sim = MODULE.get(PlayerSimulation.class);
var result = RaycastHelper.playerEntityRaycast(sim.getEntityInteractDistance());
c.getSource().getEmbed()
.title("Raycast Result")
.addField("Hit", result.hit(), false)
Expand All @@ -51,7 +52,8 @@ public LiteralArgumentBuilder<CommandContext> register() {
.primaryColor();
}))
.then(literal("b").executes(c -> {
var result = RaycastHelper.playerBlockRaycast(MODULE.get(PlayerSimulation.class).getBlockReachDistance(), false);
var sim = MODULE.get(PlayerSimulation.class);
var result = RaycastHelper.playerBlockRaycast(sim.getBlockReachDistance(), false);
c.getSource().getEmbed()
.title("Raycast Result")
.addField("Hit", result.hit(), false)
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/com/zenith/feature/world/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.Hand;

@Data
@AllArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -18,14 +19,21 @@ public class Input {
public boolean sprinting;
public boolean leftClick;
public boolean rightClick;
public boolean clickMainHand = true;
public ClickOptions clickOptions = ClickOptions.DEFAULT;

public record ClickOptions(Hand hand, ClickTarget target) {
public enum ClickTarget {
BLOCK_OR_ENTITY, BLOCK, ENTITY;
}
public static final ClickOptions DEFAULT = new ClickOptions(Hand.MAIN_HAND, ClickTarget.BLOCK_OR_ENTITY);
}

public static Builder builder() {
return new Builder();
}

public Input(Input in) {
this(in.pressingForward, in.pressingBack, in.pressingLeft, in.pressingRight, in.jumping, in.sneaking, in.sprinting, in.leftClick, in.rightClick, in.clickMainHand);
this(in.pressingForward, in.pressingBack, in.pressingLeft, in.pressingRight, in.jumping, in.sneaking, in.sprinting, in.leftClick, in.rightClick, in.clickOptions);
}

public void apply(Input in) {
Expand All @@ -45,7 +53,7 @@ public void apply(Input in) {
}
this.leftClick = in.leftClick;
this.rightClick = in.rightClick;
this.clickMainHand = in.clickMainHand;
this.clickOptions = in.clickOptions;
}

public void reset() {
Expand All @@ -58,7 +66,7 @@ public void reset() {
sprinting = false;
leftClick = false;
rightClick = false;
clickMainHand = true;
clickOptions = ClickOptions.DEFAULT;
}

public String log() {
Expand Down Expand Up @@ -104,7 +112,7 @@ public static final class Builder {
private boolean sprinting;
private boolean leftClick;
private boolean rightClick;
private boolean clickMainHand = true;
private ClickOptions clickOptions = ClickOptions.DEFAULT;

private Builder() {}

Expand Down Expand Up @@ -153,8 +161,8 @@ public Builder rightClick(boolean rightClick) {
return this;
}

public Builder clickMainHand(boolean clickMainHand) {
this.clickMainHand = clickMainHand;
public Builder clickOptions(ClickOptions clickOptions) {
this.clickOptions = clickOptions;
return this;
}

Expand All @@ -169,7 +177,7 @@ public Input build() {
input.setSprinting(sprinting);
input.setLeftClick(leftClick);
input.setRightClick(rightClick);
input.setClickMainHand(clickMainHand);
input.setClickOptions(clickOptions);
return input;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ public record BlockOrEntityRaycastResult(boolean hit, @Nullable BlockRaycastResu
public static BlockOrEntityRaycastResult miss() {
return new BlockOrEntityRaycastResult(false, null, null);
}
public static BlockOrEntityRaycastResult wrap(BlockRaycastResult blockRaycastResult) {
return new BlockOrEntityRaycastResult(blockRaycastResult.hit(), blockRaycastResult, null);
}
public static BlockOrEntityRaycastResult wrap(EntityRaycastResult entityRaycastResult) {
return new BlockOrEntityRaycastResult(entityRaycastResult.hit(), null, entityRaycastResult);
}

public boolean isEntity() {
return entity() != null;
Expand Down
29 changes: 17 additions & 12 deletions src/main/java/com/zenith/feature/world/raycast/RaycastHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,31 +180,36 @@ private static BlockRaycastResult checkBlockRaycast(
return result;
}

public static BlockOrEntityRaycastResult playerBlockOrEntityRaycast(double maxDistance) {
return blockOrEntityRaycastFromPos(CACHE.getPlayerCache().getX(), CACHE.getPlayerCache().getEyeY(), CACHE.getPlayerCache().getZ(), CACHE.getPlayerCache().getYaw(), CACHE.getPlayerCache().getPitch(), maxDistance);
public static BlockOrEntityRaycastResult playerBlockOrEntityRaycast(double blockReachDistance, double entityReachDistance) {
return blockOrEntityRaycastFromPos(
CACHE.getPlayerCache().getX(), CACHE.getPlayerCache().getEyeY(), CACHE.getPlayerCache().getZ(),
CACHE.getPlayerCache().getYaw(), CACHE.getPlayerCache().getPitch(),
blockReachDistance, entityReachDistance
);
}

public static BlockOrEntityRaycastResult blockOrEntityRaycastFromPos(final double x, final double y, final double z, final float yaw, final float pitch, final double maxDistance) {
final Vector3d rayEndPos = MathHelper.calculateRayEndPos(x, y, z, yaw, pitch, maxDistance);
return blockOrEntityRaycast(x, y, z, rayEndPos.getX(), rayEndPos.getY(), rayEndPos.getZ());
public static BlockOrEntityRaycastResult blockOrEntityRaycastFromPos(final double x, final double y, final double z, final float yaw, final float pitch, final double blockReachDistance, final double entityReachDistance) {
final Vector3d blockRayEndPos = MathHelper.calculateRayEndPos(x, y, z, yaw, pitch, blockReachDistance);
final Vector3d entityRayEndPos = MathHelper.calculateRayEndPos(x, y, z, yaw, pitch, entityReachDistance);
return blockOrEntityRaycast(x, y, z, blockRayEndPos, entityRayEndPos);
}

private static BlockOrEntityRaycastResult blockOrEntityRaycast(final double x, final double y, final double z, final double x2, final double y2, final double z2) {
final BlockRaycastResult blockRaycastResult = blockRaycast(x, y, z, x2, y2, z2, false);
final EntityRaycastResult entityRaycastResult = entityRaycast(x, y, z, x2, y2, z2);
private static BlockOrEntityRaycastResult blockOrEntityRaycast(final double x, final double y, final double z, Vector3d blockRayEndPos, Vector3d entityRayEndPos) {
final BlockRaycastResult blockRaycastResult = blockRaycast(x, y, z, blockRayEndPos.getX(), blockRayEndPos.getY(), blockRayEndPos.getZ(), false);
final EntityRaycastResult entityRaycastResult = entityRaycast(x, y, z, entityRayEndPos.getX(), entityRayEndPos.getY(), entityRayEndPos.getZ());
// if both hit, return the one that is closer to the start point
if (blockRaycastResult.hit() && entityRaycastResult.hit()) {
final double blockDist = MathHelper.distanceSq3d(x, y, z, blockRaycastResult.intersection().x(), blockRaycastResult.intersection().y(), blockRaycastResult.intersection().z());
final double entityDist = MathHelper.distanceSq3d(x, y, z, entityRaycastResult.intersection().x(), entityRaycastResult.intersection().y(), entityRaycastResult.intersection().z());
if (blockDist < entityDist) {
return new BlockOrEntityRaycastResult(true, blockRaycastResult, null);
return BlockOrEntityRaycastResult.wrap(blockRaycastResult);
} else {
return new BlockOrEntityRaycastResult(true, null, entityRaycastResult);
return BlockOrEntityRaycastResult.wrap(entityRaycastResult);
}
} else if (blockRaycastResult.hit()) {
return new BlockOrEntityRaycastResult(true, blockRaycastResult, null);
return BlockOrEntityRaycastResult.wrap(blockRaycastResult);
} else if (entityRaycastResult.hit()) {
return new BlockOrEntityRaycastResult(true, null, entityRaycastResult);
return BlockOrEntityRaycastResult.wrap(entityRaycastResult);
}
return BlockOrEntityRaycastResult.miss();
}
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/com/zenith/module/impl/Click.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.zenith.event.module.ClientBotTick;
import com.zenith.feature.world.Input;
import com.zenith.feature.world.Input.ClickOptions.ClickTarget;
import com.zenith.feature.world.InputRequest;
import com.zenith.module.Module;
import com.zenith.util.Timer;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.Hand;

import static com.github.rfresh2.EventConsumer.of;
import static com.zenith.Shared.*;
Expand All @@ -14,7 +16,7 @@ public class Click extends Module {
public static final int MOVEMENT_PRIORITY = 501;

private final Timer holdRightClickTimer = Timer.createTickTimer();
private boolean holdRightClickLastHand = false; // true if main hand, false if off hand
private Hand holdRightClickLastHand = Hand.MAIN_HAND;

@Override
public void subscribeEvents() {
Expand Down Expand Up @@ -43,13 +45,13 @@ private void onClientBotTick(ClientBotTick event) {
if (holdRightClickTimer.tick(CONFIG.client.extra.click.holdRightClickInterval)) {
var req = InputRequest.builder().priority(MOVEMENT_PRIORITY);
var in = Input.builder().rightClick(true);
boolean mainhand = switch (CONFIG.client.extra.click.holdRightClickMode) {
case MAIN_HAND -> true;
case OFF_HAND -> false;
case ALTERNATE_HANDS -> !holdRightClickLastHand;
Hand hand = switch (CONFIG.client.extra.click.holdRightClickMode) {
case MAIN_HAND -> Hand.MAIN_HAND;
case OFF_HAND -> Hand.OFF_HAND;
case ALTERNATE_HANDS -> holdRightClickLastHand == Hand.MAIN_HAND ? Hand.OFF_HAND : Hand.MAIN_HAND;
};
holdRightClickLastHand = mainhand;
in.clickMainHand(mainhand);
holdRightClickLastHand = hand;
in.clickOptions(new Input.ClickOptions(hand, ClickTarget.BLOCK_OR_ENTITY));
if (CONFIG.client.extra.click.hasRotation) {
req.yaw(CONFIG.client.extra.click.rotationYaw)
.pitch(CONFIG.client.extra.click.rotationPitch);
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/com/zenith/module/impl/PlayerSimulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.zenith.feature.world.InputRequest;
import com.zenith.feature.world.PlayerInteractionManager;
import com.zenith.feature.world.World;
import com.zenith.feature.world.raycast.BlockOrEntityRaycastResult;
import com.zenith.feature.world.raycast.RaycastHelper;
import com.zenith.mc.block.*;
import com.zenith.mc.dimension.DimensionRegistry;
Expand Down Expand Up @@ -143,7 +144,11 @@ public synchronized void doMovement(final InputRequest request) {
private void interactionTick() {
try {
if (movementInput.isLeftClick()) {
var raycast = RaycastHelper.playerBlockOrEntityRaycast(getBlockReachDistance());
BlockOrEntityRaycastResult raycast = switch (movementInput.clickOptions.target()) {
case BLOCK_OR_ENTITY -> RaycastHelper.playerBlockOrEntityRaycast(getBlockReachDistance(), getEntityInteractDistance());
case BLOCK -> BlockOrEntityRaycastResult.wrap(RaycastHelper.playerBlockRaycast(getBlockReachDistance(), false));
case ENTITY -> BlockOrEntityRaycastResult.wrap(RaycastHelper.playerEntityRaycast(getEntityInteractDistance()));
};
if (raycast.hit() && raycast.isBlock()) {
if (!wasLeftClicking && !interactions.isDestroying()) {
debug("Starting destroy block at: [{}, {}, {}]", raycast.block().x(), raycast.block().y(), raycast.block().z());
Expand Down Expand Up @@ -176,8 +181,12 @@ private void interactionTick() {
}
}
} else if (movementInput.isRightClick()) {
var raycast = RaycastHelper.playerBlockOrEntityRaycast(getBlockReachDistance());
Hand hand = movementInput.clickMainHand ? Hand.MAIN_HAND : Hand.OFF_HAND;
BlockOrEntityRaycastResult raycast = switch (movementInput.clickOptions.target()) {
case BLOCK_OR_ENTITY -> RaycastHelper.playerBlockOrEntityRaycast(getBlockReachDistance(), getEntityInteractDistance());
case BLOCK -> BlockOrEntityRaycastResult.wrap(RaycastHelper.playerBlockRaycast(getBlockReachDistance(), false));
case ENTITY -> BlockOrEntityRaycastResult.wrap(RaycastHelper.playerEntityRaycast(getEntityInteractDistance()));
};
Hand hand = movementInput.clickOptions.hand();
if (raycast.hit() && raycast.isBlock()) {
debug("Right click {} block at: [{}, {}, {}]", hand, raycast.block().x(), raycast.block().y(), raycast.block().z());
interactions.useItemOn(hand, raycast.block());
Expand Down Expand Up @@ -1104,4 +1113,8 @@ public double getEyeY() {
public double getBlockReachDistance() {
return getAttributeValue(AttributeType.Builtin.PLAYER_BLOCK_INTERACTION_RANGE, 4.5f) + CONFIG.client.extra.click.additionalBlockReach;
}

public double getEntityInteractDistance() {
return getAttributeValue(AttributeType.Builtin.PLAYER_ENTITY_INTERACTION_RANGE, 3.0f) + CONFIG.client.extra.click.additionalEntityReach;
}
}
1 change: 1 addition & 0 deletions src/main/java/com/zenith/util/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public static class Click {
public HoldRightClickMode holdRightClickMode = HoldRightClickMode.MAIN_HAND;
public int holdRightClickInterval = 5;
public float additionalBlockReach = 0;
public float additionalEntityReach = 0;
public enum HoldRightClickMode {
MAIN_HAND,
OFF_HAND,
Expand Down

0 comments on commit bddaea1

Please sign in to comment.