diff --git a/src/api/java/baritone/api/cache/IWaypoint.java b/src/api/java/baritone/api/cache/IWaypoint.java new file mode 100644 index 00000000..01df2a48 --- /dev/null +++ b/src/api/java/baritone/api/cache/IWaypoint.java @@ -0,0 +1,111 @@ +/* + * 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 net.minecraft.util.math.BlockPos; +import org.apache.commons.lang3.ArrayUtils; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * A marker for a position in the world. + * + * @author Brady + * @since 9/24/2018 + */ +public interface IWaypoint { + + /** + * @return The label for this waypoint + */ + String getName(); + + /** + * Returns the tag for this waypoint. The tag is a category + * for the waypoint in a sense, it describes the source of + * the waypoint. + * + * @return The waypoint tag + */ + Tag getTag(); + + /** + * Returns the unix epoch time in milliseconds that this waypoint + * was created. This value should only be set once, when the waypoint + * is initially created, and not when it is being loaded from file. + * + * @return The unix epoch milliseconds that this waypoint was created + */ + long getCreationTimestamp(); + + /** + * Returns the actual block position of this waypoint. + * + * @return The block position of this waypoint + */ + BlockPos getLocation(); + + enum Tag { + + /** + * Tag indicating a position explictly marked as a home base + */ + HOME("home", "base"), + + /** + * Tag indicating a position that the local player has died at + */ + DEATH("death"), + + /** + * Tag indicating a bed position + */ + BED("bed", "spawn"), + + /** + * Tag indicating that the waypoint was user-created + */ + USER("user"); + + /** + * A list of all of the + */ + private static final List TAG_LIST = Collections.unmodifiableList(Arrays.asList(Tag.values())); + + /** + * The names for the tag, anything that the tag can be referred to as. + */ + private final String[] names; + + Tag(String... names) { + this.names = names; + } + + /** + * Finds a tag from one of the names that could be used to identify said tag. + * + * @param name The name of the tag + * @return The tag, if one is found, otherwise, {@code null} + */ + public static Tag fromString(String name) { + return TAG_LIST.stream().filter(tag -> ArrayUtils.contains(tag.names, name.toLowerCase())).findFirst().orElse(null); + } + } +} diff --git a/src/main/java/baritone/cache/Waypoint.java b/src/main/java/baritone/cache/Waypoint.java index 4fd6c57a..00f4410a 100644 --- a/src/main/java/baritone/cache/Waypoint.java +++ b/src/main/java/baritone/cache/Waypoint.java @@ -17,77 +17,82 @@ package baritone.cache; -import com.google.common.collect.ImmutableList; +import baritone.api.cache.IWaypoint; import net.minecraft.util.math.BlockPos; -import org.apache.commons.lang3.ArrayUtils; import java.util.Date; -import java.util.List; /** - * A single waypoint + * Basic implementation of {@link IWaypoint} * * @author leijurv */ -public class Waypoint { +public class Waypoint implements IWaypoint { - public final String name; - public final Tag tag; + private final String name; + private final Tag tag; private final long creationTimestamp; - public final BlockPos location; + private final BlockPos location; public Waypoint(String name, Tag tag, BlockPos location) { this(name, tag, location, System.currentTimeMillis()); } - Waypoint(String name, Tag tag, BlockPos location, long creationTimestamp) { // read from disk + /** + * Constructor called when a Waypoint is read from disk, adds the creationTimestamp + * as a parameter so that it is reserved after a waypoint is wrote to the disk. + * + * @param name The waypoint name + * @param tag The waypoint tag + * @param location The waypoint location + * @param creationTimestamp When the waypoint was created + */ + Waypoint(String name, Tag tag, BlockPos location, long creationTimestamp) { this.name = name; this.tag = tag; this.location = location; this.creationTimestamp = creationTimestamp; } - @Override - public boolean equals(Object o) { - if (o == null) { - return false; - } - if (!(o instanceof Waypoint)) { - return false; - } - Waypoint w = (Waypoint) o; - return name.equals(w.name) && tag == w.tag && location.equals(w.location); - } - @Override public int hashCode() { return name.hashCode() + tag.hashCode() + location.hashCode(); //lol } - public long creationTimestamp() { - return creationTimestamp; + @Override + public String getName() { + return this.name; } + @Override + public Tag getTag() { + return this.tag; + } + + @Override + public long getCreationTimestamp() { + return this.creationTimestamp; + } + + @Override + public BlockPos getLocation() { + return this.location; + } + + @Override public String toString() { return name + " " + location.toString() + " " + new Date(creationTimestamp).toString(); } - public enum Tag { - HOME("home", "base"), - DEATH("death"), - BED("bed", "spawn"), - USER("user"); - - private static final List TAG_LIST = ImmutableList.builder().add(Tag.values()).build(); - - private final String[] names; - - Tag(String... names) { - this.names = names; + @Override + public boolean equals(Object o) { + if (o == null) { + return false; } - - public static Tag fromString(String name) { - return TAG_LIST.stream().filter(tag -> ArrayUtils.contains(tag.names, name.toLowerCase())).findFirst().orElse(null); + if (!(o instanceof IWaypoint)) { + return false; } + IWaypoint w = (IWaypoint) o; + return name.equals(w.getName()) && tag == w.getTag() && location.equals(w.getLocation()); } } diff --git a/src/main/java/baritone/cache/Waypoints.java b/src/main/java/baritone/cache/Waypoints.java index be76e34e..f0483025 100644 --- a/src/main/java/baritone/cache/Waypoints.java +++ b/src/main/java/baritone/cache/Waypoints.java @@ -97,11 +97,11 @@ public class Waypoints { out.writeLong(WAYPOINT_MAGIC_VALUE); out.writeLong(waypoints.get(tag).size()); for (Waypoint waypoint : waypoints.get(tag)) { - out.writeUTF(waypoint.name); - out.writeLong(waypoint.creationTimestamp()); - out.writeInt(waypoint.location.getX()); - out.writeInt(waypoint.location.getY()); - out.writeInt(waypoint.location.getZ()); + out.writeUTF(waypoint.getName()); + out.writeLong(waypoint.getCreationTimestamp()); + out.writeInt(waypoint.getLocation().getX()); + out.writeInt(waypoint.getLocation().getY()); + out.writeInt(waypoint.getLocation().getZ()); } } catch (IOException ex) { ex.printStackTrace(); @@ -114,12 +114,12 @@ public class Waypoints { public Waypoint getMostRecentByTag(Waypoint.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.creationTimestamp())).orElse(null); + 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.tag).add(waypoint); - save(waypoint.tag); + waypoints.get(waypoint.getTag()).add(waypoint); + save(waypoint.getTag()); } } diff --git a/src/main/java/baritone/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index 0b5e03d1..6e24f1e8 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -344,7 +344,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper { 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::creationTimestamp)); + sorted.sort(Comparator.comparingLong(Waypoint::getCreationTimestamp)); logDirect("Waypoints under tag " + tag + ":"); for (Waypoint waypoint : sorted) { logDirect(waypoint.toString()); @@ -390,7 +390,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper { event.cancel(); return; } - Goal goal = new GoalBlock(waypoint.location); + Goal goal = new GoalBlock(waypoint.getLocation()); PathingBehavior.INSTANCE.setGoal(goal); if (!PathingBehavior.INSTANCE.path()) { if (!goal.isInGoal(playerFeet())) { @@ -409,7 +409,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper { logDirect("spawn not saved, defaulting to world spawn. set goal to " + goal); PathingBehavior.INSTANCE.setGoal(goal); } else { - Goal goal = new GoalBlock(waypoint.location); + Goal goal = new GoalBlock(waypoint.getLocation()); PathingBehavior.INSTANCE.setGoal(goal); logDirect("Set goal to most recent bed " + goal); } @@ -427,7 +427,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper { if (waypoint == null) { logDirect("home not saved"); } else { - Goal goal = new GoalBlock(waypoint.location); + Goal goal = new GoalBlock(waypoint.getLocation()); PathingBehavior.INSTANCE.setGoal(goal); PathingBehavior.INSTANCE.path(); logDirect("Going to saved home " + goal);