Implement configurable transfer of data during regen

This commit is contained in:
Ben Woo 2023-09-07 11:50:30 +08:00
parent 9cb8724db2
commit 3ef72a993a
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
3 changed files with 47 additions and 34 deletions

View File

@ -10,7 +10,9 @@ import com.onarandombox.MultiverseCore.utils.result.Result;
import com.onarandombox.MultiverseCore.worldnew.config.WorldConfig; import com.onarandombox.MultiverseCore.worldnew.config.WorldConfig;
import com.onarandombox.MultiverseCore.worldnew.config.WorldsConfigManager; import com.onarandombox.MultiverseCore.worldnew.config.WorldsConfigManager;
import com.onarandombox.MultiverseCore.worldnew.generators.GeneratorProvider; import com.onarandombox.MultiverseCore.worldnew.generators.GeneratorProvider;
import com.onarandombox.MultiverseCore.worldnew.helpers.DataStore;
import com.onarandombox.MultiverseCore.worldnew.helpers.DataStore.GameRulesStore; import com.onarandombox.MultiverseCore.worldnew.helpers.DataStore.GameRulesStore;
import com.onarandombox.MultiverseCore.worldnew.helpers.DataTransfer;
import com.onarandombox.MultiverseCore.worldnew.helpers.FilesManipulator; import com.onarandombox.MultiverseCore.worldnew.helpers.FilesManipulator;
import com.onarandombox.MultiverseCore.worldnew.options.CreateWorldOptions; import com.onarandombox.MultiverseCore.worldnew.options.CreateWorldOptions;
import com.onarandombox.MultiverseCore.worldnew.options.ImportWorldOptions; import com.onarandombox.MultiverseCore.worldnew.options.ImportWorldOptions;
@ -490,8 +492,8 @@ public class WorldManager {
} }
// TODO: Configure option on whether to copy these // TODO: Configure option on whether to copy these
GameRulesStore gameRulesStore = GameRulesStore.createAndCopyFrom(world); // GameRulesStore gameRulesStore = GameRulesStore.createAndCopyFrom(world);
WorldConfigStore worldConfigStore = WorldConfigStore.createAndCopyFrom(world); // WorldConfigStore worldConfigStore = WorldConfigStore.createAndCopyFrom(world);
File worldFolder = world.getBukkitWorld().map(World::getWorldFolder).getOrNull(); // TODO: Check null? File worldFolder = world.getBukkitWorld().map(World::getWorldFolder).getOrNull(); // TODO: Check null?
File newWorldFolder = new File(Bukkit.getWorldContainer(), newWorldName); File newWorldFolder = new File(Bukkit.getWorldContainer(), newWorldName);
@ -505,8 +507,8 @@ public class WorldManager {
// TODO: Error handling // TODO: Error handling
getMVWorld(newWorldName).peek(newWorld -> { getMVWorld(newWorldName).peek(newWorld -> {
gameRulesStore.pasteTo(newWorld); // gameRulesStore.pasteTo(newWorld);
worldConfigStore.pasteTo(newWorld); // worldConfigStore.pasteTo(newWorld);
saveWorldsConfig(); saveWorldsConfig();
}); });
} }
@ -520,15 +522,23 @@ public class WorldManager {
// TODO: Teleport players out of world, and back in after regen // TODO: Teleport players out of world, and back in after regen
MVWorld world = options.world(); MVWorld world = options.world();
GameRulesStore gameRulesStore = GameRulesStore.createAndCopyFrom(world); DataTransfer<MVWorld> dataTransfer = new DataTransfer<>();
WorldConfigStore worldConfigStore = WorldConfigStore.createAndCopyFrom(world); if (options.keepWorldConfig()) {
dataTransfer.addDataStore(new WorldConfigStore(), world);
}
if (options.keepGameRule()) {
dataTransfer.addDataStore(new GameRulesStore(), world);
}
if (options.keepWorldBorder()) {
dataTransfer.addDataStore(new WorldBorderStore(), world);
}
CreateWorldOptions createWorldOptions = CreateWorldOptions.worldName(world.getName()) CreateWorldOptions createWorldOptions = CreateWorldOptions.worldName(world.getName())
.environment(world.getEnvironment()) .environment(world.getEnvironment())
.generateStructures(world.canGenerateStructures().getOrElse(true)) .generateStructures(world.canGenerateStructures().getOrElse(true))
.generator(world.getGenerator()) .generator(world.getGenerator())
.seed(options.seed()) .seed(options.seed())
.worldType(world.getWorldType().getOrElse(WorldType.NORMAL)); .worldType(world.getWorldType().getOrElse(WorldType.NORMAL));
var deleteResult = deleteWorld(world); var deleteResult = deleteWorld(world);
if (deleteResult.isFailure()) { if (deleteResult.isFailure()) {
@ -541,8 +551,7 @@ public class WorldManager {
} }
getMVWorld(createWorldOptions.worldName()).peek(newWorld -> { getMVWorld(createWorldOptions.worldName()).peek(newWorld -> {
gameRulesStore.pasteTo(newWorld); dataTransfer.pasteAllTo(newWorld);
worldConfigStore.pasteTo(newWorld);
saveWorldsConfig(); saveWorldsConfig();
}); });
return Result.success(RegenWorldResult.Success.REGENERATED, replace("{world}").with(world.getName())); return Result.success(RegenWorldResult.Success.REGENERATED, replace("{world}").with(world.getName()));

View File

@ -35,10 +35,6 @@ public interface DataStore<T> {
DataStore<T> pasteTo(T object); DataStore<T> pasteTo(T object);
class GameRulesStore implements DataStore<MVWorld> { class GameRulesStore implements DataStore<MVWorld> {
public static GameRulesStore createAndCopyFrom(MVWorld world) {
return new GameRulesStore().copyFrom(world);
}
private Map<GameRule<?>, Object> gameRuleMap; private Map<GameRule<?>, Object> gameRuleMap;
/** /**
@ -84,18 +80,14 @@ public interface DataStore<T> {
} }
} }
class WorldConfigStore implements DataStore<OfflineWorld> { class WorldConfigStore implements DataStore<MVWorld> {
public static WorldConfigStore createAndCopyFrom(OfflineWorld world) {
return new WorldConfigStore().copyFrom(world);
}
private Map<String, Object> configMap; private Map<String, Object> configMap;
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public WorldConfigStore copyFrom(OfflineWorld world) { public WorldConfigStore copyFrom(MVWorld world) {
this.configMap = new HashMap<>(); this.configMap = new HashMap<>();
world.getConfigurablePropertyNames().forEach(name -> { world.getConfigurablePropertyNames().forEach(name -> {
world.getProperty(name).peek(value -> configMap.put(name, value)).onFailure(e -> { world.getProperty(name).peek(value -> configMap.put(name, value)).onFailure(e -> {
@ -109,7 +101,7 @@ public interface DataStore<T> {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public WorldConfigStore pasteTo(OfflineWorld world) { public WorldConfigStore pasteTo(MVWorld world) {
if (configMap == null) { if (configMap == null) {
return this; return this;
} }
@ -124,16 +116,6 @@ public interface DataStore<T> {
} }
class WorldBorderStore implements DataStore<MVWorld> { class WorldBorderStore implements DataStore<MVWorld> {
/**
* Creates a new {@link WorldBorderStore} instance and copies the world border data from the given world.
*
* @param world The world to copy the world border data from.
* @return A new {@link WorldBorderStore} instance.
*/
public static WorldBorderStore createAndCopyFrom(MVWorld world) {
return new WorldBorderStore().copyFrom(world);
}
private double borderCenterX; private double borderCenterX;
private double borderCenterZ; private double borderCenterZ;
private double borderDamageAmount; private double borderDamageAmount;

View File

@ -0,0 +1,22 @@
package com.onarandombox.MultiverseCore.worldnew.helpers;
import java.util.ArrayList;
import java.util.List;
public class DataTransfer<T> {
private final List<DataStore<T>> dataStores;
public DataTransfer() {
this.dataStores = new ArrayList<>();
}
public DataTransfer<T> addDataStore(DataStore<T> dataStore, T object) {
this.dataStores.add(dataStore.copyFrom(object));
return this;
}
public DataTransfer<T> pasteAllTo(T object) {
this.dataStores.forEach(dataStore -> dataStore.pasteTo(object));
return this;
}
}