Skip to content

Commit

Permalink
Add complete command
Browse files Browse the repository at this point in the history
closes #195
  • Loading branch information
ThatGravyBoat committed Mar 26, 2024
1 parent fdc17e2 commit b94fe0f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<CommandSourceStack> QUESTS = (context, builder) -> {
SharedSuggestionProvider.suggest(
QuestHandler.quests()
.keySet()
.stream()
.map(StringArgumentType::escapeIfRequired),
builder
);
return builder.buildFuture();
};

public static LiteralArgumentBuilder<CommandSourceStack> 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<ServerPlayer> players, CommandContext<CommandSourceStack> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static void init(CommandDispatcher<CommandSourceStack> 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/resources/assets/heracles/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down

0 comments on commit b94fe0f

Please sign in to comment.