From ac72ad717bbcfe0a9fc7374b14fc0361789fe157 Mon Sep 17 00:00:00 2001 From: Brady Date: Wed, 28 Jun 2023 00:09:53 -0500 Subject: [PATCH] `bot execute` and `bot say` --- .../launch/mixins/MixinEntityPlayerSP.java | 22 ------- .../baritone/command/defaults/BotCommand.java | 64 +++++++++++++++---- 2 files changed, 53 insertions(+), 33 deletions(-) diff --git a/src/launch/java/baritone/launch/mixins/MixinEntityPlayerSP.java b/src/launch/java/baritone/launch/mixins/MixinEntityPlayerSP.java index b65604061..281ff96f5 100644 --- a/src/launch/java/baritone/launch/mixins/MixinEntityPlayerSP.java +++ b/src/launch/java/baritone/launch/mixins/MixinEntityPlayerSP.java @@ -24,12 +24,10 @@ import baritone.api.event.events.PlayerUpdateEvent; import baritone.api.event.events.SprintStateEvent; import baritone.api.event.events.type.EventState; import baritone.behavior.LookBehavior; -import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.settings.KeyBinding; import net.minecraft.entity.player.PlayerCapabilities; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; @@ -42,15 +40,12 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(EntityPlayerSP.class) public class MixinEntityPlayerSP { - @Shadow protected Minecraft mc; - @Inject( method = "sendChatMessage", at = @At("HEAD"), cancellable = true ) private void sendChatMessage(String msg, CallbackInfo ci) { - /* ChatEvent event = new ChatEvent(msg); IBaritone baritone = BaritoneAPI.getProvider().getBaritoneForPlayer((EntityPlayerSP) (Object) this); if (baritone == null) { @@ -60,23 +55,6 @@ public class MixinEntityPlayerSP { if (event.isCancelled()) { ci.cancel(); } - */ - - EntityPlayerSP self = (EntityPlayerSP) (Object) this; - if (self != this.mc.player) { - return; - } - - ChatEvent event = new ChatEvent(msg); - for (IBaritone baritone : BaritoneAPI.getProvider().getAllBaritones()) { - if (baritone == BaritoneAPI.getProvider().getPrimaryBaritone() != msg.startsWith(BaritoneAPI.getSettings().prefix.value)) { - baritone.getGameEventHandler().onSendChatMessage(event); - } - } - - if (event.isCancelled()) { - ci.cancel(); - } } @Inject( diff --git a/src/main/java/baritone/command/defaults/BotCommand.java b/src/main/java/baritone/command/defaults/BotCommand.java index f31ff33e0..801e3d19c 100644 --- a/src/main/java/baritone/command/defaults/BotCommand.java +++ b/src/main/java/baritone/command/defaults/BotCommand.java @@ -25,13 +25,17 @@ import baritone.api.command.Command; import baritone.api.command.argument.IArgConsumer; import baritone.api.command.exception.CommandException; import baritone.api.command.exception.CommandInvalidTypeException; +import baritone.api.event.events.ChatEvent; import baritone.bot.UserManager; import baritone.bot.impl.BotGuiInventory; import net.minecraft.util.Session; import java.util.*; +import java.util.stream.Collectors; import java.util.stream.Stream; +import static baritone.api.command.IBaritoneChatControl.FORCE_COMMAND_PREFIX; + /** * @author Brady * @since 3/2/2020 @@ -54,18 +58,48 @@ public class BotCommand extends Command { final Session session = new Session(username, UUID.randomUUID().toString(), "", ""); final IConnectionResult result = UserManager.INSTANCE.connect(session); logDirect(result.toString()); - } else if (action == Action.INVENTORY || action == Action.DISCONNECT) { - Optional bot = UserManager.INSTANCE.getUserByName(args.getString()); - if (!bot.isPresent()) { - throw new CommandInvalidTypeException(args.consumed(), "a bot name"); + } else if (action.requiresBotSelector()) { + final String selector = args.getString(); + final List bots; + + if (selector.equals("*")) { + bots = UserManager.INSTANCE.getUsers(); + } else if (selector.contains(",")) { + bots = Arrays.stream(selector.split(",")) + .map(UserManager.INSTANCE::getUserByName) + .filter(Optional::isPresent) + .map(Optional::get) + .collect(Collectors.toList()); + } else { + bots = UserManager.INSTANCE.getUserByName(selector) + .map(Collections::singletonList) + .orElseGet(Collections::emptyList); } + + if (bots.isEmpty()) { + throw new CommandInvalidTypeException(args.consumed(), "selector didn't match any bots"); + } + + if (action == Action.INVENTORY) { + // Only display one inventory lol + final IBaritoneUser bot = bots.get(0); + ((Baritone) baritone).showScreen(new BotGuiInventory(bot)); + return; + } + switch (action) { - case INVENTORY: { - ((Baritone) baritone).showScreen(new BotGuiInventory(bot.get())); + case DISCONNECT: { + bots.forEach(bot -> UserManager.INSTANCE.disconnect(bot, null)); break; } - case DISCONNECT: { - UserManager.INSTANCE.disconnect(bot.get(), null); + case SAY: { + final String message = args.rawRest(); + bots.forEach(bot -> bot.getPlayerContext().player().sendChatMessage(message)); + break; + } + case EXECUTE: { + final String command = FORCE_COMMAND_PREFIX + args.rawRest(); + bots.forEach(bot -> bot.getBaritone().getGameEventHandler().onSendChatMessage(new ChatEvent(command))); break; } } @@ -89,21 +123,29 @@ public class BotCommand extends Command { "", "Usage:", "> bot add/a ", - "> bot inventory/i [name]", - "> bot disconnect/dc [name]" + "> bot inventory/i [bot]", + "> bot disconnect/d [bot]", + "> bot say/s [bot] [args...]", + "> bot execute/e [bot] [args...]" ); } private enum Action { ADD("add", "a"), INVENTORY("inventory", "i"), - DISCONNECT("disconnect", "dc"); + DISCONNECT("disconnect", "d"), + SAY("say", "s"), + EXECUTE("execute", "e"); private final String[] names; Action(String... names) { this.names = names; } + public boolean requiresBotSelector() { + return this == INVENTORY || this == DISCONNECT || this == SAY || this == EXECUTE; + } + public static Action getByName(String name) { for (Action action : Action.values()) { for (String alias : action.names) {