Skip to content

Commit

Permalink
Fix yggdrasil login protect, and rename bot agent
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yh-china committed Feb 16, 2025
1 parent 4de9da7 commit e58f636
Show file tree
Hide file tree
Showing 21 changed files with 73 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import org.leavesmc.leaves.LeavesConfig;
import org.leavesmc.leaves.LeavesLogger;
import org.leavesmc.leaves.bot.agent.Actions;
import org.leavesmc.leaves.bot.agent.BotAction;
import org.leavesmc.leaves.bot.agent.BotConfig;
import org.leavesmc.leaves.bot.agent.AbstractBotAction;
import org.leavesmc.leaves.bot.agent.AbstractBotConfig;
import org.leavesmc.leaves.bot.agent.Configs;
import org.leavesmc.leaves.bot.agent.actions.CraftCustomBotAction;
import org.leavesmc.leaves.command.CommandArgumentResult;
Expand Down Expand Up @@ -116,7 +116,7 @@ public BotCommand(String name) {
list.add(String.valueOf(i));
}
} else {
BotAction<?> action = Actions.getForName(args[2]);
AbstractBotAction<?> action = Actions.getForName(args[2]);
if (action != null) {
list.addAll(action.getArgument().tabComplete(args.length - 4));
}
Expand Down Expand Up @@ -326,9 +326,9 @@ private void onAction(CommandSender sender, String @NotNull [] args) {

String index = args[3];
if (index.equals("all")) {
Set<BotAction<?>> forRemoval = new HashSet<>();
Set<AbstractBotAction<?>> forRemoval = new HashSet<>();
for (int i = 0; i < bot.getBotActions().size(); i++) {
BotAction<?> action = bot.getBotActions().get(i);
AbstractBotAction<?> action = bot.getBotActions().get(i);
BotActionStopEvent event = new BotActionStopEvent(
bot.getBukkitEntity(), action.getName(), action.getUUID(), BotActionStopEvent.Reason.COMMAND, sender
);
Expand All @@ -347,7 +347,7 @@ private void onAction(CommandSender sender, String @NotNull [] args) {
return;
}

BotAction<?> action = bot.getBotActions().get(i);
AbstractBotAction<?> action = bot.getBotActions().get(i);
BotActionStopEvent event = new BotActionStopEvent(
bot.getBukkitEntity(), action.getName(), action.getUUID(), BotActionStopEvent.Reason.COMMAND, sender
);
Expand All @@ -364,7 +364,7 @@ private void onAction(CommandSender sender, String @NotNull [] args) {
return;
}

BotAction<?> action = Actions.getForName(args[2]);
AbstractBotAction<?> action = Actions.getForName(args[2]);
if (action == null) {
sender.sendMessage(text("Invalid action", NamedTextColor.RED));
return;
Expand All @@ -382,7 +382,7 @@ private void onAction(CommandSender sender, String @NotNull [] args) {
System.arraycopy(args, 3, realArgs, 0, realArgs.length);
}

BotAction<?> newAction;
AbstractBotAction<?> newAction;
try {
if (action instanceof CraftCustomBotAction customBotAction) {
newAction = customBotAction.createCraft(player, realArgs);
Expand Down Expand Up @@ -427,7 +427,7 @@ private void onConfig(CommandSender sender, String @NotNull [] args) {
return;
}

BotConfig<?> config = Objects.requireNonNull(Configs.getConfig(args[2])).config;
AbstractBotConfig<?> config = Objects.requireNonNull(Configs.getConfig(args[2])).config;
if (args.length < 4) {
config.getMessage().forEach(sender::sendMessage);
} else {
Expand Down
29 changes: 14 additions & 15 deletions leaves-server/src/main/java/org/leavesmc/leaves/bot/ServerBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
import org.leavesmc.leaves.LeavesConfig;
import org.leavesmc.leaves.LeavesLogger;
import org.leavesmc.leaves.bot.agent.Actions;
import org.leavesmc.leaves.bot.agent.BotAction;
import org.leavesmc.leaves.bot.agent.BotConfig;
import org.leavesmc.leaves.bot.agent.AbstractBotAction;
import org.leavesmc.leaves.bot.agent.AbstractBotConfig;
import org.leavesmc.leaves.bot.agent.Configs;
import org.leavesmc.leaves.entity.CraftBot;
import org.leavesmc.leaves.event.bot.BotActionScheduleEvent;
Expand All @@ -70,11 +70,10 @@
import java.util.UUID;
import java.util.function.Predicate;

// TODO test
public class ServerBot extends ServerPlayer {

private final Map<Configs<?>, BotConfig<?>> configs;
private final List<BotAction<?>> actions;
private final Map<Configs<?>, AbstractBotConfig<?>> configs;
private final List<AbstractBotAction<?>> actions;

public boolean resume = false;
public BotCreateState createState;
Expand All @@ -97,7 +96,7 @@ public ServerBot(MinecraftServer server, ServerLevel world, GameProfile profile)
this.gameMode = new ServerBotGameMode(this);
this.actions = new ArrayList<>();

ImmutableMap.Builder<Configs<?>, BotConfig<?>> configBuilder = ImmutableMap.builder();
ImmutableMap.Builder<Configs<?>, AbstractBotConfig<?>> configBuilder = ImmutableMap.builder();
for (Configs<?> config : Configs.getConfigs()) {
configBuilder.put(config, config.config.create(this));
}
Expand Down Expand Up @@ -399,15 +398,15 @@ public void addAdditionalSaveData(@NotNull CompoundTag nbt) {

if (!this.actions.isEmpty()) {
ListTag actionNbt = new ListTag();
for (BotAction<?> action : this.actions) {
for (AbstractBotAction<?> action : this.actions) {
actionNbt.add(action.save(new CompoundTag()));
}
nbt.put("actions", actionNbt);
}

if (!this.configs.isEmpty()) {
ListTag configNbt = new ListTag();
for (BotConfig<?> config : this.configs.values()) {
for (AbstractBotConfig<?> config : this.configs.values()) {
configNbt.add(config.save(new CompoundTag()));
}
nbt.put("configs", configNbt);
Expand Down Expand Up @@ -442,9 +441,9 @@ public void readAdditionalSaveData(@NotNull CompoundTag nbt) {
ListTag actionNbt = nbt.getList("actions", 10);
for (int i = 0; i < actionNbt.size(); i++) {
CompoundTag actionTag = actionNbt.getCompound(i);
BotAction<?> action = Actions.getForName(actionTag.getString("actionName"));
AbstractBotAction<?> action = Actions.getForName(actionTag.getString("actionName"));
if (action != null) {
BotAction<?> newAction = action.create();
AbstractBotAction<?> newAction = action.create();
newAction.load(actionTag);
this.actions.add(newAction);
}
Expand Down Expand Up @@ -509,11 +508,11 @@ public void dropAll() {
private void runAction() {
if (LeavesConfig.modify.fakeplayer.canUseAction) {
this.actions.forEach(action -> action.tryTick(this));
this.actions.removeIf(BotAction::isCancelled);
this.actions.removeIf(AbstractBotAction::isCancelled);
}
}

public boolean addBotAction(BotAction<?> action, CommandSender sender) {
public boolean addBotAction(AbstractBotAction<?> action, CommandSender sender) {
if (!LeavesConfig.modify.fakeplayer.canUseAction) {
return false;
}
Expand All @@ -527,7 +526,7 @@ public boolean addBotAction(BotAction<?> action, CommandSender sender) {
return true;
}

public List<BotAction<?>> getBotActions() {
public List<AbstractBotAction<?>> getBotActions() {
return actions;
}

Expand All @@ -537,8 +536,8 @@ public List<BotAction<?>> getBotActions() {
}

@SuppressWarnings("unchecked")
public <E> BotConfig<E> getConfig(Configs<E> config) {
return (BotConfig<E>) Objects.requireNonNull(this.configs.get(config));
public <E> AbstractBotConfig<E> getConfig(Configs<E> config) {
return (AbstractBotConfig<E>) Objects.requireNonNull(this.configs.get(config));
}

public <E> E getConfigValue(Configs<E> config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.UUID;
import java.util.function.Supplier;

public abstract class BotAction<E extends BotAction<E>> {
public abstract class AbstractBotAction<E extends AbstractBotAction<E>> {

private final String name;
private final CommandArgument argument;
Expand All @@ -28,7 +28,7 @@ public abstract class BotAction<E extends BotAction<E>> {
private int needWaitTick;
private int canDoNumber;

public BotAction(String name, CommandArgument argument, Supplier<E> creator) {
public AbstractBotAction(String name, CommandArgument argument, Supplier<E> creator) {
this.name = name;
this.argument = argument;
this.uuid = UUID.randomUUID();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
import java.util.List;
import java.util.function.Supplier;

public abstract class BotConfig<E> {
public abstract class AbstractBotConfig<E> {

private final String name;
private final CommandArgument argument;
private final Supplier<BotConfig<E>> creator;
private final Supplier<AbstractBotConfig<E>> creator;
protected ServerBot bot;

public BotConfig(String name, CommandArgument argument, Supplier<BotConfig<E>> creator) {
public AbstractBotConfig(String name, CommandArgument argument, Supplier<AbstractBotConfig<E>> creator) {
this.name = name;
this.argument = argument;
this.creator = creator;
}

public BotConfig<E> setBot(ServerBot bot) {
public AbstractBotConfig<E> setBot(ServerBot bot) {
this.bot = bot;
return this;
}
Expand All @@ -48,7 +48,7 @@ public CommandArgument getArgument() {
}

@NotNull
public BotConfig<E> create(ServerBot bot) {
public AbstractBotConfig<E> create(ServerBot bot) {
return this.creator.get().setBot(bot);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public class Actions {

private static final Map<String, BotAction<?>> actions = new HashMap<>();
private static final Map<String, AbstractBotAction<?>> actions = new HashMap<>();

public static void registerAll() {
register(new AttackAction());
Expand All @@ -33,7 +33,7 @@ public static void registerAll() {
register(new RotationAction());
}

public static boolean register(@NotNull BotAction<?> action) {
public static boolean register(@NotNull AbstractBotAction<?> action) {
if (!actions.containsKey(action.getName())) {
actions.put(action.getName(), action);
return true;
Expand All @@ -51,7 +51,7 @@ public static boolean unregister(@NotNull String name) {

@NotNull
@Contract(pure = true)
public static Collection<BotAction<?>> getAll() {
public static Collection<AbstractBotAction<?>> getAll() {
return actions.values();
}

Expand All @@ -61,7 +61,7 @@ public static Set<String> getNames() {
}

@Nullable
public static BotAction<?> getForName(String name) {
public static AbstractBotAction<?> getForName(String name) {
return actions.get(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("unused")
public class Configs<E> {

private static final Map<String, Configs<?>> configs = new HashMap<>();
Expand All @@ -18,9 +19,9 @@ public class Configs<E> {
public static final Configs<Boolean> SPAWN_PHANTOM = register(new SpawnPhantomConfig());
public static final Configs<Integer> SIMULATION_DISTANCE = register(new SimulationDistanceConfig());

public final BotConfig<E> config;
public final AbstractBotConfig<E> config;

private Configs(BotConfig<E> config) {
private Configs(AbstractBotConfig<E> config) {
this.config = config;
}

Expand All @@ -36,7 +37,7 @@ public static Configs<?> getConfig(String name) {
}

@NotNull
private static <T> Configs<T> register(BotConfig<T> botConfig) {
private static <T> Configs<T> register(AbstractBotConfig<T> botConfig) {
Configs<T> config = new Configs<>(botConfig);
configs.put(botConfig.getName(), config);
return config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import net.minecraft.server.level.ServerPlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.leavesmc.leaves.bot.agent.BotAction;
import org.leavesmc.leaves.bot.agent.AbstractBotAction;
import org.leavesmc.leaves.command.CommandArgument;
import org.leavesmc.leaves.command.CommandArgumentResult;
import org.leavesmc.leaves.command.CommandArgumentType;

import java.util.List;
import java.util.function.Supplier;

public abstract class AbstractTimerAction<E extends AbstractTimerAction<E>> extends BotAction<E> {
public abstract class AbstractTimerAction<E extends AbstractTimerAction<E>> extends AbstractBotAction<E> {

public AbstractTimerAction(String name, Supplier<E> creator) {
super(name, CommandArgument.of(CommandArgumentType.INTEGER, CommandArgumentType.INTEGER), creator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.leavesmc.leaves.bot.agent.Actions;
import org.leavesmc.leaves.bot.agent.BotAction;
import org.leavesmc.leaves.bot.agent.AbstractBotAction;
import org.leavesmc.leaves.entity.botaction.BotActionType;
import org.leavesmc.leaves.entity.botaction.LeavesBotAction;

public class CraftBotAction extends LeavesBotAction {

private final BotAction<?> handle;
private final AbstractBotAction<?> handle;

public CraftBotAction(@NotNull BotAction<?> action) {
public CraftBotAction(@NotNull AbstractBotAction<?> action) {
super(BotActionType.valueOf(action.getName()), action.getTickDelay(), action.getCanDoNumber());
this.handle = action;
}

@Contract("_ -> new")
@NotNull
public static LeavesBotAction asAPICopy(BotAction<?> action) {
public static LeavesBotAction asAPICopy(AbstractBotAction<?> action) {
return new CraftBotAction(action);
}

@NotNull
public static BotAction<?> asInternalCopy(@NotNull LeavesBotAction action) {
BotAction<?> act = Actions.getForName(action.getActionName());
public static AbstractBotAction<?> asInternalCopy(@NotNull LeavesBotAction action) {
AbstractBotAction<?> act = Actions.getForName(action.getActionName());
if (act == null) {
throw new IllegalArgumentException("Invalid action name!");
}

BotAction<?> newAction = null;
AbstractBotAction<?> newAction = null;
String[] args = new String[]{String.valueOf(action.getExecuteInterval()), String.valueOf(action.getRemainingExecuteTime())};
try {
if (act instanceof CraftCustomBotAction customBotAction) {
Expand All @@ -48,7 +48,7 @@ public static BotAction<?> asInternalCopy(@NotNull LeavesBotAction action) {
return newAction;
}

public BotAction<?> getHandle() {
public AbstractBotAction<?> getHandle() {
return handle;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.leavesmc.leaves.bot.ServerBot;
import org.leavesmc.leaves.bot.agent.BotAction;
import org.leavesmc.leaves.bot.agent.AbstractBotAction;
import org.leavesmc.leaves.command.CommandArgument;
import org.leavesmc.leaves.command.CommandArgumentResult;
import org.leavesmc.leaves.entity.botaction.CustomBotAction;

public class CraftCustomBotAction extends BotAction<CraftCustomBotAction> {
public class CraftCustomBotAction extends AbstractBotAction<CraftCustomBotAction> {

private final CustomBotAction realAction;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.leavesmc.leaves.bot.ServerBot;
import org.leavesmc.leaves.bot.agent.BotAction;
import org.leavesmc.leaves.bot.agent.AbstractBotAction;
import org.leavesmc.leaves.command.CommandArgument;
import org.leavesmc.leaves.command.CommandArgumentResult;
import org.leavesmc.leaves.command.CommandArgumentType;

import java.util.List;

public class LookAction extends BotAction<LookAction> {
public class LookAction extends AbstractBotAction<LookAction> {

public LookAction() {
super("look", CommandArgument.of(CommandArgumentType.DOUBLE, CommandArgumentType.DOUBLE, CommandArgumentType.DOUBLE), LookAction::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.leavesmc.leaves.bot.ServerBot;
import org.leavesmc.leaves.bot.agent.BotAction;
import org.leavesmc.leaves.bot.agent.AbstractBotAction;
import org.leavesmc.leaves.command.CommandArgument;
import org.leavesmc.leaves.command.CommandArgumentResult;

public class RotateAction extends BotAction<RotateAction> {
public class RotateAction extends AbstractBotAction<RotateAction> {

public RotateAction() {
super("rotate", CommandArgument.EMPTY, RotateAction::new);
Expand Down
Loading

0 comments on commit e58f636

Please sign in to comment.