9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-31 04:46:29 +00:00

feat: add onSuccess and onFail to bot action api (#589)

This commit is contained in:
MC_XiaoHei
2025-07-04 15:45:55 +08:00
committed by GitHub
parent 1affbb084c
commit 6c80eb62b1
3 changed files with 50 additions and 5 deletions

View File

@@ -8,14 +8,17 @@ import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.leavesmc.leaves.bot.ServerBot;
import org.leavesmc.leaves.bot.agent.actions.CraftBotAction;
import org.leavesmc.leaves.command.CommandArgument;
import org.leavesmc.leaves.command.CommandArgumentResult;
import org.leavesmc.leaves.entity.botaction.LeavesBotAction;
import org.leavesmc.leaves.event.bot.BotActionExecuteEvent;
import org.leavesmc.leaves.event.bot.BotActionStopEvent;
import java.util.List;
import java.util.UUID;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Supplier;
//TODO onStop for fully terminate action (use, etc.)
@@ -34,6 +37,9 @@ public abstract class AbstractBotAction<E extends AbstractBotAction<E>> {
private int numberRemaining;
private boolean cancel;
private Consumer<LeavesBotAction> onFail;
private Consumer<LeavesBotAction> onSuccess;
public AbstractBotAction(String name, CommandArgument argument, Supplier<E> creator) {
this.name = name;
this.argument = argument;
@@ -77,6 +83,11 @@ public abstract class AbstractBotAction<E extends AbstractBotAction<E>> {
this.numberRemaining--;
}
this.tickToNext = this.getInitialTickInterval() - 1;
if (this.onSuccess != null) {
this.onSuccess.accept(CraftBotAction.asAPICopy(this));
}
} else if (this.onFail != null) {
this.onFail.accept(CraftBotAction.asAPICopy(this));
}
} else {
this.tickToNext--;
@@ -191,6 +202,14 @@ public abstract class AbstractBotAction<E extends AbstractBotAction<E>> {
return this.argument;
}
public void setOnFail(Consumer<LeavesBotAction> onFail) {
this.onFail = onFail;
}
public void setOnSuccess(Consumer<LeavesBotAction> onSuccess) {
this.onSuccess = onSuccess;
}
@NotNull
public E create() {
return this.creator.get();

View File

@@ -45,6 +45,10 @@ public class CraftBotAction extends LeavesBotAction {
if (newAction == null) {
throw new IllegalArgumentException("Invalid action!"); // TODO look action
}
newAction.setOnSuccess(action.getOnSuccess());
newAction.setOnFail(action.getOnFail());
return newAction;
}