Skip to content

Commit

Permalink
[VoteUp 2] 修复 Bug,提升稳定性以正常使用.
Browse files Browse the repository at this point in the history
本次 commit 包含以下内容:
- [Fixed] 修复了 help 子命令报错的问题。
- [Fixed] 修复了部分变量显示不正确的问题。
- [Fixed] 修复了投票并发表看法后仍然被视为未发表看法导致 Gui 显示不正确的问题。
- [Fixed] 修复了创建投票界面的 Gui 配置文件格式错误导致部分按钮报错的问题。
- [Fixed] 修复了无法正确保存/读取投票数据的问题。
- [Fixed] 修复了发布投票后无法正常倒计时的问题。
  - 修复了无法正确计算投票持续时间的问题。
  - 修复了无法正确启动倒记时任务的问题。
- [Fixed] 修复了修改投票 ID 后
  - 投票数据丢失的问题
  - 投票数据保存但投票 ID 修改失败的问题
  - 投票 ID 修改后仍以原 ID 保存数据的问题。
- [Fixed] 修复了投票参与者 Gui 加载/显示不正确的问题。
- [Fixed] 修复了投票发起人在不公开投票进度的情况下也无法查阅投票参与者的问题。
- [Fixed] 修复了创建投票 Gui 的三个投票选项(可编辑所投票,允许匿名投票,公开投票进度)的物品 Icon 显示不正常的问题。
- [Fixed] 修复了创建投票 Gui 中允许匿名投票按钮工作不正确的问题(点击实际上会切换可编辑所投票模式)。
- [Fixed] 修复了从创建投票 Gui 中发起投票简述/自动执行的聊天栏编辑时,一条提示信息变脸无法正确显示的问题。
- [Fixed] 调整了配置文件中 Title 显示时间,不然淡入的速度太慢了。
- [Fixed] 优化代码。
  • Loading branch information
Polar-Pumpkin committed Jun 30, 2020
1 parent 64cd85a commit 255ca79
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 75 deletions.
2 changes: 1 addition & 1 deletion VoteUp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.shoal.sir</groupId>
<artifactId>VoteUp</artifactId>
<version>2.0.3-BETA</version>
<version>2.0.4-BETA</version>
<packaging>jar</packaging>

<name>VoteUp</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ else if ("content".equalsIgnoreCase(params))
return vote.getProcess() + "%";
case ANONYMOUS:
if ("desc".equalsIgnoreCase(params))
return Msg.VOTE_ANONYMOUS_DESC.msg;
return vote.allowAnonymous ? Msg.VOTE_ANONYMOUS_DESC.msg : "";
return vote.allowAnonymous ? Msg.VOTE_ANONYMOUS_ENABLE.msg : Msg.VOTE_ANONYMOUS_DISABLE.msg;
case PUBLIC:
return vote.isPublic ? Msg.VOTE_PUBLIC_ENABLE.msg : Msg.VOTE_PUBLIC_DISABLE.msg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class VoteUpCmd extends CommandHandler {

public VoteUpCmd() {
super(VoteUp.getInstance(), "voteup");
registerSubCommand("help", new HelpCommand(plugin, null));
registerSubCommand("help", new HelpCommand(plugin, null, this));
registerSubCommand("create", new CreateCmd());
registerSubCommand("reload", new ReloadCommand(plugin, VoteUpPerm.ADMIN.node));
registerSubCommand("modify", new ModifyCmd());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ public void load(@NonNull File file) {
voteMap.put(BasicUtil.getNoExFileName(file.getName()), new Vote(file));
}

public void load(@NonNull Vote vote) {
voteMap.put(vote.voteID, vote);
}

private void startCountdown(String voteID) {
Vote data = voteMap.getOrDefault(voteID, null);
if (data == null) return;
if (!data.open) return;
if (data.isDraft) return;
if (endTaskMap.containsKey(voteID)) return;
long timeRemain = data.startTime + Vote.getDurationTimestamp(data.duration) - System.currentTimeMillis();
if (timeRemain <= 0) {
Expand Down Expand Up @@ -101,10 +106,10 @@ public void back(@NonNull Player user) {
public void start(@NonNull Player user) {
Vote vote = draftVote(user.getUniqueId());
if (vote == null) return;
startCountdown(vote.voteID);
vote.startTime = System.currentTimeMillis();
vote.open = true;
vote.isDraft = false;
startCountdown(vote.voteID);

VoteUpAPI.SOUND.voteEvent(true);
if (plugin.pConfig.getConfig().getBoolean(ConfPath.Path.SETTINGS_BROADCAST_TITLE_VOTESTART.path, true))
Expand Down Expand Up @@ -135,6 +140,7 @@ public void vote(String voteID, @NonNull Player user, Vote.Choice choice, boolea
if (vote.isVoted(uuid))
I18n.send(user, plugin.lang.get(plugin.localeKey, I18n.Type.INFO, "Vote", "Vote.Fail.Logged"));

vote.participants.removeIf(participant -> participant.uuid == uuid);
vote.participants.add(
new Vote.Participant(
uuid,
Expand Down Expand Up @@ -168,7 +174,7 @@ public void vote(String voteID, @NonNull Player user, Vote.Choice choice, boolea
Player admin = Bukkit.getPlayer(UUID.fromString(adminID));
if (admin != null) {
String announce = notice.announce(admin.getUniqueId());
if (announce != null)
if (announce != null) // TODO 这里有一个发错误格式的收到新投票信息的问题。
I18n.send(admin, VoteUpPlaceholder.parse(vote, announce));
}
}
Expand Down
33 changes: 16 additions & 17 deletions VoteUp/src/main/java/net/shoal/sir/voteup/data/Vote.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class Vote implements PData, Owned, Timestamp {
public boolean open;
public boolean cancelled;
public boolean isDraft;
public boolean allowAnonymous; // TODO 匿名投票设置
public boolean isPublic; // TODO 结果公开
public boolean allowAnonymous;
public boolean isPublic;
public boolean allowEdit; // TODO 允许投票后编辑
public Type type;
public int goal;
Expand Down Expand Up @@ -78,7 +78,7 @@ public Vote(int goal, UUID owner, String duration) {
public static long getDurationTimestamp(String duration) {
long result = 0;

String clone = duration.toUpperCase();
String clone = duration;
Duration durationType;
while ((durationType = getFirstIndexOf(clone)) != null) {
int index = clone.indexOf(durationType.code);
Expand Down Expand Up @@ -167,18 +167,6 @@ public boolean hasReason(UUID uuid) {
return false;
}

public String getReason(UUID uuid) {
Participant user = getParticipant(uuid);
if (user != null) return I18n.color(user.reason);
return I18n.color(Msg.REASON_NOT_YET.msg);
}

public Choice getChoice(UUID uuid) {
Participant user = getParticipant(uuid);
if (user != null) return user.choice;
return null;
}

public int getProcess() {
int all = participants.size();
int accept = listParticipants(user -> user.choice == Choice.ACCEPT).size();
Expand Down Expand Up @@ -287,6 +275,10 @@ public void load(@NonNull File file) {
this.title = I18n.color(setting.getString("Title", getOwnerName() + " 的投票"));
this.description = setting.getStringList("Description");
this.description.replaceAll(I18n::color);

this.allowAnonymous = setting.getBoolean("Anonymous", false);
this.isPublic = setting.getBoolean("Public", false);
this.allowEdit = setting.getBoolean("Editable", false);
}

ConfigurationSection choiceSection = setting.getConfigurationSection("Choices");
Expand All @@ -301,12 +293,12 @@ public void load(@NonNull File file) {

this.autocast = setting.getStringList("Autocast");

ConfigurationSection resultSection = setting.getConfigurationSection("Results"); // TODO 这里有一个全都是 null 的问题
ConfigurationSection resultSection = setting.getConfigurationSection("Results");
this.results = new HashMap<>();
if (resultSection != null) {
for (String resultKey : resultSection.getKeys(false)) {
Result result = EnumUtil.valueOf(Result.class, resultKey.toUpperCase());
this.results.put(result, I18n.color(choiceSection.getString(resultKey)));
this.results.put(result, I18n.color(resultSection.getString(resultKey)));
}
}

Expand Down Expand Up @@ -343,6 +335,9 @@ public void reload() {

@Override
public void save() {
if (!BasicUtil.getNoExFileName(file.getName()).equals(voteID))
this.file = new File(VoteUpAPI.VOTE_MANAGER.getFolder(), voteID + ".yml");

FileConfiguration data = YamlConfiguration.loadConfiguration(file);
ConfigurationSection information = data.createSection("Information");
information.set("Open", open);
Expand All @@ -359,6 +354,10 @@ public void save() {
List<String> desc2save = new ArrayList<>(description);
desc2save.replaceAll(s -> I18n.deColor(s, '&'));
setting.set("Description", desc2save);
setting.set("Anonymous", allowAnonymous);
setting.set("Public", isPublic);
setting.set("Editable", allowEdit);

this.choices.forEach(
(choice, s) -> setting.set("Choices." + choice.name(), I18n.deColor(s, '&'))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ public Inventory construct() {
}
}

if (keyWord != null && keyWord.target != null && !VoteUpPerm.EDIT.hasPermission(viewer, keyWord.target)) {
ConfigurationSection noPerm = file.getConfigurationSection("Settings.NoPerm");
if (noPerm != null) item = ItemUtil.build(plugin, noPerm);
else item = new ItemStack(Material.BARRIER);

if (keyWord != null && keyWord.target != null) {
switch (keyWord) {
case ALLOW_EDIT:
case PUBLIC_MODE:
Expand Down Expand Up @@ -107,7 +103,7 @@ public Inventory construct() {
break;
}

if (item.getType().name().endsWith("DYE")) if (!enable) item.setType(Material.LIGHT_GRAY_DYE);
if (item.getType().name().endsWith("DYE")) if (!enable) item.setType(Material.GRAY_DYE);
if (enable) {
ItemMeta meta = item.getItemMeta();
if (meta != null) {
Expand All @@ -118,6 +114,12 @@ public Inventory construct() {
}
break;
}

if (!VoteUpPerm.EDIT.hasPermission(viewer, keyWord.target)) {
ConfigurationSection noPerm = file.getConfigurationSection("Settings.NoPerm");
if (noPerm != null) item = ItemUtil.build(plugin, noPerm);
else item = new ItemStack(Material.BARRIER);
}
}

ConfigurationSection targetSlotSection = targetItemSection.getConfigurationSection("Position");
Expand Down Expand Up @@ -170,7 +172,7 @@ public void execute(InventoryClickEvent event) {
JsonChatUtil.sendEditableList(
user,
vote.description,
VoteUpPlaceholder.parse(vote, Msg.VOTE_VALUE_DESCRIPTION.msg),
VoteUpPlaceholder.parse(vote, String.format(Msg.VOTE_VALUE_DESCRIPTION.msg, vote.description.size())),
"&a&l[插入] ",
"/vote modify desc add ",
"&e&l[编辑] ",
Expand All @@ -184,7 +186,7 @@ public void execute(InventoryClickEvent event) {
case ALLOW_ANONYMOUS:
if (validate(user, Vote.Data.ANONYMOUS) || !plugin.pConfig.getConfig().getBoolean(ConfPath.Path.SETTINGS_ALLOW_ANONYMOUS.path, true))
break;
vote.allowEdit = !vote.allowAnonymous;
vote.allowAnonymous = !vote.allowAnonymous;
refresh(inv);
break;
case PUBLIC_MODE:
Expand All @@ -207,7 +209,7 @@ public void execute(InventoryClickEvent event) {
JsonChatUtil.sendEditableList(
user,
vote.autocast,
VoteUpPlaceholder.parse(vote, Msg.VOTE_VALUE_AUTOCAST.msg),
VoteUpPlaceholder.parse(vote, String.format(Msg.VOTE_VALUE_AUTOCAST.msg, vote.autocast.size())),
"&a&l[插入] ",
"/vote modify autocast add ",
"&e&l[编辑] ",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ else if ((keyWord == KeyWord.EDIT || keyWord == KeyWord.CANCEL) && !(VoteUpPerm.
continue;
else if ((keyWord == KeyWord.VOTE_ACCEPT || keyWord == KeyWord.VOTE_NEUTRAL || keyWord == KeyWord.VOTE_REFUSE) && !VoteUpPerm.VOTE.hasPermission(viewer, EnumUtil.valueOf(Vote.Choice.class, keyWord.name().split("[_]")[1])))
continue;
else if (keyWord == KeyWord.PARTICIPANT && !vote.isPublic)
else if (keyWord == KeyWord.PARTICIPANT && !vote.isPublic && vote.isOwner(viewer.getUniqueId()))
continue;

ConfigurationSection targetSlotSection = targetItemSection.getConfigurationSection("Position");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public ParticipantInventoryHolder(T data, @NonNull Player player, GuiManager.Gui
@Override
public Inventory construct() {
Vote vote = (Vote) data;
FileConfiguration file = VoteUpAPI.GUI_MANAGER.get(GuiManager.GuiKey.VOTE_DETAILS.filename);
FileConfiguration file = VoteUpAPI.GUI_MANAGER.get(GuiManager.GuiKey.VOTE_PARTICIPANTS.filename);
String title = "未初始化菜单";
if (file == null) return Bukkit.createInventory(this, 0, title);
title = VoteUpPlaceholder.parse(vote, file.getString("Settings.Title", Msg.ERROR_GUI_TITLE.msg));
Expand Down Expand Up @@ -115,15 +115,15 @@ public Inventory construct() {
{
put("participant", Bukkit.getOfflinePlayer(user.uuid).getName());
put("time", TimeUtil.getDescriptionTimeFromTimestamp(user.timestamp) + " &8&o" + TimeUtil.getChineseDateFormat(new Date(user.timestamp)));
put("choice", user.choice.name + " &8&o" + vote.choices.get(user.choice));
put("choice", vote.choices.get(user.choice));
put("reason", user.reason);
}
};

if (user.anonymous) {
String[] names = anonymousItem.keySet().toArray(new String[0]);
String name = names[new Random().nextInt(names.length)];
variableMap.put("participant", VoteUpPerm.ANONYMOUS.hasPermission(viewer) ? name + " &8&o(" + variableMap.get("participant") + ")" : name);
variableMap.put("participant", VoteUpPerm.ANONYMOUS.hasPermission(viewer) ? name + " &8&o(" + variableMap.get("participant") + " 已匿名)" : name);
resultItem.setType(anonymousItem.get(name));
} else if (resultItem.getType() == Material.PLAYER_HEAD) {
SkullMeta skull = (SkullMeta) resultItem.getItemMeta();
Expand All @@ -134,9 +134,9 @@ public Inventory construct() {
}

variableMap.forEach((k, v) -> ItemUtil.replace(resultItem, "%" + k + "%", v));
inv.setItem(slot, resultItem);
inv.setItem(slot, VoteUpPlaceholder.applyPlaceholder(resultItem, vote));
participantMap.put(slot, user.uuid);
} else inv.setItem(slot, item);
} else inv.setItem(slot, VoteUpPlaceholder.applyPlaceholder(item, vote));
slotItemMap.put(slot, keyWord);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public Prompt acceptInput(ConversationContext context, String input) {
return Prompt.END_OF_CONVERSATION;
}

VoteUpAPI.VOTE_MANAGER.delete(vote.voteID);
vote.voteID = I18n.deColor(input, '&');
VoteUpAPI.VOTE_MANAGER.load(vote);
// VoteUpAPI.VOTE_MANAGER.setVoteData(vote.voteID, user, TARGET, I18n.color(input));
VoteUpAPI.VOTE_MANAGER.back(user);
return Prompt.END_OF_CONVERSATION;
Expand Down
4 changes: 2 additions & 2 deletions VoteUp/src/main/java/net/shoal/sir/voteup/enums/Msg.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public enum Msg {
AUTOCAST_MODE_WHITELIST_DESC("可以执行的指令"),
AUTOCAST_MODE_BLACKLIST_DESC("不可以执行的指令"),
AUTOCAST_MODE_USERMODE_DESC("所有您可以执行的指令"),
BSTATS_ENABLE("bStats 统计信息已启用, 感谢您的支持!"),
BSTATS_DISABLE("bStats 统计信息已禁用."),
BSTATS_ENABLE("&c&lbStats 统计信息&7已启用, 感谢您的支持!"),
BSTATS_DISABLE("&c&lbStats 统计信息&7已禁用."),
ERROR_GET_CHOICE("&c获取选项内容失败"),
ERROR_GET_VOTE("&7目标投票不存在."),
ERROR_PLACEHOLDER_REQUEST("&c获取变量内容失败"),
Expand Down
52 changes: 26 additions & 26 deletions VoteUp/src/main/resources/Guis/VoteCreate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,23 @@ Items:
Y: 3
PUBLIC_MODE:
ItemStack:
Display: "&b%PUBLIC_display%"
Material: LIME_DYE
Lore:
- ''
- ' &7&o是否公开投票进度,'
- ' &7&o公开后任何玩家都可以查看'
- ' &7&o当前投票参与者以及他们所投的票.'
- ' &7&o且在收到新投票时会全服公告.'
- ''
- ' &7当前值 &b▶'
- ' &f%PUBLIC%'
- ''
- ' &b▶ &f%feature%'
- ''
Position:
X: 8
Y: 3
Display: "&b%PUBLIC_display%"
Material: LIME_DYE
Lore:
- ''
- ' &7&o是否公开投票进度,'
- ' &7&o公开后任何玩家都可以查看'
- ' &7&o当前投票参与者以及他们所投的票.'
- ' &7&o且在收到新投票时会全服公告.'
- ''
- ' &7当前值 &b▶'
- ' &f%PUBLIC%'
- ''
- ' &b▶ &f%feature%'
- ''
Position:
X: 8
Y: 3
SET_DURATION:
ItemStack:
Display: "&b%DURATION_display%"
Expand Down Expand Up @@ -250,15 +250,15 @@ Items:
Material: LIME_DYE
Lore:
- ''
- ' &7&o是否允许事后编辑自己所投的票,'
- ' &7&o开启后参与者可以在投票结束前'
- ' &7&o任意更改自己投的票和投票看法.'
- ''
- ' &7当前值 &b▶'
- ' &f%EDITABLE%'
- ''
- ' &b▶ &f%feature%'
- ''
- ' &7&o是否允许事后编辑自己所投的票,'
- ' &7&o开启后参与者可以在投票结束前'
- ' &7&o任意更改自己投的票和投票看法.'
- ''
- ' &7当前值 &b▶'
- ' &f%EDITABLE%'
- ''
- ' &b▶ &f%feature%'
- ''
Position:
X: 7
Y: 4
Expand Down
Loading

0 comments on commit 255ca79

Please sign in to comment.