diff --git a/src/main/java/baritone/bot/Baritone.java b/src/main/java/baritone/bot/Baritone.java index 3bc352b9..e13cbade 100755 --- a/src/main/java/baritone/bot/Baritone.java +++ b/src/main/java/baritone/bot/Baritone.java @@ -22,6 +22,7 @@ import baritone.bot.behavior.impl.LookBehavior; import baritone.bot.behavior.impl.MemoryBehavior; import baritone.bot.behavior.impl.PathingBehavior; import baritone.bot.event.GameEventHandler; +import baritone.bot.utils.InputOverrideHandler; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/baritone/bot/Settings.java b/src/main/java/baritone/bot/Settings.java index ad93d7fe..0c135370 100644 --- a/src/main/java/baritone/bot/Settings.java +++ b/src/main/java/baritone/bot/Settings.java @@ -17,25 +17,98 @@ package baritone.bot; +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.util.*; + /** * Baritone's settings * * @author leijurv */ public class Settings { - public boolean allowBreak = true; - public boolean allowPlaceThrowaway = true; - public double costHeuristic = 4; - public boolean chuckCaching = false; - public boolean allowWaterBucketFall = true; - public int planningTickLookAhead = 150; - public boolean renderPath = true; - public boolean chatDebug = true; - public boolean chatControl = true; - public boolean fadePath = true; - public boolean slowPath = false; + public Setting allowBreak = new Setting<>(true); + public Setting allowPlaceThrowaway = new Setting<>(true); + public Setting costHeuristic = new Setting(4D); + public Setting chuckCaching = new Setting<>(false); + public Setting allowWaterBucketFall = new Setting<>(true); + public Setting planningTickLookAhead = new Setting<>(150); + public Setting renderPath = new Setting<>(true); + public Setting chatDebug = new Setting<>(true); + public Setting chatControl = new Setting<>(true); // probably false in impact + public Setting fadePath = new Setting<>(false); // give this a better name in the UI, like "better path fps" idk + public Setting slowPath = new Setting<>(false); - Settings() { + public final Map> byName; + public final List> allSettings; + public class Setting { + public T value; + private String name; + private Class klass; + + private Setting(V value) { + this.value = value; + } + + public final T get() { + return value; + } + + public final String getName() { + return name; + } + + public String toString() { + return name + ": " + value; + } } + + // here be dragons + + { + Field[] temp = getClass().getFields(); + HashMap> tmpByName = new HashMap<>(); + List> tmpAll = new ArrayList<>(); + for (Field field : temp) { + if (field.getType().equals(Setting.class)) { + try { + ParameterizedType param = (ParameterizedType) field.getGenericType(); + Class settingType = (Class) param.getActualTypeArguments()[0]; + // can't always do field.get(this).value.getClass() because default value might be null + Setting setting = (Setting) field.get(this); + if (setting.value != null) { + if (setting.value.getClass() != settingType) { + throw new IllegalStateException("Generic mismatch" + setting.value + " " + setting.value.getClass() + " " + settingType); + } + } + String name = field.getName(); + setting.name = name; + setting.klass = settingType; + if (tmpByName.containsKey(name)) { + throw new IllegalStateException("Duplicate setting name"); + } + tmpByName.put(name, setting); + tmpAll.add(setting); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + + } + } + byName = Collections.unmodifiableMap(tmpByName); + allSettings = Collections.unmodifiableList(tmpAll); + } + + public List> getByValueType(Class klass) { + ArrayList> result = new ArrayList<>(); + for (Setting setting : allSettings) { + if (setting.klass.equals(klass)) { + result.add((Setting) setting); + } + } + return result; + } + + Settings() { } } diff --git a/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java b/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java index 53b0e036..6e848884 100644 --- a/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java +++ b/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java @@ -122,7 +122,7 @@ public class PathingBehavior extends Behavior { // and this path dosen't get us all the way there return; } - if (current.getPath().ticksRemainingFrom(current.getPosition()) < Baritone.settings().planningTickLookAhead) { + if (current.getPath().ticksRemainingFrom(current.getPosition()) < Baritone.settings().planningTickLookAhead.get()) { // and this path has 5 seconds or less left displayChatMessageRaw("Path almost over. Planning ahead..."); findPathInNewThread(current.getPath().getDest(), false); @@ -231,7 +231,7 @@ public class PathingBehavior extends Behavior { @Override public void onRenderPass(RenderEvent event) { - if (!Baritone.settings().renderPath) { + if (!Baritone.settings().renderPath.get()) { return; } // System.out.println("Render passing"); @@ -246,10 +246,10 @@ public class PathingBehavior extends Behavior { // Render the current path, if there is one if (current != null && current.getPath() != null) { int renderBegin = Math.max(current.getPosition() - 3, 0); - PathRenderer.drawPath(current.getPath(), renderBegin, player(), partialTicks, Color.RED, Baritone.settings().fadePath, 10, 20); + PathRenderer.drawPath(current.getPath(), renderBegin, player(), partialTicks, Color.RED, Baritone.settings().fadePath.get(), 10, 20); } if (next != null && next.getPath() != null) { - PathRenderer.drawPath(next.getPath(), 0, player(), partialTicks, Color.GREEN, Baritone.settings().fadePath, 10, 20); + PathRenderer.drawPath(next.getPath(), 0, player(), partialTicks, Color.GREEN, Baritone.settings().fadePath.get(), 10, 20); } long split = System.nanoTime(); @@ -262,10 +262,10 @@ public class PathingBehavior extends Behavior { // If there is a path calculation currently running, render the path calculation process AbstractNodeCostSearch.getCurrentlyRunning().ifPresent(currentlyRunning -> { currentlyRunning.bestPathSoFar().ifPresent(p -> { - PathRenderer.drawPath(p, 0, player(), partialTicks, Color.BLUE, Baritone.settings().fadePath, 10, 20); + PathRenderer.drawPath(p, 0, player(), partialTicks, Color.BLUE, Baritone.settings().fadePath.get(), 10, 20); currentlyRunning.pathToMostRecentNodeConsidered().ifPresent(mr -> { - PathRenderer.drawPath(mr, 0, player(), partialTicks, Color.CYAN, Baritone.settings().fadePath, 10, 20); + PathRenderer.drawPath(mr, 0, player(), partialTicks, Color.CYAN, Baritone.settings().fadePath.get(), 10, 20); PathRenderer.drawManySelectionBoxes(player(), Collections.singletonList(mr.getDest()), partialTicks, Color.CYAN); }); }); diff --git a/src/main/java/baritone/bot/event/GameEventHandler.java b/src/main/java/baritone/bot/event/GameEventHandler.java index ea151c2e..32bcde8c 100644 --- a/src/main/java/baritone/bot/event/GameEventHandler.java +++ b/src/main/java/baritone/bot/event/GameEventHandler.java @@ -35,15 +35,15 @@ package baritone.bot.event; import baritone.bot.Baritone; -import baritone.bot.InputOverrideHandler; import baritone.bot.behavior.Behavior; import baritone.bot.chunk.CachedWorld; import baritone.bot.chunk.CachedWorldProvider; import baritone.bot.chunk.ChunkPacker; -import baritone.bot.event.listener.IGameEventListener; import baritone.bot.event.events.*; import baritone.bot.event.events.type.EventState; +import baritone.bot.event.listener.IGameEventListener; import baritone.bot.utils.Helper; +import baritone.bot.utils.InputOverrideHandler; import net.minecraft.client.renderer.BufferBuilder; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.renderer.Tessellator; @@ -108,7 +108,7 @@ public final class GameEventHandler implements IGameEventListener, Helper { && type == ChunkEvent.Type.UNLOAD && mc.world.getChunkProvider().isChunkGeneratedAt(event.getX(), event.getZ()); - if (Baritone.settings().chuckCaching) { + if (Baritone.settings().chuckCaching.get()) { if (isPostPopulate || isPreUnload) { CachedWorldProvider.INSTANCE.ifWorldLoaded(world -> world.updateCachedChunk(event.getX(), event.getZ(), @@ -132,7 +132,7 @@ public final class GameEventHandler implements IGameEventListener, Helper { @Override public void onWorldEvent(WorldEvent event) { - if (Baritone.settings().chuckCaching) { + if (Baritone.settings().chuckCaching.get()) { CachedWorldProvider cache = CachedWorldProvider.INSTANCE; switch (event.getState()) { diff --git a/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java b/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java index 93025a53..694b05a3 100644 --- a/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java +++ b/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java @@ -81,14 +81,15 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper { } currentlyRunning = this; long startTime = System.currentTimeMillis(); - long timeoutTime = startTime + (Baritone.settings().slowPath ? 40000 : 4000); + boolean slowPath = Baritone.settings().slowPath.get(); + long timeoutTime = startTime + (slowPath ? 40000 : 4000); long lastPrintout = 0; int numNodes = 0; CalculationContext calcContext = new CalculationContext(); int numEmptyChunk = 0; - boolean cache = Baritone.settings().chuckCaching; + boolean cache = Baritone.settings().chuckCaching.get(); while (!openSet.isEmpty() && numEmptyChunk < 50 && System.currentTimeMillis() < timeoutTime) { - if (Baritone.settings().slowPath) { + if (slowPath) { try { Thread.sleep(100); } catch (InterruptedException ex) { diff --git a/src/main/java/baritone/bot/pathing/goals/GoalXZ.java b/src/main/java/baritone/bot/pathing/goals/GoalXZ.java index acc8592c..ca70c951 100644 --- a/src/main/java/baritone/bot/pathing/goals/GoalXZ.java +++ b/src/main/java/baritone/bot/pathing/goals/GoalXZ.java @@ -99,7 +99,7 @@ public class GoalXZ implements Goal { diagonal = z; } diagonal *= SQRT_2; - return (diagonal + straight) * Baritone.settings().costHeuristic; // big TODO tune + return (diagonal + straight) * Baritone.settings().costHeuristic.get(); // big TODO tune } public static GoalXZ fromDirection(Vec3d origin, float yaw, double distance) { diff --git a/src/main/java/baritone/bot/pathing/movement/CalculationContext.java b/src/main/java/baritone/bot/pathing/movement/CalculationContext.java index 6c171475..92a9de9f 100644 --- a/src/main/java/baritone/bot/pathing/movement/CalculationContext.java +++ b/src/main/java/baritone/bot/pathing/movement/CalculationContext.java @@ -42,8 +42,8 @@ public class CalculationContext implements Helper { public CalculationContext(ToolSet toolSet) { this.toolSet = toolSet; - this.hasWaterBucket = Baritone.settings().allowWaterBucketFall && InventoryPlayer.isHotbar(player().inventory.getSlotFor(STACK_BUCKET_WATER)) && !world().provider.isNether(); - this.hasThrowaway = Baritone.settings().allowPlaceThrowaway && MovementHelper.throwaway(false); + this.hasWaterBucket = Baritone.settings().allowWaterBucketFall.get() && InventoryPlayer.isHotbar(player().inventory.getSlotFor(STACK_BUCKET_WATER)) && !world().provider.isNether(); + this.hasThrowaway = Baritone.settings().allowPlaceThrowaway.get() && MovementHelper.throwaway(false); } public ToolSet getToolSet() { diff --git a/src/main/java/baritone/bot/pathing/movement/Movement.java b/src/main/java/baritone/bot/pathing/movement/Movement.java index 9db706b7..7b79fc4f 100644 --- a/src/main/java/baritone/bot/pathing/movement/Movement.java +++ b/src/main/java/baritone/bot/pathing/movement/Movement.java @@ -37,7 +37,7 @@ import net.minecraft.util.math.Vec3d; import java.util.ArrayList; import java.util.Optional; -import static baritone.bot.InputOverrideHandler.Input; +import static baritone.bot.utils.InputOverrideHandler.Input; public abstract class Movement implements Helper, MovementHelper { diff --git a/src/main/java/baritone/bot/pathing/movement/MovementHelper.java b/src/main/java/baritone/bot/pathing/movement/MovementHelper.java index 5a5732a3..4872e2fa 100644 --- a/src/main/java/baritone/bot/pathing/movement/MovementHelper.java +++ b/src/main/java/baritone/bot/pathing/movement/MovementHelper.java @@ -18,7 +18,6 @@ package baritone.bot.pathing.movement; import baritone.bot.Baritone; -import baritone.bot.InputOverrideHandler; import baritone.bot.behavior.impl.LookBehaviorUtils; import baritone.bot.pathing.movement.MovementState.MovementTarget; import baritone.bot.pathing.movement.movements.MovementDescend; @@ -137,7 +136,7 @@ public interface MovementHelper extends ActionCosts, Helper { IBlockState state = BlockStateInterface.get(position); Block block = state.getBlock(); if (!block.equals(Blocks.AIR) && !canWalkThrough(position)) { - if (!Baritone.settings().allowBreak) { + if (!Baritone.settings().allowBreak.get()) { return COST_INF; } if (avoidBreaking(position)) { diff --git a/src/main/java/baritone/bot/pathing/movement/MovementState.java b/src/main/java/baritone/bot/pathing/movement/MovementState.java index 9dea192d..efc56c08 100644 --- a/src/main/java/baritone/bot/pathing/movement/MovementState.java +++ b/src/main/java/baritone/bot/pathing/movement/MovementState.java @@ -17,7 +17,7 @@ package baritone.bot.pathing.movement; -import baritone.bot.InputOverrideHandler.Input; +import baritone.bot.utils.InputOverrideHandler.Input; import baritone.bot.utils.Rotation; import net.minecraft.util.math.Vec3d; diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java index b021e19f..7d68608c 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java @@ -17,7 +17,7 @@ package baritone.bot.pathing.movement.movements; -import baritone.bot.InputOverrideHandler; +import baritone.bot.utils.InputOverrideHandler; import baritone.bot.behavior.impl.LookBehaviorUtils; import baritone.bot.pathing.movement.CalculationContext; import baritone.bot.pathing.movement.Movement; diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementDescend.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementDescend.java index 2b25a191..fa2381e2 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementDescend.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementDescend.java @@ -17,7 +17,7 @@ package baritone.bot.pathing.movement.movements; -import baritone.bot.InputOverrideHandler; +import baritone.bot.utils.InputOverrideHandler; import baritone.bot.pathing.movement.CalculationContext; import baritone.bot.pathing.movement.Movement; import baritone.bot.pathing.movement.MovementHelper; diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementFall.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementFall.java index 592440ff..d1312a39 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementFall.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementFall.java @@ -17,7 +17,7 @@ package baritone.bot.pathing.movement.movements; -import baritone.bot.InputOverrideHandler; +import baritone.bot.utils.InputOverrideHandler; import baritone.bot.behavior.impl.LookBehaviorUtils; import baritone.bot.pathing.movement.*; import baritone.bot.pathing.movement.MovementState.MovementStatus; diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementPillar.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementPillar.java index eace641e..d8d68c87 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementPillar.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementPillar.java @@ -17,7 +17,7 @@ package baritone.bot.pathing.movement.movements; -import baritone.bot.InputOverrideHandler; +import baritone.bot.utils.InputOverrideHandler; import baritone.bot.pathing.movement.CalculationContext; import baritone.bot.pathing.movement.Movement; import baritone.bot.pathing.movement.MovementHelper; diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java index fb2cba39..92d147e2 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java @@ -17,7 +17,7 @@ package baritone.bot.pathing.movement.movements; -import baritone.bot.InputOverrideHandler; +import baritone.bot.utils.InputOverrideHandler; import baritone.bot.behavior.impl.LookBehaviorUtils; import baritone.bot.pathing.movement.CalculationContext; import baritone.bot.pathing.movement.Movement; diff --git a/src/main/java/baritone/bot/utils/BlockStateInterface.java b/src/main/java/baritone/bot/utils/BlockStateInterface.java index 7ab42c1d..cddd7351 100644 --- a/src/main/java/baritone/bot/utils/BlockStateInterface.java +++ b/src/main/java/baritone/bot/utils/BlockStateInterface.java @@ -41,7 +41,7 @@ public class BlockStateInterface implements Helper { if (chunk.isLoaded()) { return chunk.getBlockState(pos); } - if(Baritone.settings().chuckCaching) { + if (Baritone.settings().chuckCaching.get()) { CachedWorld world = CachedWorldProvider.INSTANCE.getCurrentWorld(); if (world != null) { PathingBlockType type = world.getBlockType(pos); diff --git a/src/main/java/baritone/bot/utils/ExampleBaritoneControl.java b/src/main/java/baritone/bot/utils/ExampleBaritoneControl.java index d4865449..aaf59b05 100644 --- a/src/main/java/baritone/bot/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/bot/utils/ExampleBaritoneControl.java @@ -18,6 +18,7 @@ package baritone.bot.utils; import baritone.bot.Baritone; +import baritone.bot.Settings; import baritone.bot.behavior.Behavior; import baritone.bot.behavior.impl.PathingBehavior; import baritone.bot.event.events.ChatEvent; @@ -27,6 +28,8 @@ import baritone.bot.pathing.goals.GoalXZ; import baritone.bot.pathing.goals.GoalYLevel; import net.minecraft.util.math.BlockPos; +import java.util.List; + public class ExampleBaritoneControl extends Behavior { public static ExampleBaritoneControl INSTANCE = new ExampleBaritoneControl(); @@ -40,7 +43,7 @@ public class ExampleBaritoneControl extends Behavior { @Override public void onSendChatMessage(ChatEvent event) { - if (!Baritone.settings().chatControl) { + if (!Baritone.settings().chatControl.get()) { return; } String msg = event.getMessage(); @@ -82,11 +85,6 @@ public class ExampleBaritoneControl extends Behavior { event.cancel(); return; } - if (msg.toLowerCase().equals("slowpath")) { - Baritone.settings().slowPath ^= true; - event.cancel(); - return; - } if (msg.toLowerCase().equals("cancel")) { PathingBehavior.INSTANCE.cancel(); event.cancel(); @@ -100,5 +98,14 @@ public class ExampleBaritoneControl extends Behavior { event.cancel(); return; } + List> toggleable = Baritone.settings().getByValueType(Boolean.class); + for (Settings.Setting setting : toggleable) { + if (msg.toLowerCase().equals(setting.getName().toLowerCase())) { + setting.value ^= true; + event.cancel(); + displayChatMessageRaw("Toggled " + setting.getName() + " to " + setting.value); + return; + } + } } } diff --git a/src/main/java/baritone/bot/utils/Helper.java b/src/main/java/baritone/bot/utils/Helper.java index 4f2ef227..8571f23e 100755 --- a/src/main/java/baritone/bot/utils/Helper.java +++ b/src/main/java/baritone/bot/utils/Helper.java @@ -68,7 +68,7 @@ public interface Helper { } default void displayChatMessageRaw(String message) { - if (!Baritone.settings().chatDebug) { + if (!Baritone.settings().chatDebug.get()) { System.out.println("Suppressed debug message:"); System.out.println(message); return; diff --git a/src/main/java/baritone/bot/InputOverrideHandler.java b/src/main/java/baritone/bot/utils/InputOverrideHandler.java similarity index 82% rename from src/main/java/baritone/bot/InputOverrideHandler.java rename to src/main/java/baritone/bot/utils/InputOverrideHandler.java index 63f3bb39..99e59e3f 100755 --- a/src/main/java/baritone/bot/InputOverrideHandler.java +++ b/src/main/java/baritone/bot/utils/InputOverrideHandler.java @@ -15,9 +15,25 @@ * along with Baritone. If not, see . */ -package baritone.bot; +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Baritone. If not, see . + */ + +package baritone.bot.utils; -import baritone.bot.utils.Helper; import net.minecraft.client.settings.KeyBinding; import org.lwjgl.input.Keyboard; @@ -34,7 +50,7 @@ import java.util.Map; */ public final class InputOverrideHandler implements Helper { - InputOverrideHandler() {} + public InputOverrideHandler() {} /** * Maps keybinds to whether or not we are forcing their state down. diff --git a/src/main/resources/baritone/movement/MovementManager.java b/src/main/resources/baritone/movement/MovementManager.java index 704cc929..357f7514 100644 --- a/src/main/resources/baritone/movement/MovementManager.java +++ b/src/main/resources/baritone/movement/MovementManager.java @@ -1,7 +1,7 @@ package baritone.movement; import baritone.Baritone; -import baritone.bot.InputOverrideHandler; +import baritone.bot.utils.InputOverrideHandler; import baritone.ui.LookManager; import net.minecraft.block.BlockLadder; import net.minecraft.block.BlockVine;