9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2026-01-04 15:41:31 +00:00

feat: fakeplayer move action(#451)

This commit is contained in:
MC_XiaoHei
2025-07-06 17:41:10 +08:00
parent 88c1f86cf1
commit f814168906
5 changed files with 80 additions and 0 deletions

View File

@@ -16,6 +16,7 @@ public enum BotActionType {
ROTATION("rotation"),
SNEAK("sneak"),
SWIM("swim"),
MOVE("move"),
USE("use"),
USE_ON("use_on"),
USE_TO("use_to"),

View File

@@ -0,0 +1,14 @@
package org.leavesmc.leaves.entity.botaction;
public enum MoveDirection {
FORWARD("forward"),
BACKWARD("backward"),
LEFT("left"),
RIGHT("right");
public final String name;
MoveDirection(String name) {
this.name = name;
}
}

View File

@@ -9,6 +9,7 @@ import org.leavesmc.leaves.bot.agent.actions.DropAction;
import org.leavesmc.leaves.bot.agent.actions.FishAction;
import org.leavesmc.leaves.bot.agent.actions.JumpAction;
import org.leavesmc.leaves.bot.agent.actions.LookAction;
import org.leavesmc.leaves.bot.agent.actions.MoveAction;
import org.leavesmc.leaves.bot.agent.actions.RotateAction;
import org.leavesmc.leaves.bot.agent.actions.RotationAction;
import org.leavesmc.leaves.bot.agent.actions.ShootAction;
@@ -52,6 +53,7 @@ public class Actions {
register(new SwimAction());
register(new RotationAction());
register(new ShootAction());
register(new MoveAction());
}
public static boolean register(@NotNull AbstractBotAction<?> action) {

View File

@@ -0,0 +1,61 @@
package org.leavesmc.leaves.bot.agent.actions;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.MoverType;
import net.minecraft.world.phys.Vec3;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.leavesmc.leaves.bot.ServerBot;
import org.leavesmc.leaves.bot.agent.AbstractBotAction;
import org.leavesmc.leaves.command.CommandArgument;
import org.leavesmc.leaves.command.CommandArgumentResult;
import org.leavesmc.leaves.command.CommandArgumentType;
import org.leavesmc.leaves.entity.botaction.MoveDirection;
import org.leavesmc.leaves.event.bot.BotActionStopEvent;
import java.util.Arrays;
import java.util.List;
public class MoveAction extends AbstractBotAction<MoveAction> {
private static final Pair<List<String>, String> suggestions = Pair.of(
Arrays.stream(MoveDirection.values()).map((it) -> it.name).toList(),
"<Direction>"
);
private MoveDirection direction;
public MoveAction() {
super("move", CommandArgument.of(CommandArgumentType.ofEnum(MoveDirection.class)), MoveAction::new);
this.setSuggestion(0, (sender, arg) -> suggestions);
}
@Override
public void loadCommand(@Nullable ServerPlayer player, @NotNull CommandArgumentResult result) {
direction = result.read(MoveDirection.class);
if (direction == null) {
throw new IllegalArgumentException("Invalid direction");
}
this.setInitialTickDelay(0).setInitialTickInterval(1).setInitialNumber(-1);
}
@Override
public void stop(@NotNull ServerBot bot, BotActionStopEvent.Reason reason) {
switch (direction) {
case FORWARD, BACKWARD -> bot.zza = 0.0f;
case LEFT, RIGHT -> bot.xxa = 0.0f;
}
}
@Override
public boolean doTick(@NotNull ServerBot bot) {
boolean isSneaking = bot.isShiftKeyDown();
float velocity = isSneaking ? 0.3f : 1.0f;
switch (direction) {
case FORWARD -> bot.zza = velocity;
case BACKWARD -> bot.zza = -velocity;
case LEFT -> bot.xxa = velocity;
case RIGHT -> bot.xxa = -velocity;
}
return true;
}
}

View File

@@ -105,6 +105,7 @@ public class BotActionCommand implements LeavesSubcommand {
event.callEvent();
if (!event.isCancelled()) {
forRemoval.add(action);
action.stop(bot, BotActionStopEvent.Reason.COMMAND);
}
}
bot.getBotActions().removeAll(forRemoval);
@@ -124,6 +125,7 @@ public class BotActionCommand implements LeavesSubcommand {
);
event.callEvent();
if (!event.isCancelled()) {
action.stop(bot, BotActionStopEvent.Reason.COMMAND);
bot.getBotActions().remove(i);
sender.sendMessage(bot.getScoreboardName() + "'s " + action.getName() + " stopped.");