Skip to content

Commit 5f5dcb5

Browse files
committed
feat: custom bot action
1 parent 568bf16 commit 5f5dcb5

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

leaves-server/src/main/java/org/leavesmc/leaves/LeavesConfig.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.bukkit.plugin.PluginManager;
1515
import org.jetbrains.annotations.NotNull;
1616
import org.leavesmc.leaves.bot.ServerBot;
17-
import org.leavesmc.leaves.bot.agent.Actions;
1817
import org.leavesmc.leaves.config.GlobalConfigManager;
1918
import org.leavesmc.leaves.config.annotations.GlobalConfig;
2019
import org.leavesmc.leaves.config.annotations.GlobalConfigCategory;
@@ -136,17 +135,14 @@ public static class FakeplayerConfig {
136135
private static class FakeplayerValidator extends BooleanConfigValidator {
137136
@Override
138137
public void verify(Boolean old, Boolean value) throws IllegalArgumentException {
138+
if (old == null || old.equals(value)) {
139+
return;
140+
}
139141
if (value) {
140-
Actions.registerAll();
141142
BotCommand.INSTANCE.register();
142143
} else {
143144
BotCommand.INSTANCE.unregister();
144145
}
145-
if (old != null && !old.equals(value)) {
146-
Bukkit.getOnlinePlayers().stream()
147-
.filter(BotCommand::hasPermission)
148-
.forEach(org.bukkit.entity.Player::updateCommands);
149-
}
150146
}
151147
}
152148

leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/Actions.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class Actions {
1616
private static final Map<String, AbstractBotAction<?>> actionsByName = new HashMap<>();
1717
private static final Map<Class<?>, AbstractBotAction<?>> actionsByClass = new HashMap<>();
1818

19-
public static void registerAll() {
19+
static {
2020
register(new ServerAttackAction(), AttackAction.class);
2121
register(new ServerBreakBlockAction(), BreakBlockAction.class);
2222
register(new ServerDropAction(), DropAction.class);
@@ -38,7 +38,7 @@ public static void registerAll() {
3838
register(new ServerSwapAction(), SwapAction.class);
3939
}
4040

41-
public static boolean register(@NotNull AbstractBotAction<?> action, Class<? extends BotAction<?>> type) {
41+
public static boolean register(@NotNull AbstractBotAction<?> action, Class<?> type) {
4242
if (!actionsByName.containsKey(action.getName())) {
4343
actionsByName.put(action.getName(), action);
4444
actionsByClass.put(type, action);
@@ -47,9 +47,17 @@ public static boolean register(@NotNull AbstractBotAction<?> action, Class<? ext
4747
return false;
4848
}
4949

50+
public static boolean register(@NotNull AbstractBotAction<?> action) {
51+
return register(action, action.getClass());
52+
}
53+
5054
public static boolean unregister(@NotNull String name) {
51-
// TODO add in custom action api
52-
return true;
55+
AbstractBotAction<?> action = actionsByName.remove(name);
56+
if (action != null) {
57+
actionsByClass.remove(action.getClass());
58+
return true;
59+
}
60+
return false;
5361
}
5462

5563
@NotNull

leaves-server/src/main/java/org/leavesmc/leaves/command/RootNode.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
66
import net.minecraft.commands.CommandSourceStack;
77
import net.minecraft.server.MinecraftServer;
8+
import org.bukkit.Bukkit;
89
import org.bukkit.command.CommandSender;
910
import org.jetbrains.annotations.NotNull;
1011

@@ -39,12 +40,14 @@ public void register() {
3940
.getCommands()
4041
.getDispatcher()
4142
.register((LiteralArgumentBuilder<CommandSourceStack>) compile());
43+
Bukkit.getOnlinePlayers().forEach(org.bukkit.entity.Player::updateCommands);
4244
}
4345

4446
public void unregister() {
4547
CommandDispatcher<CommandSourceStack> dispatcher = MinecraftServer.getServer()
4648
.getCommands()
4749
.getDispatcher();
4850
dispatcher.getRoot().removeCommand(name);
51+
Bukkit.getOnlinePlayers().forEach(org.bukkit.entity.Player::updateCommands);
4952
}
5053
}

leaves-server/src/main/java/org/leavesmc/leaves/entity/bot/CraftBot.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,17 @@ public String getSkinName() {
4040

4141
@Override
4242
public <T extends BotAction<T>> void addAction(@NotNull T action) {
43-
switch (action) {
44-
case CraftBotAction act -> this.getHandle().addBotAction(act.getHandle(), null);
45-
default -> throw new IllegalArgumentException("Action " + action.getClass().getName() + " is not a valid BotAction type!");
43+
if (action instanceof CraftBotAction<?, ?> act) {
44+
this.getHandle().addBotAction(act.getHandle(), null);
45+
} else {
46+
throw new IllegalArgumentException("Action " + action.getClass().getName() + " is not a valid BotAction type!");
4647
}
4748
}
4849

50+
public void addAction(@NotNull AbstractBotAction<?> action) {
51+
this.getHandle().addBotAction(action, null);
52+
}
53+
4954
@Override
5055
public BotAction<?> getAction(int index) {
5156
return (BotAction<?>) this.getHandle().getBotActions().get(index).asCraft();

0 commit comments

Comments
 (0)