diff --git a/api/src/main/java/com/artformgames/plugin/votepass/api/data/request/RequestAnswer.java b/api/src/main/java/com/artformgames/plugin/votepass/api/data/request/RequestAnswer.java index ce58149..e4ab94b 100644 --- a/api/src/main/java/com/artformgames/plugin/votepass/api/data/request/RequestAnswer.java +++ b/api/src/main/java/com/artformgames/plugin/votepass/api/data/request/RequestAnswer.java @@ -2,7 +2,9 @@ import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; import java.util.List; +import java.util.regex.Pattern; public record RequestAnswer(@NotNull String question, @NotNull List answers) { diff --git a/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/conf/PluginConfig.java b/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/conf/PluginConfig.java index 809aeaa..2731c95 100644 --- a/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/conf/PluginConfig.java +++ b/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/conf/PluginConfig.java @@ -12,7 +12,6 @@ import com.artformgames.plugin.votepass.game.ui.admin.AdminManageGUI; import com.artformgames.plugin.votepass.game.ui.request.RequestCommentsGUI; import com.artformgames.plugin.votepass.game.ui.user.AbstainToggleGUI; -import com.artformgames.plugin.votepass.game.ui.vote.QuickReviewGUI; import com.artformgames.plugin.votepass.game.ui.vote.RequestListGUI; import com.artformgames.plugin.votepass.game.ui.vote.VoteConfirmGUI; import com.artformgames.plugin.votepass.game.ui.vote.VoteHandleGUI; @@ -125,13 +124,32 @@ public static final class COMMENT extends ConfigurationRoot { public static final ConfiguredValue MAX = ConfiguredValue.of(Integer.class, 120); @HeaderComment("How many letters are displayed in a single line") - public static final ConfiguredValue LINE = ConfiguredValue.of(Integer.class, 30); + public static final ConfiguredValue LINE = ConfiguredValue.of(Integer.class, 25); @HeaderComment("Prefix for each line") public static final ConfiguredValue PREFIX = ConfiguredValue.of(String.class, "&f&o "); } + public static final class ANSWERS extends ConfigurationRoot { + + @HeaderComment("How many letters are displayed in a single line") + public static final ConfiguredValue LETTERS_PER_LINE = ConfiguredValue.of(Integer.class, 25); + + @HeaderComment("Max lines that displayed in lore") + public static final ConfiguredValue MAX_LINES = ConfiguredValue.of(Integer.class, 6); + + @HeaderComment("Prefix for each line") + public static final ConfiguredValue PREFIX = ConfiguredValue.of(String.class, "&f&o "); + + @HeaderComment("Extra lore if answers are too long, tell voters to click to view details.") + public static final ConfiguredMessageList EXTRA = ConfiguredMessageList.ofStrings( + "&f&o ... More in details!" + ); + + } + + public static final class ICON extends ConfigurationRoot { public static final ConfiguredItem INFO = ConfiguredItem.create() @@ -171,7 +189,6 @@ public static final class GUIS extends ConfigurationRoot { public static final Class REQUEST_LIST = RequestListGUI.CONFIG.class; public static final Class REQUEST_COMMENTS = RequestCommentsGUI.CONFIG.class; - public static final Class QUICK_REVIEW = QuickReviewGUI.CONFIG.class; public static final Class VOTE_HANDLE = VoteHandleGUI.CONFIG.class; public static final Class VOTE_CONFIRM = VoteConfirmGUI.CONFIG.class; diff --git a/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/listener/CommentListener.java b/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/listener/CommentListener.java index 492a0ff..b7b7ec0 100644 --- a/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/listener/CommentListener.java +++ b/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/listener/CommentListener.java @@ -58,30 +58,9 @@ public void onQuit(PlayerQuitEvent event) { cancelComment(event.getPlayer()); } - public static List getCommentLore(String content) { - List lore = new ArrayList<>(); - if (content == null || content.isBlank()) return lore; - - int line = PluginConfig.COMMENT.LINE.getNotNull(); - String prefix = PluginConfig.COMMENT.PREFIX.getNotNull(); - - int length = content.length(); - int lines = length / line + (length % line == 0 ? 0 : 1); - for (int i = 0; i < lines; i++) { - int start = i * line; - int end = Math.min((i + 1) * line, length); - lore.add(prefix + content.substring(start, end)); - } - - return lore; - } - public void handle(Player player, PendingVote vote) { Main.getInstance().getScheduler().run(() -> VoteHandleGUI.open(player, vote.getRequest(), null)); } - public static List getCommentLore(PendingVote vote) { - return getCommentLore(vote.getComments()); - } } diff --git a/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/GUIUtils.java b/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/GUIUtils.java index 5213ebe..8830e26 100644 --- a/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/GUIUtils.java +++ b/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/GUIUtils.java @@ -1,9 +1,16 @@ package com.artformgames.plugin.votepass.game.ui; import cc.carm.lib.easyplugin.gui.paged.AutoPagedGUI; +import com.artformgames.plugin.votepass.api.data.request.RequestAnswer; import com.artformgames.plugin.votepass.core.conf.CommonConfig; +import com.artformgames.plugin.votepass.game.api.vote.PendingVote; +import com.artformgames.plugin.votepass.game.conf.PluginConfig; import org.bukkit.entity.Player; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; + public class GUIUtils { public static void loadPageIcon(AutoPagedGUI gui, Player player, @@ -14,5 +21,57 @@ public static void loadPageIcon(AutoPagedGUI gui, Player player, gui.setPreviousPageUI(CommonConfig.PAGE_ITEMS.PREVIOUS_PAGE.get(player)); } + public static List formatAnswersLore(RequestAnswer answers) { + return formatAnswersLore(answers.answers()); + } + + public static List formatAnswersLore(List answers) { + + int lettersPreLine = PluginConfig.ANSWERS.LETTERS_PER_LINE.getNotNull(); + + String prefix = PluginConfig.ANSWERS.PREFIX.getNotNull(); + + List lore = new ArrayList<>(); + for (String answer : answers) { + String cleared = answer + .replaceAll("%+([一-龥_a-zA-Z0-9-]+)%+", "$1") + .replaceAll("&", "&&").replaceAll(Pattern.quote("§"), "&&") + .replaceAll("^&+$", "");// Prevent color problems + if (cleared.isBlank()) continue; + + int length = cleared.length(); + int lines = length / lettersPreLine + (length % lettersPreLine == 0 ? 0 : 1); + for (int i = 0; i < lines; i++) { + int start = i * lettersPreLine; + int end = Math.min((i + 1) * lettersPreLine, length); + lore.add(prefix + cleared.substring(start, end)); + } + } + return lore; + } + + + public static List formatCommentLine(String content) { + List lore = new ArrayList<>(); + if (content == null || content.isBlank()) return lore; + + int line = PluginConfig.COMMENT.LINE.getNotNull(); + String prefix = PluginConfig.COMMENT.PREFIX.getNotNull(); + + int length = content.length(); + int lines = length / line + (length % line == 0 ? 0 : 1); + for (int i = 0; i < lines; i++) { + int start = i * line; + int end = Math.min((i + 1) * line, length); + lore.add(prefix + content.substring(start, end)); + } + + return lore; + } + + public static List formatCommentLine(PendingVote vote) { + return formatCommentLine(vote.getComments()); + } + } diff --git a/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/admin/AdminHandleGUI.java b/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/admin/AdminHandleGUI.java index 4b5b5fe..e32fa6f 100644 --- a/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/admin/AdminHandleGUI.java +++ b/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/admin/AdminHandleGUI.java @@ -10,11 +10,13 @@ import com.artformgames.plugin.votepass.api.data.request.RequestAnswer; import com.artformgames.plugin.votepass.api.data.request.RequestInformation; import com.artformgames.plugin.votepass.game.Main; +import com.artformgames.plugin.votepass.game.conf.PluginConfig; import com.artformgames.plugin.votepass.game.conf.PluginMessages; import com.artformgames.plugin.votepass.game.ui.GUIUtils; import com.artformgames.plugin.votepass.game.ui.RequestIconInfo; import com.artformgames.plugin.votepass.game.ui.request.RequestAnswerGUI; import com.artformgames.plugin.votepass.game.ui.request.RequestCommentsGUI; +import com.artformgames.plugin.votepass.game.ui.vote.VoteHandleGUI; import net.md_5.bungee.api.chat.BaseComponent; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -22,6 +24,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.List; import java.util.Optional; public class AdminHandleGUI extends AutoPagedGUI { @@ -81,7 +84,17 @@ public void onClick(Player clicker, ClickType type) { public void loadAnswers() { for (RequestAnswer value : request.getContents().values()) { - addItem(new GUIItem(CONFIG.ITEMS.ANSWER.get(player, value.question(), value.countWords())) { + + ConfiguredItem.PreparedItem item = CONFIG.ITEMS.ANSWER.prepare(value.question(), value.countWords()); + List lore = GUIUtils.formatAnswersLore(value); + if (lore.size() > PluginConfig.ANSWERS.MAX_LINES.getNotNull()) { + item.insertLore("contents", lore.subList(0, PluginConfig.ANSWERS.MAX_LINES.getNotNull())); + item.insertLore("more-contents", PluginConfig.ANSWERS.EXTRA); + } else { + item.insertLore("contents", lore); + } + + addItem(new GUIItem(item.get(player)) { @Override public void onClick(Player clicker, ClickType type) { player.closeInventory(); @@ -130,9 +143,11 @@ public static final class ITEMS extends ConfigurationRoot { .defaultName("&7Question: &f%(question)") .defaultLore( " ", - "&fThis answer contains &e%(words) &fwords.", + "&fThis answer contains &e%(words) &fletters.", + "#contents#{1}", + "#more-contents#{1}", " ", - "&a ▶ Click &8|&f View answer contents" + "&a ▶ Click &8|&f View full answer contents" ) .params("question", "words") .build(); diff --git a/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/request/RequestCommentsGUI.java b/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/request/RequestCommentsGUI.java index 3d26a3a..9091c26 100644 --- a/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/request/RequestCommentsGUI.java +++ b/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/request/RequestCommentsGUI.java @@ -10,7 +10,6 @@ import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessageList; import com.artformgames.plugin.votepass.api.data.request.RequestInformation; import com.artformgames.plugin.votepass.api.data.vote.VoteInformation; -import com.artformgames.plugin.votepass.game.listener.CommentListener; import com.artformgames.plugin.votepass.game.ui.GUIUtils; import com.artformgames.plugin.votepass.game.ui.RequestIconInfo; import org.bukkit.Material; @@ -57,13 +56,13 @@ public void onClick(Player clicker, ClickType type) { if (vote.isApproved()) { addItem(new GUIItem(CONFIG.ITEMS.APPROVED .prepare(vote.voter().getDisplayName(), vote.getTimeString()) - .insertLore("comment", CommentListener.getCommentLore(vote.comment())) + .insertLore("comment", GUIUtils.formatCommentLine(vote.comment())) .get(player) )); } else { addItem(new GUIItem(CONFIG.ITEMS.REJECTED .prepare(vote.voter().getDisplayName(), vote.getTimeString()) - .insertLore("comment", CommentListener.getCommentLore(vote.comment())) + .insertLore("comment", GUIUtils.formatCommentLine(vote.comment())) .get(player) )); } diff --git a/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/vote/QuickReviewGUI.java b/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/vote/QuickReviewGUI.java deleted file mode 100644 index c3179f3..0000000 --- a/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/vote/QuickReviewGUI.java +++ /dev/null @@ -1,185 +0,0 @@ -package com.artformgames.plugin.votepass.game.ui.vote; - -import cc.carm.lib.configuration.core.ConfigurationRoot; -import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessageList; -import com.artformgames.plugin.votepass.api.data.request.RequestAnswer; -import com.artformgames.plugin.votepass.api.data.request.RequestInformation; -import com.artformgames.plugin.votepass.api.data.vote.VoteDecision; -import com.artformgames.plugin.votepass.api.data.vote.VoteInformation; -import com.artformgames.plugin.votepass.core.conf.TextMessages; -import com.artformgames.plugin.votepass.game.ui.RequestIconInfo; -import net.md_5.bungee.api.chat.BaseComponent; -import org.bukkit.entity.Player; -import xyz.upperlevel.spigot.book.BookUtil; - -import java.util.ArrayList; -import java.util.List; - -public class QuickReviewGUI { - - private QuickReviewGUI() { - } - - public static boolean open(Player player, RequestInformation request) { - BookUtil.BookBuilder builder = BookUtil.writtenBook(); - builder.title("#" + request.getID()); - builder.author(request.getUsername()); - - List pages = new ArrayList<>(); - pages.add(buildOverview(player, request)); - - request.getContents().forEach((index, answer) -> pages.addAll(buildAnswers(player, index, answer))); - - pages.addAll(buildComments(player, request)); - pages.add(CONFIG.RETURN.parseToLine(player, request.getID())); - - if (pages.size() > 50) return false; - - builder.pages(pages); - - BookUtil.openPlayer(player, builder.build()); - return true; - } - - private static BaseComponent[] buildOverview(Player player, RequestInformation request) { - RequestIconInfo iconInfo = RequestIconInfo.of(request); - return CONFIG.OVERVIEW.parseToLine(player, - iconInfo.displayName(), iconInfo.uuid(), iconInfo.id(), iconInfo.words(), - iconInfo.createTime(), iconInfo.expireTime(), - iconInfo.pros(), iconInfo.prosPercent(), - iconInfo.cons(), iconInfo.consPercent(), - iconInfo.abs(), iconInfo.absPercent(), - iconInfo.passRequired(), iconInfo.passRemain(), - iconInfo.size(), iconInfo.total() - ); - } - - private static List buildAnswers(Player player, int id, RequestAnswer content) { - List answers = new ArrayList<>(); - int words = content.countWords(); - if (words <= 0) { - answers.add(CONFIG.ANSWER_EMPTY.parseToLine(player, id, content.question())); - } else { - answers.add(CONFIG.ANSWER_CONTENT.parseToLine(player, id, content.question(), words)); - content.answers().stream().map(answer -> BookUtil.PageBuilder.of(answer).build()).forEach(answers::add); - } - return answers; - } - - private static List buildComments(Player player, RequestInformation request) { - List commentPages = new ArrayList<>(); - - int commented = request.countCommentedVotes(); - if (commented > 0) { - - commentPages.add(CONFIG.HAS_COMMENTS.parseToLine(player, request.getID(), commented)); - - int i = 1; - for (VoteInformation vote : request.getVotes()) { - if (vote.decision() == VoteDecision.ABSTAIN || vote.comment() == null || vote.comment().isEmpty()) { - continue; - } - if (vote.isApproved()) { - commentPages.add(CONFIG.COMMENT_APPROVED.parseToLine(player, i, vote.voter().getDisplayName(), vote.comment())); - } else { - commentPages.add(CONFIG.COMMENT_REJECTED.parseToLine(player, i, vote.voter().getDisplayName(), vote.comment())); - } - i++; - } - - } else { - commentPages.add(CONFIG.NONE_COMMENT.parseToLine(player, request.getID())); - } - - return commentPages; - } - - public static final class CONFIG extends ConfigurationRoot { - - public static final ConfiguredMessageList OVERVIEW = TextMessages.list() - .defaults( - "Handle request #%(request_id)", - " ", - "&7Applicant username", - "&6%(name)", - " ", - "&a&lApproved&7: %(pros_amount)&8(%(pros_ratio)%)", - "&c&lRejected&7: %(cons_amount)&8(%(cons_ratio)%)", - "&e&lAbstain&7: %(abstains_amount)&8(%(abstains_ratio)%)", - " ", - "&fThis request requires &e%(pass_remain)&7/%(pass_required) &fmore approves to pass.", - " ", - "&8Please turn to the next page for detailed answers." - ).params( - "name", "uuid", - "request_id", "request_words", - "create_time", "close_time", - "pros_amount", "pros_ratio", - "cons_amount", "cons_ratio", - "abstains_amount", "abstains_ratio", - "pass_required", "pass_remain", - "votes_amount", "total_amount" - ).build(); - - - public static final ConfiguredMessageList ANSWER_EMPTY = TextMessages.list() - .defaults( - "&8#%(index): &0%(question)", - " ", - "&7&oThis user did not answer the question.", - "&8Please keep turning the page to see other answers." - ).params("index", "question").build(); - - public static final ConfiguredMessageList ANSWER_CONTENT = TextMessages.list() - .defaults( - "&8#%(index): &0%(question)", - " ", - "&8This user answered %(words) words,", - "&8Please keep turning the page to see his answers." - ).params("index", "question", "words").build(); - - public static final ConfiguredMessageList HAS_COMMENTS = TextMessages.list() - .defaults( - "&8This request has a size of %(amount) comments,", - " ", - "&7&oIf you want to express your personal opinion, please return to process this request in the next page.", - "&8Please keep turning the page to see others opinions." - ).params("id", "amount").build(); - - public static final ConfiguredMessageList NONE_COMMENT = TextMessages.list() - .defaults( - "&8There are currently no other votes on this request.", - " ", - "&7&oIf you want to express your personal opinion, please return to process this request in the next page." - ).params("id").build(); - - public static final ConfiguredMessageList COMMENT_APPROVED = TextMessages.list() - .defaults( - "&8#%(index) &7from &9%(voter)", - " ", - "&7This vote is &a&lApproved &7.", - "&8", - "&8%(comment)" - ).params("index", "voter", "comment").build(); - - public static final ConfiguredMessageList COMMENT_REJECTED = TextMessages.list() - .defaults( - "&8#%(index) &7from &9%(voter)", - " ", - "&7This vote is &C&lRejected&7.", - "&8", - "&8%(comment)" - ).params("index", "voter", "comment").build(); - - public static final ConfiguredMessageList RETURN = TextMessages.list() - .defaults( - "All answers have been displayed, please click the text below to go to the detailed interface for processing.", - " ", - "[&a&l[Handle request]](hover=Click to return to the details page and continue processing related answers. run_command=/votepass handle %(id))" - ).params("id") - .build(); - - - } - -} diff --git a/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/vote/RequestListGUI.java b/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/vote/RequestListGUI.java index b25072f..050dd6a 100644 --- a/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/vote/RequestListGUI.java +++ b/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/vote/RequestListGUI.java @@ -56,16 +56,7 @@ protected GUIItem createIcon(@NotNull RequestInformation request) { .get(player)) { @Override public void onClick(Player clicker, ClickType type) { - if (type.isLeftClick() || iconInfo.words() >= 5000) { - VoteHandleGUI.open(player, request, iconInfo); - } else if (type.isRightClick()) { - player.closeInventory(); - if (!QuickReviewGUI.open(player, request)) { - VoteHandleGUI.open(player, request, iconInfo); - } - } else { - player.closeInventory(); - } + VoteHandleGUI.open(player, request, iconInfo); } }; } @@ -79,8 +70,7 @@ public static final class CONFIG extends ConfigurationRoot { public static final class ADDITIONAL_LORE extends ConfigurationRoot { public static final ConfiguredMessageList CLICK = ConfiguredMessageList.asStrings().defaults( - "&a ▶ Left click &8|&f view details", - "&a ▶ Right click &8|&f quick review" + "&a ▶ Click &8|&f View details" ).build(); } diff --git a/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/vote/VoteHandleGUI.java b/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/vote/VoteHandleGUI.java index aa69d9b..940e998 100644 --- a/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/vote/VoteHandleGUI.java +++ b/game/plugin/src/main/java/com/artformgames/plugin/votepass/game/ui/vote/VoteHandleGUI.java @@ -12,6 +12,7 @@ import com.artformgames.plugin.votepass.api.data.vote.VoteDecision; import com.artformgames.plugin.votepass.game.Main; import com.artformgames.plugin.votepass.game.api.vote.PendingVote; +import com.artformgames.plugin.votepass.game.conf.PluginConfig; import com.artformgames.plugin.votepass.game.conf.PluginMessages; import com.artformgames.plugin.votepass.game.listener.CommentListener; import com.artformgames.plugin.votepass.game.ui.GUIUtils; @@ -27,6 +28,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.List; import java.util.Optional; public class VoteHandleGUI extends AutoPagedGUI { @@ -97,7 +99,7 @@ public void onClick(Player clicker, ClickType type) { commentIcon = CONFIG.ITEMS.NOT_COMMENTED.prepare(request.countCommentedVotes()).get(player); } else { commentIcon = CONFIG.ITEMS.COMMENTED.prepare(request.countCommentedVotes()) - .insertLore("comment", CommentListener.getCommentLore(getPendingVote())) + .insertLore("comment", GUIUtils.formatCommentLine(getPendingVote())) .get(player); } @@ -118,7 +120,16 @@ public void onClick(Player clicker, ClickType type) { public void loadAnswers() { for (RequestAnswer value : request.getContents().values()) { - addItem(new GUIItem(CONFIG.ITEMS.ANSWER.get(player, value.question(), value.countWords())) { + ConfiguredItem.PreparedItem item = CONFIG.ITEMS.ANSWER.prepare(value.question(), value.countWords()); + List lore = GUIUtils.formatAnswersLore(value); + if (lore.size() > PluginConfig.ANSWERS.MAX_LINES.getNotNull()) { + item.insertLore("contents", lore.subList(0, PluginConfig.ANSWERS.MAX_LINES.getNotNull())); + item.insertLore("more-contents", PluginConfig.ANSWERS.EXTRA); + } else { + item.insertLore("contents", lore); + } + + addItem(new GUIItem(item.get(player)) { @Override public void onClick(Player clicker, ClickType type) { player.closeInventory(); @@ -129,6 +140,7 @@ public void onClick(Player clicker, ClickType type) { } } + public static final class CONFIG extends ConfigurationRoot { public static final ConfiguredMessage TITLE = ConfiguredMessage.asString() @@ -195,9 +207,11 @@ public static final class ITEMS extends ConfigurationRoot { .defaultName("&7Question: &f%(question)") .defaultLore( " ", - "&fThis answer contains &e%(words) &fwords.", + "&fThis answer contains &e%(words) &fletters.", + "#contents#{1}", + "#more-contents#{1}", " ", - "&a ▶ Click &8|&f View answer contents" + "&a ▶ Click &8|&f View full answer contents" ) .params("question", "words") .build(); diff --git a/game/plugin/src/test/java/AnswerFormatTest.java b/game/plugin/src/test/java/AnswerFormatTest.java new file mode 100644 index 0000000..6c91cdc --- /dev/null +++ b/game/plugin/src/test/java/AnswerFormatTest.java @@ -0,0 +1,50 @@ +import com.artformgames.plugin.votepass.api.data.request.RequestAnswer; +import com.artformgames.plugin.votepass.game.conf.PluginConfig; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; + +public class AnswerFormatTest { + + @Test + public void onTest() { + + System.out.println(formatAnswersLore(List.of( + "answer1answer1answer1answer1answer1answer1", + "answer2answer1answer1answer1answer1answer12", + " ", + "%%%%player%%%%", + "", + "answer3 %player_name% %%player%%" + ))); + + } + + public static List formatAnswersLore(List answers) { + + int lettersPreLine = 35; + + String prefix = "-->"; + + List lore = new ArrayList<>(); + for (String answer : answers) { + String cleared = answer + .replaceAll("%+([一-龥_a-zA-Z0-9-]+)%+", "$1") + .replaceAll("&", "&&").replaceAll(Pattern.quote("§"), "&&") + .replaceAll("^&+$", "");// Prevent color problems + if (cleared.isBlank()) continue; + + int length = cleared.length(); + int lines = length / lettersPreLine + (length % lettersPreLine == 0 ? 0 : 1); + for (int i = 0; i < lines; i++) { + int start = i * lettersPreLine; + int end = Math.min((i + 1) * lettersPreLine, length); + lore.add(prefix + cleared.substring(start, end)); + } + } + return lore; + } + +}