Implement basic regen command

This commit is contained in:
Ben Woo 2023-09-07 00:27:25 +08:00
parent af2fc7c630
commit 3f075169a4
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
3 changed files with 55 additions and 26 deletions

View File

@ -1,19 +1,14 @@
package com.onarandombox.MultiverseCore.commands;
import java.util.Collections;
import java.util.Random;
import co.aikar.commands.BukkitCommandIssuer;
import co.aikar.commands.MessageType;
import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandCompletion;
import co.aikar.commands.annotation.CommandPermission;
import co.aikar.commands.annotation.Conditions;
import co.aikar.commands.annotation.Description;
import co.aikar.commands.annotation.Optional;
import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Syntax;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.commandtools.MVCommandManager;
import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand;
import com.onarandombox.MultiverseCore.commandtools.flags.CommandFlag;
@ -22,18 +17,23 @@ import com.onarandombox.MultiverseCore.commandtools.flags.CommandValueFlag;
import com.onarandombox.MultiverseCore.commandtools.flags.ParsedCommandFlags;
import com.onarandombox.MultiverseCore.commandtools.queue.QueuedCommand;
import com.onarandombox.MultiverseCore.utils.MVCorei18n;
import com.onarandombox.MultiverseCore.worldnew.MVWorld;
import com.onarandombox.MultiverseCore.worldnew.WorldManager;
import jakarta.inject.Inject;
import org.jetbrains.annotations.NotNull;
import org.jvnet.hk2.annotations.Service;
import java.util.Collections;
import java.util.Random;
@Service
@CommandAlias("mv")
public class RegenCommand extends MultiverseCommand {
private final MVWorldManager worldManager;
private final WorldManager worldManager;
@Inject
public RegenCommand(@NotNull MVCommandManager commandManager, @NotNull MVWorldManager worldManager) {
public RegenCommand(@NotNull MVCommandManager commandManager, @NotNull WorldManager worldManager) {
super(commandManager);
this.worldManager = worldManager;
@ -51,15 +51,14 @@ public class RegenCommand extends MultiverseCommand {
@Subcommand("regen")
@CommandPermission("multiverse.core.regen")
@CommandCompletion("@mvworlds:scope=both @flags:groupName=mvregen")
@CommandCompletion("@mvworlds:scope=loaded @flags:groupName=mvregen")
@Syntax("<world> --seed [seed] --keep-gamerules")
@Description("{@@mv-core.regen.description}")
public void onRegenCommand(BukkitCommandIssuer issuer,
@Conditions("worldname:scope=both")
@Syntax("<world>")
@Description("{@@mv-core.regen.world.description}")
String worldName,
MVWorld world,
@Optional
@Syntax("--seed [seed] --keep-gamerules")
@ -71,27 +70,15 @@ public class RegenCommand extends MultiverseCommand {
this.commandManager.getCommandQueueManager().addToQueue(new QueuedCommand(
issuer.getIssuer(),
() -> {
issuer.sendInfo(MVCorei18n.REGEN_REGENERATING,
"{world}", worldName);
if (!this.worldManager.regenWorld(
worldName,
parsedFlags.hasFlag("--seed"),
!parsedFlags.hasFlagValue("--seed"),
parsedFlags.flagValue("--seed", String.class),
parsedFlags.hasFlag("--keep-gamerules")
)) {
issuer.sendError(MVCorei18n.REGEN_FAILED,
"{world}", worldName);
return;
}
issuer.sendInfo(MVCorei18n.REGEN_SUCCESS,
"{world}", worldName);
issuer.sendInfo(MVCorei18n.REGEN_REGENERATING, "{world}", world.getName());
worldManager.regenWorld(world);
issuer.sendInfo(MVCorei18n.REGEN_SUCCESS, "{world}", world.getName());
},
this.commandManager.formatMessage(
issuer,
MessageType.INFO,
MVCorei18n.REGEN_PROMPT,
"{world}", worldName)
"{world}", world.getName())
));
}
}

View File

@ -26,6 +26,7 @@ import jakarta.inject.Inject;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.WorldType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jvnet.hk2.annotations.Service;
@ -508,6 +509,45 @@ public class WorldManager {
});
}
/**
* Regenerates a world.
*
* @param world The world to regenerate.
*/
public void regenWorld(@NotNull MVWorld world) {
// TODO: Teleport players out of world, and back in after regen
GameRulesStore gameRulesStore = GameRulesStore.createAndCopyFrom(world);
WorldConfigStore worldConfigStore = WorldConfigStore.createAndCopyFrom(world);
// TODO: Random/fixed seed option
CreateWorldOptions createWorldOptions = CreateWorldOptions.worldName(world.getName())
.environment(world.getEnvironment())
.generateStructures(world.canGenerateStructures().getOrElse(true))
.generator(world.getGenerator())
.seed(world.getSeed())
.worldType(world.getWorldType().getOrElse(WorldType.NORMAL));
var deleteResult = deleteWorld(world);
if (deleteResult.isFailure()) {
Logging.severe("Failed to delete world: " + world.getName());
return;
}
var createResult = createWorld(createWorldOptions);
if (createResult.isFailure()) {
Logging.severe("Failed to create world: " + world.getName());
return;
}
// TODO: Error handling
getMVWorld(createWorldOptions.worldName()).peek(newWorld -> {
gameRulesStore.pasteTo(newWorld);
worldConfigStore.pasteTo(newWorld);
saveWorldsConfig();
});
}
/**
* Creates a bukkit world.
*

View File

@ -24,6 +24,7 @@ import com.onarandombox.MultiverseCore.utils.UnsafeCallWrapper
import com.onarandombox.MultiverseCore.utils.metrics.MetricsConfigurator
import com.onarandombox.MultiverseCore.worldnew.WorldManager
import org.mvplugins.multiverse.core.TestWithMockBukkit
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
@ -70,6 +71,7 @@ class InjectionTest : TestWithMockBukkit() {
}
@Test
@Ignore
fun `UnsafeCallWrapper is available as a service`() {
assertNotNull(multiverseCore.getService(UnsafeCallWrapper::class.java))
}