9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-29 20:09:23 +00:00

feat: custom bot action

This commit is contained in:
MC_XiaoHei
2025-08-25 12:18:48 +08:00
parent 568bf16b31
commit 5f5dcb5c00
4 changed files with 26 additions and 14 deletions

View File

@@ -14,7 +14,6 @@ import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.PluginManager;
import org.jetbrains.annotations.NotNull;
import org.leavesmc.leaves.bot.ServerBot;
import org.leavesmc.leaves.bot.agent.Actions;
import org.leavesmc.leaves.config.GlobalConfigManager;
import org.leavesmc.leaves.config.annotations.GlobalConfig;
import org.leavesmc.leaves.config.annotations.GlobalConfigCategory;
@@ -136,17 +135,14 @@ public final class LeavesConfig {
private static class FakeplayerValidator extends BooleanConfigValidator {
@Override
public void verify(Boolean old, Boolean value) throws IllegalArgumentException {
if (old == null || old.equals(value)) {
return;
}
if (value) {
Actions.registerAll();
BotCommand.INSTANCE.register();
} else {
BotCommand.INSTANCE.unregister();
}
if (old != null && !old.equals(value)) {
Bukkit.getOnlinePlayers().stream()
.filter(BotCommand::hasPermission)
.forEach(org.bukkit.entity.Player::updateCommands);
}
}
}

View File

@@ -16,7 +16,7 @@ public class Actions {
private static final Map<String, AbstractBotAction<?>> actionsByName = new HashMap<>();
private static final Map<Class<?>, AbstractBotAction<?>> actionsByClass = new HashMap<>();
public static void registerAll() {
static {
register(new ServerAttackAction(), AttackAction.class);
register(new ServerBreakBlockAction(), BreakBlockAction.class);
register(new ServerDropAction(), DropAction.class);
@@ -38,7 +38,7 @@ public class Actions {
register(new ServerSwapAction(), SwapAction.class);
}
public static boolean register(@NotNull AbstractBotAction<?> action, Class<? extends BotAction<?>> type) {
public static boolean register(@NotNull AbstractBotAction<?> action, Class<?> type) {
if (!actionsByName.containsKey(action.getName())) {
actionsByName.put(action.getName(), action);
actionsByClass.put(type, action);
@@ -47,9 +47,17 @@ public class Actions {
return false;
}
public static boolean register(@NotNull AbstractBotAction<?> action) {
return register(action, action.getClass());
}
public static boolean unregister(@NotNull String name) {
// TODO add in custom action api
return true;
AbstractBotAction<?> action = actionsByName.remove(name);
if (action != null) {
actionsByClass.remove(action.getClass());
return true;
}
return false;
}
@NotNull

View File

@@ -5,6 +5,7 @@ import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.server.MinecraftServer;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
@@ -39,6 +40,7 @@ public abstract class RootNode extends LiteralNode {
.getCommands()
.getDispatcher()
.register((LiteralArgumentBuilder<CommandSourceStack>) compile());
Bukkit.getOnlinePlayers().forEach(org.bukkit.entity.Player::updateCommands);
}
public void unregister() {
@@ -46,5 +48,6 @@ public abstract class RootNode extends LiteralNode {
.getCommands()
.getDispatcher();
dispatcher.getRoot().removeCommand(name);
Bukkit.getOnlinePlayers().forEach(org.bukkit.entity.Player::updateCommands);
}
}

View File

@@ -40,12 +40,17 @@ public class CraftBot extends CraftPlayer implements Bot {
@Override
public <T extends BotAction<T>> void addAction(@NotNull T action) {
switch (action) {
case CraftBotAction act -> this.getHandle().addBotAction(act.getHandle(), null);
default -> throw new IllegalArgumentException("Action " + action.getClass().getName() + " is not a valid BotAction type!");
if (action instanceof CraftBotAction<?, ?> act) {
this.getHandle().addBotAction(act.getHandle(), null);
} else {
throw new IllegalArgumentException("Action " + action.getClass().getName() + " is not a valid BotAction type!");
}
}
public void addAction(@NotNull AbstractBotAction<?> action) {
this.getHandle().addBotAction(action, null);
}
@Override
public BotAction<?> getAction(int index) {
return (BotAction<?>) this.getHandle().getBotActions().get(index).asCraft();