first attempt

This commit is contained in:
Leijurv 2018-08-01 13:10:48 -04:00
parent 7a01268399
commit 35a7d5d818
No known key found for this signature in database
GPG Key ID: 0936202430AE187C
20 changed files with 734 additions and 0 deletions

View File

@ -0,0 +1,15 @@
package baritone.bot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
/**
* @author Brady
* @since 7/31/2018 10:56 PM
*/
public final class GameActionHandler {
GameActionHandler() {}
public final void onPlacedBlock(ItemStack stack, BlockPos pos) {}
}

View File

@ -0,0 +1,18 @@
package baritone.bot;
import baritone.bot.event.IGameEventListener;
import baritone.Baritone;
/**
* @author Brady
* @since 7/31/2018 11:04 PM
*/
public final class GameEventHandler implements IGameEventListener {
GameEventHandler() {}
@Override
public final void onTick() {
Baritone.onTick();
}
}

View File

@ -0,0 +1,25 @@
package baritone.bot;
import java.util.ArrayList;
import java.util.List;
/**
* @author Brady
* @since 7/31/2018 10:29 PM
*/
public final class HookStateManager {
HookStateManager() {}
public final boolean shouldCancelDebugRenderRight() {
return false;
}
public final boolean shouldOverrideDebugInfoLeft() {
return false;
}
public final List<String> getDebugInfoLeft() {
return new ArrayList<>();
}
}

View File

@ -0,0 +1,122 @@
package baritone.bot;
import baritone.bot.utils.Helper;
import net.minecraft.client.settings.KeyBinding;
import org.lwjgl.input.Keyboard;
import java.util.HashMap;
import java.util.Map;
/**
* This serves as a replacement to the old {@code MovementManager}'s
* input overriding capabilities. It is vastly more extensible in the
* inputs that can be overriden.
*
* @author Brady
* @since 7/31/2018 11:20 PM
*/
public final class InputOverrideHandler implements Helper {
InputOverrideHandler() {}
/**
* Maps keybinds to whether or not we are forcing their state down
*/
private final Map<KeyBinding, Boolean> inputForceStateMap = new HashMap<>();
/**
* Maps keycodes to whether or not we are forcing their state down
*/
private final Map<Integer, Boolean> keyCodeForceStateMap = new HashMap<>();
/**
* Returns whether or not we are forcing down the specified {@link KeyBinding}.
*
* @param key The KeyBinding object
* @return Whether or not it is being forced down
*/
public final boolean isInputForcedDown(KeyBinding key) {
return inputForceStateMap.computeIfAbsent(key, k -> false);
}
/**
* Sets whether or not the specified {@link Input} is being forced down.
*
* @param input The {@link Input}
* @param forced Whether or not the state is being forced
*/
public final void setInputForceState(Input input, boolean forced) {
inputForceStateMap.put(input.getKeyBinding(), forced);
}
/**
* A redirection in multiple places of {@link Keyboard#isKeyDown}.
*
* @return Whether or not the specified key is down or overriden.
*/
public boolean isKeyDown(int keyCode) {
return Keyboard.isKeyDown(keyCode) || keyCodeForceStateMap.computeIfAbsent(keyCode, k -> false);
}
/**
* Sets whether or not the specified key code is being forced down.
*
* @param keyCode The key code
* @param forced Whether or not the state is being forced
*/
public final void setKeyForceState(int keyCode, boolean forced) {
keyCodeForceStateMap.put(keyCode, forced);
}
/**
* An {@link Enum} representing the possible inputs that we may want to force.
*/
public enum Input {
/**
* The move forward input
*/
MOVE_FORWARD(mc.gameSettings.keyBindForward),
/**
* The move back input
*/
MOVE_BACK(mc.gameSettings.keyBindBack),
/**
* The move left input
*/
MOVE_LEFT(mc.gameSettings.keyBindLeft),
/**
* The move right input
*/
MOVE_RIGHT(mc.gameSettings.keyBindRight),
/**
* The attack input
*/
CLICK_LEFT(mc.gameSettings.keyBindAttack),
/**
* The use item input
*/
CLICK_RIGHT(mc.gameSettings.keyBindUseItem);
/**
* The actual game {@link KeyBinding} being forced.
*/
private KeyBinding keyBinding;
Input(KeyBinding keyBinding) {
this.keyBinding = keyBinding;
}
/**
* @return The actual game {@link KeyBinding} being forced.
*/
public final KeyBinding getKeyBinding() {
return this.keyBinding;
}
}
}

View File

@ -0,0 +1,24 @@
package baritone.bot;
import net.minecraft.util.math.BlockPos;
/**
* @author Brady
* @since 7/31/2018 10:50 PM
*/
public final class Memory {
public final void scanBlock(BlockPos pos) {
checkActive(() -> {
// We might want to always run this method even if Baritone
// isn't active, this is just an example of the implementation
// of checkActive(Runnable).
});
}
private void checkActive(Runnable runnable) {
if (Baritone.INSTANCE.isActive()) {
runnable.run();
}
}
}

View File

@ -0,0 +1,68 @@
package baritone.bot;
/**
* @author Brady
* @since 7/31/2018 10:50 PM
*/
public enum Baritone {
/**
* Singleton instance of this class
*/
INSTANCE;
/**
* Whether or not {@link Baritone#init()} has been called yet
*/
private boolean initialized;
private Memory memory;
private HookStateManager hookStateManager;
private GameActionHandler actionHandler;
private GameEventHandler gameEventHandler;
private InputOverrideHandler inputOverrideHandler;
/**
* Whether or not Baritone is active
*/
private boolean active;
public void init() {
this.memory = new Memory();
this.hookStateManager = new HookStateManager();
this.actionHandler = new GameActionHandler();
this.gameEventHandler = new GameEventHandler();
this.inputOverrideHandler = new InputOverrideHandler();
this.active = true;
this.initialized = true;
}
public final boolean isInitialized() {
return this.initialized;
}
public final Memory getMemory() {
return this.memory;
}
public final HookStateManager getHookStateManager() {
return this.hookStateManager;
}
public final GameActionHandler getActionHandler() {
return this.actionHandler;
}
public final GameEventHandler getGameEventHandler() {
return this.gameEventHandler;
}
public final InputOverrideHandler getInputOverrideHandler() {
return this.inputOverrideHandler;
}
public final boolean isActive() {
return this.active;
}
}

View File

@ -0,0 +1,15 @@
package baritone.bot.event;
import net.minecraft.client.Minecraft;
/**
* @author Brady
* @since 7/31/2018 11:05 PM
*/
public interface IGameEventListener {
/**
* Run once per game tick from {@link Minecraft#runTick}.
*/
void onTick();
}

View File

@ -0,0 +1,12 @@
package baritone.bot.utils;
import net.minecraft.client.Minecraft;
/**
* @author Brady
* @since 8/1/2018 12:18 AM
*/
public interface Helper {
Minecraft mc = Minecraft.getMinecraft();
}

View File

@ -0,0 +1,15 @@
package baritone.bot.utils;
import java.util.function.Supplier;
/**
* @author Brady
* @since 8/1/2018 12:56 AM
*/
public final class Utils {
public static void ifConditionThen(Supplier<Boolean> condition, Runnable runnable) {
if (condition.get())
runnable.run();
}
}

View File

@ -0,0 +1,54 @@
package baritone.launch;
import net.minecraft.launchwrapper.ITweaker;
import net.minecraft.launchwrapper.LaunchClassLoader;
import org.spongepowered.asm.launch.MixinBootstrap;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.Mixins;
import org.spongepowered.tools.obfuscation.mcp.ObfuscationServiceMCP;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* @author Brady
* @since 7/31/2018 9:59 PM
*/
public class BaritoneTweaker implements ITweaker {
List<String> args;
@Override
public void acceptOptions(List<String> args, File gameDir, File assetsDir, String profile) {
this.args = new ArrayList<>(args);
if (gameDir != null) addArg("gameDir", gameDir.getAbsolutePath());
if (assetsDir != null) addArg("assetsDir", assetsDir.getAbsolutePath());
if (profile != null) addArg("version", profile);
}
@Override
public void injectIntoClassLoader(LaunchClassLoader classLoader) {
MixinBootstrap.init();
MixinEnvironment.getDefaultEnvironment().setSide(MixinEnvironment.Side.CLIENT);
MixinEnvironment.getDefaultEnvironment().setObfuscationContext(ObfuscationServiceMCP.NOTCH);
Mixins.addConfiguration("mixins.baritone.json");
}
@Override
public final String getLaunchTarget() {
return "net.minecraft.client.main.Main";
}
@Override
public final String[] getLaunchArguments() {
return this.args.toArray(new String[0]);
}
private void addArg(String label, String value) {
if (!args.contains("--" + label) && value != null) {
this.args.add("--" + label);
this.args.add(value);
}
}
}

View File

@ -0,0 +1,27 @@
package baritone.launch;
import net.minecraft.launchwrapper.LaunchClassLoader;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.tools.obfuscation.mcp.ObfuscationServiceMCP;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* @author Brady
* @since 7/31/2018 10:09 PM
*/
public class BaritoneTweakerForge extends BaritoneTweaker {
@Override
public final void acceptOptions(List<String> args, File gameDir, File assetsDir, String profile) {
this.args = new ArrayList<>();
}
@Override
public final void injectIntoClassLoader(LaunchClassLoader classLoader) {
super.injectIntoClassLoader(classLoader);
MixinEnvironment.getDefaultEnvironment().setObfuscationContext(ObfuscationServiceMCP.SEARGE);
}
}

View File

@ -0,0 +1,17 @@
package baritone.launch;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* @author Brady
* @since 7/31/2018 10:10 PM
*/
public class BaritoneTweakerOptifine extends BaritoneTweaker {
@Override
public final void acceptOptions(List<String> args, File gameDir, File assetsDir, String profile) {
this.args = new ArrayList<>();
}
}

View File

@ -0,0 +1,26 @@
package baritone.launch.mixins;
import baritone.bot.Baritone;
import net.minecraft.client.settings.GameSettings;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
/**
* @author Brady
* @since 8/1/2018 12:28 AM
*/
@Mixin(GameSettings.class)
public class MixinGameSettings {
@Redirect(
method = "isKeyDown",
at = @At(
value = "INVOKE",
target = "org/lwjgl/input/Keyboard.isKeyDown(I)Z"
)
)
private static boolean isKeyDown(int keyCode) {
return Baritone.INSTANCE.getInputOverrideHandler().isKeyDown(keyCode);
}
}

View File

@ -0,0 +1,29 @@
package baritone.launch.mixins;
import baritone.bot.Baritone;
import net.minecraft.client.gui.inventory.GuiContainer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
/**
* @author Brady
* @since 7/31/2018 10:47 PM
*/
@Mixin(GuiContainer.class)
public class MixinGuiContainer {
@Redirect(
method = {
"mouseClicked",
"mouseReleased"
},
at = @At(
value = "INVOKE",
target = "org/lwjgl/input/Keyboard.isKeyDown(I)Z"
)
)
private boolean isKeyDown(int keyCode) {
return Baritone.INSTANCE.getInputOverrideHandler().isKeyDown(keyCode);
}
}

View File

@ -0,0 +1,50 @@
package baritone.launch.mixins;
import baritone.bot.HookStateManager;
import baritone.bot.Baritone;
import net.minecraft.client.gui.GuiOverlayDebug;
import net.minecraft.client.gui.ScaledResolution;
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;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.List;
/**
* @author Brady
* @since 7/31/2018 10:28 PM
*/
@Mixin(GuiOverlayDebug.class)
public abstract class MixinGuiOverlayDebug {
@Shadow protected abstract void renderDebugInfoRight(ScaledResolution scaledResolution);
@Redirect(
method = "renderDebugInfo",
at = @At(
value = "INVOKE",
target = "net/minecraft/client/gui/GuiOverlayDebug.renderDebugInfoRight(Lnet/minecraft/client/gui/ScaledResolution;)V"
)
)
private void onRenderDebugInfoRight(GuiOverlayDebug gui, ScaledResolution scaledResolution) {
if (!Baritone.INSTANCE.getHookStateManager().shouldCancelDebugRenderRight()) {
this.renderDebugInfoRight(scaledResolution);
}
}
@Inject(
method = "call",
at = @At("HEAD"),
cancellable = true
)
private void call(CallbackInfoReturnable<List<String>> cir) {
HookStateManager hooks = Baritone.INSTANCE.getHookStateManager();
if (hooks.shouldOverrideDebugInfoLeft()) {
cir.setReturnValue(hooks.getDebugInfoLeft());
}
}
}

View File

@ -0,0 +1,30 @@
package baritone.launch.mixins;
import baritone.bot.Baritone;
import net.minecraft.client.gui.GuiScreen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
/**
* @author Brady
* @since 7/31/2018 10:38 PM
*/
@Mixin(GuiScreen.class)
public class MixinGuiScreen {
@Redirect(
method = {
"isCtrlKeyDown",
"isShiftKeyDown",
"isAltKeyDown"
},
at = @At(
value = "INVOKE",
target = "org/lwjgl/input/Keyboard.isKeyDown(I)Z"
)
)
private static boolean isKeyDown(int keyCode) {
return Baritone.INSTANCE.getInputOverrideHandler().isKeyDown(keyCode);
}
}

View File

@ -0,0 +1,55 @@
package baritone.launch.mixins;
import baritone.bot.Baritone;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.util.IntHashMap;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
/**
* @author Brady
* @since 7/31/2018 11:44 PM
*/
@Mixin(KeyBinding.class)
public abstract class MixinKeyBinding {
@Redirect(
method = "onTick",
at = @At(
value = "INVOKE",
target = "net/minecraft/util/IntHashMap.lookup(I)Ljava/lang/Object;"
)
)
private static Object lookup(IntHashMap<KeyBinding> HASH, int keyCode) {
KeyBinding keyBinding = HASH.lookup(keyCode);
// If we're overriding the key state, we don't want to be incrementing the pressTime
if (keyBinding != null && Baritone.INSTANCE.getInputOverrideHandler().isInputForcedDown(keyBinding))
return null;
return keyBinding;
}
@Inject(
method = "isPressed",
at = @At("HEAD"),
cancellable = true
)
private void isPressed(CallbackInfoReturnable<Boolean> cir) {
if (Baritone.INSTANCE.getInputOverrideHandler().isInputForcedDown((KeyBinding) (Object) this))
cir.setReturnValue(true);
}
@Inject(
method = "isKeyDown",
at = @At("HEAD"),
cancellable = true
)
private void isKeyDown(CallbackInfoReturnable<Boolean> cir) {
if (Baritone.INSTANCE.getInputOverrideHandler().isInputForcedDown((KeyBinding) (Object) this))
cir.setReturnValue(true);
}
}

View File

@ -0,0 +1,23 @@
package baritone.launch.mixins;
import net.minecraft.client.main.Main;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
/**
* @author Brady
* @since 7/31/2018 10:18 PM
*/
@Mixin(Main.class)
public class MixinMain {
@Inject(
method = "main",
at = @At("HEAD")
)
private static void main(String[] args, CallbackInfo ci) {
System.setProperty("java.net.preferIPv4Stack", "true");
}
}

View File

@ -0,0 +1,93 @@
package baritone.launch.mixins;
import baritone.bot.Baritone;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.math.BlockPos;
import org.spongepowered.asm.lib.Opcodes;
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;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
/**
* @author Brady
* @since 7/31/2018 10:51 PM
*/
@Mixin(Minecraft.class)
public class MixinMinecraft {
@Shadow private int leftClickCounter;
@Inject(
method = "init",
at = @At("RETURN")
)
private void init(CallbackInfo ci) {
Baritone.INSTANCE.init();
}
@Inject(
method = "runTick",
at = @At(
value = "FIELD",
opcode = Opcodes.GETFIELD,
target = "net/minecraft/client/Minecraft.currentScreen:Lnet/minecraft/client/gui/GuiScreen;",
ordinal = 5,
shift = At.Shift.BY,
by = -3
)
)
private void runTick(CallbackInfo ci) {
Baritone.INSTANCE.getGameEventHandler().onTick();
}
@Redirect(
method = "runTickKeyboard",
at = @At(
value = "INVOKE",
target = "org/lwjgl/input/Keyboard.isKeyDown(I)Z"
)
)
private boolean Keyboard$isKeyDown(int keyCode) {
return Baritone.INSTANCE.getInputOverrideHandler().isKeyDown(keyCode);
}
@Redirect(
method = {
"setIngameFocus",
"runTick"
},
at = @At(
value = "FIELD",
opcode = Opcodes.PUTFIELD,
target = "net/minecraft/client/Minecraft.leftClickCounter:I",
ordinal = 0
)
)
private void setLeftClickCounter(Minecraft mc, int value) {
if (!Baritone.INSTANCE.isInitialized() || !Baritone.INSTANCE.getInputOverrideHandler().isInputForcedDown(mc.gameSettings.keyBindAttack))
this.leftClickCounter = value;
}
@Inject(
method = "rightClickMouse",
at = @At(
value = "INVOKE_ASSIGN",
target = "net/minecraft/client/entity/EntityPlayerSP.swingArm(Lnet/minecraft/util/EnumHand;)V"
),
locals = LocalCapture.CAPTURE_FAILHARD
)
private void postSwingArm(CallbackInfo ci, ItemStack stack, BlockPos pos, int stackCount, EnumActionResult result) {
Minecraft mc = (Minecraft) (Object) this;
Baritone bot = Baritone.INSTANCE;
bot.getMemory().scanBlock(pos);
bot.getMemory().scanBlock(pos.offset(mc.objectMouseOver.sideHit));
bot.getActionHandler().onPlacedBlock(stack, pos);
}
}

View File

@ -0,0 +1,16 @@
{
"required": true,
"package": "baritone.launch.mixins",
"refmap": "mixins.client.refmap.json",
"compatibilityLevel": "JAVA_8",
"verbose": false,
"client": [
"MixinGameSettings",
"MixinGuiContainer",
"MixinGuiOverlayDebug",
"MixinGuiScreen",
"MixinKeyBinding",
"MixinMain",
"MixinMinecraft"
]
}