Create IWorldData and IWorldProvider interfaces in api

This fully exposes waypoints in the api, next step is cached worlds!
This commit is contained in:
Brady 2018-09-24 14:42:22 -05:00
parent 1cf4c9419f
commit 35c8b03122
No known key found for this signature in database
GPG Key ID: 73A788379A197567
10 changed files with 133 additions and 15 deletions

View File

@ -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
*/

View File

@ -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<IWaypoint> 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<IWaypoint> getAllWaypoints();
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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();
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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();
}

View File

@ -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);

View File

@ -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()));
}
}

View File

@ -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));
}
}
}

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -342,7 +342,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
event.cancel();
return;
}
Set<IWaypoint> waypoints = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getByTag(tag);
Set<IWaypoint> waypoints = WorldProvider.INSTANCE.getCurrentWorld().getWaypoints().getByTag(tag);
// might as well show them from oldest to newest
List<IWaypoint> 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 {