Add disconnect command for bots, fix sound crash
This commit is contained in:
@@ -74,8 +74,20 @@ public interface IUserManager {
|
||||
*/
|
||||
default Optional<IBaritoneUser> getUserByUUID(UUID uuid) {
|
||||
return uuid == null
|
||||
? Optional.empty()
|
||||
: this.getUsers().stream().filter(user -> user.getProfile().getId().equals(uuid)).findFirst();
|
||||
? Optional.empty()
|
||||
: this.getUsers().stream().filter(user -> user.getProfile().getId().equals(uuid)).findFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the {@link IBaritoneUser} associated with the specified username
|
||||
*
|
||||
* @param username The username of the user
|
||||
* @return The user, {@link Optional#empty()} if no match or {@code uuid} is {@code null}
|
||||
*/
|
||||
default Optional<IBaritoneUser> getUserByName(String username) {
|
||||
return username == null || username.isEmpty()
|
||||
? Optional.empty()
|
||||
: this.getUsers().stream().filter(user -> user.getProfile().getName().equalsIgnoreCase(username)).findFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -175,9 +175,8 @@ public final class UserManager implements IUserManager, Helper {
|
||||
user.getNetworkManager().closeChannel(null);
|
||||
}
|
||||
this.users.remove(user);
|
||||
if (reason != null) {
|
||||
logDirect(user.getSession().getUsername() + " Disconnected: " + reason.getUnformattedText());
|
||||
}
|
||||
logDirect(user.getSession().getUsername() + " Disconnected: " +
|
||||
(reason == null ? "Unknown" : reason.getUnformattedText()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -488,17 +488,11 @@ public final class BotNetHandlerPlayClient extends NetHandlerPlayClient {
|
||||
|
||||
@Override
|
||||
public void handleHeldItemChange(@Nonnull SPacketHeldItemChange packetIn) {
|
||||
PacketThreadUtil.checkThreadAndEnqueue(packetIn, this, this.client);
|
||||
|
||||
if (InventoryPlayer.isHotbar(packetIn.getHeldItemHotbarIndex())) {
|
||||
this.player.inventory.currentItem = packetIn.getHeldItemHotbarIndex();
|
||||
}
|
||||
super.handleHeldItemChange(packetIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleDisplayObjective(@Nonnull SPacketDisplayObjective packetIn) {
|
||||
PacketThreadUtil.checkThreadAndEnqueue(packetIn, this, this.client);
|
||||
}
|
||||
public void handleDisplayObjective(@Nonnull SPacketDisplayObjective packetIn) {}
|
||||
|
||||
@Override
|
||||
public void handleEntityMetadata(@Nonnull SPacketEntityMetadata packetIn) {
|
||||
@@ -536,18 +530,12 @@ public final class BotNetHandlerPlayClient extends NetHandlerPlayClient {
|
||||
|
||||
@Override
|
||||
public void handleSetExperience(@Nonnull SPacketSetExperience packetIn) {
|
||||
PacketThreadUtil.checkThreadAndEnqueue(packetIn, this, this.client);
|
||||
|
||||
this.player.setXPStats(packetIn.getExperienceBar(), packetIn.getTotalExperience(), packetIn.getLevel());
|
||||
super.handleSetExperience(packetIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleUpdateHealth(@Nonnull SPacketUpdateHealth packetIn) {
|
||||
PacketThreadUtil.checkThreadAndEnqueue(packetIn, this, this.client);
|
||||
|
||||
this.player.setPlayerSPHealth(packetIn.getHealth());
|
||||
this.player.getFoodStats().setFoodLevel(packetIn.getFoodLevel());
|
||||
this.player.getFoodStats().setFoodSaturationLevel(packetIn.getSaturationLevel());
|
||||
super.handleUpdateHealth(packetIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -562,11 +550,8 @@ public final class BotNetHandlerPlayClient extends NetHandlerPlayClient {
|
||||
@Override
|
||||
public void handleTimeUpdate(@Nonnull SPacketTimeUpdate packetIn) {
|
||||
PacketThreadUtil.checkThreadAndEnqueue(packetIn, this, this.client);
|
||||
|
||||
this.world.setTotalWorldTime(packetIn.getTotalWorldTime());
|
||||
this.world.setWorldTime(packetIn.getWorldTime());
|
||||
|
||||
// TODO: Calculate World TPS
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -653,19 +638,12 @@ public final class BotNetHandlerPlayClient extends NetHandlerPlayClient {
|
||||
|
||||
@Override
|
||||
public void handleCooldown(@Nonnull SPacketCooldown packetIn) {
|
||||
PacketThreadUtil.checkThreadAndEnqueue(packetIn, this, this.client);
|
||||
|
||||
if (packetIn.getTicks() == 0) { // There is no cooldown
|
||||
this.player.getCooldownTracker().removeCooldown(packetIn.getItem());
|
||||
} else {
|
||||
this.player.getCooldownTracker().setCooldown(packetIn.getItem(), packetIn.getTicks());
|
||||
}
|
||||
super.handleCooldown(packetIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMoveVehicle(@Nonnull SPacketMoveVehicle packetIn) {
|
||||
PacketThreadUtil.checkThreadAndEnqueue(packetIn, this, this.client);
|
||||
/* Atm Baritone doesn't even work on vehicles that well at all, so this is a major TODO */
|
||||
super.handleMoveVehicle(packetIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -679,7 +657,7 @@ public final class BotNetHandlerPlayClient extends NetHandlerPlayClient {
|
||||
|
||||
@Override
|
||||
public void onDisconnect(@Nonnull ITextComponent reason) {
|
||||
// TODO Maybe more world unloadinde
|
||||
// TODO Maybe more world unloading
|
||||
this.world.removeEntity(this.player);
|
||||
this.user.getManager().disconnect(this.user, reason);
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import baritone.utils.accessor.IGameSettings;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
import com.mojang.authlib.minecraft.MinecraftSessionService;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.audio.SoundHandler;
|
||||
import net.minecraft.client.main.GameConfiguration;
|
||||
import net.minecraft.client.settings.GameSettings;
|
||||
import net.minecraft.client.settings.KeyBinding;
|
||||
@@ -46,7 +47,7 @@ import java.util.concurrent.Callable;
|
||||
public final class BotMinecraft extends Minecraft implements Helper {
|
||||
|
||||
private IBaritoneUser user;
|
||||
private BotTutorial tutorial;
|
||||
private Tutorial tutorial;
|
||||
|
||||
private BotMinecraft(GameConfiguration gameConfig) {
|
||||
super(gameConfig);
|
||||
@@ -79,10 +80,15 @@ public final class BotMinecraft extends Minecraft implements Helper {
|
||||
return this.tutorial;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SoundHandler getSoundHandler() {
|
||||
return BotSoundHandler.INSTANCE;
|
||||
}
|
||||
|
||||
public static BotMinecraft allocate(IBaritoneUser user) {
|
||||
BotMinecraft bm = ObjectAllocator.allocate(BotMinecraft.class);
|
||||
bm.user = user;
|
||||
bm.tutorial = new BotTutorial(bm);
|
||||
bm.tutorial = new Tutorial(bm);
|
||||
bm.gameSettings = createGameSettings(bm);
|
||||
return bm;
|
||||
}
|
||||
|
||||
@@ -17,15 +17,25 @@
|
||||
|
||||
package baritone.bot.spec;
|
||||
|
||||
import net.minecraft.client.tutorial.Tutorial;
|
||||
import net.minecraft.client.audio.ISound;
|
||||
import net.minecraft.client.audio.SoundHandler;
|
||||
import net.minecraft.client.resources.IResourceManager;
|
||||
import net.minecraft.client.settings.GameSettings;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* @author Brady
|
||||
* @since 3/3/2020
|
||||
* @since 3/7/2020
|
||||
*/
|
||||
public final class BotTutorial extends Tutorial {
|
||||
public final class BotSoundHandler extends SoundHandler {
|
||||
|
||||
public BotTutorial(BotMinecraft minecraft) {
|
||||
super(minecraft);
|
||||
public static final BotSoundHandler INSTANCE = new BotSoundHandler(null, null);
|
||||
|
||||
public BotSoundHandler(IResourceManager manager, GameSettings gameSettingsIn) {
|
||||
super(manager, gameSettingsIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playSound(@Nonnull ISound sound) {}
|
||||
}
|
||||
@@ -20,6 +20,8 @@ package baritone.bot.spec;
|
||||
import net.minecraft.client.multiplayer.ChunkProviderClient;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.profiler.Profiler;
|
||||
import net.minecraft.util.SoundCategory;
|
||||
import net.minecraft.util.SoundEvent;
|
||||
import net.minecraft.world.DimensionType;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.WorldSettings;
|
||||
@@ -33,7 +35,7 @@ import javax.annotation.Nonnull;
|
||||
* @author Brady
|
||||
* @since 11/7/2018
|
||||
*/
|
||||
public class BotWorld extends World {
|
||||
public final class BotWorld extends World {
|
||||
|
||||
private static Profiler BOT_WORLD_PROFILER = new Profiler();
|
||||
private static int worldNum = 0;
|
||||
@@ -63,6 +65,11 @@ public class BotWorld extends World {
|
||||
return allowEmpty || !this.chunkProviderClient.provideChunk(x, z).isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playSound(double x, double y, double z, SoundEvent soundIn, SoundCategory category, float volume, float pitch, boolean distanceDelay) {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
public void addEntityToWorld(int entityID, EntityBot entity) {
|
||||
this.removeEntityFromWorld(entityID);
|
||||
this.spawnEntity(entity);
|
||||
|
||||
@@ -18,17 +18,16 @@
|
||||
package baritone.command.defaults;
|
||||
|
||||
import baritone.api.IBaritone;
|
||||
import baritone.api.bot.IBaritoneUser;
|
||||
import baritone.api.bot.connect.IConnectionResult;
|
||||
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.bot.UserManager;
|
||||
import net.minecraft.util.Session;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
@@ -43,10 +42,28 @@ public class BotCommand extends Command {
|
||||
|
||||
@Override
|
||||
public void execute(String label, IArgConsumer args) throws CommandException {
|
||||
args.requireMax(0);
|
||||
IConnectionResult result = UserManager.INSTANCE.connect(
|
||||
new Session("Bot" + System.currentTimeMillis() % 1000, UUID.randomUUID().toString(), "", ""));
|
||||
logDirect(result.toString());
|
||||
if (args.hasExactly(0)) {
|
||||
IConnectionResult result = UserManager.INSTANCE.connect(
|
||||
new Session("Bot" + System.currentTimeMillis() % 1000, UUID.randomUUID().toString(), "", ""));
|
||||
logDirect(result.toString());
|
||||
} else if (args.hasExactly(2)) {
|
||||
Action action = Action.getByName(args.getString());
|
||||
if (action == null) {
|
||||
throw new CommandInvalidTypeException(args.consumed(), "an action");
|
||||
}
|
||||
|
||||
Optional<IBaritoneUser> bot = UserManager.INSTANCE.getUserByName(args.getString());
|
||||
if (!bot.isPresent()) {
|
||||
throw new CommandInvalidTypeException(args.consumed(), "a bot name");
|
||||
}
|
||||
|
||||
switch (action) {
|
||||
case DISCONNECT: {
|
||||
UserManager.INSTANCE.disconnect(bot.get(), null);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -65,7 +82,36 @@ public class BotCommand extends Command {
|
||||
"Spawns a bot",
|
||||
"",
|
||||
"Usage:",
|
||||
"> bot"
|
||||
"> bot",
|
||||
"> bot <disconnect> <name>"
|
||||
);
|
||||
}
|
||||
|
||||
private enum Action {
|
||||
DISCONNECT("disconnect", "dc");
|
||||
private final String[] names;
|
||||
|
||||
Action(String... names) {
|
||||
this.names = names;
|
||||
}
|
||||
|
||||
public static Action getByName(String name) {
|
||||
for (Action action : Action.values()) {
|
||||
for (String alias : action.names) {
|
||||
if (alias.equalsIgnoreCase(name)) {
|
||||
return action;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String[] getAllNames() {
|
||||
Set<String> names = new HashSet<>();
|
||||
for (Action action : Action.values()) {
|
||||
names.addAll(Arrays.asList(action.names));
|
||||
}
|
||||
return names.toArray(new String[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user