This is my code now:
WorldProperties worldProperties = Sponge.getServer().createWorldProperties("flatWorld",
WorldArchetype.builder()
.generator(GeneratorTypes.FLAT)
.generatorSettings(GeneratorTypes.FLAT.getGeneratorSettings().set(???))
.build());
Sponge.getServer().loadWorld(worldProperties);
If I want to generate a world with 3;30*minecraft\:bedrock;1
, what code I have to write in ???
My current SpongeAPI version is 7.0.0-SNAPSHOT
It looks like that generatorSettings are all ignored and there is no way to avoid this right now.
I decided to wait for the implement.
By the way, I looked at SpongeCommon and found that WorldInfo and WorldSettings both have generatorSetting, is this intended?
What you’re looking for is DataQuery.of("customSettings")
I investigated generatorSettings
and found that an issue was reported some time ago:
opened 09:53PM - 28 Jan 17 UTC
closed 01:31AM - 09 Oct 17 UTC
system: world
It seems that WorldArchetype gets its settings from server.properties always ins… tead of from the Archetype. This means that passing arguments into custom WorldGeneratorModifier objects (or custom GenerationType objects in the future) doesn't work.
Command showing issue is pasted below. Logger outputs:
[16:52:43] [Server thread/INFO] [cp-quest]: Expected generator settings:
customSettings="{\"akey\":55}"
mygensetting=myvalue
[16:52:43] [Server thread/INFO] [cp-quest]: GeneratorSettings for World1-1c388ce7-49f6-4730-80a6-b4028e40e167 is customSettings=""
```java
package com.chaoticplanes.quest.test.commands;
import com.chaoticplanes.quest.*;
import com.chaoticplanes.quest.framework.listeners.*;
import com.google.inject.*;
import ninja.leaping.configurate.*;
import ninja.leaping.configurate.hocon.*;
import org.spongepowered.api.*;
import org.spongepowered.api.command.*;
import org.spongepowered.api.command.args.*;
import org.spongepowered.api.command.spec.*;
import org.spongepowered.api.data.*;
import org.spongepowered.api.data.persistence.*;
import org.spongepowered.api.event.*;
import org.spongepowered.api.event.game.state.*;
import org.spongepowered.api.text.*;
import org.spongepowered.api.world.*;
import org.spongepowered.api.world.storage.*;
import java.io.*;
import java.util.*;
import org.slf4j.*;
/**
* I use this command when debugging by doing a hot load (CTRL-SHIFT-F9) to test out new ideas without
* having to restart the server. Eventually it should NOT be in the project.
*
* @author Drakmir
* @version 1.0
* @since 1.0
*/
public class TestCommandExecutor implements CommandExecutor, QuestListener
{
private final Logger _logger;
private final QuestPlugin _plugin;
/**
* Constructor for Test Command
*
* @param inPlugin The QuestPlugin object.
* @param inLogger The logger so we can output values.
*/
@Inject
private TestCommandExecutor(final QuestPlugin inPlugin, final Logger inLogger)
{
_logger = inLogger;
_plugin = inPlugin;
}
/**
* QuestListener event method that is called on GamePostInitializationEvent
* @param inEvent The post initialization event
*/
@Listener
public void onPostInitialization(GamePostInitializationEvent inEvent)
{
CommandSpec testSpecification = CommandSpec.builder()
.executor(this)
.arguments
(
GenericArguments.repeated(
GenericArguments.optional(
GenericArguments.string(Text.of("testarg"))
)
, 9999
)
)
.extendedDescription(Text.of("Use this to execute test commands. Allows for testing this without restarting the server by using hot swapping."))
.description(Text.of("Test Something!"))
.permission("cptest")
.build();
Sponge.getCommandManager().register(_plugin, testSpecification, "cptest");
}
/**
* Helper function to save the WorldProperties both to level.dat and the world.conf file.
*/
private void saveWorldSettings(final WorldProperties inWorldProperties)
{
Server server = Sponge.getServer();
server.saveWorldProperties(inWorldProperties);
Sponge.getCommandManager().process(server.getConsole(), "sponge save -w " + inWorldProperties.getWorldName());
}
/**
* The implementation of the CommandExecutor interface that will be called when the
* cptest command is issued.
*
* @param src The user / command block / console that triggered the cptest command.
* @param args The arguments passed to it.
* @return A CommandResult (typically success)
* @throws CommandException Exception thrown if there was an issue running the test command.
*/
@Override
public CommandResult execute(final CommandSource src, final CommandContext args) throws CommandException
{
try
{
final Server server = Sponge.getServer();
final MemoryDataContainer genSettings = new MemoryDataContainer();
genSettings.set(DataQuery.of("mygensetting"), "myvalue");
genSettings.set(DataQuery.of("customSettings"), "{\"akey\":55}");
// Now with a custom archetype!
final WorldArchetype customArchetype =
Sponge
.getRegistry()
.getType(WorldArchetype.class, "cp-test:customArchetype")
.orElseGet(() ->
{
return WorldArchetype.builder()
.generatorSettings(genSettings)
.build("cp-test:customArchetype", "customArchetype");
}
);
_logger.info("Expected generator settings:\n{}", dataContainerToString(genSettings));
WorldProperties worldProps = server.createWorldProperties("World1-" + UUID.randomUUID().toString(), customArchetype);
DataContainer generatorSettingsAfterCreation = worldProps.getGeneratorSettings();
_logger.info("GeneratorSettings for {} is {}", worldProps.getWorldName(), dataContainerToString(generatorSettingsAfterCreation));
}
catch (Exception inE)
{
_logger.debug("Error with cptest command", inE);
throw new CommandException(Text.of("error with cptest command"), inE);
}
return CommandResult.success();
}
private String dataContainerToString(final DataContainer inContainer) throws IOException
{
StringWriter writer = new StringWriter();
final DataTranslator<ConfigurationNode> configurationNodeTranslator = DataTranslators.CONFIGURATION_NODE;
final ConfigurationNode node = configurationNodeTranslator.translate(inContainer);
HoconConfigurationLoader loader = HoconConfigurationLoader.builder().setSink(() -> new BufferedWriter(writer)).build();
loader.save(node);
return writer.toString();
}
}
```
So I went ahead and fixed it
committed 12:43AM - 09 Oct 17 UTC
The following code now works:
WorldProperties worldProperties = Sponge.getServer().createWorldProperties("flatWorld",
WorldArchetype.builder()
.generator(GeneratorTypes.FLAT)
.generatorSettings(DataContainer.createNew().set(DataQuery.of("customSettings"), "3;30*minecraft:bedrock;1"))
.build("myworldarchetype", "My World Archetype"));
Sponge.getServer().loadWorld(worldProperties);
WorldSettings
is an internal minecraft class that is similar in functionality to Sponge’s WorldArchetype
, and likewise WorldInfo
is similar to WorldProperties
They both have a generator setting, but WorldArchetype
is used as a template for creating worlds whereas WorldProperties
is a collection of properties per world instance.
1 Like