diff --git a/src/api/java/baritone/api/BaritoneAPI.java b/src/api/java/baritone/api/BaritoneAPI.java index bfdf60ff..75acf6d1 100644 --- a/src/api/java/baritone/api/BaritoneAPI.java +++ b/src/api/java/baritone/api/BaritoneAPI.java @@ -18,6 +18,7 @@ package baritone.api; import baritone.api.behavior.*; +import baritone.api.cache.IWorldProvider; /** * API exposure for various things implemented in Baritone. @@ -31,6 +32,7 @@ public class BaritoneAPI { // General private static final Settings settings = new Settings(); + private static IWorldProvider worldProvider; // Behaviors private static IFollowBehavior followBehavior; @@ -63,6 +65,15 @@ public class BaritoneAPI { return settings; } + /** + * FOR INTERNAL USE ONLY + */ + public static void registerProviders( + IWorldProvider worldProvider + ) { + BaritoneAPI.worldProvider = worldProvider; + } + /** * FOR INTERNAL USE ONLY */ diff --git a/src/api/java/baritone/api/cache/IWaypointCollection.java b/src/api/java/baritone/api/cache/IWaypointCollection.java index 23450eeb..051b199e 100644 --- a/src/api/java/baritone/api/cache/IWaypointCollection.java +++ b/src/api/java/baritone/api/cache/IWaypointCollection.java @@ -25,13 +25,44 @@ import java.util.Set; */ public interface IWaypointCollection { + /** + * Adds a waypoint to this collection + * + * @param waypoint The waypoint + */ void addWaypoint(IWaypoint waypoint); + /** + * Removes a waypoint from this collection + * + * @param waypoint The waypoint + */ void removeWaypoint(IWaypoint waypoint); + /** + * Gets the most recently created waypoint by the specified {@link IWaypoint.Tag} + * + * @param tag The tag + * @return The most recently created waypoint with the specified tag + */ IWaypoint getMostRecentByTag(IWaypoint.Tag tag); + /** + * Gets all of the waypoints that have the specified tag + * + * @see IWaypointCollection#getAllWaypoints() + * + * @param tag The tag + * @return All of the waypoints with the specified tag + */ Set getByTag(IWaypoint.Tag tag); + /** + * Gets all of the waypoints in this collection, regardless of the tag. + * + * @see IWaypointCollection#getByTag(IWaypoint.Tag) + * + * @return All of the waypoints in this collection + */ Set getAllWaypoints(); } diff --git a/src/api/java/baritone/api/cache/IWorldData.java b/src/api/java/baritone/api/cache/IWorldData.java new file mode 100644 index 00000000..c7ca580a --- /dev/null +++ b/src/api/java/baritone/api/cache/IWorldData.java @@ -0,0 +1,32 @@ +/* + * 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.cache; + +/** + * @author Brady + * @since 9/24/2018 + */ +public interface IWorldData { + + /** + * Returns the waypoint collection for this world. + * + * @return The waypoint collection for this world + */ + IWaypointCollection getWaypoints(); +} diff --git a/src/api/java/baritone/api/cache/IWorldProvider.java b/src/api/java/baritone/api/cache/IWorldProvider.java new file mode 100644 index 00000000..0e54ef46 --- /dev/null +++ b/src/api/java/baritone/api/cache/IWorldProvider.java @@ -0,0 +1,32 @@ +/* + * 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.cache; + +/** + * @author Brady + * @since 9/24/2018 + */ +public interface IWorldProvider { + + /** + * Returns the data of the currently loaded world + * + * @return The current world data + */ + IWorldData getCurrentWorld(); +} diff --git a/src/main/java/baritone/Baritone.java b/src/main/java/baritone/Baritone.java index 4f3e52f2..933b8954 100755 --- a/src/main/java/baritone/Baritone.java +++ b/src/main/java/baritone/Baritone.java @@ -22,6 +22,7 @@ import baritone.api.Settings; import baritone.behavior.Behavior; import baritone.api.event.listener.IGameEventListener; import baritone.behavior.*; +import baritone.cache.WorldProvider; import baritone.event.GameEventHandler; import baritone.utils.InputOverrideHandler; import net.minecraft.client.Minecraft; @@ -87,6 +88,8 @@ public enum Baritone { // We might want to change this... this.settings = BaritoneAPI.getSettings(); + BaritoneAPI.registerProviders(WorldProvider.INSTANCE); + this.behaviors = new ArrayList<>(); { registerBehavior(PathingBehavior.INSTANCE); diff --git a/src/main/java/baritone/behavior/LocationTrackingBehavior.java b/src/main/java/baritone/behavior/LocationTrackingBehavior.java index 86badc06..6828d825 100644 --- a/src/main/java/baritone/behavior/LocationTrackingBehavior.java +++ b/src/main/java/baritone/behavior/LocationTrackingBehavior.java @@ -42,12 +42,12 @@ public final class LocationTrackingBehavior extends Behavior implements Helper { @Override public void onBlockInteract(BlockInteractEvent event) { if (event.getType() == BlockInteractEvent.Type.USE && BlockStateInterface.getBlock(event.getPos()) instanceof BlockBed) { - WorldProvider.INSTANCE.getCurrentWorld().waypoints.addWaypoint(new Waypoint("bed", Waypoint.Tag.BED, event.getPos())); + WorldProvider.INSTANCE.getCurrentWorld().getWaypoints().addWaypoint(new Waypoint("bed", Waypoint.Tag.BED, event.getPos())); } } @Override public void onPlayerDeath() { - WorldProvider.INSTANCE.getCurrentWorld().waypoints.addWaypoint(new Waypoint("death", Waypoint.Tag.DEATH, playerFeet())); + WorldProvider.INSTANCE.getCurrentWorld().getWaypoints().addWaypoint(new Waypoint("death", Waypoint.Tag.DEATH, playerFeet())); } } diff --git a/src/main/java/baritone/cache/CachedWorld.java b/src/main/java/baritone/cache/CachedWorld.java index 393c93fb..6973bdb1 100644 --- a/src/main/java/baritone/cache/CachedWorld.java +++ b/src/main/java/baritone/cache/CachedWorld.java @@ -116,9 +116,7 @@ public final class CachedWorld implements Helper { int regionZ = zoff + playerRegionZ; CachedRegion region = getOrCreateRegion(regionX, regionZ); if (region != null) { - for (BlockPos pos : region.getLocationsOf(block)) { - res.add(pos); - } + res.addAll(region.getLocationsOf(block)); } } } diff --git a/src/main/java/baritone/cache/WorldData.java b/src/main/java/baritone/cache/WorldData.java index 7866e9e8..ce228f90 100644 --- a/src/main/java/baritone/cache/WorldData.java +++ b/src/main/java/baritone/cache/WorldData.java @@ -18,6 +18,8 @@ package baritone.cache; import baritone.Baritone; +import baritone.api.cache.IWaypointCollection; +import baritone.api.cache.IWorldData; import java.nio.file.Path; @@ -26,9 +28,10 @@ import java.nio.file.Path; * * @author leijurv */ -public class WorldData { +public class WorldData implements IWorldData { + public final CachedWorld cache; - public final Waypoints waypoints; + private final Waypoints waypoints; //public final MapData map; public final Path directory; @@ -44,4 +47,9 @@ public class WorldData { cache.save(); }); } + + @Override + public IWaypointCollection getWaypoints() { + return this.waypoints; + } } diff --git a/src/main/java/baritone/cache/WorldProvider.java b/src/main/java/baritone/cache/WorldProvider.java index 2cd0b6dc..786bda96 100644 --- a/src/main/java/baritone/cache/WorldProvider.java +++ b/src/main/java/baritone/cache/WorldProvider.java @@ -18,6 +18,8 @@ package baritone.cache; import baritone.Baritone; +import baritone.api.cache.IWorldData; +import baritone.api.cache.IWorldProvider; import baritone.utils.Helper; import baritone.utils.accessor.IAnvilChunkLoader; import baritone.utils.accessor.IChunkProviderServer; @@ -38,7 +40,7 @@ import java.util.function.Consumer; * @author Brady * @since 8/4/2018 11:06 AM */ -public enum WorldProvider implements Helper { +public enum WorldProvider implements IWorldProvider, Helper { INSTANCE; @@ -46,7 +48,8 @@ public enum WorldProvider implements Helper { private WorldData currentWorld; - public final WorldData getCurrentWorld() { + @Override + public final IWorldData getCurrentWorld() { return this.currentWorld; } diff --git a/src/main/java/baritone/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index beaa61dd..43e3f052 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -342,7 +342,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper { event.cancel(); return; } - Set waypoints = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getByTag(tag); + Set waypoints = WorldProvider.INSTANCE.getCurrentWorld().getWaypoints().getByTag(tag); // might as well show them from oldest to newest List sorted = new ArrayList<>(waypoints); sorted.sort(Comparator.comparingLong(IWaypoint::getCreationTimestamp)); @@ -355,7 +355,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper { } if (msg.startsWith("save")) { String name = msg.substring(4).trim(); - WorldProvider.INSTANCE.getCurrentWorld().waypoints.addWaypoint(new Waypoint(name, Waypoint.Tag.USER, playerFeet())); + WorldProvider.INSTANCE.getCurrentWorld().getWaypoints().addWaypoint(new Waypoint(name, Waypoint.Tag.USER, playerFeet())); logDirect("Saved user defined tag under name '" + name + "'. Say 'goto user' to set goal, say 'list user' to list."); event.cancel(); return; @@ -385,7 +385,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper { PathingBehavior.INSTANCE.path(); return; } - IWaypoint waypoint = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getMostRecentByTag(tag); + IWaypoint waypoint = WorldProvider.INSTANCE.getCurrentWorld().getWaypoints().getMostRecentByTag(tag); if (waypoint == null) { logDirect("None saved for tag " + tag); event.cancel(); @@ -402,7 +402,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper { return; } if (msg.equals("spawn") || msg.equals("bed")) { - IWaypoint waypoint = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getMostRecentByTag(Waypoint.Tag.BED); + IWaypoint waypoint = WorldProvider.INSTANCE.getCurrentWorld().getWaypoints().getMostRecentByTag(Waypoint.Tag.BED); if (waypoint == null) { BlockPos spawnPoint = player().getBedLocation(); // for some reason the default spawnpoint is underground sometimes @@ -418,13 +418,13 @@ public class ExampleBaritoneControl extends Behavior implements Helper { return; } if (msg.equals("sethome")) { - WorldProvider.INSTANCE.getCurrentWorld().waypoints.addWaypoint(new Waypoint("", Waypoint.Tag.HOME, playerFeet())); + WorldProvider.INSTANCE.getCurrentWorld().getWaypoints().addWaypoint(new Waypoint("", Waypoint.Tag.HOME, playerFeet())); logDirect("Saved. Say home to set goal."); event.cancel(); return; } if (msg.equals("home")) { - IWaypoint waypoint = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getMostRecentByTag(Waypoint.Tag.HOME); + IWaypoint waypoint = WorldProvider.INSTANCE.getCurrentWorld().getWaypoints().getMostRecentByTag(Waypoint.Tag.HOME); if (waypoint == null) { logDirect("home not saved"); } else {