Skip to content

Commit

Permalink
Action Bar Show Vote Details & Bug Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohuihui1022 committed Jan 20, 2024
1 parent 7675193 commit f0aa215
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>top.mpt.huihui</groupId>
<artifactId>answerit</artifactId>
<version>1.51-SNAPSHOT</version>
<version>1.52-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Answerit</name>
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/top/mpt/huihui/answerit/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ public final class Main extends JavaPlugin {
/* Write */
// 是否检查玩家聊天
public static boolean isCheckChat = false;
// 存票
public static List<Boolean> voteResult = new ArrayList<>();
// 存票(正确的票数和错误的票数)
public static int voteRight = 0;
public static int voteWrong = 0;

// 存玩家(防止重复投票)
public static List<String> voteList = new ArrayList<>();
// 避免玩家投票结束后进行投票
Expand All @@ -38,6 +40,8 @@ public final class Main extends JavaPlugin {
public static List<String> Online_Players = new ArrayList<>();
// 设置normal项(用于broadcast)
public static String normal = BLUE + "[AnswerIt] ";
// 投票结束时间(用于给玩家显示ActionBar)
public static long voteEndTime = 0;
@Override
public void onEnable() {
instance = this;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/top/mpt/huihui/answerit/commands/impl/q.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/top/mpt/huihui/answerit/commands/impl/vote.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
// 添加物品
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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 */
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

});
}
}
22 changes: 11 additions & 11 deletions src/main/java/top/mpt/huihui/answerit/scheduler/Timer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Language

# need "/reload" to enable
# 需要输入指令"/reload"来应用语言
# need "/answer reload" to enable
# 需要输入指令"/answer reload"来应用语言

# please write the file name with suffix
# 请带上文件后缀
Expand All @@ -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

5 changes: 5 additions & 0 deletions src/main/resources/lang/en_us.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
5 changes: 5 additions & 0 deletions src/main/resources/lang/zh_cn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit f0aa215

Please sign in to comment.