From b12c2ea62f5f1eefe149d8c96afcf4ebdf42fc3b Mon Sep 17 00:00:00 2001 From: Brady Date: Sat, 29 Sep 2018 23:52:01 -0500 Subject: [PATCH 1/5] UTILIZE Service Loaders https://i.imgur.com/kAm0xd3.png --- src/api/java/baritone/api/BaritoneAPI.java | 62 ++++++----------- .../java/baritone/api/IBaritoneProvider.java | 42 ++++++++++++ src/main/java/baritone/Baritone.java | 12 ---- src/main/java/baritone/BaritoneProvider.java | 67 +++++++++++++++++++ .../services/baritone.api.IBaritoneProvider | 1 + 5 files changed, 129 insertions(+), 55 deletions(-) create mode 100644 src/api/java/baritone/api/IBaritoneProvider.java create mode 100644 src/main/java/baritone/BaritoneProvider.java create mode 100644 src/main/resources/META-INF/services/baritone.api.IBaritoneProvider diff --git a/src/api/java/baritone/api/BaritoneAPI.java b/src/api/java/baritone/api/BaritoneAPI.java index aa9491db..821c4fab 100644 --- a/src/api/java/baritone/api/BaritoneAPI.java +++ b/src/api/java/baritone/api/BaritoneAPI.java @@ -20,6 +20,9 @@ package baritone.api; import baritone.api.behavior.*; import baritone.api.cache.IWorldProvider; +import java.util.Iterator; +import java.util.ServiceLoader; + /** * API exposure for various things implemented in Baritone. *

@@ -30,35 +33,36 @@ import baritone.api.cache.IWorldProvider; */ public class BaritoneAPI { - // General - private static final Settings settings = new Settings(); - private static IWorldProvider worldProvider; + private static IBaritoneProvider baritone; + private static Settings settings; - // Behaviors - private static IFollowBehavior followBehavior; - private static ILookBehavior lookBehavior; - private static IMemoryBehavior memoryBehavior; - private static IMineBehavior mineBehavior; - private static IPathingBehavior pathingBehavior; + static { + ServiceLoader baritoneLoader = ServiceLoader.load(IBaritoneProvider.class); + Iterator instances = baritoneLoader.iterator(); + if (instances.hasNext()) + baritone = instances.next(); + + settings = new Settings(); + } public static IFollowBehavior getFollowBehavior() { - return followBehavior; + return baritone.getFollowBehavior(); } public static ILookBehavior getLookBehavior() { - return lookBehavior; + return baritone.getLookBehavior(); } public static IMemoryBehavior getMemoryBehavior() { - return memoryBehavior; + return baritone.getMemoryBehavior(); } public static IMineBehavior getMineBehavior() { - return mineBehavior; + return baritone.getMineBehavior(); } public static IPathingBehavior getPathingBehavior() { - return pathingBehavior; + return baritone.getPathingBehavior(); } public static Settings getSettings() { @@ -66,34 +70,6 @@ public class BaritoneAPI { } public static IWorldProvider getWorldProvider() { - return worldProvider; + return baritone.getWorldProvider(); } - - /** - * FOR INTERNAL USE ONLY - */ - public static void registerProviders( - IWorldProvider worldProvider - ) { - BaritoneAPI.worldProvider = worldProvider; - } - - /** - * FOR INTERNAL USE ONLY - */ - // @formatter:off - public static void registerDefaultBehaviors( - IFollowBehavior followBehavior, - ILookBehavior lookBehavior, - IMemoryBehavior memoryBehavior, - IMineBehavior mineBehavior, - IPathingBehavior pathingBehavior - ) { - BaritoneAPI.followBehavior = followBehavior; - BaritoneAPI.lookBehavior = lookBehavior; - BaritoneAPI.memoryBehavior = memoryBehavior; - BaritoneAPI.mineBehavior = mineBehavior; - BaritoneAPI.pathingBehavior = pathingBehavior; - } - // @formatter:on } diff --git a/src/api/java/baritone/api/IBaritoneProvider.java b/src/api/java/baritone/api/IBaritoneProvider.java new file mode 100644 index 00000000..a1ad99a2 --- /dev/null +++ b/src/api/java/baritone/api/IBaritoneProvider.java @@ -0,0 +1,42 @@ +/* + * 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 . + */ + +package baritone.api; + +import baritone.api.behavior.*; +import baritone.api.cache.IWorldProvider; + +/** + * @author Brady + * @since 9/29/2018 + */ +public interface IBaritoneProvider { + + IFollowBehavior getFollowBehavior(); + + ILookBehavior getLookBehavior(); + + IMemoryBehavior getMemoryBehavior(); + + IMineBehavior getMineBehavior(); + + IPathingBehavior getPathingBehavior(); + + Settings getSettings(); + + IWorldProvider getWorldProvider(); +} diff --git a/src/main/java/baritone/Baritone.java b/src/main/java/baritone/Baritone.java index 4a764bcc..b7904d00 100755 --- a/src/main/java/baritone/Baritone.java +++ b/src/main/java/baritone/Baritone.java @@ -88,8 +88,6 @@ public enum Baritone { // We might want to change this... this.settings = BaritoneAPI.getSettings(); - BaritoneAPI.registerProviders(WorldProvider.INSTANCE); - this.behaviors = new ArrayList<>(); { registerBehavior(PathingBehavior.INSTANCE); @@ -98,16 +96,6 @@ public enum Baritone { registerBehavior(LocationTrackingBehavior.INSTANCE); registerBehavior(FollowBehavior.INSTANCE); registerBehavior(MineBehavior.INSTANCE); - - // TODO: Clean this up - // Maybe combine this call in someway with the registerBehavior calls? - BaritoneAPI.registerDefaultBehaviors( - FollowBehavior.INSTANCE, - LookBehavior.INSTANCE, - MemoryBehavior.INSTANCE, - MineBehavior.INSTANCE, - PathingBehavior.INSTANCE - ); } if (BaritoneAutoTest.ENABLE_AUTO_TEST) { registerEventListener(BaritoneAutoTest.INSTANCE); diff --git a/src/main/java/baritone/BaritoneProvider.java b/src/main/java/baritone/BaritoneProvider.java new file mode 100644 index 00000000..039d09a5 --- /dev/null +++ b/src/main/java/baritone/BaritoneProvider.java @@ -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 . + */ + +package baritone; + +import baritone.api.IBaritoneProvider; +import baritone.api.Settings; +import baritone.api.behavior.*; +import baritone.api.cache.IWorldProvider; +import baritone.behavior.*; +import baritone.cache.WorldProvider; + +/** + * @author Brady + * @since 9/29/2018 + */ +public final class BaritoneProvider implements IBaritoneProvider { + + @Override + public IFollowBehavior getFollowBehavior() { + return FollowBehavior.INSTANCE; + } + + @Override + public ILookBehavior getLookBehavior() { + return LookBehavior.INSTANCE; + } + + @Override + public IMemoryBehavior getMemoryBehavior() { + return MemoryBehavior.INSTANCE; + } + + @Override + public IMineBehavior getMineBehavior() { + return MineBehavior.INSTANCE; + } + + @Override + public IPathingBehavior getPathingBehavior() { + return PathingBehavior.INSTANCE; + } + + @Override + public Settings getSettings() { + return Baritone.settings(); + } + + @Override + public IWorldProvider getWorldProvider() { + return WorldProvider.INSTANCE; + } +} diff --git a/src/main/resources/META-INF/services/baritone.api.IBaritoneProvider b/src/main/resources/META-INF/services/baritone.api.IBaritoneProvider new file mode 100644 index 00000000..934db7d3 --- /dev/null +++ b/src/main/resources/META-INF/services/baritone.api.IBaritoneProvider @@ -0,0 +1 @@ +baritone.BaritoneProvider \ No newline at end of file From 3184eaf595015da9eb37bfc2c47360e55fb06fb1 Mon Sep 17 00:00:00 2001 From: Brady Date: Sat, 29 Sep 2018 23:59:32 -0500 Subject: [PATCH 2/5] Remove IBaritoneProvider#getSettings --- src/api/java/baritone/api/IBaritoneProvider.java | 2 -- src/main/java/baritone/BaritoneProvider.java | 6 ------ 2 files changed, 8 deletions(-) diff --git a/src/api/java/baritone/api/IBaritoneProvider.java b/src/api/java/baritone/api/IBaritoneProvider.java index a1ad99a2..03b457bb 100644 --- a/src/api/java/baritone/api/IBaritoneProvider.java +++ b/src/api/java/baritone/api/IBaritoneProvider.java @@ -36,7 +36,5 @@ public interface IBaritoneProvider { IPathingBehavior getPathingBehavior(); - Settings getSettings(); - IWorldProvider getWorldProvider(); } diff --git a/src/main/java/baritone/BaritoneProvider.java b/src/main/java/baritone/BaritoneProvider.java index 039d09a5..370a398e 100644 --- a/src/main/java/baritone/BaritoneProvider.java +++ b/src/main/java/baritone/BaritoneProvider.java @@ -18,7 +18,6 @@ package baritone; import baritone.api.IBaritoneProvider; -import baritone.api.Settings; import baritone.api.behavior.*; import baritone.api.cache.IWorldProvider; import baritone.behavior.*; @@ -55,11 +54,6 @@ public final class BaritoneProvider implements IBaritoneProvider { return PathingBehavior.INSTANCE; } - @Override - public Settings getSettings() { - return Baritone.settings(); - } - @Override public IWorldProvider getWorldProvider() { return WorldProvider.INSTANCE; From d58ad6f68f1bbe6503ace5cd709888adcae4fc9f Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 30 Sep 2018 08:03:33 -0700 Subject: [PATCH 3/5] finalize --- src/api/java/baritone/api/BaritoneAPI.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/api/java/baritone/api/BaritoneAPI.java b/src/api/java/baritone/api/BaritoneAPI.java index 821c4fab..dec7ec13 100644 --- a/src/api/java/baritone/api/BaritoneAPI.java +++ b/src/api/java/baritone/api/BaritoneAPI.java @@ -31,16 +31,15 @@ import java.util.ServiceLoader; * @author Brady * @since 9/23/2018 */ -public class BaritoneAPI { +public final class BaritoneAPI { - private static IBaritoneProvider baritone; - private static Settings settings; + private final static IBaritoneProvider baritone; + private final static Settings settings; static { ServiceLoader baritoneLoader = ServiceLoader.load(IBaritoneProvider.class); Iterator instances = baritoneLoader.iterator(); - if (instances.hasNext()) - baritone = instances.next(); + baritone = instances.next(); settings = new Settings(); } From 09ac3c615cbfa10ced9c18a8befe610d9546e2ef Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 30 Sep 2018 09:14:23 -0700 Subject: [PATCH 4/5] more time maybe --- src/main/java/baritone/utils/BaritoneAutoTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/baritone/utils/BaritoneAutoTest.java b/src/main/java/baritone/utils/BaritoneAutoTest.java index 53cd55fe..27b9bfb3 100644 --- a/src/main/java/baritone/utils/BaritoneAutoTest.java +++ b/src/main/java/baritone/utils/BaritoneAutoTest.java @@ -41,7 +41,7 @@ public class BaritoneAutoTest implements AbstractGameEventListener, Helper { private static final long TEST_SEED = -928872506371745L; private static final BlockPos STARTING_POSITION = new BlockPos(0, 65, 0); private static final Goal GOAL = new GoalBlock(69, 121, 420); - private static final int MAX_TICKS = 3700; + private static final int MAX_TICKS = 4000; /** * Called right after the {@link GameSettings} object is created in the {@link Minecraft} instance. From 08b9eab9f3a3fc8f39837547a2cf1c0a24405c0d Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 30 Sep 2018 21:32:13 -0700 Subject: [PATCH 5/5] draw most recent node considered even if there is no valid best --- src/main/java/baritone/behavior/PathingBehavior.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 377af7f3..c212ac5d 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -24,10 +24,11 @@ import baritone.api.event.events.PlayerUpdateEvent; import baritone.api.event.events.RenderEvent; import baritone.api.event.events.TickEvent; import baritone.api.pathing.goals.Goal; +import baritone.api.pathing.goals.GoalXZ; +import baritone.api.utils.interfaces.IGoalRenderPos; import baritone.pathing.calc.AStarPathFinder; import baritone.pathing.calc.AbstractNodeCostSearch; import baritone.pathing.calc.IPathFinder; -import baritone.api.pathing.goals.GoalXZ; import baritone.pathing.movement.MovementHelper; import baritone.pathing.path.IPath; import baritone.pathing.path.PathExecutor; @@ -35,7 +36,6 @@ import baritone.utils.BlockBreakHelper; import baritone.utils.BlockStateInterface; import baritone.utils.Helper; import baritone.utils.PathRenderer; -import baritone.api.utils.interfaces.IGoalRenderPos; import baritone.utils.pathing.BetterBlockPos; import net.minecraft.init.Blocks; import net.minecraft.util.math.BlockPos; @@ -420,11 +420,11 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, AbstractNodeCostSearch.getCurrentlyRunning().ifPresent(currentlyRunning -> { currentlyRunning.bestPathSoFar().ifPresent(p -> { PathRenderer.drawPath(p, 0, player(), partialTicks, Baritone.settings().colorBestPathSoFar.get(), Baritone.settings().fadePath.get(), 10, 20); - currentlyRunning.pathToMostRecentNodeConsidered().ifPresent(mr -> { + }); + currentlyRunning.pathToMostRecentNodeConsidered().ifPresent(mr -> { - PathRenderer.drawPath(mr, 0, player(), partialTicks, Baritone.settings().colorMostRecentConsidered.get(), Baritone.settings().fadePath.get(), 10, 20); - PathRenderer.drawManySelectionBoxes(player(), Collections.singletonList(mr.getDest()), partialTicks, Baritone.settings().colorMostRecentConsidered.get()); - }); + PathRenderer.drawPath(mr, 0, player(), partialTicks, Baritone.settings().colorMostRecentConsidered.get(), Baritone.settings().fadePath.get(), 10, 20); + PathRenderer.drawManySelectionBoxes(player(), Collections.singletonList(mr.getDest()), partialTicks, Baritone.settings().colorMostRecentConsidered.get()); }); }); //long end = System.nanoTime();