Skip to content

Commit

Permalink
Add support for custom biome provider plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
benwoo1110 committed Jan 28, 2025
1 parent 13d75b1 commit 40cafbd
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,8 @@ final class CreateCommand extends CoreCommand {
.addAlias("-a")
.build());

private final CommandValueFlag<Biome> biomeFlag = flag(CommandValueFlag.builder("--biome", Biome.class)
private final CommandValueFlag<String> biomeFlag = flag(CommandValueFlag.builder("--biome", String.class)
.addAlias("-b")
.completion(input -> Lists.newArrayList(Registry.BIOME).stream()
.filter(biome -> biome != Biome.CUSTOM)
.map(biome -> biome.getKey().getKey())
.toList())
.context(biomeStr -> Registry.BIOME.get(NamespacedKey.minecraft(biomeStr)))
.build());

@Inject
Expand Down Expand Up @@ -122,7 +117,7 @@ void onCreateCommand(
issuer.sendInfo(MVCorei18n.CREATE_LOADING);

worldManager.createWorld(CreateWorldOptions.worldName(worldName)
.biome(parsedFlags.flagValue(biomeFlag, Biome.CUSTOM))
.biome(parsedFlags.flagValue(biomeFlag, ""))
.environment(environment)
.seed(parsedFlags.flagValue(seedFlag))
.worldType(parsedFlags.flagValue(worldTypeFlag, WorldType.NORMAL))
Expand All @@ -148,7 +143,7 @@ private void messageWorldDetails(MVCommandIssuer issuer, String worldName,
replace("{adjustSpawn}").with(String.valueOf(!parsedFlags.hasFlag(noAdjustSpawnFlag))));
if (parsedFlags.hasFlag(biomeFlag)) {
issuer.sendInfo(MVCorei18n.CREATE_PROPERTIES_BIOME,
replace("{biome}").with(parsedFlags.flagValue(biomeFlag, Biome.CUSTOM).name()));
replace("{biome}").with(parsedFlags.flagValue(biomeFlag)));
}
if (parsedFlags.hasFlag(generatorFlag)) {
issuer.sendInfo(MVCorei18n.CREATE_PROPERTIES_GENERATOR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,8 @@ final class ImportCommand extends CoreCommand {
.addAlias("-n")
.build());

private final CommandValueFlag<Biome> biomeFlag = flag(CommandValueFlag.builder("--biome", Biome.class)
private final CommandValueFlag<String> biomeFlag = flag(CommandValueFlag.builder("--biome", String.class)
.addAlias("-b")
.completion(input -> Lists.newArrayList(Registry.BIOME).stream()
.filter(biome -> biome != Biome.CUSTOM)
.map(biome -> biome.getKey().getKey())
.toList())
.context(biomeStr -> Registry.BIOME.get(NamespacedKey.minecraft(biomeStr)))
.build());

@Inject
Expand Down Expand Up @@ -91,7 +86,7 @@ void onImportCommand(

issuer.sendInfo(MVCorei18n.IMPORT_IMPORTING, Replace.WORLD.with(worldName));
worldManager.importWorld(ImportWorldOptions.worldName(worldName)
.biome(parsedFlags.flagValue(biomeFlag, Biome.CUSTOM))
.biome(parsedFlags.flagValue(biomeFlag, ""))
.environment(environment)
.generator(parsedFlags.flagValue(generatorFlag, String.class))
.useSpawnAdjust(!parsedFlags.hasFlag(noAdjustSpawnFlag)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private Map<String, String> getInfo(LoadedMultiverseWorld world) {
getEntryFeeInfo(outMap, world);
outMap.put("Respawn World", world.getRespawnWorldName());
outMap.put("World Type", world.getWorldType().get().toString());
outMap.put("Biome", world.getBiome() == null ? "@vanilla" : world.getBiome().getKey().getKey());
outMap.put("Biome", world.getBiome());
outMap.put("Generator", world.getGenerator());
outMap.put("Generate Structures", world.canGenerateStructures().get().toString());
outMap.put("World Scale", String.valueOf(world.getScale()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.bukkit.World;
import org.bukkit.WorldType;
import org.bukkit.entity.Player;
import org.bukkit.generator.BiomeProvider;
import org.jetbrains.annotations.NotNull;

import org.mvplugins.multiverse.core.teleportation.BlockSafety;
Expand Down Expand Up @@ -45,10 +44,6 @@ public final class LoadedMultiverseWorld extends MultiverseWorld {
private void setupWorldConfig(World world) {
worldConfig.setMVWorld(this);
worldConfig.load();
BiomeProvider biomeProvider = world.getBiomeProvider();
if (biomeProvider instanceof SingleBiomeProvider singleBiomeProvider) {
worldConfig.setBiome(singleBiomeProvider.getBiome());
}
worldConfig.setEnvironment(world.getEnvironment());
worldConfig.setSeed(world.getSeed());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import org.mvplugins.multiverse.core.configuration.handle.StringPropertyHandle;
Expand Down Expand Up @@ -237,7 +238,7 @@ public Try<Void> setBedRespawn(boolean bedRespawn) {
*
* @return The biome used for this world
*/
public @Nullable Biome getBiome() {
public @NotNull String getBiome() {
return worldConfig.getBiome();
}

Expand Down
133 changes: 74 additions & 59 deletions src/main/java/org/mvplugins/multiverse/core/world/WorldConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -49,67 +48,70 @@ final class WorldConfig {
this.configNodes = new WorldConfigNodes(multiverseCore);
this.configHandle = MemoryConfigurationHandle.builder(configSection, configNodes.getNodes())
.logger(Logging.getLogger())
.migrator(ConfigMigrator.builder(configNodes.version)
.addVersionMigrator(initialVersionMigrator())
.build())
.migrator(migrator())
.build();
this.stringPropertyHandle = new StringPropertyHandle(configHandle);
load();
}

private VersionMigrator initialVersionMigrator() {
return VersionMigrator.builder(1.0)
.addAction(MoveMigratorAction.of("adjustSpawn", "adjust-spawn"))
.addAction(BooleanMigratorAction.of("adjust-spawn"))
.addAction(MoveMigratorAction.of("allowFlight", "allow-flight"))
.addAction(BooleanMigratorAction.of("allow-flight"))
.addAction(MoveMigratorAction.of("allowWeather", "allow-weather"))
.addAction(BooleanMigratorAction.of("allow-weather"))
.addAction(MoveMigratorAction.of("autoHeal", "auto-heal"))
.addAction(BooleanMigratorAction.of("auto-heal"))
.addAction(MoveMigratorAction.of("autoLoad", "auto-load"))
.addAction(BooleanMigratorAction.of("auto-load"))
.addAction(MoveMigratorAction.of("bedRespawn", "bed-respawn"))
.addAction(BooleanMigratorAction.of("bed-respawn"))
//.addAction(MoveMigratorAction.of("difficulty", "difficulty"))
.addAction(MoveMigratorAction.of("entryfee.amount", "entry-fee.amount"))
.addAction(DoubleMigratorAction.of("entry-fee.amount"))
.addAction(MoveMigratorAction.of("entryfee.currency", "entry-fee.currency"))
.addAction(DeleteMigratorAction.of("entryfee"))
//.addAction(MoveMigratorAction.of("environment", "environment"))
.addAction(MoveMigratorAction.of("gameMode", "gamemode"))
//.addAction(MoveMigratorAction.of("generator", "generator"))
.addAction(NullStringMigratorAction.of("generator"))
//.addAction(MoveMigratorAction.of("hidden", "hidden"))
.addAction(BooleanMigratorAction.of("hidden"))
//.addAction(MoveMigratorAction.of("hunger", "hunger"))
.addAction(BooleanMigratorAction.of("hunger"))
.addAction(MoveMigratorAction.of("keepSpawnInMemory", "keep-spawn-in-memory"))
.addAction(BooleanMigratorAction.of("keep-spawn-in-memory"))
.addAction(MoveMigratorAction.of("playerLimit", "player-limit"))
.addAction(IntegerMigratorAction.of("player-limit"))
.addAction(MoveMigratorAction.of("portalForm", "portal-form"))
//.addAction(MoveMigratorAction.of("pvp", "pvp"))
.addAction(BooleanMigratorAction.of("pvp"))
.addAction(MoveMigratorAction.of("respawnWorld", "respawn-world"))
//.addAction(MoveMigratorAction.of("scale", "scale"))
.addAction(DoubleMigratorAction.of("scale"))
//.addAction(MoveMigratorAction.of("seed", "seed"))
.addAction(LongMigratorAction.of("seed"))
.addAction(MoveMigratorAction.of("spawnLocation", "spawn-location"))
//.addAction(MoveMigratorAction.of("spawning.animals.spawn", "spawning.animals.spawn"))
.addAction(BooleanMigratorAction.of("spawning.animals.spawn"))
.addAction(MoveMigratorAction.of("spawning.animals.spawnrate", "spawning.animals.tick-rate"))
.addAction(IntegerMigratorAction.of("spawning.animals.tick-rate"))
//.addAction(MoveMigratorAction.of("spawning.animals.exceptions", "spawning.animals.exceptions"))
//.addAction(MoveMigratorAction.of("spawning.monsters.spawn", "spawning.monsters.spawn"))
.addAction(BooleanMigratorAction.of("spawning.monsters.spawn"))
.addAction(MoveMigratorAction.of("spawning.monsters.spawnrate", "spawning.monsters.tick-rate"))
.addAction(IntegerMigratorAction.of("spawning.monsters.tick-rate"))
//.addAction(MoveMigratorAction.of("spawning.monsters.exceptions", "spawning.monsters.exceptions"))
.addAction(MoveMigratorAction.of("worldBlacklist", "world-blacklist"))
.addAction(new EntryFeeMigrator())
.addAction(new LegacyAliasMigrator())
private ConfigMigrator migrator() {
return ConfigMigrator.builder(configNodes.version)
.addVersionMigrator(VersionMigrator.builder(1.0)
.addAction(MoveMigratorAction.of("adjustSpawn", "adjust-spawn"))
.addAction(BooleanMigratorAction.of("adjust-spawn"))
.addAction(MoveMigratorAction.of("allowFlight", "allow-flight"))
.addAction(BooleanMigratorAction.of("allow-flight"))
.addAction(MoveMigratorAction.of("allowWeather", "allow-weather"))
.addAction(BooleanMigratorAction.of("allow-weather"))
.addAction(MoveMigratorAction.of("autoHeal", "auto-heal"))
.addAction(BooleanMigratorAction.of("auto-heal"))
.addAction(MoveMigratorAction.of("autoLoad", "auto-load"))
.addAction(BooleanMigratorAction.of("auto-load"))
.addAction(MoveMigratorAction.of("bedRespawn", "bed-respawn"))
.addAction(BooleanMigratorAction.of("bed-respawn"))
//.addAction(MoveMigratorAction.of("difficulty", "difficulty"))
.addAction(MoveMigratorAction.of("entryfee.amount", "entry-fee.amount"))
.addAction(DoubleMigratorAction.of("entry-fee.amount"))
.addAction(MoveMigratorAction.of("entryfee.currency", "entry-fee.currency"))
.addAction(DeleteMigratorAction.of("entryfee"))
//.addAction(MoveMigratorAction.of("environment", "environment"))
.addAction(MoveMigratorAction.of("gameMode", "gamemode"))
//.addAction(MoveMigratorAction.of("generator", "generator"))
.addAction(NullStringMigratorAction.of("generator"))
//.addAction(MoveMigratorAction.of("hidden", "hidden"))
.addAction(BooleanMigratorAction.of("hidden"))
//.addAction(MoveMigratorAction.of("hunger", "hunger"))
.addAction(BooleanMigratorAction.of("hunger"))
.addAction(MoveMigratorAction.of("keepSpawnInMemory", "keep-spawn-in-memory"))
.addAction(BooleanMigratorAction.of("keep-spawn-in-memory"))
.addAction(MoveMigratorAction.of("playerLimit", "player-limit"))
.addAction(IntegerMigratorAction.of("player-limit"))
.addAction(MoveMigratorAction.of("portalForm", "portal-form"))
//.addAction(MoveMigratorAction.of("pvp", "pvp"))
.addAction(BooleanMigratorAction.of("pvp"))
.addAction(MoveMigratorAction.of("respawnWorld", "respawn-world"))
//.addAction(MoveMigratorAction.of("scale", "scale"))
.addAction(DoubleMigratorAction.of("scale"))
//.addAction(MoveMigratorAction.of("seed", "seed"))
.addAction(LongMigratorAction.of("seed"))
.addAction(MoveMigratorAction.of("spawnLocation", "spawn-location"))
//.addAction(MoveMigratorAction.of("spawning.animals.spawn", "spawning.animals.spawn"))
.addAction(BooleanMigratorAction.of("spawning.animals.spawn"))
.addAction(MoveMigratorAction.of("spawning.animals.spawnrate", "spawning.animals.tick-rate"))
.addAction(IntegerMigratorAction.of("spawning.animals.tick-rate"))
//.addAction(MoveMigratorAction.of("spawning.animals.exceptions", "spawning.animals.exceptions"))
//.addAction(MoveMigratorAction.of("spawning.monsters.spawn", "spawning.monsters.spawn"))
.addAction(BooleanMigratorAction.of("spawning.monsters.spawn"))
.addAction(MoveMigratorAction.of("spawning.monsters.spawnrate", "spawning.monsters.tick-rate"))
.addAction(IntegerMigratorAction.of("spawning.monsters.tick-rate"))
//.addAction(MoveMigratorAction.of("spawning.monsters.exceptions", "spawning.monsters.exceptions"))
.addAction(MoveMigratorAction.of("worldBlacklist", "world-blacklist"))
.addAction(new EntryFeeMigrator())
.addAction(new LegacyAliasMigrator())
.build())
.addVersionMigrator(VersionMigrator.builder(1.1)
.addAction(new BiomeMigrator())
.build())
.build();
}

Expand Down Expand Up @@ -184,11 +186,11 @@ Try<Void> setAutoLoad(boolean autoLoad) {
return configHandle.set(configNodes.autoLoad, autoLoad);
}

Biome getBiome() {
String getBiome() {
return configHandle.get(configNodes.biome);
}

Try<Void> setBiome(Biome biome) {
Try<Void> setBiome(String biome) {
return configHandle.set(configNodes.biome, biome);
}

Expand Down Expand Up @@ -506,4 +508,17 @@ private enum EnglishChatStyle {

}
}

private static final class BiomeMigrator implements MigratorAction {
@Override
public void migrate(ConfigurationSection config) {
String biome = config.getString("biome", "");
if (biome.equals("@vanilla")) {
biome = "";
} else if (!biome.isEmpty()) {
biome = "@single:" + biome;
}
config.set("biome", biome);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,36 +111,9 @@ private <T> ConfigNode<T> node(ConfigNode.Builder<T, ?> nodeBuilder) {
final ConfigNode<Boolean> bedRespawn = node(ConfigNode.builder("bed-respawn", Boolean.class)
.defaultValue(true));

final ConfigNode<Biome> biome = node(ConfigNode.builder("biome", Biome.class)
.defaultValue(Biome.CUSTOM)
.name(null)
.serializer(new NodeSerializer<>() {
private static final String VANILLA_BIOME_BEHAVIOUR = "@vanilla";

@Override
public Biome deserialize(Object object, Class<Biome> type) {
if (object instanceof Biome) {
return (Biome) object;
}
try {
String biomeStr = String.valueOf(object);
if (biomeStr.equalsIgnoreCase(VANILLA_BIOME_BEHAVIOUR)) {
return null;
}
return Biome.valueOf(biomeStr.toUpperCase());
} catch (IllegalArgumentException e) {
return null;
}
}

@Override
public Object serialize(Biome biome, Class<Biome> type) {
if (biome == null || biome == Biome.CUSTOM) {
return VANILLA_BIOME_BEHAVIOUR;
}
return biome.name().toLowerCase();
}
}));
final ConfigNode<String> biome = node(ConfigNode.builder("biome", String.class)
.defaultValue("@error")
.name(null));

final ConfigNode<Difficulty> difficulty = node(ConfigNode.builder("difficulty", Difficulty.class)
.defaultValue(Difficulty.NORMAL)
Expand Down
Loading

0 comments on commit 40cafbd

Please sign in to comment.