Create IWaypoint interface in api

This commit is contained in:
Brady 2018-09-24 12:36:03 -05:00
parent 5650c86a7d
commit d6c2c053db
No known key found for this signature in database
GPG Key ID: 73A788379A197567
4 changed files with 166 additions and 50 deletions

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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> 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);
}
}
}

View File

@ -17,77 +17,82 @@
package baritone.cache; package baritone.cache;
import com.google.common.collect.ImmutableList; import baritone.api.cache.IWaypoint;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import org.apache.commons.lang3.ArrayUtils;
import java.util.Date; import java.util.Date;
import java.util.List;
/** /**
* A single waypoint * Basic implementation of {@link IWaypoint}
* *
* @author leijurv * @author leijurv
*/ */
public class Waypoint { public class Waypoint implements IWaypoint {
public final String name; private final String name;
public final Tag tag; private final Tag tag;
private final long creationTimestamp; private final long creationTimestamp;
public final BlockPos location; private final BlockPos location;
public Waypoint(String name, Tag tag, BlockPos location) { public Waypoint(String name, Tag tag, BlockPos location) {
this(name, tag, location, System.currentTimeMillis()); 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.name = name;
this.tag = tag; this.tag = tag;
this.location = location; this.location = location;
this.creationTimestamp = creationTimestamp; 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 @Override
public int hashCode() { public int hashCode() {
return name.hashCode() + tag.hashCode() + location.hashCode(); //lol return name.hashCode() + tag.hashCode() + location.hashCode(); //lol
} }
public long creationTimestamp() { @Override
return creationTimestamp; 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() { public String toString() {
return name + " " + location.toString() + " " + new Date(creationTimestamp).toString(); return name + " " + location.toString() + " " + new Date(creationTimestamp).toString();
} }
public enum Tag { @Override
HOME("home", "base"), public boolean equals(Object o) {
DEATH("death"), if (o == null) {
BED("bed", "spawn"), return false;
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;
}
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());
} }
} }

View File

@ -97,11 +97,11 @@ public class Waypoints {
out.writeLong(WAYPOINT_MAGIC_VALUE); out.writeLong(WAYPOINT_MAGIC_VALUE);
out.writeLong(waypoints.get(tag).size()); out.writeLong(waypoints.get(tag).size());
for (Waypoint waypoint : waypoints.get(tag)) { for (Waypoint waypoint : waypoints.get(tag)) {
out.writeUTF(waypoint.name); out.writeUTF(waypoint.getName());
out.writeLong(waypoint.creationTimestamp()); out.writeLong(waypoint.getCreationTimestamp());
out.writeInt(waypoint.location.getX()); out.writeInt(waypoint.getLocation().getX());
out.writeInt(waypoint.location.getY()); out.writeInt(waypoint.getLocation().getY());
out.writeInt(waypoint.location.getZ()); out.writeInt(waypoint.getLocation().getZ());
} }
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
@ -114,12 +114,12 @@ public class Waypoints {
public Waypoint getMostRecentByTag(Waypoint.Tag tag) { public Waypoint getMostRecentByTag(Waypoint.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.creationTimestamp())).orElse(null); return this.waypoints.get(tag).stream().min(Comparator.comparingLong(w -> -w.getCreationTimestamp())).orElse(null);
} }
public void addWaypoint(Waypoint waypoint) { public void addWaypoint(Waypoint waypoint) {
// no need to check for duplicate, because it's a Set not a List // no need to check for duplicate, because it's a Set not a List
waypoints.get(waypoint.tag).add(waypoint); waypoints.get(waypoint.getTag()).add(waypoint);
save(waypoint.tag); save(waypoint.getTag());
} }
} }

View File

@ -344,7 +344,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
Set<Waypoint> waypoints = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getByTag(tag); Set<Waypoint> 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<Waypoint> sorted = new ArrayList<>(waypoints);
sorted.sort(Comparator.comparingLong(Waypoint::creationTimestamp)); sorted.sort(Comparator.comparingLong(Waypoint::getCreationTimestamp));
logDirect("Waypoints under tag " + tag + ":"); logDirect("Waypoints under tag " + tag + ":");
for (Waypoint waypoint : sorted) { for (Waypoint waypoint : sorted) {
logDirect(waypoint.toString()); logDirect(waypoint.toString());
@ -390,7 +390,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
event.cancel(); event.cancel();
return; return;
} }
Goal goal = new GoalBlock(waypoint.location); Goal goal = new GoalBlock(waypoint.getLocation());
PathingBehavior.INSTANCE.setGoal(goal); PathingBehavior.INSTANCE.setGoal(goal);
if (!PathingBehavior.INSTANCE.path()) { if (!PathingBehavior.INSTANCE.path()) {
if (!goal.isInGoal(playerFeet())) { 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); logDirect("spawn not saved, defaulting to world spawn. set goal to " + goal);
PathingBehavior.INSTANCE.setGoal(goal); PathingBehavior.INSTANCE.setGoal(goal);
} else { } else {
Goal goal = new GoalBlock(waypoint.location); Goal goal = new GoalBlock(waypoint.getLocation());
PathingBehavior.INSTANCE.setGoal(goal); PathingBehavior.INSTANCE.setGoal(goal);
logDirect("Set goal to most recent bed " + goal); logDirect("Set goal to most recent bed " + goal);
} }
@ -427,7 +427,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
if (waypoint == null) { if (waypoint == null) {
logDirect("home not saved"); logDirect("home not saved");
} else { } else {
Goal goal = new GoalBlock(waypoint.location); Goal goal = new GoalBlock(waypoint.getLocation());
PathingBehavior.INSTANCE.setGoal(goal); PathingBehavior.INSTANCE.setGoal(goal);
PathingBehavior.INSTANCE.path(); PathingBehavior.INSTANCE.path();
logDirect("Going to saved home " + goal); logDirect("Going to saved home " + goal);