mirror of
https://github.com/LeavesMC/Leaves.git
synced 2025-12-29 20:09:23 +00:00
fix: fix custom bot action api, refactor
This commit is contained in:
@@ -38,8 +38,7 @@ public interface Bot extends Player {
|
||||
*
|
||||
* @param action bot action
|
||||
*/
|
||||
@org.jetbrains.annotations.ApiStatus.Experimental
|
||||
void addAction(@NotNull BotAction action);
|
||||
<T extends BotAction<T>> 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
|
||||
|
||||
@@ -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);
|
||||
|
||||
/**
|
||||
|
||||
@@ -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 <T extends BotAction<T>> void addAction(@NotNull T action) {
|
||||
CraftBotAction<?> result;
|
||||
Supplier<T> 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);
|
||||
|
||||
@@ -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 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.");
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user