forked from SteamWar/SteamWar
Extracted Trace Deserialisation/Serialisation process into Repo
This commit is contained in:
+96
@@ -0,0 +1,96 @@
|
|||||||
|
package de.steamwar.bausystem.features.tracer;
|
||||||
|
|
||||||
|
import de.steamwar.bausystem.region.Region;
|
||||||
|
import lombok.Cleanup;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Location;
|
||||||
|
import org.bukkit.util.Vector;
|
||||||
|
|
||||||
|
import java.io.EOFException;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.zip.GZIPInputStream;
|
||||||
|
|
||||||
|
public class TraceRepository {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Increment this when changing serialisation format
|
||||||
|
*/
|
||||||
|
public static final int SERIALISATION_VERSION = 1;
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
protected static int getVersion(File metadataFile) {
|
||||||
|
@Cleanup
|
||||||
|
ObjectInputStream reader = new ObjectInputStream(new FileInputStream(metadataFile));
|
||||||
|
reader.readUTF();
|
||||||
|
reader.readUTF();
|
||||||
|
reader.readObject();
|
||||||
|
try {
|
||||||
|
int version = reader.readInt();
|
||||||
|
return version;
|
||||||
|
} catch (EOFException e) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
protected static Trace readTrace(File metadataFile) {
|
||||||
|
@Cleanup
|
||||||
|
ObjectInputStream reader = new ObjectInputStream(new FileInputStream(metadataFile));
|
||||||
|
UUID uuid = UUID.fromString(reader.readUTF());
|
||||||
|
Region region = Region.getREGION_MAP().get(reader.readUTF());
|
||||||
|
Date date = (Date) reader.readObject();
|
||||||
|
File recordsFile = new File(uuid + ".records");
|
||||||
|
int recordsCount = reader.readInt();
|
||||||
|
|
||||||
|
return new Trace(uuid, region, date, metadataFile, recordsFile, recordsCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
protected static TNTPoint readTraceRecord(ObjectInputStream objectInput) {
|
||||||
|
|
||||||
|
int tntId = objectInput.readInt();
|
||||||
|
boolean explosion = objectInput.readBoolean();
|
||||||
|
boolean inWater = objectInput.readBoolean();
|
||||||
|
boolean afterFirstExplosion = objectInput.readBoolean();
|
||||||
|
boolean destroyedBuildArea = objectInput.readBoolean();
|
||||||
|
boolean destroyedTestBlock = objectInput.readBoolean();
|
||||||
|
long ticksSinceStart = objectInput.readLong();
|
||||||
|
int fuse = objectInput.readInt();
|
||||||
|
double locX = objectInput.readDouble();
|
||||||
|
double locY = objectInput.readDouble();
|
||||||
|
double locZ = objectInput.readDouble();
|
||||||
|
Location location = new Location(Bukkit.getWorlds().get(0), locX, locY, locZ);
|
||||||
|
double velX = objectInput.readDouble();
|
||||||
|
double velY = objectInput.readDouble();
|
||||||
|
double velZ = objectInput.readDouble();
|
||||||
|
Vector velocity = new Vector(velX, velY, velZ);
|
||||||
|
|
||||||
|
return new TNTPoint(tntId, explosion, inWater, afterFirstExplosion, destroyedBuildArea, destroyedTestBlock, ticksSinceStart, fuse, location, velocity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
protected static List<TNTPoint> readTraceRecords(Trace trace) {
|
||||||
|
File recordsFile = trace.getRecordsSaveFile();
|
||||||
|
@Cleanup
|
||||||
|
ObjectInputStream inputStream = new ObjectInputStream(new GZIPInputStream(new FileInputStream(recordsFile)));
|
||||||
|
|
||||||
|
List<TNTPoint> records = new ArrayList<>();
|
||||||
|
for (int i = 0; i < trace.getRecordsCount(); i++) {
|
||||||
|
records.add(readTraceRecord(inputStream));
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<Integer, List<TNTPoint>> histories = new HashMap<>();
|
||||||
|
for (TNTPoint record : records) {
|
||||||
|
int tntId = record.getTntId();
|
||||||
|
List<TNTPoint> history = histories.computeIfAbsent(tntId, id -> new ArrayList<>());
|
||||||
|
history.add(record);
|
||||||
|
record.setHistory(history);
|
||||||
|
}
|
||||||
|
|
||||||
|
return records;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user