Skip to content

Commit 3ea4985

Browse files
rewrite i18N, added token
1 parent 9654aaf commit 3ea4985

File tree

12 files changed

+95
-50
lines changed

12 files changed

+95
-50
lines changed

src/main/java/top/mpt/huihui/answerit/Main.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package top.mpt.huihui.answerit;
22

3-
import com.google.common.base.Charsets;
43
import org.bukkit.ChatColor;
54
import org.bukkit.configuration.file.FileConfiguration;
65
import org.bukkit.configuration.file.YamlConfiguration;
76
import org.bukkit.plugin.java.JavaPlugin;
87
import top.mpt.huihui.answerit.executor.CommandHandler;
98
import top.mpt.huihui.answerit.listener.InvOpen;
109
import top.mpt.huihui.answerit.listener.PlayerChat;
10+
import top.mpt.huihui.answerit.utils.i18N;
1111

1212
import java.io.*;
13-
import java.nio.channels.FileChannel;
1413
import java.util.ArrayList;
1514
import java.util.List;
1615

@@ -40,11 +39,11 @@ public void onEnable() {
4039
getConfig().options().copyDefaults();
4140
saveDefaultConfig();
4241
File file = new File(getDataFolder() + "\\lang\\", getConfig().getString("lang"));
43-
if (!file.exists()) {
44-
saveResource("lang/zh_cn.yml", false);
45-
saveResource("lang/en_us.yml", false);
46-
}
42+
saveResource("lang/zh_cn.yml", false);
43+
saveResource("lang/en_us.yml", false);
44+
4745
config = YamlConfiguration.loadConfiguration(file);
46+
i18N.setYaml(config);
4847

4948
// 指令
5049
getCommand("answer").setExecutor(new CommandHandler());

src/main/java/top/mpt/huihui/answerit/commands/impl/q.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
import top.mpt.huihui.answerit.commands.ICommand;
99
import top.mpt.huihui.answerit.prize.prize;
1010
import top.mpt.huihui.answerit.utils.ChatUtils;
11-
import top.mpt.huihui.answerit.utils.ConfigUtils;
1211
import top.mpt.huihui.answerit.utils.PlayerUtils;
12+
import top.mpt.huihui.answerit.utils.i18N;
13+
1314
import static top.mpt.huihui.answerit.Main.*;
1415

1516
import java.util.ArrayList;
@@ -45,7 +46,7 @@ public boolean onCommand(CommandSender sender, String[] args) {
4546
TextComponent message = null;
4647
// 防止index out of range
4748
if (args.length < 3){
48-
PlayerUtils.send(sender, ConfigUtils.getConfig(config, "global.command_err_format"));
49+
PlayerUtils.send(sender, i18N.getLang("global.command_err_format"));
4950
PlayerUtils.send(sender, "/answer q [PlayerName] [Question] [Type] [answer*n if select]");
5051
return true;
5152
}
@@ -54,7 +55,7 @@ public boolean onCommand(CommandSender sender, String[] args) {
5455
target = Bukkit.getPlayer(args[0]);
5556
if (Objects.equals(args[2], "select") || Objects.equals(args[2], "Select")){
5657
// 给玩家发送消息
57-
PlayerUtils.send(sender, ConfigUtils.getConfig(config, "select.player_choose_answer"));
58+
PlayerUtils.send(sender, i18N.getLang("select.player_choose_answer"));
5859
// 定义回答文本
5960
StringBuilder answerText = new StringBuilder();
6061
// 判断回答文本
@@ -88,8 +89,8 @@ public boolean onCommand(CommandSender sender, String[] args) {
8889
/* then to commands.impl.setAnswer */
8990

9091
} else if (Objects.equals(args[2], "write") || Objects.equals(args[2], "Write")){
91-
List<String> global_receiver_info = (List<String>) ConfigUtils.getListConfig(config, "global.receiver_info");
92-
List<String> global_broadcast_info = (List<String>) ConfigUtils.getListConfig(config, "global.broadcast_info");
92+
List<String> global_receiver_info = (List<String>) i18N.getLangList("global.receiver_info");
93+
List<String> global_broadcast_info = (List<String>) i18N.getLangList("global.broadcast_info");
9394
PlayerUtils.send(target, "#AQUA#=====================================");
9495
PlayerUtils.send(target, global_receiver_info.get(0), sender.getName());
9596
PlayerUtils.send(target, global_receiver_info.get(1) + " Write");
@@ -111,7 +112,7 @@ public boolean onCommand(CommandSender sender, String[] args) {
111112
}
112113

113114
} else {
114-
sender.sendMessage((String) ConfigUtils.getConfig(config, "sender_err"));
115+
sender.sendMessage((String) i18N.getLang("sender_err"));
115116
}
116117
return true;
117118
}

src/main/java/top/mpt/huihui/answerit/commands/impl/reload.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.bukkit.plugin.java.JavaPlugin;
66
import top.mpt.huihui.answerit.Main;
77
import top.mpt.huihui.answerit.commands.ICommand;
8+
import top.mpt.huihui.answerit.utils.i18N;
9+
810
import static top.mpt.huihui.answerit.Main.*;
911

1012
import java.io.File;
@@ -19,6 +21,7 @@ public boolean onCommand(CommandSender sender, String[] args) {
1921
main.reloadConfig();
2022
File file = new File(main.getDataFolder() + "\\lang", main.getConfig().getString("lang"));
2123
config = YamlConfiguration.loadConfiguration(file);
24+
i18N.setYaml(config);
2225
sender.sendMessage("[AnswerIt] Plugin Reload Completed");
2326
return true;
2427
}

src/main/java/top/mpt/huihui/answerit/commands/impl/send.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
import top.mpt.huihui.answerit.commands.ICommand;
77
import top.mpt.huihui.answerit.prize.prize;
88
import top.mpt.huihui.answerit.utils.ChatUtils;
9-
import top.mpt.huihui.answerit.utils.ConfigUtils;
109
import top.mpt.huihui.answerit.utils.PlayerUtils;
10+
import top.mpt.huihui.answerit.utils.i18N;
11+
12+
import java.util.Objects;
13+
1114
import static top.mpt.huihui.answerit.Main.*;
1215

1316
public class send extends ICommand {
@@ -21,27 +24,30 @@ public boolean onCommand(CommandSender sender, String[] args){
2124
// args[1] == NameFlying(提问者)
2225
if (sender instanceof Player){
2326
if (!prize.canPrize){
24-
PlayerUtils.send(sender, ConfigUtils.getConfig(config, "global.player_cant_answer"));
27+
PlayerUtils.send(sender, i18N.getLang("global.player_cant_answer"));
2528
return true;
2629
}
27-
if (args.length != 2){
28-
PlayerUtils.send(sender, ConfigUtils.getConfig(config, "global.command_err_format"));
30+
if (args.length != 3){
31+
PlayerUtils.send(sender, i18N.getLang("global.command_err_format"));
32+
return true;
33+
} if (!args[2].equals(setAnswer.token)){ // 检验token
34+
PlayerUtils.send(sender, "#RED#Token error.");
2935
} else if (args[0].equals("答对啦!")){
30-
ChatUtils.broadcast((String) ConfigUtils.getConfig(config, "select.target_choose_right_answer"), sender.getName(), args[1]);
36+
ChatUtils.broadcast((String) i18N.getLang("select.target_choose_right_answer"), sender.getName(), args[1]);
3137
prize.setPrizePlayer((Player) sender);
3238
prize.setTargetPlayer(Bukkit.getPlayer(args[1]));
3339
prize.executePrize();
3440
} else if (args[0].equals("答错了!")){
35-
ChatUtils.broadcast((String) ConfigUtils.getConfig(config, "select.target_choose_wrong_answer"), sender.getName(), args[1]);
41+
ChatUtils.broadcast((String) i18N.getLang("select.target_choose_wrong_answer"), sender.getName(), args[1]);
3642
prize.setTargetPlayer((Player) sender);
3743
prize.setPrizePlayer(Bukkit.getPlayer(args[1]));
3844
prize.executePrize();
3945
} else {
40-
PlayerUtils.send(sender, "#GREEN#不对啊qwq,肯定是服务器出问题了,这行字按道理来说不会出现的qwq。不要找灰灰好吧");
46+
PlayerUtils.send(sender, "#RED#Error format was executed by players.");
4147
}
4248
/* to prize.prize */
4349
} else {
44-
sender.sendMessage((String) ConfigUtils.getConfig(config, "sender_err"));
50+
sender.sendMessage((String) i18N.getLang("sender_err"));
4551
}
4652
return true;
4753
}

src/main/java/top/mpt/huihui/answerit/commands/impl/setAnswer.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
import top.mpt.huihui.answerit.commands.ICommand;
88
import org.bukkit.command.CommandSender;
99
import top.mpt.huihui.answerit.utils.ChatUtils;
10-
import top.mpt.huihui.answerit.utils.ConfigUtils;
1110
import top.mpt.huihui.answerit.utils.PlayerUtils;
12-
import static top.mpt.huihui.answerit.Main.*;
11+
import top.mpt.huihui.answerit.utils.i18N;
1312

1413
import java.util.List;
1514
import java.util.Objects;
15+
import java.util.Random;
1616

1717
public class setAnswer extends ICommand {
1818
public setAnswer(){
@@ -23,7 +23,11 @@ public setAnswer(){
2323
public static String answerText = "";
2424
public static Player sender = null;
2525
public static Player target = null;
26+
// 创建token,防止玩家手动输入/answer send 答对啦!
27+
public static String token = "";
2628
public boolean onCommand(CommandSender sender, String[] args){
29+
// generate token
30+
token = getRandomString(10);
2731
/* args[0] = "NameFlying"
2832
* args[1] = "Answer"
2933
* args[2] = "select"
@@ -38,7 +42,7 @@ public boolean onCommand(CommandSender sender, String[] args){
3842
// 发送消息
3943
sendQuestion(((Player) sender).getPlayer(), args[2], args[3]);
4044
} else {
41-
sender.sendMessage((String) ConfigUtils.getConfig(config, "sender_err"));
45+
sender.sendMessage((String) i18N.getLang("sender_err"));
4246
}
4347
return true;
4448
}
@@ -63,7 +67,7 @@ public void sendQuestion(Player sender, String type, String text){
6367
* 回答选项/请在公屏上打出答案:[很好] [非常好] [不好] [完全不好]
6468
* 答对了有奖励,答错了有惩罚(?
6569
*/
66-
List<String> global_receiver_info = (List<String>) ConfigUtils.getListConfig(config, "global.receiver_info");
70+
List<String> global_receiver_info = (List<String>) i18N.getLangList("global.receiver_info");
6771
PlayerUtils.send(target, "#AQUA#=====================================");
6872
PlayerUtils.send(target, global_receiver_info.get(0), sender.getName());
6973
PlayerUtils.send(target, global_receiver_info.get(1) + " %s", type );
@@ -73,15 +77,17 @@ public void sendQuestion(Player sender, String type, String text){
7377
String[] answerText = setAnswer.answerText.split(",");
7478
// ClickEvent用
7579
TextComponent message = null;
80+
81+
7682
for (int i = 0; i <= answerText.length - 1; i++){
7783
TextComponent single = new TextComponent(
7884
ChatUtils.translateColor("#BLUE#[#GREEN#" + answerText[i] + "#BLUE#]#RESET# ")
7985
);
8086
ClickEvent clickEvent;
8187
if (answerText[i].equals(answer)){
82-
clickEvent = new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/answer send 答对啦! " + sender.getName());
88+
clickEvent = new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/answer send 答对啦! " + sender.getName() + " " + token);
8389
} else {
84-
clickEvent = new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/answer send 答错了! " + sender.getName());
90+
clickEvent = new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/answer send 答错了! " + sender.getName() + " " + token);
8591
}
8692
single.setClickEvent(clickEvent);
8793
if (i == 0){
@@ -95,7 +101,18 @@ public void sendQuestion(Player sender, String type, String text){
95101
/* wait Target Answer */
96102
/* to commands.impl.send */
97103
} else { // 提问类型不是write也不是select
98-
PlayerUtils.send(target, ConfigUtils.getConfig(config, "mode_err"), sender.getName());
104+
PlayerUtils.send(target, i18N.getLang("mode_err"), sender.getName());
105+
}
106+
}
107+
108+
public static String getRandomString(int length){
109+
String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
110+
Random random = new Random();
111+
StringBuffer sb = new StringBuffer();
112+
for(int i=0; i<length; i++){
113+
int number=random.nextInt(62);
114+
sb.append(str.charAt(number));
99115
}
116+
return sb.toString();
100117
}
101118
}

src/main/java/top/mpt/huihui/answerit/commands/impl/vote.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import org.bukkit.entity.Player;
55
import top.mpt.huihui.answerit.commands.ICommand;
66
import top.mpt.huihui.answerit.utils.ChatUtils;
7-
import top.mpt.huihui.answerit.utils.ConfigUtils;
87
import top.mpt.huihui.answerit.utils.PlayerUtils;
8+
import top.mpt.huihui.answerit.utils.i18N;
99

1010
import java.util.Objects;
1111

@@ -25,31 +25,31 @@ public boolean onCommand(CommandSender sender, String[] args) {
2525
// 防止重复投票
2626
for (String player : voteList){
2727
if (Objects.equals(player, sender.getName())){
28-
PlayerUtils.send(sender, ConfigUtils.getConfig(config, "write.player_cant_vote"));
28+
PlayerUtils.send(sender, i18N.getLang("write.player_cant_vote"));
2929
return true;
3030
}
3131
}
3232
// 避免玩家投票结束后再次投票
3333
if (!canVote){
34-
PlayerUtils.send(sender, ConfigUtils.getConfig(config, "write.vote_timeout"));
34+
PlayerUtils.send(sender, i18N.getLang("write.vote_timeout"));
3535
return true;
3636
}
3737
if (args.length != 1){
38-
PlayerUtils.send(sender, ConfigUtils.getConfig(config, "global.command_err_format"));
38+
PlayerUtils.send(sender, i18N.getLang("write.vote_timeout"));
3939
return true;
4040
}
4141
if (args[0].equals("true")){
42-
ChatUtils.broadcast((String) ConfigUtils.getConfig(config, "write.server_vote_right_info"), sender.getName());
42+
ChatUtils.broadcast((String) i18N.getLang("write.server_vote_right_info"), sender.getName());
4343
voteResult.add(true);
4444
} else if (args[0].equals("false")){
45-
ChatUtils.broadcast((String) ConfigUtils.getConfig(config, "write.server_vote_wrong_info"), sender.getName());
45+
ChatUtils.broadcast((String) i18N.getLang("write.server_vote_wrong_info"), sender.getName());
4646
voteResult.add(false);
4747
}
4848
// 投票列表添加玩家
4949
voteList.add(sender.getName());
5050
/* influence scheduler.Timer */
5151
} else {
52-
sender.sendMessage((String) ConfigUtils.getConfig(config, "sender_err"));
52+
sender.sendMessage((String) i18N.getLang("sender_err"));
5353
}
5454
return true;
5555
}

src/main/java/top/mpt/huihui/answerit/listener/PlayerChat.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import top.mpt.huihui.answerit.scheduler.Timer;
1313
import top.mpt.huihui.answerit.utils.ChatUtils;
1414
import top.mpt.huihui.answerit.utils.ConfigUtils;
15+
import top.mpt.huihui.answerit.utils.i18N;
1516

1617
import static top.mpt.huihui.answerit.Main.*;
1718

@@ -27,16 +28,16 @@ public void onPlayerChat(AsyncPlayerChatEvent event){
2728
if (event.getPlayer().equals(target)){
2829
// 设置聊天发送的格式
2930
event.setCancelled(true);
30-
ChatUtils.broadcast((String) ConfigUtils.getConfig(config, "write.target_answer_info"), target.getName(), event.getMessage());
31+
ChatUtils.broadcast((String) i18N.getLang("write.target_answer_info"), target.getName(), event.getMessage());
3132
// 设置ClickEvent
3233
ClickEvent clickEventT = new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/answer vote true");
3334
ClickEvent clickEventF = new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/answer vote false");
3435
// 设置文字
3536
TextComponent componentT = new TextComponent(
36-
ChatUtils.translateColor(ConfigUtils.getConfig(config, "write.vote_right"))
37+
ChatUtils.translateColor(i18N.getLang("write.vote_right"))
3738
);
3839
TextComponent componentF = new TextComponent(
39-
ChatUtils.translateColor(ConfigUtils.getConfig(config, "write.vote_wrong"))
40+
ChatUtils.translateColor(i18N.getLang("write.vote_wrong"))
4041
);
4142
// setClickEvent
4243
componentT.setClickEvent(clickEventT);
@@ -49,8 +50,8 @@ public void onPlayerChat(AsyncPlayerChatEvent event){
4950
Bukkit.spigot().broadcast(componentT);
5051
// 开始计时
5152
int delaySecond = (int) ConfigUtils.getConfig(instance.getConfig(), "Write-wait-time", 30);
52-
ChatUtils.broadcast((String) ConfigUtils.getConfig(config, "timer.timer_start_info"), delaySecond);
53-
ChatUtils.broadcast((String) ConfigUtils.getConfig(config, "timer.timer_start_tip"));
53+
ChatUtils.broadcast((String) i18N.getLang("timer.timer_start_info"), delaySecond);
54+
ChatUtils.broadcast((String) i18N.getLang("timer.timer_start_tip"));
5455
new Timer().runTaskLater(Main.getPlugin(Main.class), delaySecond * 20L);
5556
// 撤销事件
5657
isCheckChat = false;

src/main/java/top/mpt/huihui/answerit/scheduler/Timer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.bukkit.scheduler.BukkitRunnable;
55
import top.mpt.huihui.answerit.prize.prize;
66
import top.mpt.huihui.answerit.utils.ChatUtils;
7-
import top.mpt.huihui.answerit.utils.ConfigUtils;
7+
import top.mpt.huihui.answerit.utils.i18N;
88

99
import static top.mpt.huihui.answerit.Main.*;
1010

@@ -26,14 +26,14 @@ public void run() {
2626
// 清空数组
2727
voteResult.clear();
2828
voteList.clear();
29-
ChatUtils.broadcast((String) ConfigUtils.getConfig(config, "timer.timer_over_summary"), trueCount, falseCount);
29+
ChatUtils.broadcast((String) i18N.getLang("timer.timer_over_summary"), trueCount, falseCount);
3030
if (trueCount == falseCount){
31-
ChatUtils.broadcast((String) ConfigUtils.getConfig(config, "timer.votes_equal"));
31+
ChatUtils.broadcast((String) i18N.getLang("timer.votes_equal"));
3232
} else if (trueCount > falseCount){
33-
ChatUtils.broadcast((String) ConfigUtils.getConfig(config, "timer.votes_right"));
33+
ChatUtils.broadcast((String) i18N.getLang("timer.votes_right"));
3434
prize.executePrize();
3535
} else {
36-
ChatUtils.broadcast((String) ConfigUtils.getConfig(config, "timer.votes_wrong"));
36+
ChatUtils.broadcast((String) i18N.getLang("timer.votes_wrong"));
3737
// 调换顺序
3838
Player prizePlayer = prize.getPrizePlayer();
3939
prize.setPrizePlayer(prize.getTargetPlayer());

src/main/java/top/mpt/huihui/answerit/utils/ConfigUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package top.mpt.huihui.answerit.utils;
22

33
import org.bukkit.configuration.file.FileConfiguration;
4-
import top.mpt.huihui.answerit.Main;
54

6-
import java.io.File;
75
import java.util.List;
86

97
/**
@@ -13,6 +11,7 @@
1311
public class ConfigUtils {
1412
/**
1513
* 获取Config
14+
* @param config FileConfiguration
1615
* @param path 名称
1716
* @return ConfigValue
1817
*/
@@ -22,6 +21,7 @@ public static Object getConfig(FileConfiguration config, String path) {
2221

2322
/**
2423
* 获取Config
24+
* @param config FileConfiguration
2525
* @param path 名称
2626
* @param defaultValue 默认值
2727
* @return ConfigValue
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package top.mpt.huihui.answerit.utils;
2+
3+
import org.bukkit.configuration.file.FileConfiguration;
4+
5+
import java.util.List;
6+
7+
public class i18N {
8+
private static FileConfiguration config;
9+
public static void setYaml(FileConfiguration configuration){
10+
config = configuration;
11+
}
12+
public static Object getLang(String path){
13+
return ConfigUtils.getConfig(config, path);
14+
}
15+
public static List<?> getLangList(String path){
16+
return ConfigUtils.getListConfig(config, path);
17+
}
18+
}

0 commit comments

Comments
 (0)