use of org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge in project SpongeCommon by SpongePowered.
the class BlockMixin_EntityCollision method collision$initializeCollisionState.
@Override
public void collision$initializeCollisionState(final net.minecraft.world.level.Level world) {
final InheritableConfigHandle<WorldConfig> worldConfigAdapter = ((PrimaryLevelDataBridge) world.getLevelData()).bridge$configAdapter();
final ConfigHandle<CommonConfig> globalConfigAdapter = SpongeConfigs.getCommon();
final EntityCollisionCategory worldCollCat = worldConfigAdapter.get().entityCollision;
this.collision$setMaxCollisions(worldCollCat.maxEntitiesWithinAABB);
boolean requiresSave = false;
String[] ids = Sponge.game().registry(RegistryTypes.BLOCK_TYPE).valueKey((BlockType) this).toString().split(":");
String modId = ids[0];
String name = ids[1];
final EntityCollisionCategory.ModSubCategory worldCollMod = worldConfigAdapter.getOrCreateValue(s -> s.entityCollision.mods.get(modId), c -> {
// TODO: finish after populating?
final EntityCollisionCategory.ModSubCategory globalCollision = new EntityCollisionCategory.ModSubCategory(modId);
c.entityCollision.mods.put(modId, globalCollision);
globalCollision.blocks.put(name, this.collision$getMaxCollisions());
}, worldCollCat.autoPopulate);
if (worldCollMod != null) {
if (!worldCollMod.enabled) {
this.collision$setMaxCollisions(-1);
return;
}
// check mod overrides
Integer modCollisionMax = worldCollMod.blockDefault;
if (modCollisionMax != null) {
this.collision$setMaxCollisions(modCollisionMax);
}
// entity overrides
Integer blockMaxCollision = worldCollMod.blocks.get(name);
if (blockMaxCollision == null && worldCollCat.autoPopulate) {
worldCollMod.blocks.put(name, this.collision$getMaxCollisions());
requiresSave = true;
} else if (blockMaxCollision != null) {
this.collision$setMaxCollisions(blockMaxCollision);
}
}
// don't bother saving for negative values
if (this.collision$getMaxCollisions() <= 0) {
return;
}
if (requiresSave) {
globalConfigAdapter.save();
}
}
use of org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge in project SpongeCommon by SpongePowered.
the class LevelChunkMixin method impl$setTrackedUUID.
private void impl$setTrackedUUID(final BlockPos pos, final UUID uuid, final PlayerTracker.Type type, final BiConsumer<PlayerTracker, Integer> consumer) {
if (((LevelBridge) this.level).bridge$isFake()) {
return;
}
final PrimaryLevelDataBridge worldInfo = (PrimaryLevelDataBridge) this.level.getLevelData();
final int index = uuid == null ? -1 : worldInfo.bridge$getIndexForUniqueId(uuid);
if (pos.getY() <= 255) {
final short blockPos = Constants.Sponge.blockPosToShort(pos);
this.impl$computePlayerTracker(this.impl$trackedShortBlockPositions, blockPos, index, type, consumer);
return;
}
final int blockPos = Constants.Sponge.blockPosToInt(pos);
this.impl$computePlayerTracker(this.impl$trackedIntBlockPositions, blockPos, index, type, consumer);
}
use of org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge 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;
});
}
use of org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge 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);
}
use of org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge 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();
}
Aggregations