9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2026-01-06 15:51:33 +00:00

Revert "fix: fix custom bot action api, refactor"

This reverts commit afefaec186.
This commit is contained in:
MC_XiaoHei
2025-07-08 19:30:09 +08:00
parent afefaec186
commit df61130ad2
9 changed files with 17 additions and 68 deletions

View File

@@ -39,7 +39,7 @@ public interface Bot extends Player {
* @param action bot action
*/
@org.jetbrains.annotations.ApiStatus.Experimental
void addAction(@NotNull BotAction<?> action);
void addAction(@NotNull BotAction action);
/**
* Get the copy action in giving index
@@ -48,7 +48,7 @@ public interface Bot extends Player {
* @return Action of that index
*/
@org.jetbrains.annotations.ApiStatus.Experimental
BotAction<?> getAction(int index);
BotAction getAction(int index);
/**
* Get action size

View File

@@ -21,13 +21,13 @@ public interface CustomBotAction extends BotAction<CustomBotAction> {
boolean doTick(Bot bot);
/**
* Created a new action instance from command.
* Created a new action instance.
*
* @param player player who create this action
* @param args passed action arguments
* @return a new action instance with given args
*/
@Nullable CustomBotAction fromCommand(Player player, String[] args);
@Nullable CustomBotAction getNew(@Nullable Player player, String[] args);
/**
* Requests a list of possible completions for a action argument.

View File

@@ -28,7 +28,6 @@ import org.leavesmc.leaves.entity.bot.action.BotAction;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
public class Actions {
@@ -61,8 +60,7 @@ public class Actions {
public static boolean register(@NotNull CraftBotAction<?> action) {
if (!actionsByName.containsKey(action.getName())) {
actionsByName.put(action.getName(), action);
Class<?> interfaceClass = action.getInterfaceClass();
actionsByClass.put(Objects.requireNonNullElseGet(interfaceClass, action::getClass), action);
actionsByClass.put(action.getInterfaceClass(), action);
return true;
}
return false;
@@ -70,9 +68,7 @@ public class Actions {
public static boolean unregister(@NotNull String name) {
if (actionsByName.containsKey(name)) {
CraftBotAction<?> action = actionsByName.get(name);
Class<?> interfaceClass = action.getInterfaceClass();
actionsByClass.remove(Objects.requireNonNullElseGet(interfaceClass, action::getClass));
actionsByClass.remove(actionsByName.get(name).getInterfaceClass());
actionsByName.remove(name);
return true;
}

View File

@@ -1,9 +1,8 @@
package org.leavesmc.leaves.bot.agent.actions;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
public interface CraftCustomAction<E> {
E createCraft(Player player, String[] args);
E createEmptyCraft();
public E createCraft(@Nullable Player player, String[] args);
}

View File

@@ -7,8 +7,6 @@ import org.leavesmc.leaves.bot.ServerBot;
import org.leavesmc.leaves.command.CommandArgument;
import org.leavesmc.leaves.entity.bot.action.CustomBotAction;
import java.lang.reflect.InvocationTargetException;
public class CraftCustomBotAction extends CraftBotAction<CustomBotAction> implements CraftCustomAction<CraftCustomBotAction> {
private final CustomBotAction realAction;
@@ -20,23 +18,13 @@ public class CraftCustomBotAction extends CraftBotAction<CustomBotAction> implem
@Override
public CraftCustomBotAction createCraft(@Nullable Player player, String[] args) {
CustomBotAction newRealAction = realAction.fromCommand(player, args);
CustomBotAction newRealAction = realAction.getNew(player, args);
if (newRealAction != null) {
return new CraftCustomBotAction(this.getName(), newRealAction);
}
return null;
}
@Override
public CraftCustomBotAction createEmptyCraft() {
try {
CustomBotAction newRealAction = realAction.getClass().getConstructor().newInstance();
return new CraftCustomBotAction(this.getName(), newRealAction);
} catch (InstantiationException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean doTick(@NotNull ServerBot bot) {
return realAction.doTick(bot.getBukkitEntity());

View File

@@ -7,8 +7,6 @@ import org.leavesmc.leaves.bot.ServerBot;
import org.leavesmc.leaves.command.CommandArgument;
import org.leavesmc.leaves.entity.bot.action.CustomStateBotAction;
import java.lang.reflect.InvocationTargetException;
public class CraftCustomStateBotAction extends CraftStateBotAction<CustomStateBotAction> implements CraftCustomAction<CraftCustomStateBotAction> {
private final CustomStateBotAction realAction;
@@ -27,16 +25,6 @@ public class CraftCustomStateBotAction extends CraftStateBotAction<CustomStateBo
return null;
}
@Override
public CraftCustomStateBotAction createEmptyCraft() {
try {
CustomStateBotAction newRealAction = realAction.getClass().getConstructor().newInstance();
return new CraftCustomStateBotAction(this.getName(), newRealAction);
} catch (InstantiationException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
@Override
public Class<CustomStateBotAction> getInterfaceClass() {
return null;

View File

@@ -7,8 +7,6 @@ import org.leavesmc.leaves.bot.ServerBot;
import org.leavesmc.leaves.command.CommandArgument;
import org.leavesmc.leaves.entity.bot.action.CustomTimerBotAction;
import java.lang.reflect.InvocationTargetException;
public class CraftCustomTimerBotAction extends CraftTimerBotAction<CustomTimerBotAction> implements CraftCustomAction<CraftCustomTimerBotAction> {
private final CustomTimerBotAction realAction;
@@ -32,16 +30,6 @@ public class CraftCustomTimerBotAction extends CraftTimerBotAction<CustomTimerBo
return null;
}
@Override
public CraftCustomTimerBotAction createEmptyCraft() {
try {
CustomTimerBotAction newRealAction = realAction.getClass().getConstructor().newInstance();
return new CraftCustomTimerBotAction(this.getName(), newRealAction);
} catch (InstantiationException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
@Override
public int getDoNumber() {
return realAction.getDoNumber();

View File

@@ -39,15 +39,12 @@ public class CraftBot extends CraftPlayer implements Bot {
}
@Override
public void addAction(@NotNull BotAction<?> action) {
if (!(action instanceof CraftBotAction<?> craftBotAction)) {
throw new IllegalArgumentException("Action must be an instance of CraftBotAction! Are you forget to use `BotManager#newAction`?");
}
this.getHandle().addBotAction(craftBotAction, null);
public void addAction(@NotNull BotAction action) {
this.getHandle().addBotAction((CraftBotAction<?>) action, null);
}
@Override
public BotAction<?> getAction(int index) {
public BotAction getAction(int index) {
return this.getHandle().getBotActions().get(index);
}
@@ -75,7 +72,7 @@ public class CraftBot extends CraftPlayer implements Bot {
}
@Override
public boolean teleport(Location location, PlayerTeleportEvent.@NotNull TeleportCause cause, io.papermc.paper.entity.TeleportFlag @NotNull ... flags) {
public boolean teleport(Location location, PlayerTeleportEvent.@NotNull TeleportCause cause, io.papermc.paper.entity.TeleportFlag... 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);

View File

@@ -11,14 +11,11 @@ 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.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;
@@ -33,7 +30,7 @@ public class CraftBotManager implements BotManager {
public CraftBotManager() {
this.botList = MinecraftServer.getServer().getBotList();
this.botViews = Collections.unmodifiableList(Lists.transform(botList.bots, ServerBot::getBukkitEntity));
this.botViews = Collections.unmodifiableList(Lists.transform(botList.bots, bot -> bot.getBukkitEntity()));
}
@Override
@@ -78,18 +75,14 @@ public class CraftBotManager implements BotManager {
@SuppressWarnings("unchecked")
@Override
public <T extends BotAction<T>> T newAction(@NotNull Class<T> type) {
if (type.isAssignableFrom(CustomBotAction.class) || type.isAssignableFrom(CustomTimerBotAction.class)) {
throw new IllegalArgumentException("Custom bot action should create by yourself.");
}
T action = Actions.getForClass(type);
if (action == null) {
throw new IllegalArgumentException("No action registered for type: " + type.getName());
}
try {
if (type.isAssignableFrom(CustomBotAction.class)) {
return (T) ((CraftCustomBotAction) action).createEmptyCraft();
} else if (type.isAssignableFrom(CustomTimerBotAction.class)) {
return (T) ((CraftCustomTimerBotAction) action).createEmptyCraft();
} else if (type.isAssignableFrom(CustomStateBotAction.class)) {
return (T) ((CraftCustomStateBotAction) action).createEmptyCraft();
}
return (T) ((CraftBotAction<?>) action).create();
} catch (Exception e) {
throw new RuntimeException("Failed to create action of type: " + type.getName(), e);