This commit is contained in:
Leijurv
2018-11-14 17:17:52 -08:00
parent 42c78337c7
commit 7cb38352ac
12 changed files with 136 additions and 27 deletions
@@ -18,6 +18,7 @@
package baritone.launch.mixins; package baritone.launch.mixins;
import baritone.api.BaritoneAPI; import baritone.api.BaritoneAPI;
import baritone.api.IBaritone;
import baritone.api.behavior.IPathingBehavior; import baritone.api.behavior.IPathingBehavior;
import baritone.api.event.events.ChatEvent; import baritone.api.event.events.ChatEvent;
import baritone.api.event.events.PlayerUpdateEvent; import baritone.api.event.events.PlayerUpdateEvent;
@@ -44,7 +45,11 @@ public class MixinEntityPlayerSP {
) )
private void sendChatMessage(String msg, CallbackInfo ci) { private void sendChatMessage(String msg, CallbackInfo ci) {
ChatEvent event = new ChatEvent((EntityPlayerSP) (Object) this, msg); ChatEvent event = new ChatEvent((EntityPlayerSP) (Object) this, msg);
BaritoneAPI.getProvider().getBaritoneForPlayer((EntityPlayerSP) (Object) this).getGameEventHandler().onSendChatMessage(event); for (IBaritone ibaritone : BaritoneAPI.getProvider().getAllBaritones()) {
System.out.println("Sending chat event to baritone " + ibaritone);
ibaritone.getGameEventHandler().onSendChatMessage(event);
}
//BaritoneAPI.getProvider().getBaritoneForPlayer((EntityPlayerSP) (Object) this).getGameEventHandler().onSendChatMessage(event);
if (event.isCancelled()) { if (event.isCancelled()) {
ci.cancel(); ci.cancel();
} }
+4 -7
View File
@@ -36,7 +36,6 @@ import baritone.utils.BaritoneAutoTest;
import baritone.utils.ExampleBaritoneControl; import baritone.utils.ExampleBaritoneControl;
import baritone.utils.InputOverrideHandler; import baritone.utils.InputOverrideHandler;
import baritone.utils.PathingControlManager; import baritone.utils.PathingControlManager;
import baritone.utils.player.PrimaryPlayerContext;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import java.io.File; import java.io.File;
@@ -74,7 +73,7 @@ public class Baritone implements IBaritone {
*/ */
private boolean initialized; private boolean initialized;
private GameEventHandler gameEventHandler; private final GameEventHandler gameEventHandler;
private List<Behavior> behaviors; private List<Behavior> behaviors;
private PathingBehavior pathingBehavior; private PathingBehavior pathingBehavior;
@@ -89,11 +88,12 @@ public class Baritone implements IBaritone {
private PathingControlManager pathingControlManager; private PathingControlManager pathingControlManager;
private IPlayerContext playerContext; private final IPlayerContext playerContext;
private WorldProvider worldProvider; private WorldProvider worldProvider;
Baritone() { public Baritone(IPlayerContext playerContext) {
this.gameEventHandler = new GameEventHandler(this); this.gameEventHandler = new GameEventHandler(this);
this.playerContext = playerContext;
} }
public synchronized void init() { public synchronized void init() {
@@ -101,9 +101,6 @@ public class Baritone implements IBaritone {
return; return;
} }
// Define this before behaviors try and get it, or else it will be null and the builds will fail!
this.playerContext = PrimaryPlayerContext.INSTANCE;
this.behaviors = new ArrayList<>(); this.behaviors = new ArrayList<>();
{ {
// the Behavior constructor calls baritone.registerBehavior(this) so this populates the behaviors arraylist // the Behavior constructor calls baritone.registerBehavior(this) so this populates the behaviors arraylist
+11 -4
View File
@@ -20,9 +20,12 @@ package baritone;
import baritone.api.IBaritone; import baritone.api.IBaritone;
import baritone.api.IBaritoneProvider; import baritone.api.IBaritoneProvider;
import baritone.api.cache.IWorldScanner; import baritone.api.cache.IWorldScanner;
import baritone.bot.IBaritoneUser;
import baritone.bot.UserManager;
import baritone.cache.WorldScanner; import baritone.cache.WorldScanner;
import baritone.utils.player.PrimaryPlayerContext;
import java.util.Collections; import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
@@ -31,7 +34,7 @@ import java.util.List;
*/ */
public final class BaritoneProvider implements IBaritoneProvider { public final class BaritoneProvider implements IBaritoneProvider {
private final Baritone primary = new Baritone(); private final Baritone primary = new Baritone(PrimaryPlayerContext.INSTANCE);
@Override @Override
public IBaritone getPrimaryBaritone() { public IBaritone getPrimaryBaritone() {
@@ -40,8 +43,12 @@ public final class BaritoneProvider implements IBaritoneProvider {
@Override @Override
public List<IBaritone> getAllBaritones() { public List<IBaritone> getAllBaritones() {
// TODO return a CopyOnWriteArrayList List<IBaritone> baritones = new ArrayList<>();
return Collections.singletonList(primary); baritones.add(getPrimaryBaritone());
for (IBaritoneUser ibu : UserManager.INSTANCE.users()) {
baritones.add(ibu.getBaritone());
}
return baritones;
} }
@Override @Override
@@ -17,6 +17,8 @@
package baritone.bot; package baritone.bot;
import baritone.Baritone;
import baritone.api.IBaritone;
import baritone.bot.spec.EntityBot; import baritone.bot.spec.EntityBot;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import net.minecraft.network.NetworkManager; import net.minecraft.network.NetworkManager;
@@ -38,10 +40,14 @@ class BaritoneUser implements IBaritoneUser {
private GameProfile profile; private GameProfile profile;
private INetHandlerPlayClient netHandlerPlayClient; private INetHandlerPlayClient netHandlerPlayClient;
private final Baritone baritone;
BaritoneUser(UserManager manager, NetworkManager networkManager, Session session) { BaritoneUser(UserManager manager, NetworkManager networkManager, Session session) {
this.manager = manager; this.manager = manager;
this.networkManager = networkManager; this.networkManager = networkManager;
this.session = session; this.session = session;
this.baritone = new Baritone(new BotPlayerContext(this)); // OPPA GANGNAM STYLE
this.baritone.init(); // actually massive iq
} }
@Override @Override
@@ -80,4 +86,9 @@ class BaritoneUser implements IBaritoneUser {
public UserManager getManager() { public UserManager getManager() {
return this.manager; return this.manager;
} }
@Override
public IBaritone getBaritone() {
return baritone;
}
} }
@@ -0,0 +1,67 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.bot;
import baritone.api.BaritoneAPI;
import baritone.api.cache.IWorldData;
import baritone.api.utils.IPlayerContext;
import baritone.bot.handler.BotNetHandlerPlayClient;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.multiplayer.PlayerControllerMP;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
public class BotPlayerContext implements IPlayerContext {
private final IBaritoneUser bot;
public BotPlayerContext(IBaritoneUser bot) {
this.bot = bot;
}
@Override
public EntityPlayerSP player() {
if (bot.getConnection() == null) {
return null;
}
return ((BotNetHandlerPlayClient) bot.getConnection()).player();
}
@Override
public PlayerControllerMP playerController() {
return Minecraft.getMinecraft().playerController; // idk LOL
}
@Override
public World world() {
if (bot.getConnection() == null) {
return null;
}
return ((BotNetHandlerPlayClient) bot.getConnection()).world();
}
@Override
public IWorldData worldData() {
return BaritoneAPI.getProvider().getBaritoneForPlayer(player()).getWorldProvider().getCurrentWorld();
}
@Override
public RayTraceResult objectMouseOver() {
return Minecraft.getMinecraft().objectMouseOver; // idk LOL
}
}
@@ -17,6 +17,7 @@
package baritone.bot; package baritone.bot;
import baritone.api.IBaritone;
import baritone.bot.spec.EntityBot; import baritone.bot.spec.EntityBot;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import net.minecraft.network.INetHandler; import net.minecraft.network.INetHandler;
@@ -33,7 +34,7 @@ public interface IBaritoneUser {
/** /**
* Called when the user successfully logs into a server. * Called when the user successfully logs into a server.
* *
* @param profile The game profile returned by the server on login * @param profile The game profile returned by the server on login
* @param netHandlerPlayClient The client play network handler * @param netHandlerPlayClient The client play network handler
*/ */
void onLoginSuccess(GameProfile profile, INetHandlerPlayClient netHandlerPlayClient); void onLoginSuccess(GameProfile profile, INetHandlerPlayClient netHandlerPlayClient);
@@ -76,4 +77,6 @@ public interface IBaritoneUser {
* @return The manager that spawned this {@link IBaritoneUser}. * @return The manager that spawned this {@link IBaritoneUser}.
*/ */
UserManager getManager(); UserManager getManager();
IBaritone getBaritone();
} }
@@ -35,6 +35,7 @@ import net.minecraft.util.Session;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
@@ -176,4 +177,8 @@ public final class UserManager implements Helper {
public final BotWorldProvider getWorldProvider() { public final BotWorldProvider getWorldProvider() {
return this.worldProvider; return this.worldProvider;
} }
public final List<IBaritoneUser> users() {
return Collections.unmodifiableList(this.users);
}
} }
@@ -425,9 +425,9 @@ public class BotNetHandlerPlayClient extends NetHandlerPlayClient {
PacketThreadUtil.checkThreadAndEnqueue(packetIn, this, this.client); PacketThreadUtil.checkThreadAndEnqueue(packetIn, this, this.client);
PlayerCapabilities c = this.player.capabilities; PlayerCapabilities c = this.player.capabilities;
c.disableDamage = packetIn.isInvulnerable(); c.disableDamage = packetIn.isInvulnerable();
c.isFlying = packetIn.isFlying(); c.isFlying = packetIn.isFlying();
c.allowFlying = packetIn.isAllowFlying(); c.allowFlying = packetIn.isAllowFlying();
c.isCreativeMode = packetIn.isCreativeMode(); c.isCreativeMode = packetIn.isCreativeMode();
c.setFlySpeed(packetIn.getFlySpeed()); c.setFlySpeed(packetIn.getFlySpeed());
c.setPlayerWalkSpeed(packetIn.getWalkSpeed()); c.setPlayerWalkSpeed(packetIn.getWalkSpeed());
@@ -454,7 +454,7 @@ public class BotNetHandlerPlayClient extends NetHandlerPlayClient {
Entity entity = packetIn.getEntity(this.world); Entity entity = packetIn.getEntity(this.world);
if (entity instanceof EntityLivingBase) { if (entity instanceof EntityLivingBase) {
((EntityLivingBase)entity).removeActivePotionEffect(packetIn.getPotion()); ((EntityLivingBase) entity).removeActivePotionEffect(packetIn.getPotion());
} }
} }
@@ -686,4 +686,12 @@ public class BotNetHandlerPlayClient extends NetHandlerPlayClient {
public ClientAdvancementManager getAdvancementManager() { public ClientAdvancementManager getAdvancementManager() {
throw new UnsupportedOperationException("This method shouldn't have been called; That is unepic!"); throw new UnsupportedOperationException("This method shouldn't have been called; That is unepic!");
} }
public EntityBot player() {
return player;
}
public BotWorld world() {
return world;
}
} }
@@ -17,6 +17,8 @@
package baritone.bot.spec; package baritone.bot.spec;
import baritone.api.utils.IInputOverrideHandler;
import baritone.api.utils.input.Input;
import baritone.bot.IBaritoneUser; import baritone.bot.IBaritoneUser;
import net.minecraft.util.MovementInput; import net.minecraft.util.MovementInput;
@@ -37,8 +39,13 @@ public class BotMovementInput extends MovementInput {
this.moveForward = 0.0F; this.moveForward = 0.0F;
// These are placeholders until an input overrider is implemented for bots // These are placeholders until an input overrider is implemented for bots
boolean forward, back, left, right, jump, sneak; IInputOverrideHandler i = user.getBaritone().getInputOverrideHandler();
forward = back = left = right = jump = sneak = false; boolean forward = i.isInputForcedDown(Input.MOVE_FORWARD);
boolean back = i.isInputForcedDown(Input.MOVE_BACK);
boolean left = i.isInputForcedDown(Input.MOVE_LEFT);
boolean right = i.isInputForcedDown(Input.MOVE_RIGHT);
jump = i.isInputForcedDown(Input.JUMP); // oppa
boolean sneak = i.isInputForcedDown(Input.SNEAK);
if (this.forwardKeyDown = forward) { if (this.forwardKeyDown = forward) {
this.moveForward++; this.moveForward++;
@@ -56,8 +63,6 @@ public class BotMovementInput extends MovementInput {
this.moveStrafe--; this.moveStrafe--;
} }
this.jump = true;
if (this.sneak = sneak) { if (this.sneak = sneak) {
this.moveStrafe *= 0.3D; this.moveStrafe *= 0.3D;
this.moveForward *= 0.3D; this.moveForward *= 0.3D;
@@ -67,10 +67,6 @@ public final class GameEventHandler implements IEventBus, Helper {
@Override @Override
public final void onSendChatMessage(ChatEvent event) { public final void onSendChatMessage(ChatEvent event) {
// TODO temporary bot event call prevention
if (event.getPlayer() != baritone.getPlayerContext().player())
return;
// Ensure UserManager is created to prevent a ConcurrentModificationException // Ensure UserManager is created to prevent a ConcurrentModificationException
Objects.requireNonNull(UserManager.INSTANCE); Objects.requireNonNull(UserManager.INSTANCE);
@@ -27,6 +27,7 @@ import baritone.api.utils.SettingsUtil;
import baritone.behavior.Behavior; import baritone.behavior.Behavior;
import baritone.behavior.PathingBehavior; import baritone.behavior.PathingBehavior;
import baritone.bot.UserManager; import baritone.bot.UserManager;
import baritone.bot.connect.ConnectionResult;
import baritone.cache.ChunkPacker; import baritone.cache.ChunkPacker;
import baritone.cache.Waypoint; import baritone.cache.Waypoint;
import baritone.pathing.movement.Movement; import baritone.pathing.movement.Movement;
@@ -469,7 +470,9 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
} }
// TODO: Temporary command to test bots offline // TODO: Temporary command to test bots offline
if (msg.equals("bot")) { if (msg.equals("bot")) {
UserManager.INSTANCE.connect(new Session("Bot" + System.currentTimeMillis() % 1000, UUID.randomUUID().toString(), "", "")); System.out.println("DOING A BOT");
ConnectionResult result = UserManager.INSTANCE.connect(new Session("Bot" + System.currentTimeMillis() % 1000, UUID.randomUUID().toString(), "", ""));
System.out.println(result);
return true; return true;
} }
return false; return false;
@@ -63,7 +63,9 @@ public final class PathRenderer implements Helper {
// System.out.println(event.getPartialTicks()); // System.out.println(event.getPartialTicks());
float partialTicks = event.getPartialTicks(); float partialTicks = event.getPartialTicks();
Goal goal = behavior.getGoal(); Goal goal = behavior.getGoal();
if (behavior.baritone.getPlayerContext().world() == null) {
return;
}
int thisPlayerDimension = behavior.baritone.getPlayerContext().world().provider.getDimensionType().getId(); int thisPlayerDimension = behavior.baritone.getPlayerContext().world().provider.getDimensionType().getId();
int currentRenderViewDimension = BaritoneAPI.getProvider().getPrimaryBaritone().getPlayerContext().world().provider.getDimensionType().getId(); int currentRenderViewDimension = BaritoneAPI.getProvider().getPrimaryBaritone().getPlayerContext().world().provider.getDimensionType().getId();