Create IWaypointCollection interface in api

This commit is contained in:
Brady 2018-09-24 12:48:00 -05:00
parent d6c2c053db
commit df88b02ed5
No known key found for this signature in database
GPG Key ID: 73A788379A197567
3 changed files with 79 additions and 22 deletions

View File

@ -0,0 +1,37 @@
/*
* 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;
import java.util.Set;
/**
* @author Brady
* @since 9/24/2018
*/
public interface IWaypointCollection {
void addWaypoint(IWaypoint waypoint);
void removeWaypoint(IWaypoint waypoint);
IWaypoint getMostRecentByTag(IWaypoint.Tag tag);
Set<IWaypoint> getByTag(IWaypoint.Tag tag);
Set<IWaypoint> getAllWaypoints();
}

View File

@ -17,19 +17,22 @@
package baritone.cache;
import baritone.api.cache.IWaypoint;
import baritone.api.cache.IWaypointCollection;
import net.minecraft.util.math.BlockPos;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;
/**
* Waypoints for a world
*
* @author leijurv
*/
public class Waypoints {
public class Waypoints implements IWaypointCollection {
/**
* Magic value to detect invalid waypoint files
@ -37,7 +40,7 @@ public class Waypoints {
private static final long WAYPOINT_MAGIC_VALUE = 121977993584L; // good value
private final Path directory;
private final Map<Waypoint.Tag, Set<Waypoint>> waypoints;
private final Map<IWaypoint.Tag, Set<IWaypoint>> waypoints;
Waypoints(Path directory) {
this.directory = directory;
@ -58,9 +61,9 @@ public class Waypoints {
}
private synchronized void load(Waypoint.Tag tag) {
waypoints.put(tag, new HashSet<>());
this.waypoints.put(tag, new HashSet<>());
Path fileName = directory.resolve(tag.name().toLowerCase() + ".mp4");
Path fileName = this.directory.resolve(tag.name().toLowerCase() + ".mp4");
if (!Files.exists(fileName)) {
return;
}
@ -82,21 +85,21 @@ public class Waypoints {
int x = in.readInt();
int y = in.readInt();
int z = in.readInt();
waypoints.get(tag).add(new Waypoint(name, tag, new BlockPos(x, y, z), creationTimestamp));
this.waypoints.get(tag).add(new Waypoint(name, tag, new BlockPos(x, y, z), creationTimestamp));
}
} catch (IOException ignored) {}
}
private synchronized void save(Waypoint.Tag tag) {
Path fileName = directory.resolve(tag.name().toLowerCase() + ".mp4");
Path fileName = this.directory.resolve(tag.name().toLowerCase() + ".mp4");
try (
FileOutputStream fileOut = new FileOutputStream(fileName.toFile());
BufferedOutputStream bufOut = new BufferedOutputStream(fileOut);
DataOutputStream out = new DataOutputStream(bufOut)
) {
out.writeLong(WAYPOINT_MAGIC_VALUE);
out.writeLong(waypoints.get(tag).size());
for (Waypoint waypoint : waypoints.get(tag)) {
out.writeLong(this.waypoints.get(tag).size());
for (IWaypoint waypoint : this.waypoints.get(tag)) {
out.writeUTF(waypoint.getName());
out.writeLong(waypoint.getCreationTimestamp());
out.writeInt(waypoint.getLocation().getX());
@ -108,18 +111,34 @@ public class Waypoints {
}
}
public Set<Waypoint> getByTag(Waypoint.Tag tag) {
return Collections.unmodifiableSet(waypoints.get(tag));
@Override
public void addWaypoint(IWaypoint waypoint) {
// no need to check for duplicate, because it's a Set not a List
if (waypoints.get(waypoint.getTag()).add(waypoint)) {
save(waypoint.getTag());
}
}
public Waypoint getMostRecentByTag(Waypoint.Tag tag) {
@Override
public void removeWaypoint(IWaypoint waypoint) {
if (waypoints.get(waypoint.getTag()).remove(waypoint)) {
save(waypoint.getTag());
}
}
@Override
public IWaypoint getMostRecentByTag(IWaypoint.Tag tag) {
// Find a waypoint of the given tag which has the greatest timestamp value, indicating the most recent
return this.waypoints.get(tag).stream().min(Comparator.comparingLong(w -> -w.getCreationTimestamp())).orElse(null);
}
public void addWaypoint(Waypoint waypoint) {
// no need to check for duplicate, because it's a Set not a List
waypoints.get(waypoint.getTag()).add(waypoint);
save(waypoint.getTag());
@Override
public Set<IWaypoint> getByTag(IWaypoint.Tag tag) {
return Collections.unmodifiableSet(this.waypoints.get(tag));
}
@Override
public Set<IWaypoint> getAllWaypoints() {
return this.waypoints.values().stream().flatMap(Collection::stream).collect(Collectors.toSet());
}
}

View File

@ -19,6 +19,7 @@ package baritone.utils;
import baritone.Baritone;
import baritone.api.Settings;
import baritone.api.cache.IWaypoint;
import baritone.api.event.events.ChatEvent;
import baritone.api.pathing.goals.Goal;
import baritone.behavior.Behavior;
@ -341,12 +342,12 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
event.cancel();
return;
}
Set<Waypoint> waypoints = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getByTag(tag);
Set<IWaypoint> waypoints = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getByTag(tag);
// might as well show them from oldest to newest
List<Waypoint> sorted = new ArrayList<>(waypoints);
sorted.sort(Comparator.comparingLong(Waypoint::getCreationTimestamp));
List<IWaypoint> sorted = new ArrayList<>(waypoints);
sorted.sort(Comparator.comparingLong(IWaypoint::getCreationTimestamp));
logDirect("Waypoints under tag " + tag + ":");
for (Waypoint waypoint : sorted) {
for (IWaypoint waypoint : sorted) {
logDirect(waypoint.toString());
}
event.cancel();
@ -384,7 +385,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
PathingBehavior.INSTANCE.path();
return;
}
Waypoint waypoint = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getMostRecentByTag(tag);
IWaypoint waypoint = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getMostRecentByTag(tag);
if (waypoint == null) {
logDirect("None saved for tag " + tag);
event.cancel();
@ -401,7 +402,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
return;
}
if (msg.equals("spawn") || msg.equals("bed")) {
Waypoint waypoint = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getMostRecentByTag(Waypoint.Tag.BED);
IWaypoint waypoint = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getMostRecentByTag(Waypoint.Tag.BED);
if (waypoint == null) {
BlockPos spawnPoint = player().getBedLocation();
// for some reason the default spawnpoint is underground sometimes
@ -423,7 +424,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
return;
}
if (msg.equals("home")) {
Waypoint waypoint = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getMostRecentByTag(Waypoint.Tag.HOME);
IWaypoint waypoint = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getMostRecentByTag(Waypoint.Tag.HOME);
if (waypoint == null) {
logDirect("home not saved");
} else {