From fe48dce0b395f9a3a96ddf7cc027b463916e031d Mon Sep 17 00:00:00 2001 From: MC_XiaoHei Date: Tue, 8 Jul 2025 20:09:09 +0800 Subject: [PATCH] fix: fix custom bot action api, refactor --- .../org/leavesmc/leaves/entity/bot/Bot.java | 6 +-- .../leaves/entity/bot/BotManager.java | 2 - .../org/leavesmc/leaves/entity/CraftBot.java | 38 +++++++++++++++++-- .../leaves/entity/CraftBotManager.java | 13 +++++-- 4 files changed, 46 insertions(+), 13 deletions(-) diff --git a/leaves-api/src/main/java/org/leavesmc/leaves/entity/bot/Bot.java b/leaves-api/src/main/java/org/leavesmc/leaves/entity/bot/Bot.java index 479ea83e..df7dbdd0 100644 --- a/leaves-api/src/main/java/org/leavesmc/leaves/entity/bot/Bot.java +++ b/leaves-api/src/main/java/org/leavesmc/leaves/entity/bot/Bot.java @@ -38,8 +38,7 @@ public interface Bot extends Player { * * @param action bot action */ - @org.jetbrains.annotations.ApiStatus.Experimental - void addAction(@NotNull BotAction action); + > void addAction(@NotNull T action); /** * Get the copy action in giving index @@ -47,8 +46,7 @@ public interface Bot extends Player { * @param index index of actions * @return Action of that index */ - @org.jetbrains.annotations.ApiStatus.Experimental - BotAction getAction(int index); + BotAction getAction(int index); /** * Get action size diff --git a/leaves-api/src/main/java/org/leavesmc/leaves/entity/bot/BotManager.java b/leaves-api/src/main/java/org/leavesmc/leaves/entity/bot/BotManager.java index d70ddccc..428fed95 100644 --- a/leaves-api/src/main/java/org/leavesmc/leaves/entity/bot/BotManager.java +++ b/leaves-api/src/main/java/org/leavesmc/leaves/entity/bot/BotManager.java @@ -44,7 +44,6 @@ public interface BotManager { * @param action action executor * @return true if success, or false */ - @org.jetbrains.annotations.ApiStatus.Experimental boolean registerCustomBotAction(String name, CustomBotAction action); /** @@ -53,7 +52,6 @@ public interface BotManager { * @param name action name * @return true if success, or false */ - @org.jetbrains.annotations.ApiStatus.Experimental boolean unregisterCustomBotAction(String name); /** diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/entity/CraftBot.java b/leaves-server/src/main/java/org/leavesmc/leaves/entity/CraftBot.java index 00c7de05..cc9e5364 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/entity/CraftBot.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/entity/CraftBot.java @@ -9,13 +9,21 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.leavesmc.leaves.bot.BotList; import org.leavesmc.leaves.bot.ServerBot; +import org.leavesmc.leaves.bot.agent.Actions; import org.leavesmc.leaves.bot.agent.actions.CraftBotAction; +import org.leavesmc.leaves.bot.agent.actions.CraftCustomBotAction; +import org.leavesmc.leaves.bot.agent.actions.CraftCustomStateBotAction; +import org.leavesmc.leaves.bot.agent.actions.CraftCustomTimerBotAction; import org.leavesmc.leaves.entity.bot.Bot; import org.leavesmc.leaves.entity.bot.action.BotAction; +import org.leavesmc.leaves.entity.bot.action.CustomBotAction; +import org.leavesmc.leaves.entity.bot.action.CustomStateBotAction; +import org.leavesmc.leaves.entity.bot.action.CustomTimerBotAction; import org.leavesmc.leaves.event.bot.BotActionStopEvent; import org.leavesmc.leaves.event.bot.BotRemoveEvent; import java.util.UUID; +import java.util.function.Supplier; public class CraftBot extends CraftPlayer implements Bot { @@ -38,13 +46,35 @@ public class CraftBot extends CraftPlayer implements Bot { return this.getHandle().createPlayer; } + @SuppressWarnings("unchecked") @Override - public void addAction(@NotNull BotAction action) { - this.getHandle().addBotAction((CraftBotAction) action, null); + public > void addAction(@NotNull T action) { + CraftBotAction result; + Supplier getRegAction = () -> (T) Actions.getForClass(action.getClass()); + switch (action) { + case CustomBotAction act -> { + T regAction = getRegAction.get(); + if (regAction == null) throw new IllegalStateException("Action " + action.getClass().getName() + " is not registered!"); + result = new CraftCustomBotAction(regAction.getName(), act); + } + case CustomTimerBotAction act -> { + T regAction = getRegAction.get(); + if (regAction == null) throw new IllegalStateException("Action " + action.getClass().getName() + " is not registered!"); + result = new CraftCustomTimerBotAction(regAction.getName(), act); + } + case CustomStateBotAction act -> { + T regAction = getRegAction.get(); + if (regAction == null) throw new IllegalStateException("Action " + action.getClass().getName() + " is not registered!"); + result = new CraftCustomStateBotAction(regAction.getName(), act); + } + case CraftBotAction craftBotAction -> result = craftBotAction; + default -> throw new IllegalArgumentException("Action " + action.getClass().getName() + " is not a valid BotAction type!"); + } + this.getHandle().addBotAction(result, null); } @Override - public BotAction getAction(int index) { + public BotAction getAction(int index) { return this.getHandle().getBotActions().get(index); } @@ -72,7 +102,7 @@ public class CraftBot extends CraftPlayer implements Bot { } @Override - public boolean teleport(Location location, PlayerTeleportEvent.@NotNull TeleportCause cause, io.papermc.paper.entity.TeleportFlag... flags) { + public boolean teleport(Location location, PlayerTeleportEvent.@NotNull TeleportCause cause, io.papermc.paper.entity.TeleportFlag @NotNull ... flags) { Preconditions.checkArgument(location != null, "location cannot be null"); Preconditions.checkState(location.getWorld().equals(this.getWorld()), "[Leaves] Fakeplayers do not support changing world, Please use leaves fakeplayer-api instead!"); return super.teleport(location, cause, flags); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/entity/CraftBotManager.java b/leaves-server/src/main/java/org/leavesmc/leaves/entity/CraftBotManager.java index 2c4e9cb6..f47db479 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/entity/CraftBotManager.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/entity/CraftBotManager.java @@ -16,9 +16,11 @@ import org.leavesmc.leaves.entity.bot.BotCreator; import org.leavesmc.leaves.entity.bot.BotManager; import org.leavesmc.leaves.entity.bot.action.BotAction; import org.leavesmc.leaves.entity.bot.action.CustomBotAction; +import org.leavesmc.leaves.entity.bot.action.CustomStateBotAction; import org.leavesmc.leaves.entity.bot.action.CustomTimerBotAction; import org.leavesmc.leaves.event.bot.BotCreateEvent; +import java.lang.reflect.InvocationTargetException; import java.util.Collection; import java.util.Collections; import java.util.UUID; @@ -30,7 +32,7 @@ public class CraftBotManager implements BotManager { public CraftBotManager() { this.botList = MinecraftServer.getServer().getBotList(); - this.botViews = Collections.unmodifiableList(Lists.transform(botList.bots, bot -> bot.getBukkitEntity())); + this.botViews = Collections.unmodifiableList(Lists.transform(botList.bots, ServerBot::getBukkitEntity)); } @Override @@ -75,8 +77,13 @@ public class CraftBotManager implements BotManager { @SuppressWarnings("unchecked") @Override public > T newAction(@NotNull Class type) { - if (type.isAssignableFrom(CustomBotAction.class) || type.isAssignableFrom(CustomTimerBotAction.class)) { - throw new IllegalArgumentException("Custom bot action should create by yourself."); + if (type.isAssignableFrom(CustomBotAction.class) + || type.isAssignableFrom(CustomTimerBotAction.class) + || type.isAssignableFrom(CustomStateBotAction.class) + ) try { + return type.getConstructor().newInstance(); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + throw new RuntimeException(e); } T action = Actions.getForClass(type); if (action == null) {