Skip to content

Commit

Permalink
fix(answer): Try to fix the problem of missing line
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Sep 19, 2023
1 parent 41c3899 commit 644f9f3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
package com.artformgames.plugin.votepass.game.ui;

import cc.carm.lib.easyplugin.gui.paged.AutoPagedGUI;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredItem;
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 org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class GUIUtils {

public static ItemStack loadAnswersIcon(Player player,
RequestAnswer answer, ConfiguredItem itemConfig) {
int maxLine = PluginConfig.ANSWERS.MAX_LINES.getNotNull();
ConfiguredItem.PreparedItem item = itemConfig.prepare(answer.question(), answer.countWords());

List<String> lore = GUIUtils.formatAnswersLore(answer);
if (lore.size() > maxLine) {
item.insertLore("contents", lore.subList(0, maxLine));
item.insertLore("more-contents", PluginConfig.ANSWERS.EXTRA);
} else if (!lore.isEmpty()) {
item.insertLore("contents", lore);
}
return item.get(player);
}

public static void loadPageIcon(AutoPagedGUI gui, Player player,
int previousSlot, int nextSlot) {
gui.setPreviousPageSlot(previousSlot);
Expand All @@ -21,57 +40,54 @@ public static void loadPageIcon(AutoPagedGUI gui, Player player,
gui.setPreviousPageUI(CommonConfig.PAGE_ITEMS.PREVIOUS_PAGE.get(player));
}

public static List<String> formatAnswersLore(RequestAnswer answers) {
public static List<String> formatAnswersLore(@NotNull RequestAnswer answers) {
return formatAnswersLore(answers.answers());
}

public static List<String> formatAnswersLore(List<String> answers) {

int lettersPreLine = PluginConfig.ANSWERS.LETTERS_PER_LINE.getNotNull();
public static List<String> formatAnswersLore(@NotNull List<String> answers) {
return formatAnswersLore(
answers, PluginConfig.ANSWERS.PREFIX.getNotNull(),
PluginConfig.ANSWERS.LETTERS_PER_LINE.getNotNull()
);
}

String prefix = PluginConfig.ANSWERS.PREFIX.getNotNull();
public static List<String> formatAnswersLore(List<String> answers, String prefix, int lettersPreLine) {
return answers.stream()
.flatMap(answer -> sortContent(answer, prefix, lettersPreLine).stream())
.collect(Collectors.toList());
}

List<String> 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<String> formatCommentLine(String content) {
return sortContent(
content,
PluginConfig.COMMENT.PREFIX.getNotNull(),
PluginConfig.COMMENT.LINE.getNotNull()
);
}

public static List<String> formatCommentLine(PendingVote vote) {
return formatCommentLine(vote.getComments());
}

public static List<String> formatCommentLine(String content) {
public static List<String> sortContent(String content, String prefix, int lineLength) {
List<String> lore = new ArrayList<>();
if (content == null || content.isBlank()) return lore;
if (content == null) return lore;

int line = PluginConfig.COMMENT.LINE.getNotNull();
String prefix = PluginConfig.COMMENT.PREFIX.getNotNull();
content = content
.replaceAll("%+([一-龥_a-zA-Z0-9-]+)%+", "$1")
.replaceAll("&", "&&").replaceAll(Pattern.quote("§"), "&&")
.replaceAll("^&+$", "");// Prevent color problems
if (content.isBlank()) return lore;

int length = content.length();
int lines = length / line + (length % line == 0 ? 0 : 1);
int lines = length / lineLength + (length % lineLength == 0 ? 0 : 1);
for (int i = 0; i < lines; i++) {
int start = i * line;
int end = Math.min((i + 1) * line, length);
int start = i * lineLength;
int end = Math.min((i + 1) * lineLength, length);
lore.add(prefix + content.substring(start, end));
}

return lore;
}

public static List<String> formatCommentLine(PendingVote vote) {
return formatCommentLine(vote.getComments());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@
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;
import org.bukkit.event.inventory.ClickType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Optional;

public class AdminHandleGUI extends AutoPagedGUI {
Expand Down Expand Up @@ -83,23 +80,13 @@ public void onClick(Player clicker, ClickType type) {
}

public void loadAnswers() {
for (RequestAnswer value : request.getContents().values()) {

ConfiguredItem.PreparedItem item = CONFIG.ITEMS.ANSWER.prepare(value.question(), value.countWords());
List<String> 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)) {
for (RequestAnswer answer : request.getContents().values()) {
addItem(new GUIItem(GUIUtils.loadAnswersIcon(player, answer, CONFIG.ITEMS.ANSWER)) {
@Override
public void onClick(Player clicker, ClickType type) {
player.closeInventory();
PluginMessages.VOTE.VIEWING.send(player, request.getID(), request.getUserDisplayName(), value.question());
RequestAnswerGUI.open(player, request, value, CONFIG.BOOK.RETURN.parseToLine(player, request.getID()));
PluginMessages.VOTE.VIEWING.send(player, request.getID(), request.getUserDisplayName(), answer.question());
RequestAnswerGUI.open(player, request, answer, CONFIG.BOOK.RETURN.parseToLine(player, request.getID()));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
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;
Expand All @@ -28,7 +27,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Optional;

public class VoteHandleGUI extends AutoPagedGUI {
Expand Down Expand Up @@ -119,22 +117,13 @@ public void onClick(Player clicker, ClickType type) {
}

public void loadAnswers() {
for (RequestAnswer value : request.getContents().values()) {
ConfiguredItem.PreparedItem item = CONFIG.ITEMS.ANSWER.prepare(value.question(), value.countWords());
List<String> 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)) {
for (RequestAnswer answer : request.getContents().values()) {
addItem(new GUIItem(GUIUtils.loadAnswersIcon(player, answer, CONFIG.ITEMS.ANSWER)) {
@Override
public void onClick(Player clicker, ClickType type) {
player.closeInventory();
PluginMessages.VOTE.VIEWING.send(player, request.getID(), request.getUserDisplayName(), value.question());
RequestAnswerGUI.open(player, request, value, CONFIG.BOOK.RETURN.parseToLine(player, request.getID()));
PluginMessages.VOTE.VIEWING.send(player, request.getID(), request.getUserDisplayName(), answer.question());
RequestAnswerGUI.open(player, request, answer, CONFIG.BOOK.RETURN.parseToLine(player, request.getID()));
}
});
}
Expand Down
46 changes: 14 additions & 32 deletions game/plugin/src/test/java/AnswerFormatTest.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,32 @@
import com.artformgames.plugin.votepass.api.data.request.RequestAnswer;
import com.artformgames.plugin.votepass.game.conf.PluginConfig;
import com.artformgames.plugin.votepass.game.ui.GUIUtils;
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",
List<String> contents = List.of(
"This is the first line of the answer",
"And this is the second line",
"Of course the third line",
" ",
"%%%%player%%%%",
"",
"answer3 %player_name% %%player%%"
)));
);

}
List<String> formatted = GUIUtils.formatAnswersLore(contents, "-->", 35);

System.out.println("Formatted ");
formatted.forEach(System.out::println);

System.out.println(" ");
System.out.println("Limited x3");
formatted.subList(0, 3).forEach(System.out::println);

public static List<String> formatAnswersLore(List<String> answers) {

int lettersPreLine = 35;

String prefix = "-->";

List<String> 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;
}

}

0 comments on commit 644f9f3

Please sign in to comment.