9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-29 11:59:17 +00:00

fix: fix null in actions reg

This commit is contained in:
MC_XiaoHei
2025-07-09 12:23:32 +08:00
parent bcfbc1d362
commit 980862ba2e
5 changed files with 36 additions and 9 deletions

View File

@@ -6,6 +6,7 @@ import org.jetbrains.annotations.Nullable;
import org.leavesmc.leaves.bot.agent.actions.CraftAttackAction;
import org.leavesmc.leaves.bot.agent.actions.CraftBotAction;
import org.leavesmc.leaves.bot.agent.actions.CraftBreakBlockAction;
import org.leavesmc.leaves.bot.agent.actions.CraftCustomAction;
import org.leavesmc.leaves.bot.agent.actions.CraftDropAction;
import org.leavesmc.leaves.bot.agent.actions.CraftFishAction;
import org.leavesmc.leaves.bot.agent.actions.CraftJumpAction;
@@ -28,6 +29,7 @@ import org.leavesmc.leaves.entity.bot.action.BotAction;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
public class Actions {
@@ -57,10 +59,18 @@ public class Actions {
register(new CraftMoveAction());
}
public static @NotNull Class<?> getActionClass(@NotNull CraftBotAction<?> action) {
Class<?> actionClass = action.getInterfaceClass();
if (actionClass == null && action instanceof CraftCustomAction<?> act) {
actionClass = act.getRealActionClass();
}
return Objects.requireNonNull(actionClass, "Class " + action.getClass() + " is not registered as a BotAction!");
}
public static boolean register(@NotNull CraftBotAction<?> action) {
if (!actionsByName.containsKey(action.getName())) {
actionsByName.put(action.getName(), action);
actionsByClass.put(action.getInterfaceClass(), action);
actionsByClass.put(getActionClass(action), action);
return true;
}
return false;
@@ -68,7 +78,7 @@ public class Actions {
public static boolean unregister(@NotNull String name) {
if (actionsByName.containsKey(name)) {
actionsByClass.remove(actionsByName.get(name).getInterfaceClass());
actionsByClass.remove(getActionClass(actionsByName.get(name)));
actionsByName.remove(name);
return true;
}

View File

@@ -5,4 +5,6 @@ import org.jetbrains.annotations.Nullable;
public interface CraftCustomAction<E> {
E createCraft(@Nullable Player player, String[] args);
Class<?> getRealActionClass();
}

View File

@@ -34,4 +34,9 @@ public class CraftCustomBotAction extends CraftBotAction<AbstractCustomBotAction
public Class<AbstractCustomBotAction> getInterfaceClass() {
return null;
}
@Override
public Class<?> getRealActionClass() {
return realAction.getClass();
}
}

View File

@@ -25,13 +25,18 @@ public class CraftCustomStateBotAction extends CraftStateBotAction<AbstractCusto
return null;
}
@Override
public boolean doTick(@NotNull ServerBot bot) {
return realAction.doTick(bot.getBukkitEntity());
}
@Override
public Class<AbstractCustomStateBotAction> getInterfaceClass() {
return null;
}
@Override
public boolean doTick(@NotNull ServerBot bot) {
return realAction.doTick(bot.getBukkitEntity());
public Class<?> getRealActionClass() {
return realAction.getClass();
}
}

View File

@@ -15,11 +15,6 @@ public class CraftCustomTimerBotAction extends CraftTimerBotAction<AbstractCusto
this.realAction = realAction;
}
@Override
public Class<AbstractCustomTimerBotAction> getInterfaceClass() {
return null;
}
@Override
public CraftCustomTimerBotAction createCraft(@Nullable Player player, String[] args) {
AbstractCustomTimerBotAction newRealAction = realAction.getNew(player, args);
@@ -33,4 +28,14 @@ public class CraftCustomTimerBotAction extends CraftTimerBotAction<AbstractCusto
public boolean doTick(@NotNull ServerBot bot) {
return realAction.doTick(bot.getBukkitEntity());
}
@Override
public Class<AbstractCustomTimerBotAction> getInterfaceClass() {
return null;
}
@Override
public Class<?> getRealActionClass() {
return realAction.getClass();
}
}