9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-19 14:59:32 +00:00

feat: mount action(#619) (#649)

* feat: mount action(#619)

* fix: fix comment

* feat: optimize performance

* fix: update manhattanDistance method to use precise coordinates

* refactor: remove canAddPassengerPublic

* refactor: remove single method

* fix: rebuild patches

* fix: rebuild patches

* fix: rebuild patches

* fix: rebuild patches

* fix: remove ???
This commit is contained in:
MC_XiaoHei
2025-08-04 21:12:15 +08:00
committed by GitHub
parent ea91106ae5
commit 230e0987a9
4 changed files with 149 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
package org.leavesmc.leaves.entity.bot.action;
import org.bukkit.Bukkit;
/**
* Represents an action for a bot to mount to nearby vehicles.
*/
public interface MountAction extends BotAction<MountAction> {
static MountAction create() {
return Bukkit.getBotManager().newAction(MountAction.class);
}
}

View File

@@ -35,6 +35,7 @@ public class Actions {
register(new ServerSwimAction(), SwimAction.class);
register(new ServerRotationAction(), RotationAction.class);
register(new ServerMoveAction(), MoveAction.class);
register(new ServerMountAction(), MountAction.class);
}
public static boolean register(@NotNull ServerBotAction<?> action, Class<? extends BotAction<?>> type) {

View File

@@ -0,0 +1,54 @@
package org.leavesmc.leaves.bot.agent.actions;
import net.minecraft.world.entity.Entity;
import org.bukkit.Location;
import org.bukkit.craftbukkit.entity.CraftVehicle;
import org.bukkit.entity.Vehicle;
import org.jetbrains.annotations.NotNull;
import org.leavesmc.leaves.bot.ServerBot;
import org.leavesmc.leaves.command.CommandArgument;
import org.leavesmc.leaves.entity.bot.actions.CraftMountAction;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
public class ServerMountAction extends ServerBotAction<ServerMountAction> {
public ServerMountAction() {
super("mount", CommandArgument.EMPTY, ServerMountAction::new);
}
@Override
public boolean doTick(@NotNull ServerBot bot) {
Location center = bot.getBukkitEntity().getLocation();
Collection<Vehicle> nearbyVehicles = center.getNearbyEntitiesByType(
Vehicle.class,
3,
vehicle -> manhattanDistance(bot, ((CraftVehicle) vehicle).getHandle()) <= 2
);
List<Vehicle> vehicles = nearbyVehicles.stream().sorted(Comparator.comparingDouble(
(vehicle) -> center.distanceSquared(vehicle.getLocation())
)).toList();
for (Vehicle vehicle : vehicles) {
if (bot.startRiding(((CraftVehicle) vehicle).getHandle(), false)) {
return true;
}
}
return false;
}
@Override
public Object asCraft() {
return new CraftMountAction(this);
}
private double manhattanDistance(@NotNull Entity entity1, @NotNull Entity entity2) {
return Math.abs(entity1.getX() - entity2.getX()) +
Math.abs(entity1.getY() - entity2.getY()) +
Math.abs(entity1.getZ() - entity2.getZ());
}
}

View File

@@ -0,0 +1,82 @@
package org.leavesmc.leaves.entity.bot.actions;
import org.jetbrains.annotations.NotNull;
import org.leavesmc.leaves.bot.ServerBot;
import org.leavesmc.leaves.bot.agent.actions.*;
import org.leavesmc.leaves.entity.bot.action.*;
import java.util.UUID;
import java.util.function.Consumer;
public class CraftMountAction extends CraftBotAction implements MountAction {
private final ServerMountAction serverAction;
private Consumer<MountAction> onFail = null;
private Consumer<MountAction> onSuccess = null;
private Consumer<MountAction> onStop = null;
public CraftMountAction(ServerMountAction serverAction) {
this.serverAction = serverAction;
}
public boolean doTick(@NotNull ServerBot bot) {
return serverAction.doTick(bot);
}
@Override
public ServerBotAction<?> getHandle() {
return serverAction;
}
@Override
public String getName() {
return serverAction.getName();
}
@Override
public UUID getUUID() {
return serverAction.getUUID();
}
@Override
public void setCancelled(boolean cancel) {
serverAction.setCancelled(cancel);
}
@Override
public boolean isCancelled() {
return serverAction.isCancelled();
}
@Override
public void setOnFail(Consumer<MountAction> onFail) {
this.onFail = onFail;
serverAction.setOnFail(it -> onFail.accept(new CraftMountAction(it)));
}
@Override
public Consumer<MountAction> getOnFail() {
return onFail;
}
@Override
public void setOnSuccess(Consumer<MountAction> onSuccess) {
this.onSuccess = onSuccess;
serverAction.setOnSuccess(it -> onSuccess.accept(new CraftMountAction(it)));
}
@Override
public Consumer<MountAction> getOnSuccess() {
return onSuccess;
}
@Override
public void setOnStop(Consumer<MountAction> onStop) {
this.onStop = onStop;
serverAction.setOnStop(it -> onStop.accept(new CraftMountAction(it)));
}
@Override
public Consumer<MountAction> getOnStop() {
return onStop;
}
}