9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-29 20:09:23 +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

@@ -1,9 +1,11 @@
package org.leavesmc.leaves.entity.botaction;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.UUID;
import java.util.function.Consumer;
@org.jetbrains.annotations.ApiStatus.Experimental
public class LeavesBotAction {
@@ -13,26 +15,38 @@ public class LeavesBotAction {
private final int initialTickDelay;
private final int initialTickInterval;
private final int initialNumber;
private final @Nullable Consumer<LeavesBotAction> onSuccess;
private final @Nullable Consumer<LeavesBotAction> onFail;
private Player actionPlayer;
private int tickToNext;
private int numberRemaining;
private boolean cancel;
public LeavesBotAction(BotActionType type, int initialTickInterval, int initialNumber) {
this(type.getName(), UUID.randomUUID(), 0, initialTickInterval, initialNumber);
public LeavesBotAction(@NotNull BotActionType type, int initialTickInterval, int initialNumber) {
this(type.getName(), UUID.randomUUID(), 0, initialTickInterval, initialNumber, null, null);
}
public LeavesBotAction(BotActionType type, int initialTickDelay, int initialTickInterval, int initialNumber) {
this(type.getName(), UUID.randomUUID(), initialTickDelay, initialTickInterval, initialNumber);
public LeavesBotAction(@NotNull BotActionType type, int initialTickInterval, int initialNumber, @Nullable Consumer<LeavesBotAction> onSuccess, @Nullable Consumer<LeavesBotAction> onFail) {
this(type.getName(), UUID.randomUUID(), 0, initialTickInterval, initialNumber, onSuccess, onFail);
}
protected LeavesBotAction(String name, UUID actionUUID, int initialTickDelay, int initialTickInterval, int initialNumber) {
public LeavesBotAction(@NotNull BotActionType type, int initialTickDelay, int initialTickInterval, int initialNumber) {
this(type.getName(), UUID.randomUUID(), initialTickDelay, initialTickInterval, initialNumber, null, null);
}
public LeavesBotAction(@NotNull BotActionType type, int initialTickDelay, int initialTickInterval, int initialNumber, @Nullable Consumer<LeavesBotAction> onSuccess, @Nullable Consumer<LeavesBotAction> onFail) {
this(type.getName(), UUID.randomUUID(), initialTickDelay, initialTickInterval, initialNumber, onSuccess, onFail);
}
protected LeavesBotAction(String name, UUID actionUUID, int initialTickDelay, int initialTickInterval, int initialNumber, @Nullable Consumer<LeavesBotAction> onSuccess, @Nullable Consumer<LeavesBotAction> onFail) {
this.actionName = name;
this.uuid = actionUUID;
this.initialTickDelay = initialTickDelay;
this.initialTickInterval = initialTickInterval;
this.initialNumber = initialNumber;
this.onSuccess = onSuccess;
this.onFail = onFail;
}
public String getActionName() {
@@ -87,4 +101,12 @@ public class LeavesBotAction {
public void setTickToNext(int tickToNext) {
this.tickToNext = tickToNext;
}
public @Nullable Consumer<LeavesBotAction> getOnSuccess() {
return onSuccess;
}
public @Nullable Consumer<LeavesBotAction> getOnFail() {
return onFail;
}
}

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;
}