Skip to content

Commit

Permalink
feat(review): Use GUI to display contents.
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Sep 12, 2023
1 parent 408b90b commit f323ff1
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> answers) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -125,13 +124,32 @@ public static final class COMMENT extends ConfigurationRoot {
public static final ConfiguredValue<Integer> MAX = ConfiguredValue.of(Integer.class, 120);

@HeaderComment("How many letters are displayed in a single line")
public static final ConfiguredValue<Integer> LINE = ConfiguredValue.of(Integer.class, 30);
public static final ConfiguredValue<Integer> LINE = ConfiguredValue.of(Integer.class, 25);

@HeaderComment("Prefix for each line")
public static final ConfiguredValue<String> 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<Integer> LETTERS_PER_LINE = ConfiguredValue.of(Integer.class, 25);

@HeaderComment("Max lines that displayed in lore")
public static final ConfiguredValue<Integer> MAX_LINES = ConfiguredValue.of(Integer.class, 6);

@HeaderComment("Prefix for each line")
public static final ConfiguredValue<String> 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<String> EXTRA = ConfiguredMessageList.ofStrings(
"&f&o ... More in details!"
);

}


public static final class ICON extends ConfigurationRoot {

public static final ConfiguredItem INFO = ConfiguredItem.create()
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,9 @@ public void onQuit(PlayerQuitEvent event) {
cancelComment(event.getPlayer());
}

public static List<String> getCommentLore(String content) {
List<String> 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<String> getCommentLore(PendingVote vote) {
return getCommentLore(vote.getComments());
}

}
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -14,5 +21,57 @@ public static void loadPageIcon(AutoPagedGUI gui, Player player,
gui.setPreviousPageUI(CommonConfig.PAGE_ITEMS.PREVIOUS_PAGE.get(player));
}

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

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

int lettersPreLine = PluginConfig.ANSWERS.LETTERS_PER_LINE.getNotNull();

String prefix = PluginConfig.ANSWERS.PREFIX.getNotNull();

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) {
List<String> 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<String> formatCommentLine(PendingVote vote) {
return formatCommentLine(vote.getComments());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@
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 @@ -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<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)) {
@Override
public void onClick(Player clicker, ClickType type) {
player.closeInventory();
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
));
}
Expand Down
Loading

0 comments on commit f323ff1

Please sign in to comment.