Search in sources :

Example 6 with SerializationBehavior

use of org.spongepowered.api.world.SerializationBehavior in project SpongeCommon by SpongePowered.

the class WorldTest method createRandomCheckerboardWorld.

private CommandResult createRandomCheckerboardWorld(final CommandContext context) {
    final WorldManager wm = Sponge.server().worldManager();
    final ServerPlayer player = (ServerPlayer) context.cause().root();
    final String owner = player.name();
    final Random random = player.world().random();
    final List<RegistryReference<Biome>> allBiomes = Sponge.server().registry(RegistryTypes.BIOME).streamEntries().map(RegistryEntry::asReference).collect(Collectors.toList());
    final List<RegistryReference<Biome>> biomes = IntStream.range(0, random.nextInt(allBiomes.size())).mapToObj(i -> allBiomes.get(random.nextInt(allBiomes.size()))).collect(Collectors.toList());
    final List<Structure> allStructures = RegistryTypes.STRUCTURE.get().stream().collect(Collectors.toList());
    final Map<Structure, SeparatedStructureConfig> abundantStructures = IntStream.range(0, random.nextInt(allStructures.size())).mapToObj(i -> allStructures.get(random.nextInt(allStructures.size()))).distinct().collect(Collectors.toMap(s -> s, s -> SeparatedStructureConfig.of(random.nextInt(3) + 2, 1, random.nextInt(10))));
    final Map<Structure, SeparatedStructureConfig> rareStructures = IntStream.range(0, random.nextInt(8) + 2).mapToObj(i -> allStructures.get(random.nextInt(allStructures.size()))).distinct().collect(Collectors.toMap(s -> s, s -> SeparatedStructureConfig.of(random.nextInt(10) + 6, 5, random.nextInt(10))));
    final StructureGenerationConfig structureConfig = StructureGenerationConfig.builder().addStructures(abundantStructures).addStructures(rareStructures).build();
    final NoiseConfig noiseConfig = NoiseConfig.builder().height(256).build();
    final NoiseGeneratorConfig noiseGenConfig = NoiseGeneratorConfig.builder().structureConfig(structureConfig).noiseConfig(noiseConfig).seaLevel(// 2 rolls
    random.nextInt(61 - 1) + 1 + random.nextInt(30)).build();
    final ResourceKey worldKey = ResourceKey.of(this.plugin, owner.toLowerCase());
    final WorldTemplate customTemplate = WorldTemplate.builder().from(WorldTemplate.overworld()).key(worldKey).worldType(WorldTypes.OVERWORLD).serializationBehavior(SerializationBehavior.NONE).loadOnStartup(false).performsSpawnLogic(true).displayName(Component.text("Custom world by " + owner)).generator(ChunkGenerator.noise(BiomeProvider.checkerboard(CheckerboardBiomeConfig.builder().addBiomes(biomes).scale(random.nextInt(16 - 2) + 2).build()), noiseGenConfig)).build();
    if (player.world().key().equals(worldKey)) {
        player.setLocation(ServerLocation.of(wm.defaultWorld(), wm.defaultWorld().properties().spawnPosition()));
    }
    context.sendMessage(Identity.nil(), Component.text("Generating your world..."));
    wm.deleteWorld(worldKey).thenCompose(b -> wm.loadWorld(customTemplate)).thenAccept(w -> this.transportToWorld(player, w)).exceptionally(e -> {
        context.sendMessage(Identity.nil(), Component.text("Failed to teleport!", NamedTextColor.DARK_RED));
        e.printStackTrace();
        return null;
    });
    return CommandResult.success();
}
Also used : IntStream(java.util.stream.IntStream) RegistryEntry(org.spongepowered.api.registry.RegistryEntry) PortalType(org.spongepowered.api.world.portal.PortalType) Command(org.spongepowered.api.command.Command) ServerWorld(org.spongepowered.api.world.server.ServerWorld) Game(org.spongepowered.api.Game) SerializationBehavior(org.spongepowered.api.world.SerializationBehavior) WorldType(org.spongepowered.api.world.WorldType) Inject(com.google.inject.Inject) Random(java.util.Random) Biome(org.spongepowered.api.world.biome.Biome) Title(net.kyori.adventure.title.Title) WorldTemplate(org.spongepowered.api.world.server.WorldTemplate) BiomeProvider(org.spongepowered.api.world.biome.provider.BiomeProvider) Parameter(org.spongepowered.api.command.parameter.Parameter) Component(net.kyori.adventure.text.Component) Map(java.util.Map) NoiseGeneratorConfig(org.spongepowered.api.world.generation.config.NoiseGeneratorConfig) ResourceKey(org.spongepowered.api.ResourceKey) ChunkGenerator(org.spongepowered.api.world.generation.ChunkGenerator) NoSuchElementException(java.util.NoSuchElementException) CommonParameters(org.spongepowered.api.command.parameter.CommonParameters) RegisterCommandEvent(org.spongepowered.api.event.lifecycle.RegisterCommandEvent) CommandResult(org.spongepowered.api.command.CommandResult) RegistryKey(org.spongepowered.api.registry.RegistryKey) Plugin(org.spongepowered.plugin.builtin.jvm.Plugin) WorldTypes(org.spongepowered.api.world.WorldTypes) WorldManager(org.spongepowered.api.world.server.WorldManager) Structure(org.spongepowered.api.world.generation.structure.Structure) Identity(net.kyori.adventure.identity.Identity) CheckerboardBiomeConfig(org.spongepowered.api.world.biome.provider.CheckerboardBiomeConfig) Sponge(org.spongepowered.api.Sponge) Axis(org.spongepowered.api.util.Axis) TypeToken(io.leangen.geantyref.TypeToken) RegistryTypes(org.spongepowered.api.registry.RegistryTypes) Collectors(java.util.stream.Collectors) NamedTextColor(net.kyori.adventure.text.format.NamedTextColor) SeparatedStructureConfig(org.spongepowered.api.world.generation.config.structure.SeparatedStructureConfig) List(java.util.List) RegistryReference(org.spongepowered.api.registry.RegistryReference) Vector3d(org.spongepowered.math.vector.Vector3d) PluginContainer(org.spongepowered.plugin.PluginContainer) StructureGenerationConfig(org.spongepowered.api.world.generation.config.structure.StructureGenerationConfig) CommandContext(org.spongepowered.api.command.parameter.CommandContext) Listener(org.spongepowered.api.event.Listener) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) NoiseConfig(org.spongepowered.api.world.generation.config.noise.NoiseConfig) ServerLocation(org.spongepowered.api.world.server.ServerLocation) NoiseGeneratorConfig(org.spongepowered.api.world.generation.config.NoiseGeneratorConfig) RegistryReference(org.spongepowered.api.registry.RegistryReference) NoiseConfig(org.spongepowered.api.world.generation.config.noise.NoiseConfig) WorldManager(org.spongepowered.api.world.server.WorldManager) ResourceKey(org.spongepowered.api.ResourceKey) StructureGenerationConfig(org.spongepowered.api.world.generation.config.structure.StructureGenerationConfig) Random(java.util.Random) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) SeparatedStructureConfig(org.spongepowered.api.world.generation.config.structure.SeparatedStructureConfig) Structure(org.spongepowered.api.world.generation.structure.Structure) WorldTemplate(org.spongepowered.api.world.server.WorldTemplate)

Aggregations

SerializationBehavior (org.spongepowered.api.world.SerializationBehavior)6 ServerLevel (net.minecraft.server.level.ServerLevel)3 Redirect (org.spongepowered.asm.mixin.injection.Redirect)3 PrimaryLevelDataBridge (org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge)3 Overwrite (org.spongepowered.asm.mixin.Overwrite)2 Inject (com.google.inject.Inject)1 TypeToken (io.leangen.geantyref.TypeToken)1 List (java.util.List)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 Random (java.util.Random)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Identity (net.kyori.adventure.identity.Identity)1 Component (net.kyori.adventure.text.Component)1 NamedTextColor (net.kyori.adventure.text.format.NamedTextColor)1 Title (net.kyori.adventure.title.Title)1 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)1 ServerChunkCache (net.minecraft.server.level.ServerChunkCache)1 PrimaryLevelData (net.minecraft.world.level.storage.PrimaryLevelData)1