Skip to content

Commit ab2d796

Browse files
Bug Fix
1 parent f0aa215 commit ab2d796

File tree

6 files changed

+63
-17
lines changed

6 files changed

+63
-17
lines changed

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

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

3+
import org.bukkit.Bukkit;
34
import org.bukkit.ChatColor;
45
import org.bukkit.configuration.file.FileConfiguration;
56
import org.bukkit.configuration.file.YamlConfiguration;
67
import org.bukkit.entity.Player;
78
import org.bukkit.plugin.java.JavaPlugin;
89
import top.mpt.huihui.answerit.executor.CommandHandler;
10+
import top.mpt.huihui.answerit.listener.InvClose;
911
import top.mpt.huihui.answerit.listener.InvOpen;
1012
import top.mpt.huihui.answerit.listener.PlayerChat;
1113
import top.mpt.huihui.answerit.listener.PlayerJoinAndQuit;
@@ -49,8 +51,7 @@ public void onEnable() {
4951
getConfig().options().copyDefaults();
5052
saveDefaultConfig();
5153

52-
53-
54+
Bukkit.getOnlinePlayers().forEach(it -> Online_Players.add(it.getName()) );
5455

5556
File file = new File(getDataFolder() + "/lang/", getConfig().getString("lang"));
5657
saveResource("lang/zh_cn.yml", false);
@@ -63,6 +64,7 @@ public void onEnable() {
6364
// 注册事件
6465
getServer().getPluginManager().registerEvents(new PlayerChat(), this);
6566
getServer().getPluginManager().registerEvents(new InvOpen(), this);
67+
getServer().getPluginManager().registerEvents(new InvClose(), this);
6668
getServer().getPluginManager().registerEvents(new PlayerJoinAndQuit(), this);
6769
getLogger().info(normal + ChatColor.AQUA + "Plugin Enabled");
6870
/* wait player's commands */
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package top.mpt.huihui.answerit.listener;
2+
3+
import org.bukkit.entity.Player;
4+
import org.bukkit.event.EventHandler;
5+
import org.bukkit.event.Listener;
6+
import org.bukkit.event.inventory.InventoryCloseEvent;
7+
import top.mpt.huihui.answerit.prize.prize;
8+
import top.mpt.huihui.answerit.utils.PlayerUtils;
9+
import top.mpt.huihui.answerit.utils.i18N;
10+
11+
import static top.mpt.huihui.answerit.Main.playersOnQuestioning;
12+
13+
public class InvClose implements Listener{
14+
@EventHandler
15+
public void onInvClose(InventoryCloseEvent event){
16+
if (prize.canPrize){
17+
if (event.getPlayer().equals(prize.getPrizePlayer())){
18+
Player prizePlayer = prize.getPrizePlayer();
19+
Player targetPlayer = prize.getTargetPlayer();
20+
// 奖励结束,移除玩家
21+
if (playersOnQuestioning.contains(prizePlayer) && playersOnQuestioning.contains(targetPlayer)){
22+
playersOnQuestioning.remove(prizePlayer);
23+
playersOnQuestioning.remove(targetPlayer);
24+
}
25+
PlayerUtils.send(prize.getPrizePlayer(), i18N.getLang("global.inv_closed"));
26+
prize.clearAllPlayer();
27+
}
28+
}
29+
}
30+
}

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import org.bukkit.event.EventHandler;
44
import org.bukkit.event.Listener;
5+
import org.bukkit.event.inventory.ClickType;
56
import org.bukkit.event.inventory.InventoryClickEvent;
67
import org.bukkit.entity.Player;
78
import top.mpt.huihui.answerit.prize.prize;
9+
import top.mpt.huihui.answerit.utils.ChatUtils;
10+
import top.mpt.huihui.answerit.utils.PlayerUtils;
811

912
import static top.mpt.huihui.answerit.Main.playersOnQuestioning;
1013

@@ -15,29 +18,19 @@ public void onInvClicked(InventoryClickEvent event){
1518
// 防止过分占用服务器资源,因为有的时候会疯狂报错
1619
if (prize.canPrize){
1720
if (event.getInventory().equals(prize.getTargetPlayer().getInventory())){
21+
Player prizePlayer = prize.getPrizePlayer();
22+
Player targetPlayer = prize.getTargetPlayer();
1823
if (event.getCurrentItem() != null){
19-
Player prizePlayer = prize.getPrizePlayer();
20-
Player targetPlayer = prize.getTargetPlayer();
2124
// 添加物品
2225
prizePlayer.getInventory().addItem(event.getCurrentItem());
2326
// 移除物品
2427
targetPlayer.getInventory().remove(event.getCurrentItem());
2528
// 关闭inventory
2629
prizePlayer.closeInventory();
27-
// 奖励结束,移除玩家
28-
// 防止玩家因为sb腐竹reload而插件报错无法获取奖励
29-
if (playersOnQuestioning.contains(prizePlayer) && playersOnQuestioning.contains(targetPlayer)){
30-
playersOnQuestioning.remove(prizePlayer);
31-
playersOnQuestioning.remove(targetPlayer);
32-
}
33-
// 结束奖励
34-
prize.clearAllPlayer();
3530

3631
/* to the end of this(select / write) process */
3732
}
38-
3933
}
40-
4134
}
4235
}
4336
}

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

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

33
import org.bukkit.entity.Player;
4+
import top.mpt.huihui.answerit.utils.ChatUtils;
5+
import top.mpt.huihui.answerit.utils.PlayerUtils;
6+
import top.mpt.huihui.answerit.utils.i18N;
7+
8+
import static top.mpt.huihui.answerit.Main.playersOnQuestioning;
49

510
/**
611
* 奖罚系统
@@ -65,8 +70,20 @@ public static void clearAllPlayer(){
6570
* 默认为打开对方背包并且可以拿取任意一个物品
6671
*/
6772
public static void executePrize(){
68-
// 执行奖励
69-
prizePlayer.openInventory(targetPlayer.getInventory());
70-
/* to listener.InvOpen */
73+
if (prizePlayer.getName().equals(targetPlayer.getName())){
74+
PlayerUtils.send(prizePlayer, i18N.getLang("self_err"));
75+
PlayerUtils.send(prizePlayer, i18N.getLang("global.inv_closed"));
76+
// 结束奖励 //
77+
// 移除两次是因为这不是集合是数组 //
78+
playersOnQuestioning.remove(prizePlayer);
79+
playersOnQuestioning.remove(targetPlayer);
80+
clearAllPlayer();
81+
}
82+
else {
83+
// 执行奖励
84+
prizePlayer.openInventory(targetPlayer.getInventory());
85+
/* to listener.InvOpen */
86+
}
87+
7188
}
7289
}

src/main/resources/lang/en_us.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
sender_err: "Please let players execute this command!"
77
mode_err: "#RED#Mode is wrong, please ask %s to check about."
88
player_err: "#RED#Cannot find players who named that. Or maybe the player isn't online."
9+
self_err: "#RED#Why would you asked yourself a question and answered it?"
910

1011
please_wait_err: "#AQUA#Someone is already in Q&A. To prevent any bugs, please wait for them to complete the Q&A before asking any questions to someone."
1112

@@ -26,6 +27,7 @@ global:
2627

2728
player_cant_answer: "#RED#You cannot answer it again!!!"
2829
command_err_format: "#AQUA#The format you entered is incorrect!"
30+
inv_closed: "#AQUA#This Q&A operation has ended."
2931

3032
select:
3133
player_send_question: "#AQUA#The target has received your question, please be patient and wait for his or her answer."

src/main/resources/lang/zh_cn.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
sender_err: "请让玩家执行指令!"
66
mode_err: "#RED#提问类型有误,请让%s检查一下。"
77
player_err: "#RED#找不到玩家或玩家不在线"
8+
self_err: "#RED#你何必自己提问自己呢?"
89

910
please_wait_err: "#AQUA#此玩家正在进行问答操作,为了防止出现bug,请等待他们完成问答后在进行提问。"
1011

@@ -25,6 +26,7 @@ global:
2526

2627
player_cant_answer: "#RED#想啥呢?答完题了还想再答是吧???给爷爬!!!"
2728
command_err_format: "#AQUA#您输入的格式并不正确!"
29+
inv_closed: "#AQUA#本次问答操作已结束。"
2830

2931
select:
3032
player_send_question: "#AQUA#对方已收到您的问题,请耐心等待对方回答。"

0 commit comments

Comments
 (0)