use of org.lanternpowered.server.game.Lantern in project LanternServer by LanternPowered.
the class LanternWorldPropertiesIO method read.
static LevelData read(Path directory, @Nullable String worldName, @Nullable UUID uniqueId) throws IOException {
final DataView rootDataView = IOHelper.read(directory.resolve(LEVEL_DATA), file -> NbtStreamUtils.read(Files.newInputStream(file), true)).orElseThrow(() -> new FileNotFoundException("Unable to find " + LEVEL_DATA + "!"));
final DataView dataView = rootDataView.getView(DATA).get();
if (worldName == null) {
worldName = dataView.getString(NAME).get();
}
final DataView spongeRootDataView = IOHelper.read(directory.resolve(SPONGE_LEVEL_DATA), file -> NbtStreamUtils.read(Files.newInputStream(file), true)).orElse(null);
final DataView spongeContainer = spongeRootDataView != null ? spongeRootDataView.getView(DataQueries.SPONGE_DATA).orElse(null) : null;
if (uniqueId == null) {
// Try for the sponge (lantern) storage format
if (spongeContainer != null) {
final Long most = spongeContainer.getLong(UUID_MOST).orElseGet(() -> spongeContainer.getLong(OLD_UUID_MOST).orElse(null));
final Long least = spongeContainer.getLong(UUID_LEAST).orElseGet(() -> spongeContainer.getLong(OLD_UUID_LEAST).orElse(null));
if (most != null && least != null) {
uniqueId = new UUID(most, least);
}
}
// The uuid storage bukkit used, try this one first
final Path uuidFile;
if (uniqueId == null && Files.exists((uuidFile = directory.resolve(BUKKIT_UUID_DATA)))) {
try (DataInputStream in = new DataInputStream(Files.newInputStream(uuidFile))) {
uniqueId = new UUID(in.readLong(), in.readLong());
} catch (IOException e) {
Lantern.getLogger().error("Unable to access {}, ignoring...", BUKKIT_UUID_DATA, e);
}
Files.delete(uuidFile);
}
if (uniqueId == null) {
uniqueId = UUID.randomUUID();
}
}
BitSet dimensionMap = null;
if (rootDataView.contains(FORGE)) {
final DataView forgeView = rootDataView.getView(FORGE).get();
if (forgeView.contains(DIMENSION_DATA)) {
dimensionMap = new BitSet(LanternWorldManager.DIMENSION_MAP_SIZE);
final int[] intArray = (int[]) forgeView.getView(DIMENSION_DATA).get().get(DIMENSION_ARRAY).get();
for (int i = 0; i < intArray.length; i++) {
for (int j = 0; j < Integer.SIZE; j++) {
dimensionMap.set(i * Integer.SIZE + j, (intArray[i] & (1 << j)) != 0);
}
}
}
}
final Integer dimensionId = spongeContainer != null ? spongeContainer.getInt(DIMENSION_INDEX).orElse(null) : null;
return new LevelData(worldName, uniqueId, rootDataView, spongeRootDataView, dimensionId, dimensionMap);
}
Aggregations