diff --git a/common/src/main/java/earth/terrarium/heracles/common/commands/CompleteCommand.java b/common/src/main/java/earth/terrarium/heracles/common/commands/CompleteCommand.java new file mode 100644 index 00000000..2cca2b97 --- /dev/null +++ b/common/src/main/java/earth/terrarium/heracles/common/commands/CompleteCommand.java @@ -0,0 +1,62 @@ +package earth.terrarium.heracles.common.commands; + +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.suggestion.SuggestionProvider; +import earth.terrarium.heracles.api.quests.Quest; +import earth.terrarium.heracles.common.handlers.progress.QuestProgressHandler; +import earth.terrarium.heracles.common.handlers.quests.QuestHandler; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.commands.SharedSuggestionProvider; +import net.minecraft.commands.arguments.EntityArgument; +import net.minecraft.network.chat.Component; +import net.minecraft.server.level.ServerPlayer; + +import java.util.Collection; +import java.util.List; + +public class CompleteCommand { + + private static final SuggestionProvider QUESTS = (context, builder) -> { + SharedSuggestionProvider.suggest( + QuestHandler.quests() + .keySet() + .stream() + .map(StringArgumentType::escapeIfRequired), + builder + ); + return builder.buildFuture(); + }; + + public static LiteralArgumentBuilder complete() { + return Commands.literal("complete") + .requires(source -> source.hasPermission(2)) + .then(Commands.argument("quest", StringArgumentType.string()) + .suggests(QUESTS) + .then(Commands.argument("target", EntityArgument.players()) + .executes(context -> complete(EntityArgument.getPlayers(context, "target"), context)) + ) + .executes(context -> complete(List.of(context.getSource().getPlayerOrException()), context)) + ); + } + + private static int complete(Collection players, CommandContext context) { + CommandSourceStack source = context.getSource(); + String quest = StringArgumentType.getString(context, "quest"); + Quest questObj = QuestHandler.get(quest); + if (questObj == null) { + source.sendFailure(Component.translatable("commands.heracles.complete.failed", quest)); + return 0; + } + for (ServerPlayer player : players) { + QuestProgressHandler.getProgress(source.getServer(), player.getUUID()).completeQuest(quest, questObj, player); + } + source.sendSuccess( + () -> Component.translatable("commands.heracles.complete.success", quest), + false + ); + return 1; + } +} diff --git a/common/src/main/java/earth/terrarium/heracles/common/commands/ModCommands.java b/common/src/main/java/earth/terrarium/heracles/common/commands/ModCommands.java index 5f17e15f..5d8657a1 100644 --- a/common/src/main/java/earth/terrarium/heracles/common/commands/ModCommands.java +++ b/common/src/main/java/earth/terrarium/heracles/common/commands/ModCommands.java @@ -37,6 +37,7 @@ public static void init(CommandDispatcher dispatcher) { }))) .then(ResetCommand.reset()) .then(ResetCommand.resetAll()) + .then(CompleteCommand.complete()) .then(Commands.literal("dummy") .requires(source -> source.hasPermission(2)) .then(Commands.argument("id", StringArgumentType.string()) diff --git a/common/src/main/java/earth/terrarium/heracles/common/handlers/progress/QuestProgress.java b/common/src/main/java/earth/terrarium/heracles/common/handlers/progress/QuestProgress.java index 41878504..65e9e3eb 100644 --- a/common/src/main/java/earth/terrarium/heracles/common/handlers/progress/QuestProgress.java +++ b/common/src/main/java/earth/terrarium/heracles/common/handlers/progress/QuestProgress.java @@ -60,6 +60,10 @@ public boolean isComplete() { return complete; } + public void setComplete(boolean complete) { + this.complete = complete; + } + public void claimReward(String reward) { claimed.add(reward); } diff --git a/common/src/main/java/earth/terrarium/heracles/common/handlers/progress/QuestsProgress.java b/common/src/main/java/earth/terrarium/heracles/common/handlers/progress/QuestsProgress.java index d1c5a526..1c96192b 100644 --- a/common/src/main/java/earth/terrarium/heracles/common/handlers/progress/QuestsProgress.java +++ b/common/src/main/java/earth/terrarium/heracles/common/handlers/progress/QuestsProgress.java @@ -70,6 +70,13 @@ public void resetQuest(String quest, ServerPlayer player) { this.completableQuests.updateCompleteQuests(this, player); } + public void completeQuest(String id, Quest quest, ServerPlayer player) { + QuestProgress progress = getProgress(id); + progress.setComplete(true); + this.progress.put(id, progress); + sendOutQuestChanged(id, quest, progress, player); + } + public void reset() { progress.clear(); completableQuests.updateCompleteQuests(this); diff --git a/common/src/main/resources/assets/heracles/lang/en_us.json b/common/src/main/resources/assets/heracles/lang/en_us.json index 370ff3e7..00f8db13 100644 --- a/common/src/main/resources/assets/heracles/lang/en_us.json +++ b/common/src/main/resources/assets/heracles/lang/en_us.json @@ -105,6 +105,8 @@ "commands.heracles.reset.success": "[Heracles] Quest %s reset successfully.", "commands.heracles.resetall.success": "[Heracles] Quests reset successfully.", + "commands.heracles.complete.success": "[Heracles] Quest %s successfully completed.", + "commands.heracles.complete.failed": "[Heracles] Quest %s could not be completed.", "commands.heracles.pin.success.pinned": "[Heracles] Quest %s pinned.", "commands.heracles.pin.success.unpinned": "[Heracles] Quest %s unpinned.", "commands.heracles.pin.dummy.completed": "[Heracles] Quest %s completed."