From 069ebfe24e9879e8f5d391442785987ba673bb82 Mon Sep 17 00:00:00 2001 From: s809 <43530948+s809@users.noreply.github.com> Date: Sat, 11 Jan 2025 02:55:09 +0500 Subject: [PATCH] Add pregen command --- .../common/commands/AbstractCommand.java | 44 ++++++++- .../common/commands/CommandInitializer.java | 1 + .../common/commands/PregenCommand.java | 99 +++++++++++++++++++ coreSubProjects | 2 +- 4 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 common/src/main/java/com/seibel/distanthorizons/common/commands/PregenCommand.java diff --git a/common/src/main/java/com/seibel/distanthorizons/common/commands/AbstractCommand.java b/common/src/main/java/com/seibel/distanthorizons/common/commands/AbstractCommand.java index febb0c7f4..2d056e973 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/commands/AbstractCommand.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/commands/AbstractCommand.java @@ -33,11 +33,49 @@ public abstract class AbstractCommand protected int sendSuccessResponse(CommandContext commandContext, String text) { #if MC_VER >= MC_1_20_1 - commandContext.getSource().sendSuccess(() -> Component.literal(text), true); + commandContext.getSource().sendSuccess(() -> Component.literal(text), false); #elif MC_VER >= MC_1_19_2 - commandContext.getSource().sendSuccess(Component.literal(text), true); + commandContext.getSource().sendSuccess(Component.literal(text), false); #else - commandContext.getSource().sendSuccess(new TranslatableComponent(text), true); + commandContext.getSource().sendSuccess(new TranslatableComponent(text), false); + #endif + return 1; + } + + /** + * Sends a failure response to the player with the given text. + * + * @param commandContext The command context to send the response to. + * @param text The text to display in the failure message. + * @return 1, indicating that the command was successful. + */ + protected int sendFailureResponse(CommandContext commandContext, String text) + { + #if MC_VER >= MC_1_20_1 + commandContext.getSource().sendFailure(Component.literal(text)); + #elif MC_VER >= MC_1_19_2 + commandContext.getSource().sendFailure(Component.literal(text)); + #else + commandContext.getSource().sendFailure(new TranslatableComponent(text)); + #endif + return 1; + } + + /** + * Sends a failure response to the player with the given text. + * + * @param commandContext The command context to send the response to. + * @param text The text to display in the failure message. + * @return 1, indicating that the command was successful. + */ + protected int sendSystemMessage(CommandContext commandContext, String text) + { + #if MC_VER >= MC_1_20_1 + commandContext.getSource().sendSystemMessage(Component.literal(text)); + #elif MC_VER >= MC_1_19_2 + commandContext.getSource().sendSystemMessage(Component.literal(text)); + #else + commandContext.getSource().sendSuccess(new TranslatableComponent(text), false); #endif return 1; } diff --git a/common/src/main/java/com/seibel/distanthorizons/common/commands/CommandInitializer.java b/common/src/main/java/com/seibel/distanthorizons/common/commands/CommandInitializer.java index d03274498..0cf47d98d 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/commands/CommandInitializer.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/commands/CommandInitializer.java @@ -36,6 +36,7 @@ public class CommandInitializer builder.then(new ConfigCommand().buildCommand()); builder.then(new DebugCommand().buildCommand()); + builder.then(new PregenCommand().buildCommand()); if (DEBUG_CODEC_CRASH_MESSAGE) { diff --git a/common/src/main/java/com/seibel/distanthorizons/common/commands/PregenCommand.java b/common/src/main/java/com/seibel/distanthorizons/common/commands/PregenCommand.java new file mode 100644 index 000000000..76f0ff207 --- /dev/null +++ b/common/src/main/java/com/seibel/distanthorizons/common/commands/PregenCommand.java @@ -0,0 +1,99 @@ +package com.seibel.distanthorizons.common.commands; + +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.seibel.distanthorizons.common.wrappers.world.ServerLevelWrapper; +import com.seibel.distanthorizons.core.generation.PregenManager; +import com.seibel.distanthorizons.core.pos.blockPos.DhBlockPos2D; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.arguments.DimensionArgument; +import net.minecraft.commands.arguments.coordinates.ColumnPosArgument; +import net.minecraft.server.level.ColumnPos; +import net.minecraft.server.level.ServerLevel; + +import java.util.concurrent.CompletableFuture; + +import static com.mojang.brigadier.arguments.IntegerArgumentType.getInteger; +import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; +import static net.minecraft.commands.Commands.argument; +import static net.minecraft.commands.Commands.literal; + +public class PregenCommand extends AbstractCommand +{ + private final PregenManager pregenManager = new PregenManager(); + + @Override + public LiteralArgumentBuilder buildCommand() + { + LiteralArgumentBuilder statusCommand = literal("status") + .executes(this::pregenStatus); + + LiteralArgumentBuilder startCommand = literal("start") + .then(argument("dimension", DimensionArgument.dimension()) + .then(argument("origin", ColumnPosArgument.columnPos()) + .then(argument("chunkRadius", integer(32)) + .executes(this::pregenStart)))); + + LiteralArgumentBuilder stopCommand = literal("stop") + .executes(this::pregenStop); + + return literal("pregen") + .then(statusCommand) + .then(startCommand) + .then(stopCommand); + } + + + private int pregenStatus(CommandContext c) + { + if (this.pregenManager.getRunningPregen() != null) + { + return this.sendSuccessResponse(c, "Pregen is running"); + } + else + { + return this.sendSuccessResponse(c, "Pregen is not running"); + } + } + + private int pregenStart(CommandContext c) throws CommandSyntaxException + { + this.sendSuccessResponse(c, "Starting pregen"); + + ServerLevel level = DimensionArgument.getDimension(c, "dimension"); + ColumnPos origin = ColumnPosArgument.getColumnPos(c, "origin"); + int chunkRadius = getInteger(c, "chunkRadius"); + + CompletableFuture future = this.pregenManager.startPregen( + ServerLevelWrapper.getWrapper(level), + new DhBlockPos2D(#if MC_VER >= MC_1_19_2 origin.x(), origin.z() #else origin.x, origin.z #endif), + chunkRadius, + update -> this.sendSystemMessage(c, update) + ); + + future.whenComplete((result, throwable) -> { + if (throwable != null) + { + this.sendFailureResponse(c, "Pregen failed: " + throwable.getMessage() + "\n Check the logs for more details."); + return; + } + + this.sendSuccessResponse(c, "Pregen is complete"); + }); + + return 1; + } + + private int pregenStop(CommandContext c) + { + CompletableFuture runningPregen = this.pregenManager.getRunningPregen(); + if (runningPregen != null) + { + runningPregen.cancel(true); + } + + return 1; + } + +} diff --git a/coreSubProjects b/coreSubProjects index 29040519e..ec19e4321 160000 --- a/coreSubProjects +++ b/coreSubProjects @@ -1 +1 @@ -Subproject commit 29040519e0db7d2ae50e636777bb23149e065e6c +Subproject commit ec19e432169d498ea5673c61e18434b111e14324