Skip to content

Commit 86799d8

Browse files
committed
✅ 客户端也使用服务端的翻译逻辑
1 parent 864cdbd commit 86799d8

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/main/java/top/byteeeee/quickcommand/commands/quickcommandcommand/QuickCommandCommand.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
8585
// help
8686
.then(ClientCommandManager.literal("help")
8787
.executes(context -> QuickCommandCommandHelper.help(context.getSource().getPlayer())))
88+
89+
// language
90+
.then(ClientCommandManager.literal("language")
91+
.then(ClientCommandManager.argument("language", StringArgumentType.string())
92+
.suggests(QuickCommandCommandHelper.CLIENT_LANGUAGE_SUGGESTION)
93+
.executes(context -> QuickCommandCommandHelper.setLanguage(
94+
context.getSource().getPlayer(),
95+
StringArgumentType.getString(context, "language")
96+
))))
8897
);
8998
}
9099
}

src/main/java/top/byteeeee/quickcommand/commands/quickcommandcommand/ServerQuickCommandCommand.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
import com.mojang.brigadier.arguments.BoolArgumentType;
2525
import com.mojang.brigadier.arguments.IntegerArgumentType;
2626
import com.mojang.brigadier.arguments.StringArgumentType;
27-
import com.mojang.brigadier.suggestion.SuggestionProvider;
2827

29-
import net.minecraft.command.CommandSource;
3028
import net.minecraft.server.command.ServerCommandSource;
3129

3230
import top.byteeeee.quickcommand.helpers.QuickCommandCommandHelper;
@@ -35,8 +33,6 @@
3533
import static net.minecraft.server.command.CommandManager.literal;
3634

3735
public class ServerQuickCommandCommand {
38-
private static final SuggestionProvider<ServerCommandSource> LANGUAGE_SUGGESTION = (context, builder) -> CommandSource.suggestMatching(new String[]{"en_us", "zh_cn"}, builder);
39-
4036
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
4137
dispatcher.register(literal("serverQuickCommand")
4238
.executes(context -> QuickCommandCommandHelper.showListWithRun(context.getSource().getPlayer()))
@@ -93,7 +89,7 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
9389
// language
9490
.then(literal("language")
9591
.then(argument("language", StringArgumentType.string())
96-
.suggests(LANGUAGE_SUGGESTION)
92+
.suggests(QuickCommandCommandHelper.SERVER_LANGUAGE_SUGGESTION)
9793
.executes(context -> QuickCommandCommandHelper.setLanguage(
9894
context.getSource().getPlayer(),
9995
StringArgumentType.getString(context, "language")

src/main/java/top/byteeeee/quickcommand/helpers/QuickCommandCommandHelper.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020

2121
package top.byteeeee.quickcommand.helpers;
2222

23+
import com.mojang.brigadier.suggestion.SuggestionProvider;
24+
25+
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
26+
import net.minecraft.command.CommandSource;
2327
import net.minecraft.entity.player.PlayerEntity;
24-
import net.minecraft.server.network.ServerPlayerEntity;
28+
import net.minecraft.server.command.ServerCommandSource;
2529
import net.minecraft.text.MutableText;
2630
import net.minecraft.util.Formatting;
2731

@@ -34,6 +38,8 @@
3438

3539
public class QuickCommandCommandHelper {
3640
private static final Translator translator = new Translator("command");
41+
public static final SuggestionProvider<ServerCommandSource> SERVER_LANGUAGE_SUGGESTION = (context, builder) -> CommandSource.suggestMatching(new String[]{"en_us", "zh_cn"}, builder);
42+
public static final SuggestionProvider<FabricClientCommandSource> CLIENT_LANGUAGE_SUGGESTION = (context, builder) -> CommandSource.suggestMatching(new String[]{"en_us", "zh_cn"}, builder);
3743
public static final Map<String, String> QUICK_COMMAND_MAP = new LinkedHashMap<>();
3844
private static final String MSG_HEAD = EnvironmentHelper.isServer() ? "§b<ServerQuickCommand>§r " : "§b<QuickCommand>§r ";
3945
public static boolean awaitingConfirmation = false;
@@ -108,6 +114,7 @@ public static int showListWithRun(PlayerEntity player) {
108114
).formatted(Formatting.AQUA, Formatting.BOLD),
109115
false
110116
);
117+
111118
player.sendMessage(
112119
translator.tr("setLanguageTitle").formatted(Formatting.LIGHT_PURPLE)
113120
.append(Messenger.endl())
@@ -116,6 +123,7 @@ public static int showListWithRun(PlayerEntity player) {
116123
.append(QuickCommandButton.setEnglishButton())
117124
.append(Messenger.endl()), false
118125
);
126+
119127
player.sendMessage(translator.tr("commandListTitle").formatted(Formatting.LIGHT_PURPLE), false);
120128
if (QUICK_COMMAND_MAP.isEmpty()) {
121129
player.sendMessage(Messenger.s("··· ··· ···").formatted(Formatting.AQUA), false);
@@ -139,6 +147,7 @@ public static int showListWithRun(PlayerEntity player) {
139147
player.sendMessage(message, false);
140148
counter++;
141149
}
150+
142151
final String displayCommandInListMsgLine = "-----------------------------";
143152
player.sendMessage(
144153
Messenger.s(displayCommandInListMsgLine).formatted(Formatting.DARK_AQUA)
@@ -151,6 +160,7 @@ public static int showListWithRun(PlayerEntity player) {
151160
.append(Messenger.endl()),
152161
false
153162
);
163+
154164
player.sendMessage(
155165
translator.tr("easyOperation").formatted(Formatting.LIGHT_PURPLE).append(Messenger.endl())
156166
.append(QuickCommandButton.addCommandButton()).append(Messenger.endl())
@@ -208,7 +218,7 @@ public static void saveToJson() {
208218
QuickCommandConfig.saveToJson(QUICK_COMMAND_MAP);
209219
}
210220

211-
public static int setLanguage(ServerPlayerEntity player, String language) {
221+
public static int setLanguage(PlayerEntity player, String language) {
212222
List<String> availableLanguages = Arrays.asList("zh_cn", "en_us");
213223
if (!availableLanguages.contains(language)) {
214224
return 0;

0 commit comments

Comments
 (0)