Search in sources :

Example 6 with WorldConfig

use of org.spongepowered.common.config.inheritable.WorldConfig in project SpongeCommon by SpongePowered.

the class SpongeWorldManager method loadLevel.

public void loadLevel() {
    final PrimaryLevelData defaultLevelData = (PrimaryLevelData) this.server.getWorldData();
    final WorldGenSettings defaultGenerationSettings = defaultLevelData.worldGenSettings();
    final LevelSettings defaultLevelSettings = ((PrimaryLevelDataAccessor) defaultLevelData).accessor$settings();
    final MappedRegistry<LevelStem> templates = defaultGenerationSettings.dimensions();
    final boolean multiworldEnabled = this.server.isSingleplayer() || this.server.isNetherEnabled();
    if (!multiworldEnabled) {
        SpongeCommon.logger().warn("The option 'allow-nether' has been set to 'false' in the server.properties. " + "Multi-World support has been disabled and no worlds besides the default world will be loaded.");
    }
    for (final RegistryEntry<LevelStem> entry : ((Registry<LevelStem>) (Object) templates).streamEntries().collect(Collectors.toList())) {
        final ResourceKey worldKey = entry.key();
        final LevelStem template = entry.value();
        final LevelStemBridge templateBridge = (LevelStemBridge) (Object) template;
        ((ResourceKeyBridge) templateBridge).bridge$setKey(worldKey);
        final boolean isDefaultWorld = this.isDefaultWorld(worldKey);
        if (!isDefaultWorld && !multiworldEnabled) {
            continue;
        }
        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);
        if (!isDefaultWorld && !templateBridge.bridge$loadOnStartup()) {
            SpongeCommon.logger().warn("World '{}' has been disabled from loading at startup. Skipping...", worldKey);
            continue;
        }
        final String directoryName = this.getDirectoryName(worldKey);
        final boolean isVanillaSubLevel = this.isVanillaSubWorld(directoryName);
        final LevelStorageSource.LevelStorageAccess storageSource;
        if (isDefaultWorld) {
            storageSource = ((MinecraftServerAccessor) this.server).accessor$storageSource();
        } else {
            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) {
                throw new RuntimeException(String.format("Failed to create level data for world '%s'!", worldKey), e);
            }
        }
        PrimaryLevelData levelData;
        final boolean isDebugGeneration;
        if (isDefaultWorld) {
            levelData = defaultLevelData;
            isDebugGeneration = defaultGenerationSettings.isDebug();
        } else {
            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 = ((WorldGenSettingsBridge) defaultLevelData.worldGenSettings()).bridge$copy();
                }
                isDebugGeneration = generationSettings.isDebug();
                ((DimensionGeneratorSettingsAccessor) generationSettings).accessor$dimensions(new MappedRegistry<>(net.minecraft.core.Registry.LEVEL_STEM_REGISTRY, Lifecycle.stable()));
                levelData = new PrimaryLevelData(levelSettings, generationSettings, Lifecycle.stable());
            } else {
                isDebugGeneration = levelData.worldGenSettings().isDebug();
            }
        }
        ((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 long seed = BiomeManager.obfuscateSeed(levelData.worldGenSettings().seed());
        final net.minecraft.resources.ResourceKey<Level> registryKey = SpongeWorldManager.createRegistryKey(worldKey);
        final ChunkProgressListener chunkStatusListener = ((MinecraftServerAccessor) this.server).accessor$progressListenerFactory().create(11);
        final List<CustomSpawner> spawners;
        if (isDefaultWorld) {
            spawners = ImmutableList.of(new PhantomSpawner(), new PatrolSpawner(), new CatSpawner(), new VillageSiege(), new WanderingTraderSpawner(levelData));
        } else {
            spawners = ImmutableList.of();
        }
        final ServerLevel world = new ServerLevel(this.server, ((MinecraftServerAccessor) this.server).accessor$executor(), storageSource, levelData, registryKey, (DimensionType) worldType, chunkStatusListener, template.generator(), isDebugGeneration, seed, spawners, true);
        // Ensure that the world border is registered.
        world.getWorldBorder().applySettings(levelData.getWorldBorder());
        this.worlds.put(registryKey, world);
        this.prepareWorld(world, isDebugGeneration);
    }
    ((MinecraftServerAccessor) this.server).invoker$forceDifficulty();
    for (final Map.Entry<net.minecraft.resources.ResourceKey<Level>, ServerLevel> entry : this.worlds.entrySet()) {
        try {
            this.postWorldLoad(entry.getValue(), true).get();
        } catch (final InterruptedException | ExecutionException e) {
            throw new IllegalStateException(e);
        }
    }
    ((SpongeUserManager) Sponge.server().userManager()).init();
    ((SpongeServer) SpongeCommon.server()).getPlayerDataManager().load();
}
Also used : DimensionGeneratorSettingsAccessor(org.spongepowered.common.accessor.world.gen.DimensionGeneratorSettingsAccessor) MinecraftServerAccessor(org.spongepowered.common.accessor.server.MinecraftServerAccessor) Difficulty(net.minecraft.world.Difficulty) GameType(net.minecraft.world.level.GameType) PrimaryLevelData(net.minecraft.world.level.storage.PrimaryLevelData) WorldGenSettings(net.minecraft.world.level.levelgen.WorldGenSettings) WanderingTraderSpawner(net.minecraft.world.entity.npc.WanderingTraderSpawner) WorldType(org.spongepowered.api.world.WorldType) VillageSiege(net.minecraft.world.entity.ai.village.VillageSiege) LevelStorageSource(net.minecraft.world.level.storage.LevelStorageSource) PrimaryLevelDataBridge(org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge) ResourceKey(org.spongepowered.api.ResourceKey) JsonObject(com.google.gson.JsonObject) Level(net.minecraft.world.level.Level) ServerLevel(net.minecraft.server.level.ServerLevel) LevelSettings(net.minecraft.world.level.LevelSettings) Map(java.util.Map) ServerLevel(net.minecraft.server.level.ServerLevel) ChunkProgressListener(net.minecraft.server.level.progress.ChunkProgressListener) WorldConfig(org.spongepowered.common.config.inheritable.WorldConfig) PatrolSpawner(net.minecraft.world.level.levelgen.PatrolSpawner) CustomSpawner(net.minecraft.world.level.CustomSpawner) CatSpawner(net.minecraft.world.entity.npc.CatSpawner) LevelStemBridge(org.spongepowered.common.bridge.world.level.dimension.LevelStemBridge) ExecutionException(java.util.concurrent.ExecutionException) SpongeUserManager(org.spongepowered.common.user.SpongeUserManager) ResourceKeyBridge(org.spongepowered.common.bridge.ResourceKeyBridge) GameRules(net.minecraft.world.level.GameRules) IOException(java.io.IOException) LevelStem(net.minecraft.world.level.dimension.LevelStem) Tag(net.minecraft.nbt.Tag) PrimaryLevelDataAccessor(org.spongepowered.common.accessor.world.level.storage.PrimaryLevelDataAccessor) PhantomSpawner(net.minecraft.world.level.levelgen.PhantomSpawner)

Example 7 with WorldConfig

use of org.spongepowered.common.config.inheritable.WorldConfig in project SpongeCommon by SpongePowered.

the class EntityActivationRange method initializeEntityActivationState.

/**
 * Initialize entity activation state.
 *
 * @param entity Entity to check
 */
public static void initializeEntityActivationState(final Entity entity) {
    final ActivationCapabilityBridge spongeEntity = (ActivationCapabilityBridge) entity;
    if (entity.level.isClientSide()) {
        return;
    }
    // types that should always be active
    if (entity instanceof Player && !((PlatformEntityBridge) entity).bridge$isFakePlayer() || entity instanceof ThrowableProjectile || entity instanceof EnderDragon || entity instanceof EnderDragonPart || entity instanceof WitherBoss || entity instanceof AbstractHurtingProjectile || entity instanceof LightningBolt || entity instanceof PrimedTnt || entity instanceof Painting || entity instanceof EndCrystal || entity instanceof FireworkRocketEntity || // Always tick falling blocks
    entity instanceof FallingBlockEntity) {
        return;
    }
    final InheritableConfigHandle<WorldConfig> configAdapter = SpongeGameConfigs.getForWorld(entity.level);
    final EntityActivationRangeCategory config = configAdapter.get().entityActivationRange;
    final EntityTypeBridge type = (EntityTypeBridge) entity.getType();
    final ResourceLocation key = EntityType.getKey(entity.getType());
    final byte activationType = spongeEntity.activation$getActivationType();
    final String activationTypeName = EntityActivationRange.activationTypeMappings.getOrDefault(activationType, "misc");
    if (!type.bridge$isActivationRangeInitialized()) {
        EntityActivationRange.addEntityToConfig(config.autoPopulate, key, activationType, activationTypeName);
        type.bridge$setActivationRangeInitialized(true);
    }
    final EntityActivationRangeCategory.ModSubCategory entityMod = config.mods.get(key.getNamespace());
    final int defaultActivationRange = config.globalRanges.get(activationTypeName);
    if (entityMod == null) {
        // use default activation range
        spongeEntity.activation$setActivationRange(defaultActivationRange);
        if (defaultActivationRange > 0) {
            spongeEntity.activation$setDefaultActivationState(false);
        }
    } else {
        if (!entityMod.enabled) {
            spongeEntity.activation$setDefaultActivationState(true);
            return;
        }
        final Integer defaultModActivationRange = entityMod.defaultRanges.get(activationTypeName);
        final Integer entityActivationRange = entityMod.entities.get(key.getPath());
        if (defaultModActivationRange != null && entityActivationRange == null) {
            spongeEntity.activation$setActivationRange(defaultModActivationRange);
            if (defaultModActivationRange > 0) {
                spongeEntity.activation$setDefaultActivationState(false);
            }
        } else if (entityActivationRange != null) {
            spongeEntity.activation$setActivationRange(entityActivationRange);
            if (entityActivationRange > 0) {
                spongeEntity.activation$setDefaultActivationState(false);
            }
        }
    }
}
Also used : PrimedTnt(net.minecraft.world.entity.item.PrimedTnt) FallingBlockEntity(net.minecraft.world.entity.item.FallingBlockEntity) Player(net.minecraft.world.entity.player.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) WorldConfig(org.spongepowered.common.config.inheritable.WorldConfig) EntityTypeBridge(org.spongepowered.common.bridge.world.entity.EntityTypeBridge) EndCrystal(net.minecraft.world.entity.boss.enderdragon.EndCrystal) FireworkRocketEntity(net.minecraft.world.entity.projectile.FireworkRocketEntity) Painting(net.minecraft.world.entity.decoration.Painting) LightningBolt(net.minecraft.world.entity.LightningBolt) EnderDragon(net.minecraft.world.entity.boss.enderdragon.EnderDragon) ActivationCapabilityBridge(org.spongepowered.common.bridge.activation.ActivationCapabilityBridge) WitherBoss(net.minecraft.world.entity.boss.wither.WitherBoss) EnderDragonPart(net.minecraft.world.entity.boss.EnderDragonPart) AbstractHurtingProjectile(net.minecraft.world.entity.projectile.AbstractHurtingProjectile) ResourceLocation(net.minecraft.resources.ResourceLocation) ThrowableProjectile(net.minecraft.world.entity.projectile.ThrowableProjectile) EntityActivationRangeCategory(org.spongepowered.common.config.inheritable.EntityActivationRangeCategory)

Aggregations

WorldConfig (org.spongepowered.common.config.inheritable.WorldConfig)7 PrimaryLevelDataBridge (org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge)5 IOException (java.io.IOException)3 ServerLevel (net.minecraft.server.level.ServerLevel)3 JsonObject (com.google.gson.JsonObject)2 Path (java.nio.file.Path)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 Tag (net.minecraft.nbt.Tag)2 ResourceLocation (net.minecraft.resources.ResourceLocation)2 ChunkProgressListener (net.minecraft.server.level.progress.ChunkProgressListener)2 Difficulty (net.minecraft.world.Difficulty)2 VillageSiege (net.minecraft.world.entity.ai.village.VillageSiege)2 CatSpawner (net.minecraft.world.entity.npc.CatSpawner)2 WanderingTraderSpawner (net.minecraft.world.entity.npc.WanderingTraderSpawner)2 CustomSpawner (net.minecraft.world.level.CustomSpawner)2 GameRules (net.minecraft.world.level.GameRules)2 GameType (net.minecraft.world.level.GameType)2 Level (net.minecraft.world.level.Level)2 LevelSettings (net.minecraft.world.level.LevelSettings)2