Create IWaypointCollection interface in api
This commit is contained in:
parent
d6c2c053db
commit
df88b02ed5
37
src/api/java/baritone/api/cache/IWaypointCollection.java
vendored
Normal file
37
src/api/java/baritone/api/cache/IWaypointCollection.java
vendored
Normal 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();
|
||||||
|
}
|
49
src/main/java/baritone/cache/Waypoints.java
vendored
49
src/main/java/baritone/cache/Waypoints.java
vendored
@ -17,19 +17,22 @@
|
|||||||
|
|
||||||
package baritone.cache;
|
package baritone.cache;
|
||||||
|
|
||||||
|
import baritone.api.cache.IWaypoint;
|
||||||
|
import baritone.api.cache.IWaypointCollection;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Waypoints for a world
|
* Waypoints for a world
|
||||||
*
|
*
|
||||||
* @author leijurv
|
* @author leijurv
|
||||||
*/
|
*/
|
||||||
public class Waypoints {
|
public class Waypoints implements IWaypointCollection {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Magic value to detect invalid waypoint files
|
* 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 static final long WAYPOINT_MAGIC_VALUE = 121977993584L; // good value
|
||||||
|
|
||||||
private final Path directory;
|
private final Path directory;
|
||||||
private final Map<Waypoint.Tag, Set<Waypoint>> waypoints;
|
private final Map<IWaypoint.Tag, Set<IWaypoint>> waypoints;
|
||||||
|
|
||||||
Waypoints(Path directory) {
|
Waypoints(Path directory) {
|
||||||
this.directory = directory;
|
this.directory = directory;
|
||||||
@ -58,9 +61,9 @@ public class Waypoints {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void load(Waypoint.Tag tag) {
|
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)) {
|
if (!Files.exists(fileName)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -82,21 +85,21 @@ public class Waypoints {
|
|||||||
int x = in.readInt();
|
int x = in.readInt();
|
||||||
int y = in.readInt();
|
int y = in.readInt();
|
||||||
int z = 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) {}
|
} catch (IOException ignored) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void save(Waypoint.Tag tag) {
|
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 (
|
try (
|
||||||
FileOutputStream fileOut = new FileOutputStream(fileName.toFile());
|
FileOutputStream fileOut = new FileOutputStream(fileName.toFile());
|
||||||
BufferedOutputStream bufOut = new BufferedOutputStream(fileOut);
|
BufferedOutputStream bufOut = new BufferedOutputStream(fileOut);
|
||||||
DataOutputStream out = new DataOutputStream(bufOut)
|
DataOutputStream out = new DataOutputStream(bufOut)
|
||||||
) {
|
) {
|
||||||
out.writeLong(WAYPOINT_MAGIC_VALUE);
|
out.writeLong(WAYPOINT_MAGIC_VALUE);
|
||||||
out.writeLong(waypoints.get(tag).size());
|
out.writeLong(this.waypoints.get(tag).size());
|
||||||
for (Waypoint waypoint : waypoints.get(tag)) {
|
for (IWaypoint waypoint : this.waypoints.get(tag)) {
|
||||||
out.writeUTF(waypoint.getName());
|
out.writeUTF(waypoint.getName());
|
||||||
out.writeLong(waypoint.getCreationTimestamp());
|
out.writeLong(waypoint.getCreationTimestamp());
|
||||||
out.writeInt(waypoint.getLocation().getX());
|
out.writeInt(waypoint.getLocation().getX());
|
||||||
@ -108,18 +111,34 @@ public class Waypoints {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Waypoint> getByTag(Waypoint.Tag tag) {
|
@Override
|
||||||
return Collections.unmodifiableSet(waypoints.get(tag));
|
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
|
// 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);
|
return this.waypoints.get(tag).stream().min(Comparator.comparingLong(w -> -w.getCreationTimestamp())).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addWaypoint(Waypoint waypoint) {
|
@Override
|
||||||
// no need to check for duplicate, because it's a Set not a List
|
public Set<IWaypoint> getByTag(IWaypoint.Tag tag) {
|
||||||
waypoints.get(waypoint.getTag()).add(waypoint);
|
return Collections.unmodifiableSet(this.waypoints.get(tag));
|
||||||
save(waypoint.getTag());
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<IWaypoint> getAllWaypoints() {
|
||||||
|
return this.waypoints.values().stream().flatMap(Collection::stream).collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ package baritone.utils;
|
|||||||
|
|
||||||
import baritone.Baritone;
|
import baritone.Baritone;
|
||||||
import baritone.api.Settings;
|
import baritone.api.Settings;
|
||||||
|
import baritone.api.cache.IWaypoint;
|
||||||
import baritone.api.event.events.ChatEvent;
|
import baritone.api.event.events.ChatEvent;
|
||||||
import baritone.api.pathing.goals.Goal;
|
import baritone.api.pathing.goals.Goal;
|
||||||
import baritone.behavior.Behavior;
|
import baritone.behavior.Behavior;
|
||||||
@ -341,12 +342,12 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
|
|||||||
event.cancel();
|
event.cancel();
|
||||||
return;
|
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
|
// might as well show them from oldest to newest
|
||||||
List<Waypoint> sorted = new ArrayList<>(waypoints);
|
List<IWaypoint> sorted = new ArrayList<>(waypoints);
|
||||||
sorted.sort(Comparator.comparingLong(Waypoint::getCreationTimestamp));
|
sorted.sort(Comparator.comparingLong(IWaypoint::getCreationTimestamp));
|
||||||
logDirect("Waypoints under tag " + tag + ":");
|
logDirect("Waypoints under tag " + tag + ":");
|
||||||
for (Waypoint waypoint : sorted) {
|
for (IWaypoint waypoint : sorted) {
|
||||||
logDirect(waypoint.toString());
|
logDirect(waypoint.toString());
|
||||||
}
|
}
|
||||||
event.cancel();
|
event.cancel();
|
||||||
@ -384,7 +385,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
|
|||||||
PathingBehavior.INSTANCE.path();
|
PathingBehavior.INSTANCE.path();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Waypoint waypoint = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getMostRecentByTag(tag);
|
IWaypoint waypoint = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getMostRecentByTag(tag);
|
||||||
if (waypoint == null) {
|
if (waypoint == null) {
|
||||||
logDirect("None saved for tag " + tag);
|
logDirect("None saved for tag " + tag);
|
||||||
event.cancel();
|
event.cancel();
|
||||||
@ -401,7 +402,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (msg.equals("spawn") || msg.equals("bed")) {
|
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) {
|
if (waypoint == null) {
|
||||||
BlockPos spawnPoint = player().getBedLocation();
|
BlockPos spawnPoint = player().getBedLocation();
|
||||||
// for some reason the default spawnpoint is underground sometimes
|
// for some reason the default spawnpoint is underground sometimes
|
||||||
@ -423,7 +424,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (msg.equals("home")) {
|
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) {
|
if (waypoint == null) {
|
||||||
logDirect("home not saved");
|
logDirect("home not saved");
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user