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.WorldsConfigManager;
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.DataTransfer;
import com.onarandombox.MultiverseCore.worldnew.helpers.FilesManipulator;
import com.onarandombox.MultiverseCore.worldnew.options.CreateWorldOptions;
import com.onarandombox.MultiverseCore.worldnew.options.ImportWorldOptions;
@ -490,8 +492,8 @@ public class WorldManager {
}
// TODO: Configure option on whether to copy these
GameRulesStore gameRulesStore = GameRulesStore.createAndCopyFrom(world);
WorldConfigStore worldConfigStore = WorldConfigStore.createAndCopyFrom(world);
// GameRulesStore gameRulesStore = GameRulesStore.createAndCopyFrom(world);
// WorldConfigStore worldConfigStore = WorldConfigStore.createAndCopyFrom(world);
File worldFolder = world.getBukkitWorld().map(World::getWorldFolder).getOrNull(); // TODO: Check null?
File newWorldFolder = new File(Bukkit.getWorldContainer(), newWorldName);
@ -505,8 +507,8 @@ public class WorldManager {
// TODO: Error handling
getMVWorld(newWorldName).peek(newWorld -> {
gameRulesStore.pasteTo(newWorld);
worldConfigStore.pasteTo(newWorld);
// gameRulesStore.pasteTo(newWorld);
// worldConfigStore.pasteTo(newWorld);
saveWorldsConfig();
});
}
@ -520,8 +522,16 @@ public class WorldManager {
// TODO: Teleport players out of world, and back in after regen
MVWorld world = options.world();
GameRulesStore gameRulesStore = GameRulesStore.createAndCopyFrom(world);
WorldConfigStore worldConfigStore = WorldConfigStore.createAndCopyFrom(world);
DataTransfer<MVWorld> dataTransfer = new DataTransfer<>();
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())
.environment(world.getEnvironment())
@ -541,8 +551,7 @@ public class WorldManager {
}
getMVWorld(createWorldOptions.worldName()).peek(newWorld -> {
gameRulesStore.pasteTo(newWorld);
worldConfigStore.pasteTo(newWorld);
dataTransfer.pasteAllTo(newWorld);
saveWorldsConfig();
});
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);
class GameRulesStore implements DataStore<MVWorld> {
public static GameRulesStore createAndCopyFrom(MVWorld world) {
return new GameRulesStore().copyFrom(world);
}
private Map<GameRule<?>, Object> gameRuleMap;
/**
@ -84,18 +80,14 @@ public interface DataStore<T> {
}
}
class WorldConfigStore implements DataStore<OfflineWorld> {
public static WorldConfigStore createAndCopyFrom(OfflineWorld world) {
return new WorldConfigStore().copyFrom(world);
}
class WorldConfigStore implements DataStore<MVWorld> {
private Map<String, Object> configMap;
/**
* {@inheritDoc}
*/
@Override
public WorldConfigStore copyFrom(OfflineWorld world) {
public WorldConfigStore copyFrom(MVWorld world) {
this.configMap = new HashMap<>();
world.getConfigurablePropertyNames().forEach(name -> {
world.getProperty(name).peek(value -> configMap.put(name, value)).onFailure(e -> {
@ -109,7 +101,7 @@ public interface DataStore<T> {
* {@inheritDoc}
*/
@Override
public WorldConfigStore pasteTo(OfflineWorld world) {
public WorldConfigStore pasteTo(MVWorld world) {
if (configMap == null) {
return this;
}
@ -124,16 +116,6 @@ public interface DataStore<T> {
}
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 borderCenterZ;
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;
}
}