baritone/src/main/java/baritone/chunk/Waypoints.java

111 lines
3.8 KiB
Java
Raw Normal View History

2018-08-22 22:35:32 +00:00
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.chunk;
2018-08-22 23:56:05 +00:00
import net.minecraft.util.math.BlockPos;
import java.io.*;
2018-08-22 22:35:32 +00:00
import java.nio.file.Files;
import java.nio.file.Path;
2018-08-22 23:56:05 +00:00
import java.util.*;
2018-08-22 22:35:32 +00:00
2018-08-22 23:56:05 +00:00
/**
* Waypoints for a world
*
* @author leijurv
*/
2018-08-22 22:35:32 +00:00
public class Waypoints {
private final Path directory;
2018-08-22 23:56:05 +00:00
private final Map<Waypoint.Tag, Set<Waypoint>> waypoints;
2018-08-22 22:35:32 +00:00
Waypoints(Path directory) {
this.directory = directory;
if (!Files.exists(directory)) {
try {
Files.createDirectories(directory);
} catch (IOException ignored) {}
}
System.out.println("Would save waypoints to " + directory);
2018-08-22 23:56:05 +00:00
this.waypoints = new HashMap<>();
load();
}
private void load() {
for (Waypoint.Tag tag : Waypoint.Tag.values()) {
load(tag);
}
}
private synchronized void load(Waypoint.Tag tag) {
waypoints.put(tag, new HashSet<>());
Path fileName = directory.resolve(tag.name().toLowerCase());
if (!Files.exists(fileName))
return;
try (
FileInputStream fileIn = new FileInputStream(fileName.toFile());
BufferedInputStream bufIn = new BufferedInputStream(fileIn);
DataInputStream in = new DataInputStream(bufIn)
2018-08-22 23:56:05 +00:00
) {
while (true) {
String name = in.readUTF();
long creationTimestamp = in.readLong();
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));
}
} catch (IOException ignored) {}
2018-08-22 23:56:05 +00:00
}
private synchronized void save(Waypoint.Tag tag) {
Path fileName = directory.resolve(tag.name().toLowerCase());
try (
FileOutputStream fileOut = new FileOutputStream(fileName.toFile());
BufferedOutputStream bufOut = new BufferedOutputStream(fileOut);
DataOutputStream out = new DataOutputStream(bufOut)
2018-08-22 23:56:05 +00:00
) {
for (Waypoint waypoint : waypoints.get(tag)) {
out.writeUTF(waypoint.name);
out.writeLong(waypoint.creationTimestamp());
2018-08-22 23:56:05 +00:00
out.writeInt(waypoint.location.getX());
out.writeInt(waypoint.location.getY());
out.writeInt(waypoint.location.getZ());
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
public Set<Waypoint> getByTag(Waypoint.Tag tag) {
return Collections.unmodifiableSet(waypoints.get(tag));
}
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);
2018-08-22 23:56:05 +00:00
}
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);
2018-08-22 22:35:32 +00:00
}
}