From df88b02ed50b9685400174d56d443fa77bb14944 Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 24 Sep 2018 12:48:00 -0500 Subject: [PATCH] Create IWaypointCollection interface in api --- .../api/cache/IWaypointCollection.java | 37 ++++++++++++++ src/main/java/baritone/cache/Waypoints.java | 49 +++++++++++++------ .../utils/ExampleBaritoneControl.java | 15 +++--- 3 files changed, 79 insertions(+), 22 deletions(-) create mode 100644 src/api/java/baritone/api/cache/IWaypointCollection.java diff --git a/src/api/java/baritone/api/cache/IWaypointCollection.java b/src/api/java/baritone/api/cache/IWaypointCollection.java new file mode 100644 index 00000000..23450eeb --- /dev/null +++ b/src/api/java/baritone/api/cache/IWaypointCollection.java @@ -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 . + */ + +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 getByTag(IWaypoint.Tag tag); + + Set getAllWaypoints(); +} diff --git a/src/main/java/baritone/cache/Waypoints.java b/src/main/java/baritone/cache/Waypoints.java index f0483025..2122ddaf 100644 --- a/src/main/java/baritone/cache/Waypoints.java +++ b/src/main/java/baritone/cache/Waypoints.java @@ -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> waypoints; + private final Map> 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 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 getByTag(IWaypoint.Tag tag) { + return Collections.unmodifiableSet(this.waypoints.get(tag)); + } + + @Override + public Set getAllWaypoints() { + return this.waypoints.values().stream().flatMap(Collection::stream).collect(Collectors.toSet()); } } diff --git a/src/main/java/baritone/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index 6e24f1e8..beaa61dd 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -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 waypoints = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getByTag(tag); + Set waypoints = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getByTag(tag); // might as well show them from oldest to newest - List sorted = new ArrayList<>(waypoints); - sorted.sort(Comparator.comparingLong(Waypoint::getCreationTimestamp)); + List 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 {