Search in sources :

Example 1 with Lantern

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);
}
Also used : DataInputStream(java.io.DataInputStream) MoonPhase(org.lanternpowered.server.data.world.MoonPhase) DimensionType(org.spongepowered.api.world.DimensionType) DifficultyRegistryModule(org.lanternpowered.server.game.registry.type.world.DifficultyRegistryModule) DataQuery(org.spongepowered.api.data.DataQuery) GameModes(org.spongepowered.api.entity.living.player.gamemode.GameModes) WorldConfig(org.lanternpowered.server.config.world.WorldConfig) DataQueries(org.lanternpowered.server.data.DataQueries) LanternDifficulty(org.lanternpowered.server.world.difficulty.LanternDifficulty) ArrayList(java.util.ArrayList) NbtStreamUtils(org.lanternpowered.server.data.persistence.nbt.NbtStreamUtils) Gson(com.google.gson.Gson) SerializationBehaviors(org.spongepowered.api.world.SerializationBehaviors) Difficulties(org.spongepowered.api.world.difficulty.Difficulties) GameModeRegistryModule(org.lanternpowered.server.game.registry.type.entity.player.GameModeRegistryModule) Path(java.nio.file.Path) RuleDataTypes(org.lanternpowered.server.world.rules.RuleDataTypes) DimensionTypes(org.spongepowered.api.world.DimensionTypes) PortalAgentType(org.spongepowered.api.world.PortalAgentType) Nullable(javax.annotation.Nullable) GeneratorTypes(org.spongepowered.api.world.GeneratorTypes) RuleType(org.lanternpowered.server.world.rules.RuleType) Weather(org.spongepowered.api.world.weather.Weather) LanternWeather(org.lanternpowered.server.world.weather.LanternWeather) Files(java.nio.file.Files) PortalAgentTypes(org.spongepowered.api.world.PortalAgentTypes) IOHelper(org.lanternpowered.server.data.io.IOHelper) GeneratorType(org.spongepowered.api.world.GeneratorType) CatalogType(org.spongepowered.api.CatalogType) Sponge(org.spongepowered.api.Sponge) DataContainer(org.spongepowered.api.data.DataContainer) AbstractFlatGeneratorType(org.lanternpowered.server.world.gen.flat.AbstractFlatGeneratorType) IOException(java.io.IOException) UUID(java.util.UUID) JsonDataFormat(org.lanternpowered.server.data.persistence.json.JsonDataFormat) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Weathers(org.spongepowered.api.world.weather.Weathers) LanternGameMode(org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode) List(java.util.List) Object2IntMap(it.unimi.dsi.fastutil.objects.Object2IntMap) DataView(org.spongepowered.api.data.DataView) Lantern(org.lanternpowered.server.game.Lantern) LanternDimensionType(org.lanternpowered.server.world.dimension.LanternDimensionType) Vector3i(com.flowpowered.math.vector.Vector3i) Entry(java.util.Map.Entry) Optional(java.util.Optional) BitSet(java.util.BitSet) Path(java.nio.file.Path) FileNotFoundException(java.io.FileNotFoundException) BitSet(java.util.BitSet) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) DataView(org.spongepowered.api.data.DataView) UUID(java.util.UUID)

Aggregations

Vector3i (com.flowpowered.math.vector.Vector3i)1 Gson (com.google.gson.Gson)1 Object2IntMap (it.unimi.dsi.fastutil.objects.Object2IntMap)1 DataInputStream (java.io.DataInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 WorldConfig (org.lanternpowered.server.config.world.WorldConfig)1 DataQueries (org.lanternpowered.server.data.DataQueries)1 IOHelper (org.lanternpowered.server.data.io.IOHelper)1 JsonDataFormat (org.lanternpowered.server.data.persistence.json.JsonDataFormat)1