feat: save and load flat world
package nullengine.world.provider;
+import configuration.Config;
+import configuration.parser.ConfigParsers;
+import nullengine.block.Block;
import nullengine.game.Game;
+import nullengine.registry.Registries;
import nullengine.world.BaseWorldProvider;
import nullengine.world.World;
import nullengine.world.WorldCommon;
@@ -10,18 +14,35 @@ import nullengine.world.impl.FlatWorldCreationSetting;
import javax.annotation.Nonnull;
import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
public class FlatWorldProvider extends BaseWorldProvider {
@Nonnull
@Override
public World create(@Nonnull Game game, @Nonnull Path storagePath, @Nonnull String name, @Nonnull WorldCreationSetting creationSetting) {
FlatWorldCreationSetting setting = (FlatWorldCreationSetting) creationSetting;
+ Config config = new Config();
+ List<String> layers = new ArrayList<>();
+ for (Block block : setting.getLayers()) {
+ layers.add(block.getName().getUniqueName());
+ }
+ config.set("layers", layers);
+ ConfigParsers.save(storagePath.resolve("world.json"), config);
return new WorldCommon(game, this, storagePath, creationSetting, new FlatChunkGenerator(setting.getLayers()));
}
@Nonnull
@Override
public World load(@Nonnull Game game, @Nonnull Path storagePath) {
- return null;
+ Config config = ConfigParsers.load(storagePath.resolve("world.json"));
+ List<Object> layers = config.getList("layers", List.of());
+ Block[] blocks = new Block[layers.size()];
+ for (int i = 0; i < layers.size(); i++) {
+ blocks[i] = Registries.getBlockRegistry().getValue((String) layers.get(i));
+ }
+ FlatWorldCreationSetting creationSetting = new FlatWorldCreationSetting();
+ creationSetting.layers(blocks);
+ return new WorldCommon(game, this, storagePath, creationSetting, new FlatChunkGenerator(blocks));
}
}
