Skip to content

Commit

Permalink
Add toast option to command reward (#753)
Browse files Browse the repository at this point in the history
feat: Add toast message to command reward
  • Loading branch information
UnRealDinnerbone authored Nov 1, 2024
1 parent baad4dc commit 2879950
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package dev.ftb.mods.ftbquests.quest.reward;

import dev.architectury.networking.NetworkManager;
import dev.ftb.mods.ftblibrary.config.ConfigGroup;
import dev.ftb.mods.ftblibrary.icon.Color4I;
import dev.ftb.mods.ftblibrary.icon.Icon;
import dev.ftb.mods.ftbquests.net.DisplayRewardToastMessage;
import dev.ftb.mods.ftbquests.quest.Quest;
import dev.ftb.mods.ftbteams.api.FTBTeamsAPI;
import net.fabricmc.api.EnvType;
Expand All @@ -24,15 +28,18 @@

public class CommandReward extends Reward {
private static final String DEFAULT_COMMAND = "/say Hi, @p!";
private static final Icon REWARD_ICON = Icon.getIcon("minecraft:block/command_block_back");
public static final Pattern PATTERN = Pattern.compile("[{](\\w+)}");

private String command;
private boolean elevatePerms;
private boolean silent;
private String feedbackMessage;

public CommandReward(long id, Quest quest) {
super(id, quest);
command = DEFAULT_COMMAND;
feedbackMessage = "";
}

@Override
Expand All @@ -48,6 +55,7 @@ public void writeData(CompoundTag nbt, HolderLookup.Provider provider) {
nbt.putBoolean("elevate_perms", true);
}
if (silent) nbt.putBoolean("silent", true);
nbt.putString("feedback_message", feedbackMessage);
}

@Override
Expand All @@ -56,6 +64,7 @@ public void readData(CompoundTag nbt, HolderLookup.Provider provider) {
command = nbt.getString("command");
elevatePerms = nbt.getBoolean("elevate_perms");
silent = nbt.getBoolean("silent");
feedbackMessage = nbt.getString("feedback_message");
}

@Override
Expand All @@ -64,6 +73,7 @@ public void writeNetData(RegistryFriendlyByteBuf buffer) {
buffer.writeUtf(command, Short.MAX_VALUE);
buffer.writeBoolean(elevatePerms);
buffer.writeBoolean(silent);
buffer.writeUtf(feedbackMessage, Short.MAX_VALUE);
}

@Override
Expand All @@ -72,6 +82,7 @@ public void readNetData(RegistryFriendlyByteBuf buffer) {
command = buffer.readUtf(Short.MAX_VALUE);
elevatePerms = buffer.readBoolean();
silent = buffer.readBoolean();
feedbackMessage = buffer.readUtf(Short.MAX_VALUE);
}

@Override
Expand All @@ -81,6 +92,7 @@ public void fillConfigGroup(ConfigGroup config) {
config.addString("command", command, v -> command = v, DEFAULT_COMMAND).setNameKey("ftbquests.reward.ftbquests.command");
config.addBool("elevate", elevatePerms, v -> elevatePerms = v, false);
config.addBool("silent", silent, v -> silent = v, false);
config.addString("feedback_message", feedbackMessage, v -> feedbackMessage = v, "");
}

@Override
Expand Down Expand Up @@ -113,6 +125,11 @@ public void claim(ServerPlayer player, boolean notify) {
if (silent) source = source.withSuppressedOutput();

player.server.getCommands().performPrefixedCommand(source, cmd);

if (notify) {
String key = feedbackMessage.isEmpty() ? "ftbquests.reward.ftbquests.command.success" : feedbackMessage;
NetworkManager.sendToPlayer(player, new DisplayRewardToastMessage(id, Component.translatable(key), REWARD_ICON));
}
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions common/src/main/resources/assets/ftbquests/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,13 @@
"ftbquests.reward.ftbquests.xp": "XP",
"ftbquests.reward.ftbquests.xp_levels": "XP Levels",
"ftbquests.reward.ftbquests.command": "Command",
"ftbquests.reward.ftbquests.command.success": "Successfully claimed command reward",
"ftbquests.reward.ftbquests.command.tooltip": "Vanilla substitutions, e.g. @p, are supported\nSpecial substitutions:\n{x} / {y} / {z} - player's current X/Y/Z pos\n{quest} - ID of this quest\n{chapter} - ID of this chapter\n{team} - short name of player's team",
"ftbquests.reward.ftbquests.command.elevate": "Run with Elevated Permission",
"ftbquests.reward.ftbquests.command.elevate.tooltip": "Run the command as if the player had permission level 2",
"ftbquests.reward.ftbquests.command.silent": "Silent",
"ftbquests.reward.ftbquests.command.feedback_message": "Feedback Toast",
"ftbquests.reward.ftbquests.command.feedback_message.tooltip": "Toast message on command success",
"ftbquests.reward.ftbquests.advancement": "Advancement",
"ftbquests.reward.ftbquests.advancement.criterion": "Criterion",
"ftbquests.reward.ftbquests.advancement.criterion.tooltip": "Optional, set if you want only one criterion to be granted",
Expand Down

0 comments on commit 2879950

Please sign in to comment.