Create IWaypoint interface in api
This commit is contained in:
81
src/main/java/baritone/cache/Waypoint.java
vendored
81
src/main/java/baritone/cache/Waypoint.java
vendored
@@ -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> TAG_LIST = ImmutableList.<Tag>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());
|
||||
}
|
||||
}
|
||||
|
16
src/main/java/baritone/cache/Waypoints.java
vendored
16
src/main/java/baritone/cache/Waypoints.java
vendored
@@ -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());
|
||||
}
|
||||
}
|
||||
|
@@ -344,7 +344,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
|
||||
Set<Waypoint> 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::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);
|
||||
|
Reference in New Issue
Block a user