Search in sources :

Example 1 with GlowWorldBorder

use of net.glowstone.GlowWorldBorder in project Glowstone by GlowstoneMC.

the class NbtWorldMetadataService method readWorldData.

@Override
public WorldFinalValues readWorldData() {
    // determine UUID of world
    UUID uid = null;
    File uuidFile = new File(dir, UUID_FILE);
    if (uuidFile.exists()) {
        try (DataInputStream in = new DataInputStream(new FileInputStream(uuidFile))) {
            uid = new UUID(in.readLong(), in.readLong());
        } catch (IOException e) {
            handleWorldException(UUID_FILE, e);
        }
    }
    if (uid == null) {
        uid = UUID.randomUUID();
    }
    // read in world information
    CompoundTag level = new CompoundTag();
    File levelFile = new File(dir, LEVEL_FILE);
    if (levelFile.exists()) {
        try (NbtInputStream in = new NbtInputStream(new FileInputStream(levelFile))) {
            CompoundTag levelOuter = in.readCompound();
            level = levelOuter.tryGetCompound("Data").orElseGet(() -> {
                ConsoleMessages.Warn.Io.NO_WORLD_DATA_TAG.log(world.getName());
                return levelOuter;
            });
        } catch (IOException e) {
            handleWorldException(LEVEL_FILE, e);
        }
    }
    // seed
    final long seed = level.tryGetLong("RandomSeed").orElse(0L);
    // time of day and weather status
    if (level.readBoolean("thundering", world::setThundering)) {
        level.remove("thundering");
    }
    if (level.readBoolean("raining", world::setStorm)) {
        level.remove("raining");
    }
    if (level.readInt("thunderTime", world::setThunderDuration)) {
        level.remove("thunderTime");
    }
    if (level.readInt("rainTime", world::setWeatherDuration)) {
        level.remove("rainTime");
    }
    if (level.readLong("Time", world::setFullTime)) {
        level.remove("Time");
    }
    if (level.readLong("DayTime", world::setTime)) {
        level.remove("DayTime");
    }
    if (level.readString("generatorName", generatorName -> world.setWorldType(WorldType.getByName(generatorName)))) {
        level.remove("generatorName");
    }
    // spawn position
    if (level.isInt("SpawnX") && level.isInt("SpawnY") && level.isInt("SpawnZ")) {
        world.setSpawnLocation(level.getInt("SpawnX"), level.getInt("SpawnY"), level.getInt("SpawnZ"), false);
        level.remove("SpawnX");
        level.remove("SpawnY");
        level.remove("SpawnZ");
    }
    // game rules
    if (level.readCompound("GameRules", gameRules -> gameRules.getValue().keySet().stream().filter(gameRules::isString).forEach(key -> world.setGameRuleValue(key, gameRules.getString(key))))) {
        level.remove("GameRules");
    }
    // world border
    Location borderCenter = new Location(world, 0, 0, 0);
    if (level.readDouble("BorderCenterX", borderCenter::setX)) {
        level.remove("BorderCenterX");
    }
    if (level.readDouble("BorderCenterZ", borderCenter::setZ)) {
        level.remove("BorderCenterZ");
    }
    GlowWorldBorder worldBorder = world.getWorldBorder();
    worldBorder.setCenter(borderCenter);
    if (level.readDouble("BorderSize", worldBorder::setSize)) {
        level.remove("BorderSize");
    }
    if (level.isDouble("BorderSizeLerpTarget") && level.isLong("BorderSizeLerpTime")) {
        worldBorder.setSize(level.getDouble("BorderSizeLerpTarget"), level.getLong("BorderSizeLerpTime"));
        level.remove("BorderSizeLerpTarget");
        level.remove("BorderSizeLerpTime");
    }
    if (level.readDouble("BorderSafeZone", worldBorder::setDamageBuffer)) {
        level.remove("BorderSafeZone");
    }
    if (level.readDouble("BorderWarningTime", time -> worldBorder.setWarningTime((int) time))) {
        level.remove("BorderWarningTime");
    }
    if (level.readDouble("BorderWarningBlocks", distance -> worldBorder.setWarningDistance((int) distance))) {
        level.remove("BorderWarningBlocks");
    }
    if (level.readDouble("BorderDamagePerBlock", worldBorder::setDamageAmount)) {
        level.remove("BorderDamagePerBlock");
    }
    // strip single-player Player tag if it exists
    if (level.isCompound("Player")) {
        ConsoleMessages.Warn.Io.REMOVING_SINGLE_PLAYER.log(world.getName());
        level.remove("Player");
    }
    // save unknown tags for later
    unknownTags = level;
    return new WorldFinalValues(seed, uid);
}
Also used : CompoundTag(net.glowstone.util.nbt.CompoundTag) DataInputStream(java.io.DataInputStream) NbtOutputStream(net.glowstone.util.nbt.NbtOutputStream) NbtInputStream(net.glowstone.util.nbt.NbtInputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ConsoleMessages(net.glowstone.i18n.ConsoleMessages) FileInputStream(java.io.FileInputStream) UUID(java.util.UUID) WorldType(org.bukkit.WorldType) File(java.io.File) GlowWorldBorder(net.glowstone.GlowWorldBorder) ServerProvider(net.glowstone.ServerProvider) Calendar(java.util.Calendar) WorldMetadataService(net.glowstone.io.WorldMetadataService) Location(org.bukkit.Location) DataOutputStream(java.io.DataOutputStream) Server(org.bukkit.Server) GlowWorld(net.glowstone.GlowWorld) GlowWorldBorder(net.glowstone.GlowWorldBorder) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) NbtInputStream(net.glowstone.util.nbt.NbtInputStream) FileInputStream(java.io.FileInputStream) UUID(java.util.UUID) File(java.io.File) CompoundTag(net.glowstone.util.nbt.CompoundTag) Location(org.bukkit.Location)

Aggregations

DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Calendar (java.util.Calendar)1 UUID (java.util.UUID)1 GlowWorld (net.glowstone.GlowWorld)1 GlowWorldBorder (net.glowstone.GlowWorldBorder)1 ServerProvider (net.glowstone.ServerProvider)1 ConsoleMessages (net.glowstone.i18n.ConsoleMessages)1 WorldMetadataService (net.glowstone.io.WorldMetadataService)1 CompoundTag (net.glowstone.util.nbt.CompoundTag)1 NbtInputStream (net.glowstone.util.nbt.NbtInputStream)1 NbtOutputStream (net.glowstone.util.nbt.NbtOutputStream)1 Location (org.bukkit.Location)1 Server (org.bukkit.Server)1 WorldType (org.bukkit.WorldType)1