mirror of https://github.com/cabaletta/baritone
`bot execute` and `bot say`
This commit is contained in:
parent
a3df7ebd4c
commit
ac72ad717b
|
@ -24,12 +24,10 @@ import baritone.api.event.events.PlayerUpdateEvent;
|
||||||
import baritone.api.event.events.SprintStateEvent;
|
import baritone.api.event.events.SprintStateEvent;
|
||||||
import baritone.api.event.events.type.EventState;
|
import baritone.api.event.events.type.EventState;
|
||||||
import baritone.behavior.LookBehavior;
|
import baritone.behavior.LookBehavior;
|
||||||
import net.minecraft.client.Minecraft;
|
|
||||||
import net.minecraft.client.entity.EntityPlayerSP;
|
import net.minecraft.client.entity.EntityPlayerSP;
|
||||||
import net.minecraft.client.settings.KeyBinding;
|
import net.minecraft.client.settings.KeyBinding;
|
||||||
import net.minecraft.entity.player.PlayerCapabilities;
|
import net.minecraft.entity.player.PlayerCapabilities;
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
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.At;
|
||||||
import org.spongepowered.asm.mixin.injection.Inject;
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
import org.spongepowered.asm.mixin.injection.Redirect;
|
import org.spongepowered.asm.mixin.injection.Redirect;
|
||||||
|
@ -42,15 +40,12 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||||
@Mixin(EntityPlayerSP.class)
|
@Mixin(EntityPlayerSP.class)
|
||||||
public class MixinEntityPlayerSP {
|
public class MixinEntityPlayerSP {
|
||||||
|
|
||||||
@Shadow protected Minecraft mc;
|
|
||||||
|
|
||||||
@Inject(
|
@Inject(
|
||||||
method = "sendChatMessage",
|
method = "sendChatMessage",
|
||||||
at = @At("HEAD"),
|
at = @At("HEAD"),
|
||||||
cancellable = true
|
cancellable = true
|
||||||
)
|
)
|
||||||
private void sendChatMessage(String msg, CallbackInfo ci) {
|
private void sendChatMessage(String msg, CallbackInfo ci) {
|
||||||
/*
|
|
||||||
ChatEvent event = new ChatEvent(msg);
|
ChatEvent event = new ChatEvent(msg);
|
||||||
IBaritone baritone = BaritoneAPI.getProvider().getBaritoneForPlayer((EntityPlayerSP) (Object) this);
|
IBaritone baritone = BaritoneAPI.getProvider().getBaritoneForPlayer((EntityPlayerSP) (Object) this);
|
||||||
if (baritone == null) {
|
if (baritone == null) {
|
||||||
|
@ -60,23 +55,6 @@ public class MixinEntityPlayerSP {
|
||||||
if (event.isCancelled()) {
|
if (event.isCancelled()) {
|
||||||
ci.cancel();
|
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(
|
@Inject(
|
||||||
|
|
|
@ -25,13 +25,17 @@ import baritone.api.command.Command;
|
||||||
import baritone.api.command.argument.IArgConsumer;
|
import baritone.api.command.argument.IArgConsumer;
|
||||||
import baritone.api.command.exception.CommandException;
|
import baritone.api.command.exception.CommandException;
|
||||||
import baritone.api.command.exception.CommandInvalidTypeException;
|
import baritone.api.command.exception.CommandInvalidTypeException;
|
||||||
|
import baritone.api.event.events.ChatEvent;
|
||||||
import baritone.bot.UserManager;
|
import baritone.bot.UserManager;
|
||||||
import baritone.bot.impl.BotGuiInventory;
|
import baritone.bot.impl.BotGuiInventory;
|
||||||
import net.minecraft.util.Session;
|
import net.minecraft.util.Session;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static baritone.api.command.IBaritoneChatControl.FORCE_COMMAND_PREFIX;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Brady
|
* @author Brady
|
||||||
* @since 3/2/2020
|
* @since 3/2/2020
|
||||||
|
@ -54,18 +58,48 @@ public class BotCommand extends Command {
|
||||||
final Session session = new Session(username, UUID.randomUUID().toString(), "", "");
|
final Session session = new Session(username, UUID.randomUUID().toString(), "", "");
|
||||||
final IConnectionResult result = UserManager.INSTANCE.connect(session);
|
final IConnectionResult result = UserManager.INSTANCE.connect(session);
|
||||||
logDirect(result.toString());
|
logDirect(result.toString());
|
||||||
} else if (action == Action.INVENTORY || action == Action.DISCONNECT) {
|
} else if (action.requiresBotSelector()) {
|
||||||
Optional<IBaritoneUser> bot = UserManager.INSTANCE.getUserByName(args.getString());
|
final String selector = args.getString();
|
||||||
if (!bot.isPresent()) {
|
final List<IBaritoneUser> bots;
|
||||||
throw new CommandInvalidTypeException(args.consumed(), "a bot name");
|
|
||||||
|
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) {
|
switch (action) {
|
||||||
case INVENTORY: {
|
case DISCONNECT: {
|
||||||
((Baritone) baritone).showScreen(new BotGuiInventory(bot.get()));
|
bots.forEach(bot -> UserManager.INSTANCE.disconnect(bot, null));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case DISCONNECT: {
|
case SAY: {
|
||||||
UserManager.INSTANCE.disconnect(bot.get(), null);
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,21 +123,29 @@ public class BotCommand extends Command {
|
||||||
"",
|
"",
|
||||||
"Usage:",
|
"Usage:",
|
||||||
"> bot add/a <name>",
|
"> bot add/a <name>",
|
||||||
"> bot inventory/i [name]",
|
"> bot inventory/i [bot]",
|
||||||
"> bot disconnect/dc [name]"
|
"> bot disconnect/d [bot]",
|
||||||
|
"> bot say/s [bot] [args...]",
|
||||||
|
"> bot execute/e [bot] [args...]"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum Action {
|
private enum Action {
|
||||||
ADD("add", "a"),
|
ADD("add", "a"),
|
||||||
INVENTORY("inventory", "i"),
|
INVENTORY("inventory", "i"),
|
||||||
DISCONNECT("disconnect", "dc");
|
DISCONNECT("disconnect", "d"),
|
||||||
|
SAY("say", "s"),
|
||||||
|
EXECUTE("execute", "e");
|
||||||
private final String[] names;
|
private final String[] names;
|
||||||
|
|
||||||
Action(String... names) {
|
Action(String... names) {
|
||||||
this.names = names;
|
this.names = names;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean requiresBotSelector() {
|
||||||
|
return this == INVENTORY || this == DISCONNECT || this == SAY || this == EXECUTE;
|
||||||
|
}
|
||||||
|
|
||||||
public static Action getByName(String name) {
|
public static Action getByName(String name) {
|
||||||
for (Action action : Action.values()) {
|
for (Action action : Action.values()) {
|
||||||
for (String alias : action.names) {
|
for (String alias : action.names) {
|
||||||
|
|
Loading…
Reference in New Issue