Skip to content

Commit f0aa215

Browse files
Action Bar Show Vote Details & Bug Fix
1 parent 7675193 commit f0aa215

File tree

11 files changed

+80
-22
lines changed

11 files changed

+80
-22
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>top.mpt.huihui</groupId>
88
<artifactId>answerit</artifactId>
9-
<version>1.51-SNAPSHOT</version>
9+
<version>1.52-SNAPSHOT</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Answerit</name>

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ public final class Main extends JavaPlugin {
2626
/* Write */
2727
// 是否检查玩家聊天
2828
public static boolean isCheckChat = false;
29-
// 存票
30-
public static List<Boolean> voteResult = new ArrayList<>();
29+
// 存票(正确的票数和错误的票数)
30+
public static int voteRight = 0;
31+
public static int voteWrong = 0;
32+
3133
// 存玩家(防止重复投票)
3234
public static List<String> voteList = new ArrayList<>();
3335
// 避免玩家投票结束后进行投票
@@ -38,6 +40,8 @@ public final class Main extends JavaPlugin {
3840
public static List<String> Online_Players = new ArrayList<>();
3941
// 设置normal项(用于broadcast)
4042
public static String normal = BLUE + "[AnswerIt] ";
43+
// 投票结束时间(用于给玩家显示ActionBar)
44+
public static long voteEndTime = 0;
4145
@Override
4246
public void onEnable() {
4347
instance = this;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public boolean onCommand(CommandSender sender, String[] args) {
5656
PlayerUtils.send(sender, i18N.getLang("player_err"));
5757
return true;
5858
}
59-
// 如果服务器上正在发生提问
59+
// 如果该玩家正在被提问或正在对别的玩家进行提问
6060
if (playersOnQuestioning.contains(target) || playersOnQuestioning.contains(q.sender)){
6161
PlayerUtils.send(q.sender, i18N.getLang("please_wait_err"));
6262
return true;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ public boolean onCommand(CommandSender sender, String[] args) {
4040
}
4141
if (args[0].equals("true")){
4242
ChatUtils.broadcast((String) i18N.getLang("write.server_vote_right_info"), sender.getName());
43-
voteResult.add(true);
43+
voteRight += 1;
4444
} else if (args[0].equals("false")){
4545
ChatUtils.broadcast((String) i18N.getLang("write.server_vote_wrong_info"), sender.getName());
46-
voteResult.add(false);
46+
voteWrong += 1;
4747
}
4848
// 投票列表添加玩家
4949
voteList.add(sender.getName());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public void onInvClicked(InventoryClickEvent event){
1515
// 防止过分占用服务器资源,因为有的时候会疯狂报错
1616
if (prize.canPrize){
1717
if (event.getInventory().equals(prize.getTargetPlayer().getInventory())){
18-
if (!(event.getCurrentItem() == null)){
18+
if (event.getCurrentItem() != null){
1919
Player prizePlayer = prize.getPrizePlayer();
2020
Player targetPlayer = prize.getTargetPlayer();
2121
// 添加物品

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
import org.bukkit.event.player.AsyncPlayerChatEvent;
1010
import top.mpt.huihui.answerit.Main;
1111
import top.mpt.huihui.answerit.commands.impl.q;
12+
import top.mpt.huihui.answerit.scheduler.ShowVoteProcess;
1213
import top.mpt.huihui.answerit.scheduler.Timer;
1314
import top.mpt.huihui.answerit.utils.ChatUtils;
1415
import top.mpt.huihui.answerit.utils.ConfigUtils;
16+
import top.mpt.huihui.answerit.utils.LogUtils;
1517
import top.mpt.huihui.answerit.utils.i18N;
1618

1719
import static top.mpt.huihui.answerit.Main.*;
@@ -52,7 +54,14 @@ public void onPlayerChat(AsyncPlayerChatEvent event){
5254
int delaySecond = (int) ConfigUtils.getConfig(instance.getConfig(), "Write-wait-time", 30);
5355
ChatUtils.broadcast((String) i18N.getLang("timer.timer_start_info"), delaySecond);
5456
ChatUtils.broadcast((String) i18N.getLang("timer.timer_start_tip"));
57+
// 预知投票结束时间
58+
voteEndTime = System.currentTimeMillis() + delaySecond * 1000L;
59+
LogUtils.info("Vote end time(Dbg info): " + voteEndTime);
60+
// 显示投票进度
61+
new ShowVoteProcess().runTaskTimer(Main.getPlugin(Main.class), 0L, 1L);
62+
// 处理投票结果
5563
new Timer().runTaskLater(Main.getPlugin(Main.class), delaySecond * 20L);
64+
5665
// 撤销事件
5766
isCheckChat = false;
5867
/* opened scheduler.Timer */
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package top.mpt.huihui.answerit.scheduler;
2+
3+
import net.md_5.bungee.api.ChatMessageType;
4+
import net.md_5.bungee.api.chat.BaseComponent;
5+
import net.md_5.bungee.api.chat.TextComponent;
6+
import org.bukkit.Bukkit;
7+
import org.bukkit.scheduler.BukkitRunnable;
8+
import top.mpt.huihui.answerit.Main;
9+
import top.mpt.huihui.answerit.utils.ChatUtils;
10+
import top.mpt.huihui.answerit.utils.i18N;
11+
12+
public class ShowVoteProcess extends BukkitRunnable {
13+
@Override
14+
public void run() {
15+
// 计算剩余投票时间
16+
double EndTime = ((double) (Main.voteEndTime - System.currentTimeMillis()) / 1000);
17+
18+
Bukkit.getServer().getOnlinePlayers().forEach(p ->{
19+
if (Main.voteEndTime == 0){
20+
BaseComponent error = new TextComponent(ChatUtils.translateColor(i18N.getLang("action_bar.error")));
21+
p.spigot().sendMessage(ChatMessageType.ACTION_BAR, error);
22+
this.cancel();
23+
}
24+
else if (EndTime <= 0.0D){
25+
this.cancel();
26+
}
27+
else {
28+
BaseComponent base = new TextComponent(ChatUtils.translateColor(i18N.getLang("action_bar.base")
29+
, String.valueOf(EndTime), Main.voteRight, Main.voteWrong));
30+
p.spigot().sendMessage(ChatMessageType.ACTION_BAR, base);
31+
}
32+
33+
});
34+
}
35+
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.bukkit.entity.Player;
44
import org.bukkit.scheduler.BukkitRunnable;
5+
import top.mpt.huihui.answerit.Main;
56
import top.mpt.huihui.answerit.prize.prize;
67
import top.mpt.huihui.answerit.utils.ChatUtils;
78
import top.mpt.huihui.answerit.utils.i18N;
@@ -15,23 +16,22 @@ public class Timer extends BukkitRunnable {
1516
/* be influenced on commands.impl.vote */
1617
@Override
1718
public void run() {
18-
int trueCount = 0;
19-
int falseCount = 0;
20-
for (Boolean result : voteResult){
21-
if (result){
22-
trueCount++;
23-
} else {
24-
falseCount++;
25-
}
26-
}
27-
// 清空数组
28-
voteResult.clear();
19+
int trueCount = voteRight;
20+
int falseCount = voteWrong;
21+
// 清空数据
22+
voteRight = 0;
23+
voteWrong = 0;
2924
voteList.clear();
3025
// 设置可以被奖励
3126
canPrize = true;
3227
ChatUtils.broadcast((String) i18N.getLang("timer.timer_over_summary"), trueCount, falseCount);
3328
if (trueCount == falseCount){
3429
ChatUtils.broadcast((String) i18N.getLang("timer.votes_equal"));
30+
// 如果票数相等,执行清空操作
31+
playersOnQuestioning.remove(prize.getPrizePlayer());
32+
playersOnQuestioning.remove(prize.getTargetPlayer());
33+
prize.clearAllPlayer();
34+
3535
} else if (trueCount > falseCount){
3636
ChatUtils.broadcast((String) i18N.getLang("timer.votes_right"));
3737
prize.executePrize();

src/main/resources/config.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Language
22

3-
# need "/reload" to enable
4-
# 需要输入指令"/reload"来应用语言
3+
# need "/answer reload" to enable
4+
# 需要输入指令"/answer reload"来应用语言
55

66
# please write the file name with suffix
77
# 请带上文件后缀
@@ -11,7 +11,7 @@ lang: "zh_cn.yml"
1111
# Write Mode, Time for voting(seconds)
1212
# Write模式下,投票的等待时间(秒)
1313

14-
# need "/reload" to enable
15-
# 需要输入指令"/reload"应用
14+
# need "/answer reload" to enable
15+
# 需要输入指令"/answer reload"应用
1616
Write-wait-time: 30
1717

src/main/resources/lang/en_us.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,8 @@ timer:
5555
votes_equal: "#GOLD#Equal votes, no reward or punishment for them."
5656
votes_right: "#GREEN#Answer correct!"
5757
votes_wrong: "#RED#Answer Incorrect!"
58+
59+
action_bar:
60+
# action bar when player voting
61+
base: "#GOLD#Voting time:%s seconds #WHITE#| #AQUA#Voting progress: #GREEN#%d Agreed #RED#%d Disagreed"
62+
error: "#RED#Unknown error occurred, unable to display ActionBar"

0 commit comments

Comments
 (0)