From f0aa21576a0fc2c0995ea849ef331c52700576ca Mon Sep 17 00:00:00 2001
From: X_huihui <2519350300@qq.com>
Date: Sat, 20 Jan 2024 12:33:55 +0800
Subject: [PATCH] Action Bar Show Vote Details & Bug Fix
---
pom.xml | 2 +-
.../java/top/mpt/huihui/answerit/Main.java | 8 +++--
.../mpt/huihui/answerit/commands/impl/q.java | 2 +-
.../huihui/answerit/commands/impl/vote.java | 4 +--
.../mpt/huihui/answerit/listener/InvOpen.java | 2 +-
.../huihui/answerit/listener/PlayerChat.java | 9 +++++
.../answerit/scheduler/ShowVoteProcess.java | 35 +++++++++++++++++++
.../mpt/huihui/answerit/scheduler/Timer.java | 22 ++++++------
src/main/resources/config.yml | 8 ++---
src/main/resources/lang/en_us.yml | 5 +++
src/main/resources/lang/zh_cn.yml | 5 +++
11 files changed, 80 insertions(+), 22 deletions(-)
create mode 100644 src/main/java/top/mpt/huihui/answerit/scheduler/ShowVoteProcess.java
diff --git a/pom.xml b/pom.xml
index eae678c..fc1974f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
top.mpt.huihui
answerit
- 1.51-SNAPSHOT
+ 1.52-SNAPSHOT
jar
Answerit
diff --git a/src/main/java/top/mpt/huihui/answerit/Main.java b/src/main/java/top/mpt/huihui/answerit/Main.java
index 76d95ab..023fa21 100644
--- a/src/main/java/top/mpt/huihui/answerit/Main.java
+++ b/src/main/java/top/mpt/huihui/answerit/Main.java
@@ -26,8 +26,10 @@ public final class Main extends JavaPlugin {
/* Write */
// 是否检查玩家聊天
public static boolean isCheckChat = false;
- // 存票
- public static List voteResult = new ArrayList<>();
+ // 存票(正确的票数和错误的票数)
+ public static int voteRight = 0;
+ public static int voteWrong = 0;
+
// 存玩家(防止重复投票)
public static List voteList = new ArrayList<>();
// 避免玩家投票结束后进行投票
@@ -38,6 +40,8 @@ public final class Main extends JavaPlugin {
public static List Online_Players = new ArrayList<>();
// 设置normal项(用于broadcast)
public static String normal = BLUE + "[AnswerIt] ";
+ // 投票结束时间(用于给玩家显示ActionBar)
+ public static long voteEndTime = 0;
@Override
public void onEnable() {
instance = this;
diff --git a/src/main/java/top/mpt/huihui/answerit/commands/impl/q.java b/src/main/java/top/mpt/huihui/answerit/commands/impl/q.java
index 4996ced..e50d83d 100644
--- a/src/main/java/top/mpt/huihui/answerit/commands/impl/q.java
+++ b/src/main/java/top/mpt/huihui/answerit/commands/impl/q.java
@@ -56,7 +56,7 @@ public boolean onCommand(CommandSender sender, String[] args) {
PlayerUtils.send(sender, i18N.getLang("player_err"));
return true;
}
- // 如果服务器上正在发生提问
+ // 如果该玩家正在被提问或正在对别的玩家进行提问
if (playersOnQuestioning.contains(target) || playersOnQuestioning.contains(q.sender)){
PlayerUtils.send(q.sender, i18N.getLang("please_wait_err"));
return true;
diff --git a/src/main/java/top/mpt/huihui/answerit/commands/impl/vote.java b/src/main/java/top/mpt/huihui/answerit/commands/impl/vote.java
index 1f4c4c6..1cd6bd6 100644
--- a/src/main/java/top/mpt/huihui/answerit/commands/impl/vote.java
+++ b/src/main/java/top/mpt/huihui/answerit/commands/impl/vote.java
@@ -40,10 +40,10 @@ public boolean onCommand(CommandSender sender, String[] args) {
}
if (args[0].equals("true")){
ChatUtils.broadcast((String) i18N.getLang("write.server_vote_right_info"), sender.getName());
- voteResult.add(true);
+ voteRight += 1;
} else if (args[0].equals("false")){
ChatUtils.broadcast((String) i18N.getLang("write.server_vote_wrong_info"), sender.getName());
- voteResult.add(false);
+ voteWrong += 1;
}
// 投票列表添加玩家
voteList.add(sender.getName());
diff --git a/src/main/java/top/mpt/huihui/answerit/listener/InvOpen.java b/src/main/java/top/mpt/huihui/answerit/listener/InvOpen.java
index f03256d..4a486e4 100644
--- a/src/main/java/top/mpt/huihui/answerit/listener/InvOpen.java
+++ b/src/main/java/top/mpt/huihui/answerit/listener/InvOpen.java
@@ -15,7 +15,7 @@ public void onInvClicked(InventoryClickEvent event){
// 防止过分占用服务器资源,因为有的时候会疯狂报错
if (prize.canPrize){
if (event.getInventory().equals(prize.getTargetPlayer().getInventory())){
- if (!(event.getCurrentItem() == null)){
+ if (event.getCurrentItem() != null){
Player prizePlayer = prize.getPrizePlayer();
Player targetPlayer = prize.getTargetPlayer();
// 添加物品
diff --git a/src/main/java/top/mpt/huihui/answerit/listener/PlayerChat.java b/src/main/java/top/mpt/huihui/answerit/listener/PlayerChat.java
index ba43447..52d171a 100644
--- a/src/main/java/top/mpt/huihui/answerit/listener/PlayerChat.java
+++ b/src/main/java/top/mpt/huihui/answerit/listener/PlayerChat.java
@@ -9,9 +9,11 @@
import org.bukkit.event.player.AsyncPlayerChatEvent;
import top.mpt.huihui.answerit.Main;
import top.mpt.huihui.answerit.commands.impl.q;
+import top.mpt.huihui.answerit.scheduler.ShowVoteProcess;
import top.mpt.huihui.answerit.scheduler.Timer;
import top.mpt.huihui.answerit.utils.ChatUtils;
import top.mpt.huihui.answerit.utils.ConfigUtils;
+import top.mpt.huihui.answerit.utils.LogUtils;
import top.mpt.huihui.answerit.utils.i18N;
import static top.mpt.huihui.answerit.Main.*;
@@ -52,7 +54,14 @@ public void onPlayerChat(AsyncPlayerChatEvent event){
int delaySecond = (int) ConfigUtils.getConfig(instance.getConfig(), "Write-wait-time", 30);
ChatUtils.broadcast((String) i18N.getLang("timer.timer_start_info"), delaySecond);
ChatUtils.broadcast((String) i18N.getLang("timer.timer_start_tip"));
+ // 预知投票结束时间
+ voteEndTime = System.currentTimeMillis() + delaySecond * 1000L;
+ LogUtils.info("Vote end time(Dbg info): " + voteEndTime);
+ // 显示投票进度
+ new ShowVoteProcess().runTaskTimer(Main.getPlugin(Main.class), 0L, 1L);
+ // 处理投票结果
new Timer().runTaskLater(Main.getPlugin(Main.class), delaySecond * 20L);
+
// 撤销事件
isCheckChat = false;
/* opened scheduler.Timer */
diff --git a/src/main/java/top/mpt/huihui/answerit/scheduler/ShowVoteProcess.java b/src/main/java/top/mpt/huihui/answerit/scheduler/ShowVoteProcess.java
new file mode 100644
index 0000000..e92d3ea
--- /dev/null
+++ b/src/main/java/top/mpt/huihui/answerit/scheduler/ShowVoteProcess.java
@@ -0,0 +1,35 @@
+package top.mpt.huihui.answerit.scheduler;
+
+import net.md_5.bungee.api.ChatMessageType;
+import net.md_5.bungee.api.chat.BaseComponent;
+import net.md_5.bungee.api.chat.TextComponent;
+import org.bukkit.Bukkit;
+import org.bukkit.scheduler.BukkitRunnable;
+import top.mpt.huihui.answerit.Main;
+import top.mpt.huihui.answerit.utils.ChatUtils;
+import top.mpt.huihui.answerit.utils.i18N;
+
+public class ShowVoteProcess extends BukkitRunnable {
+ @Override
+ public void run() {
+ // 计算剩余投票时间
+ double EndTime = ((double) (Main.voteEndTime - System.currentTimeMillis()) / 1000);
+
+ Bukkit.getServer().getOnlinePlayers().forEach(p ->{
+ if (Main.voteEndTime == 0){
+ BaseComponent error = new TextComponent(ChatUtils.translateColor(i18N.getLang("action_bar.error")));
+ p.spigot().sendMessage(ChatMessageType.ACTION_BAR, error);
+ this.cancel();
+ }
+ else if (EndTime <= 0.0D){
+ this.cancel();
+ }
+ else {
+ BaseComponent base = new TextComponent(ChatUtils.translateColor(i18N.getLang("action_bar.base")
+ , String.valueOf(EndTime), Main.voteRight, Main.voteWrong));
+ p.spigot().sendMessage(ChatMessageType.ACTION_BAR, base);
+ }
+
+ });
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/top/mpt/huihui/answerit/scheduler/Timer.java b/src/main/java/top/mpt/huihui/answerit/scheduler/Timer.java
index 4233c6d..1c072d1 100644
--- a/src/main/java/top/mpt/huihui/answerit/scheduler/Timer.java
+++ b/src/main/java/top/mpt/huihui/answerit/scheduler/Timer.java
@@ -2,6 +2,7 @@
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
+import top.mpt.huihui.answerit.Main;
import top.mpt.huihui.answerit.prize.prize;
import top.mpt.huihui.answerit.utils.ChatUtils;
import top.mpt.huihui.answerit.utils.i18N;
@@ -15,23 +16,22 @@ public class Timer extends BukkitRunnable {
/* be influenced on commands.impl.vote */
@Override
public void run() {
- int trueCount = 0;
- int falseCount = 0;
- for (Boolean result : voteResult){
- if (result){
- trueCount++;
- } else {
- falseCount++;
- }
- }
- // 清空数组
- voteResult.clear();
+ int trueCount = voteRight;
+ int falseCount = voteWrong;
+ // 清空数据
+ voteRight = 0;
+ voteWrong = 0;
voteList.clear();
// 设置可以被奖励
canPrize = true;
ChatUtils.broadcast((String) i18N.getLang("timer.timer_over_summary"), trueCount, falseCount);
if (trueCount == falseCount){
ChatUtils.broadcast((String) i18N.getLang("timer.votes_equal"));
+ // 如果票数相等,执行清空操作
+ playersOnQuestioning.remove(prize.getPrizePlayer());
+ playersOnQuestioning.remove(prize.getTargetPlayer());
+ prize.clearAllPlayer();
+
} else if (trueCount > falseCount){
ChatUtils.broadcast((String) i18N.getLang("timer.votes_right"));
prize.executePrize();
diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml
index a9785dc..0345715 100644
--- a/src/main/resources/config.yml
+++ b/src/main/resources/config.yml
@@ -1,7 +1,7 @@
# Language
-# need "/reload" to enable
-# 需要输入指令"/reload"来应用语言
+# need "/answer reload" to enable
+# 需要输入指令"/answer reload"来应用语言
# please write the file name with suffix
# 请带上文件后缀
@@ -11,7 +11,7 @@ lang: "zh_cn.yml"
# Write Mode, Time for voting(seconds)
# Write模式下,投票的等待时间(秒)
-# need "/reload" to enable
-# 需要输入指令"/reload"应用
+# need "/answer reload" to enable
+# 需要输入指令"/answer reload"应用
Write-wait-time: 30
diff --git a/src/main/resources/lang/en_us.yml b/src/main/resources/lang/en_us.yml
index b2be380..a9c00aa 100644
--- a/src/main/resources/lang/en_us.yml
+++ b/src/main/resources/lang/en_us.yml
@@ -55,3 +55,8 @@ timer:
votes_equal: "#GOLD#Equal votes, no reward or punishment for them."
votes_right: "#GREEN#Answer correct!"
votes_wrong: "#RED#Answer Incorrect!"
+
+action_bar:
+ # action bar when player voting
+ base: "#GOLD#Voting time:%s seconds #WHITE#| #AQUA#Voting progress: #GREEN#%d Agreed #RED#%d Disagreed"
+ error: "#RED#Unknown error occurred, unable to display ActionBar"
diff --git a/src/main/resources/lang/zh_cn.yml b/src/main/resources/lang/zh_cn.yml
index 75269d3..d83cf9c 100644
--- a/src/main/resources/lang/zh_cn.yml
+++ b/src/main/resources/lang/zh_cn.yml
@@ -54,3 +54,8 @@ timer:
votes_equal: "#GOLD#票数相等,没有奖励也没有惩罚(悲"
votes_right: "#GREEN#答案正确!"
votes_wrong: "#RED#答案错误!"
+
+action_bar:
+ # action bar when player voting
+ base: "#GOLD#投票剩余时间:%s 秒 #WHITE#| #AQUA#投票进度: #GREEN#%d 个同意 #RED#%d 个不同意"
+ error: "#RED#发生未知错误,无法显示ActionBar"