Search in sources :

Example 91 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.

the class TransactionSink method logBlockDrops.

@SuppressWarnings("ConstantConditions")
default EffectTransactor logBlockDrops(final Level serverWorld, final BlockPos pos, final BlockState state, @Nullable final BlockEntity tileEntity) {
    final WeakReference<ServerLevel> worldRef = new WeakReference<>((ServerLevel) serverWorld);
    final Supplier<ServerLevel> worldSupplier = () -> Objects.requireNonNull(worldRef.get(), "ServerWorld dereferenced");
    final SpongeBlockSnapshot original = TrackingUtil.createPooledSnapshot(state, pos, BlockChangeFlags.NONE, Constants.World.DEFAULT_BLOCK_CHANGE_LIMIT, tileEntity, worldSupplier, Optional::empty, Optional::empty);
    original.blockChange = BlockChange.MODIFY;
    final PrepareBlockDropsTransaction transaction = new PrepareBlockDropsTransaction(pos, state, original);
    this.logTransaction(transaction);
    return this.pushEffect(new ResultingTransactionBySideEffect(PrepareBlockDrops.getInstance()));
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) Optional(java.util.Optional) PrepareBlockDropsTransaction(org.spongepowered.common.event.tracking.context.transaction.block.PrepareBlockDropsTransaction) WeakReference(java.lang.ref.WeakReference)

Example 92 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.

the class TransactionSink method logEntitySpawn.

@SuppressWarnings("ConstantConditions")
default void logEntitySpawn(final PhaseContext<@NonNull ?> current, final TrackedWorldBridge serverWorld, final Entity entityIn) {
    final WeakReference<ServerLevel> worldRef = new WeakReference<>((ServerLevel) serverWorld);
    final Supplier<ServerLevel> worldSupplier = () -> Objects.requireNonNull(worldRef.get(), "ServerWorld dereferenced");
    final Supplier<SpawnType> contextualType = current.getSpawnTypeForTransaction(entityIn);
    final SpawnEntityTransaction transaction = new SpawnEntityTransaction(worldSupplier, entityIn, contextualType);
    this.logTransaction(transaction);
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) SpawnType(org.spongepowered.api.event.cause.entity.SpawnType) SpawnEntityTransaction(org.spongepowered.common.event.tracking.context.transaction.world.SpawnEntityTransaction) WeakReference(java.lang.ref.WeakReference)

Example 93 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.

the class SpongeWorldManager method loadSpawnChunksAsync.

private CompletableFuture<ServerLevel> loadSpawnChunksAsync(final ServerLevel world) {
    final BlockPos spawnPoint = world.getSharedSpawnPos();
    final ChunkPos chunkPos = new ChunkPos(spawnPoint);
    final ServerChunkCache serverChunkProvider = world.getChunkSource();
    serverChunkProvider.getLightEngine().setTaskPerBatch(500);
    final int borderRadius = 11;
    final int diameter = ((borderRadius - 1) * 2) + 1;
    final int spawnChunks = diameter * diameter;
    serverChunkProvider.addRegionTicket(SpongeWorldManager.SPAWN_CHUNKS, chunkPos, borderRadius, world.dimension().location());
    final CompletableFuture<ServerLevel> generationFuture = new CompletableFuture<>();
    Sponge.asyncScheduler().submit(Task.builder().plugin(Launch.instance().platformPlugin()).execute(task -> {
        if (serverChunkProvider.getTickingGenerated() >= spawnChunks) {
            Sponge.server().scheduler().submit(Task.builder().plugin(Launch.instance().platformPlugin()).execute(() -> generationFuture.complete(world)).build());
            // Notify the future that we are done
            // And cancel this task
            task.cancel();
            MinecraftServerAccessor.accessor$LOGGER().info("Done preparing start region for world '{}' ({})", world.dimension().location(), RegistryTypes.WORLD_TYPE.get().valueKey((WorldType) world.dimensionType()));
        }
    }).interval(10, TimeUnit.MILLISECONDS).build());
    return generationFuture.thenApply(v -> {
        this.updateForcedChunks(world, serverChunkProvider);
        serverChunkProvider.getLightEngine().setTaskPerBatch(5);
        // Sponge Start - Release the chunk ticket if spawn is not set to be kept loaded...
        if (!((PrimaryLevelDataBridge) world.getLevelData()).bridge$performsSpawnLogic()) {
            serverChunkProvider.removeRegionTicket(SpongeWorldManager.SPAWN_CHUNKS, chunkPos, 11, world.dimension().location());
        }
        return world;
    });
}
Also used : ServerLevelBridge(org.spongepowered.common.bridge.server.level.ServerLevelBridge) SpongeGameConfigs(org.spongepowered.common.config.SpongeGameConfigs) WorldConfig(org.spongepowered.common.config.inheritable.WorldConfig) DimensionGeneratorSettingsAccessor(org.spongepowered.common.accessor.world.gen.DimensionGeneratorSettingsAccessor) MinecraftServer(net.minecraft.server.MinecraftServer) FutureUtil(org.spongepowered.common.util.FutureUtil) Map(java.util.Map) Path(java.nio.file.Path) WanderingTraderSpawner(net.minecraft.world.entity.npc.WanderingTraderSpawner) BootstrapProperties(org.spongepowered.common.server.BootstrapProperties) WorldManager(org.spongepowered.api.world.server.WorldManager) ForcedChunksSavedData(net.minecraft.world.level.ForcedChunksSavedData) InheritableConfigHandle(org.spongepowered.common.config.inheritable.InheritableConfigHandle) RegistryAccess(net.minecraft.core.RegistryAccess) FileVisitResult(java.nio.file.FileVisitResult) PlatformHooks(org.spongepowered.common.hooks.PlatformHooks) Stream(java.util.stream.Stream) TicketType(net.minecraft.server.level.TicketType) MappedRegistry(net.minecraft.core.MappedRegistry) GameType(net.minecraft.world.level.GameType) CrashReport(net.minecraft.CrashReport) CustomSpawner(net.minecraft.world.level.CustomSpawner) RegistryEntry(org.spongepowered.api.registry.RegistryEntry) LevelStorageSource_LevelStorageAccessAccessor(org.spongepowered.common.accessor.world.level.storage.LevelStorageSource_LevelStorageAccessAccessor) Registry(org.spongepowered.api.registry.Registry) WorldGenSettings(net.minecraft.world.level.levelgen.WorldGenSettings) PrimaryLevelDataBridge(org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge) JsonParser(com.google.gson.JsonParser) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) VillageSiege(net.minecraft.world.entity.ai.village.VillageSiege) ResourceKey(org.spongepowered.api.ResourceKey) Server(org.spongepowered.api.Server) DataResult(com.mojang.serialization.DataResult) Difficulty(net.minecraft.world.Difficulty) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) IOException(java.io.IOException) Lifecycle(com.mojang.serialization.Lifecycle) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Decoder(com.mojang.serialization.Decoder) RegistryTypes(org.spongepowered.api.registry.RegistryTypes) InputStreamReader(java.io.InputStreamReader) Pair(com.mojang.datafixers.util.Pair) File(java.io.File) ChunkPos(net.minecraft.world.level.ChunkPos) ExecutionException(java.util.concurrent.ExecutionException) RegistryReadOps(net.minecraft.resources.RegistryReadOps) DataPackSerializer(org.spongepowered.common.datapack.DataPackSerializer) LevelStem(net.minecraft.world.level.dimension.LevelStem) LevelResource(net.minecraft.world.level.storage.LevelResource) ResourceLocation(net.minecraft.resources.ResourceLocation) JsonObject(com.google.gson.JsonObject) SpongeServer(org.spongepowered.common.SpongeServer) MinecraftServerAccessor(org.spongepowered.common.accessor.server.MinecraftServerAccessor) PrimaryLevelDataAccessor(org.spongepowered.common.accessor.world.level.storage.PrimaryLevelDataAccessor) JsonOps(com.mojang.serialization.JsonOps) DynamicOps(com.mojang.serialization.DynamicOps) PatrolSpawner(net.minecraft.world.level.levelgen.PatrolSpawner) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) WorldData(net.minecraft.world.level.storage.WorldData) BiomeManager(net.minecraft.world.level.biome.BiomeManager) WorldTypes(org.spongepowered.api.world.WorldTypes) ChunkProgressListener(net.minecraft.server.level.progress.ChunkProgressListener) PrimaryLevelData(net.minecraft.world.level.storage.PrimaryLevelData) Collection(java.util.Collection) Launch(org.spongepowered.common.launch.Launch) GameRules(net.minecraft.world.level.GameRules) Sponge(org.spongepowered.api.Sponge) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) PhantomSpawner(net.minecraft.world.level.levelgen.PhantomSpawner) Objects(java.util.Objects) Util(net.minecraft.Util) List(java.util.List) CommandStorage(net.minecraft.world.level.storage.CommandStorage) BlockPos(net.minecraft.core.BlockPos) Optional(java.util.Optional) Level(net.minecraft.world.level.Level) LevelStemBridge(org.spongepowered.common.bridge.world.level.dimension.LevelStemBridge) FilenameUtils(org.apache.commons.io.FilenameUtils) Tag(net.minecraft.nbt.Tag) ServerChunkCache(net.minecraft.server.level.ServerChunkCache) Constants(org.spongepowered.common.util.Constants) WorldType(org.spongepowered.api.world.WorldType) CompletableFuture(java.util.concurrent.CompletableFuture) DimensionType(net.minecraft.world.level.dimension.DimensionType) ServerLevel(net.minecraft.server.level.ServerLevel) ServerWorldProperties(org.spongepowered.api.world.server.storage.ServerWorldProperties) OptionalInt(java.util.OptionalInt) JsonElement(com.google.gson.JsonElement) WorldTemplate(org.spongepowered.api.world.server.WorldTemplate) ImmutableList(com.google.common.collect.ImmutableList) LongIterator(it.unimi.dsi.fastutil.longs.LongIterator) Task(org.spongepowered.api.scheduler.Task) ResourceKeyBridge(org.spongepowered.common.bridge.ResourceKeyBridge) LevelStorageSource(net.minecraft.world.level.storage.LevelStorageSource) Features(net.minecraft.data.worldgen.Features) ReportedException(net.minecraft.ReportedException) SpongeUserManager(org.spongepowered.common.user.SpongeUserManager) SpongeCommon(org.spongepowered.common.SpongeCommon) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) CatSpawner(net.minecraft.world.entity.npc.CatSpawner) WorldGenSettingsBridge(org.spongepowered.common.bridge.world.level.levelgen.WorldGenSettingsBridge) TimeUnit(java.util.concurrent.TimeUnit) LevelSettings(net.minecraft.world.level.LevelSettings) Comparator(java.util.Comparator) Collections(java.util.Collections) RegistryWriteOps(net.minecraft.resources.RegistryWriteOps) InputStream(java.io.InputStream) ServerLevel(net.minecraft.server.level.ServerLevel) CompletableFuture(java.util.concurrent.CompletableFuture) BlockPos(net.minecraft.core.BlockPos) ChunkPos(net.minecraft.world.level.ChunkPos) ServerChunkCache(net.minecraft.server.level.ServerChunkCache)

Example 94 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.

the class SpongeWorldManager method loadWorld0.

private CompletableFuture<org.spongepowered.api.world.server.ServerWorld> loadWorld0(final net.minecraft.resources.ResourceKey<Level> registryKey, final LevelStem template, final WorldGenSettings generatorSettings) {
    final PrimaryLevelData defaultLevelData = (PrimaryLevelData) this.server.getWorldData();
    final LevelSettings defaultLevelSettings = ((PrimaryLevelDataAccessor) defaultLevelData).accessor$settings();
    final LevelStemBridge templateBridge = (LevelStemBridge) (Object) template;
    final ResourceKey worldKey = ((ResourceKeyBridge) templateBridge).bridge$getKey();
    final WorldType worldType = (WorldType) template.type();
    final ResourceKey worldTypeKey = RegistryTypes.WORLD_TYPE.get().valueKey((WorldType) template.type());
    MinecraftServerAccessor.accessor$LOGGER().info("Loading world '{}' ({})", worldKey, worldTypeKey);
    final String directoryName = this.getDirectoryName(worldKey);
    final boolean isVanillaSubLevel = this.isVanillaSubWorld(directoryName);
    final LevelStorageSource.LevelStorageAccess storageSource;
    try {
        if (isVanillaSubLevel) {
            storageSource = LevelStorageSource.createDefault(this.defaultWorldDirectory).createAccess(directoryName);
        } else {
            storageSource = LevelStorageSource.createDefault(this.customWorldsDirectory).createAccess(worldKey.namespace() + File.separator + worldKey.value());
        }
    } catch (final IOException e) {
        e.printStackTrace();
        return FutureUtil.completedWithException(new RuntimeException(String.format("Failed to create level data for world '%s'!", worldKey), e));
    }
    PrimaryLevelData levelData;
    levelData = (PrimaryLevelData) storageSource.getDataTag((DynamicOps<Tag>) BootstrapProperties.worldSettingsAdapter, defaultLevelSettings.getDataPackConfig());
    if (levelData == null) {
        final LevelSettings levelSettings;
        final WorldGenSettings generationSettings;
        if (this.server.isDemo()) {
            levelSettings = MinecraftServer.DEMO_SETTINGS;
            generationSettings = WorldGenSettings.demoSettings(BootstrapProperties.registries);
        } else {
            levelSettings = new LevelSettings(directoryName, (GameType) (Object) BootstrapProperties.gameMode.get(Sponge.game()), templateBridge.bridge$hardcore().orElse(BootstrapProperties.hardcore), (Difficulty) (Object) BootstrapProperties.difficulty.get(Sponge.game()), templateBridge.bridge$commands().orElse(BootstrapProperties.commands), new GameRules(), defaultLevelData.getDataPackConfig());
            generationSettings = generatorSettings;
        }
        levelData = new PrimaryLevelData(levelSettings, generationSettings, Lifecycle.stable());
    }
    ((PrimaryLevelDataBridge) levelData).bridge$populateFromDimension(template);
    final InheritableConfigHandle<WorldConfig> configAdapter = SpongeGameConfigs.createWorld(worldTypeKey, worldKey);
    ((PrimaryLevelDataBridge) levelData).bridge$configAdapter(configAdapter);
    levelData.setModdedInfo(this.server.getServerModName(), this.server.getModdedStatus().isPresent());
    final boolean isDebugGeneration = levelData.worldGenSettings().isDebug();
    final long seed = BiomeManager.obfuscateSeed(levelData.worldGenSettings().seed());
    final ChunkProgressListener chunkStatusListener = ((MinecraftServerAccessor) this.server).accessor$progressListenerFactory().create(11);
    final ServerLevel world = new ServerLevel(this.server, ((MinecraftServerAccessor) this.server).accessor$executor(), storageSource, levelData, registryKey, (DimensionType) worldType, chunkStatusListener, template.generator(), isDebugGeneration, seed, ImmutableList.of(), true);
    this.worlds.put(registryKey, world);
    return SpongeCommon.asyncScheduler().submit(() -> this.prepareWorld(world, isDebugGeneration)).thenApply(w -> {
        ((MinecraftServerAccessor) this.server).invoker$forceDifficulty();
        return w;
    }).thenCompose(w -> this.postWorldLoad(w, false)).thenApply(w -> (org.spongepowered.api.world.server.ServerWorld) w);
}
Also used : MinecraftServerAccessor(org.spongepowered.common.accessor.server.MinecraftServerAccessor) ServerLevelBridge(org.spongepowered.common.bridge.server.level.ServerLevelBridge) SpongeGameConfigs(org.spongepowered.common.config.SpongeGameConfigs) WorldConfig(org.spongepowered.common.config.inheritable.WorldConfig) DimensionGeneratorSettingsAccessor(org.spongepowered.common.accessor.world.gen.DimensionGeneratorSettingsAccessor) MinecraftServer(net.minecraft.server.MinecraftServer) FutureUtil(org.spongepowered.common.util.FutureUtil) Map(java.util.Map) Path(java.nio.file.Path) WanderingTraderSpawner(net.minecraft.world.entity.npc.WanderingTraderSpawner) BootstrapProperties(org.spongepowered.common.server.BootstrapProperties) WorldManager(org.spongepowered.api.world.server.WorldManager) ForcedChunksSavedData(net.minecraft.world.level.ForcedChunksSavedData) InheritableConfigHandle(org.spongepowered.common.config.inheritable.InheritableConfigHandle) RegistryAccess(net.minecraft.core.RegistryAccess) FileVisitResult(java.nio.file.FileVisitResult) PlatformHooks(org.spongepowered.common.hooks.PlatformHooks) Stream(java.util.stream.Stream) TicketType(net.minecraft.server.level.TicketType) MappedRegistry(net.minecraft.core.MappedRegistry) GameType(net.minecraft.world.level.GameType) CrashReport(net.minecraft.CrashReport) CustomSpawner(net.minecraft.world.level.CustomSpawner) RegistryEntry(org.spongepowered.api.registry.RegistryEntry) LevelStorageSource_LevelStorageAccessAccessor(org.spongepowered.common.accessor.world.level.storage.LevelStorageSource_LevelStorageAccessAccessor) Registry(org.spongepowered.api.registry.Registry) WorldGenSettings(net.minecraft.world.level.levelgen.WorldGenSettings) PrimaryLevelDataBridge(org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge) JsonParser(com.google.gson.JsonParser) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) VillageSiege(net.minecraft.world.entity.ai.village.VillageSiege) ResourceKey(org.spongepowered.api.ResourceKey) Server(org.spongepowered.api.Server) DataResult(com.mojang.serialization.DataResult) Difficulty(net.minecraft.world.Difficulty) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) IOException(java.io.IOException) Lifecycle(com.mojang.serialization.Lifecycle) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Decoder(com.mojang.serialization.Decoder) RegistryTypes(org.spongepowered.api.registry.RegistryTypes) InputStreamReader(java.io.InputStreamReader) Pair(com.mojang.datafixers.util.Pair) File(java.io.File) ChunkPos(net.minecraft.world.level.ChunkPos) ExecutionException(java.util.concurrent.ExecutionException) RegistryReadOps(net.minecraft.resources.RegistryReadOps) DataPackSerializer(org.spongepowered.common.datapack.DataPackSerializer) LevelStem(net.minecraft.world.level.dimension.LevelStem) LevelResource(net.minecraft.world.level.storage.LevelResource) ResourceLocation(net.minecraft.resources.ResourceLocation) JsonObject(com.google.gson.JsonObject) SpongeServer(org.spongepowered.common.SpongeServer) MinecraftServerAccessor(org.spongepowered.common.accessor.server.MinecraftServerAccessor) PrimaryLevelDataAccessor(org.spongepowered.common.accessor.world.level.storage.PrimaryLevelDataAccessor) JsonOps(com.mojang.serialization.JsonOps) DynamicOps(com.mojang.serialization.DynamicOps) PatrolSpawner(net.minecraft.world.level.levelgen.PatrolSpawner) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) WorldData(net.minecraft.world.level.storage.WorldData) BiomeManager(net.minecraft.world.level.biome.BiomeManager) WorldTypes(org.spongepowered.api.world.WorldTypes) ChunkProgressListener(net.minecraft.server.level.progress.ChunkProgressListener) PrimaryLevelData(net.minecraft.world.level.storage.PrimaryLevelData) Collection(java.util.Collection) Launch(org.spongepowered.common.launch.Launch) GameRules(net.minecraft.world.level.GameRules) Sponge(org.spongepowered.api.Sponge) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) PhantomSpawner(net.minecraft.world.level.levelgen.PhantomSpawner) Objects(java.util.Objects) Util(net.minecraft.Util) List(java.util.List) CommandStorage(net.minecraft.world.level.storage.CommandStorage) BlockPos(net.minecraft.core.BlockPos) Optional(java.util.Optional) Level(net.minecraft.world.level.Level) LevelStemBridge(org.spongepowered.common.bridge.world.level.dimension.LevelStemBridge) FilenameUtils(org.apache.commons.io.FilenameUtils) Tag(net.minecraft.nbt.Tag) ServerChunkCache(net.minecraft.server.level.ServerChunkCache) Constants(org.spongepowered.common.util.Constants) WorldType(org.spongepowered.api.world.WorldType) CompletableFuture(java.util.concurrent.CompletableFuture) DimensionType(net.minecraft.world.level.dimension.DimensionType) ServerLevel(net.minecraft.server.level.ServerLevel) ServerWorldProperties(org.spongepowered.api.world.server.storage.ServerWorldProperties) OptionalInt(java.util.OptionalInt) JsonElement(com.google.gson.JsonElement) WorldTemplate(org.spongepowered.api.world.server.WorldTemplate) ImmutableList(com.google.common.collect.ImmutableList) LongIterator(it.unimi.dsi.fastutil.longs.LongIterator) Task(org.spongepowered.api.scheduler.Task) ResourceKeyBridge(org.spongepowered.common.bridge.ResourceKeyBridge) LevelStorageSource(net.minecraft.world.level.storage.LevelStorageSource) Features(net.minecraft.data.worldgen.Features) ReportedException(net.minecraft.ReportedException) SpongeUserManager(org.spongepowered.common.user.SpongeUserManager) SpongeCommon(org.spongepowered.common.SpongeCommon) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) CatSpawner(net.minecraft.world.entity.npc.CatSpawner) WorldGenSettingsBridge(org.spongepowered.common.bridge.world.level.levelgen.WorldGenSettingsBridge) TimeUnit(java.util.concurrent.TimeUnit) LevelSettings(net.minecraft.world.level.LevelSettings) Comparator(java.util.Comparator) Collections(java.util.Collections) RegistryWriteOps(net.minecraft.resources.RegistryWriteOps) InputStream(java.io.InputStream) ServerLevel(net.minecraft.server.level.ServerLevel) ChunkProgressListener(net.minecraft.server.level.progress.ChunkProgressListener) Difficulty(net.minecraft.world.Difficulty) WorldConfig(org.spongepowered.common.config.inheritable.WorldConfig) GameType(net.minecraft.world.level.GameType) PrimaryLevelData(net.minecraft.world.level.storage.PrimaryLevelData) WorldGenSettings(net.minecraft.world.level.levelgen.WorldGenSettings) WorldType(org.spongepowered.api.world.WorldType) LevelStemBridge(org.spongepowered.common.bridge.world.level.dimension.LevelStemBridge) ResourceKeyBridge(org.spongepowered.common.bridge.ResourceKeyBridge) GameRules(net.minecraft.world.level.GameRules) LevelStorageSource(net.minecraft.world.level.storage.LevelStorageSource) IOException(java.io.IOException) PrimaryLevelDataBridge(org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge) ResourceKey(org.spongepowered.api.ResourceKey) LevelSettings(net.minecraft.world.level.LevelSettings) Tag(net.minecraft.nbt.Tag) PrimaryLevelDataAccessor(org.spongepowered.common.accessor.world.level.storage.PrimaryLevelDataAccessor)

Example 95 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.

the class SpongeWorldManager method copyWorld.

@Override
public CompletableFuture<Boolean> copyWorld(final ResourceKey key, final ResourceKey copyKey) {
    final net.minecraft.resources.ResourceKey<Level> registryKey = SpongeWorldManager.createRegistryKey(Objects.requireNonNull(key, "key"));
    final net.minecraft.resources.ResourceKey<Level> copyRegistryKey = SpongeWorldManager.createRegistryKey(Objects.requireNonNull(copyKey, "copyKey"));
    if (Level.OVERWORLD.equals(copyRegistryKey)) {
        return CompletableFuture.completedFuture(false);
    }
    if (!this.worldExists(key)) {
        return CompletableFuture.completedFuture(false);
    }
    if (this.worldExists(copyKey)) {
        return CompletableFuture.completedFuture(false);
    }
    final ServerLevel loadedWorld = this.worlds.get(registryKey);
    boolean disableLevelSaving = false;
    if (loadedWorld != null) {
        disableLevelSaving = loadedWorld.noSave;
        loadedWorld.save(null, true, loadedWorld.noSave);
        loadedWorld.noSave = true;
    }
    final boolean isDefaultWorld = this.isDefaultWorld(key);
    final boolean isVanillaWorld = this.isVanillaWorld(key);
    final String directoryName = this.getDirectoryName(key);
    final Path originalDirectory = isDefaultWorld ? this.defaultWorldDirectory : isVanillaWorld ? this.defaultWorldDirectory.resolve(directoryName) : this.customWorldsDirectory.resolve(key.namespace()).resolve(key.value());
    final boolean isVanillaCopyWorld = this.isVanillaWorld(copyKey);
    final String copyDirectoryName = this.getDirectoryName(copyKey);
    final Path copyDirectory = isVanillaCopyWorld ? this.defaultWorldDirectory.resolve(copyDirectoryName) : this.customWorldsDirectory.resolve(copyKey.namespace()).resolve(copyKey.value());
    try {
        Files.walkFileTree(originalDirectory, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException {
                // Silly recursion if the default world is being copied
                if (dir.getFileName().toString().equals(Constants.Sponge.World.DIMENSIONS_DIRECTORY)) {
                    return FileVisitResult.SKIP_SUBTREE;
                }
                // Silly copying of vanilla sub worlds if the default world is being copied
                if (isDefaultWorld && SpongeWorldManager.this.isVanillaSubWorld(dir.getFileName().toString())) {
                    return FileVisitResult.SKIP_SUBTREE;
                }
                final Path relativize = originalDirectory.relativize(dir);
                final Path directory = copyDirectory.resolve(relativize);
                Files.createDirectories(directory);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
                final String fileName = file.getFileName().toString();
                // Do not copy backups (not relevant anymore)
                if (fileName.equals(Constants.Sponge.World.LEVEL_SPONGE_DAT_OLD)) {
                    return FileVisitResult.CONTINUE;
                }
                if (fileName.equals(Constants.World.LEVEL_DAT_OLD)) {
                    return FileVisitResult.CONTINUE;
                }
                Files.copy(file, copyDirectory.resolve(originalDirectory.relativize(file)), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (final IOException e) {
        // Bail the whole deal if we hit IO problems!
        try {
            Files.deleteIfExists(copyDirectory);
        } catch (final IOException ignore) {
        }
        return FutureUtil.completedWithException(e);
    }
    if (loadedWorld != null) {
        loadedWorld.noSave = disableLevelSaving;
    }
    final Path dimensionTemplate = this.getDataPackFile(key);
    final Path copiedDimensionTemplate = this.getDataPackFile(copyKey);
    try {
        Files.createDirectories(copiedDimensionTemplate.getParent());
        Files.copy(dimensionTemplate, copiedDimensionTemplate);
    } catch (final IOException e) {
        FutureUtil.completedWithException(e);
    }
    final JsonObject fixedObject;
    try (final InputStream stream = Files.newInputStream(copiedDimensionTemplate);
        final InputStreamReader reader = new InputStreamReader(stream)) {
        final JsonParser parser = new JsonParser();
        final JsonElement element = parser.parse(reader);
        final JsonObject root = element.getAsJsonObject();
        final JsonObject spongeData = root.getAsJsonObject("#sponge");
        spongeData.remove("unique_id");
        fixedObject = root;
    } catch (final IOException e) {
        return FutureUtil.completedWithException(e);
    }
    if (fixedObject != null) {
        try (final BufferedWriter writer = Files.newBufferedWriter(copiedDimensionTemplate)) {
            writer.write(fixedObject.toString());
        } catch (final IOException e) {
            FutureUtil.completedWithException(e);
        }
    }
    return CompletableFuture.completedFuture(true);
}
Also used : Path(java.nio.file.Path) ServerLevel(net.minecraft.server.level.ServerLevel) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) JsonElement(com.google.gson.JsonElement) Level(net.minecraft.world.level.Level) ServerLevel(net.minecraft.server.level.ServerLevel) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) JsonParser(com.google.gson.JsonParser)

Aggregations

ServerLevel (net.minecraft.server.level.ServerLevel)115 BlockPos (net.minecraft.core.BlockPos)30 Nullable (org.checkerframework.checker.nullness.qual.Nullable)21 Level (net.minecraft.world.level.Level)20 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)16 Entity (net.minecraft.world.entity.Entity)14 ServerPlayer (net.minecraft.server.level.ServerPlayer)11 ItemStack (net.minecraft.world.item.ItemStack)10 SpongeBlockSnapshot (org.spongepowered.common.block.SpongeBlockSnapshot)10 IOException (java.io.IOException)9 LevelChunk (net.minecraft.world.level.chunk.LevelChunk)9 CraftWorld (org.bukkit.craftbukkit.v1_18_R1.CraftWorld)9 ArrayList (java.util.ArrayList)8 BlockState (net.minecraft.world.level.block.state.BlockState)8 List (java.util.List)7 Optional (java.util.Optional)7 UUID (java.util.UUID)7 Vec3 (net.minecraft.world.phys.Vec3)7 CraftWorld (org.bukkit.craftbukkit.v1_17_R1.CraftWorld)7 Cause (org.spongepowered.api.event.Cause)7