Skip to content

Commit

Permalink
feat(question): Support #question# lore insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Sep 20, 2023
1 parent a516914 commit 96948a8
Show file tree
Hide file tree
Showing 18 changed files with 42 additions and 55 deletions.
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.artformgames</groupId>
<artifactId>votepass-parent</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
</parent>
<properties>
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.artformgames</groupId>
<artifactId>votepass-parent</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
</parent>
<properties>
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import cc.carm.lib.configuration.core.annotation.ConfigPath;
import cc.carm.lib.configuration.core.annotation.HeaderComment;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.value.item.ConfiguredItem;
import com.artformgames.plugin.votepass.core.database.DBConfiguration;
import com.artformgames.plugin.votepass.core.utils.TimeStringUtils;
import org.bukkit.Material;
Expand Down
2 changes: 1 addition & 1 deletion game/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.artformgames</groupId>
<artifactId>votepass-parent</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>
Expand Down
2 changes: 1 addition & 1 deletion game/plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.artformgames</groupId>
<artifactId>votepass-parent</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import cc.carm.lib.configuration.core.annotation.HeaderComment;
import cc.carm.lib.configuration.core.value.type.ConfiguredMap;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.value.item.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessageList;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredSound;
import com.artformgames.plugin.votepass.core.utils.TimeStringUtils;
Expand Down Expand Up @@ -126,8 +126,6 @@ public static final class COMMENT extends ConfigurationRoot {
@HeaderComment("How many letters are displayed in a single line")
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 ");

}

Expand All @@ -139,9 +137,6 @@ public static final class ANSWERS extends ConfigurationRoot {
@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!"
Expand Down Expand Up @@ -182,6 +177,7 @@ public static final class ICON extends ConfigurationRoot {

}

@SuppressWarnings("unused")
public static final class GUIS extends ConfigurationRoot {

public static final Class<?> ABSTAIN_TOGGLE = AbstainToggleGUI.CONFIG.class;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.artformgames.plugin.votepass.game.ui;

import cc.carm.lib.easyplugin.gui.paged.AutoPagedGUI;
import cc.carm.lib.easyplugin.utils.ColorParser;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.value.item.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.value.item.PreparedItem;
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;
Expand All @@ -21,14 +21,18 @@ 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());
PreparedItem item = itemConfig.prepare(answer.question(), answer.countWords());

item.insertLore("question", sortContent(
answer.question(), PluginConfig.ANSWERS.LETTERS_PER_LINE.getNotNull()
));

List<String> lore = GUIUtils.formatAnswersLore(answer);
if (lore.size() > maxLine) {
item.insertLore("contents", lore.subList(0, maxLine));
item.insertLore("contents", lore.subList(0, maxLine), true);
item.insertLore("more-contents", PluginConfig.ANSWERS.EXTRA);
} else if (!lore.isEmpty()) {
item.insertLore("contents", lore);
item.insertLore("contents", lore, true);
}
return item.get(player);
}
Expand All @@ -46,48 +50,35 @@ public static List<String> formatAnswersLore(@NotNull RequestAnswer answers) {
}

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

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

public static List<String> formatCommentLine(String content) {
return sortContent(
content,
PluginConfig.COMMENT.PREFIX.getNotNull(),
PluginConfig.COMMENT.LINE.getNotNull()
);
return sortContent(content, PluginConfig.COMMENT.LINE.getNotNull());
}

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

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

content = content
.replaceAll("%+([一-龥_a-zA-Z0-9-]+)%+", "$1")
.replaceAll(Pattern.quote("§"), "&")
.replaceAll("^&+$", "");// Prevent color problems
content = ColorParser.clear(content);

if (content.isBlank()) return lore;
content = content.replaceAll(Pattern.quote("§"), "&");// Prevent color problems

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

return lore;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.artformgames.plugin.votepass.game.ui;

import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.value.item.PreparedItem;
import com.artformgames.plugin.votepass.api.data.request.RequestInformation;
import com.artformgames.plugin.votepass.api.data.vote.VoteInformation;
import com.artformgames.plugin.votepass.core.conf.CommonConfig;
Expand Down Expand Up @@ -65,7 +65,7 @@ public Object[] generateParams() {
};
}

public ConfiguredItem.PreparedItem prepareIcon() {
public PreparedItem prepareIcon() {
return PluginConfig.ICON.INFO
.prepare(generateParams())
.setSkullOwner(info.getUserUUID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import cc.carm.lib.easyplugin.gui.GUIItem;
import cc.carm.lib.easyplugin.gui.GUIType;
import cc.carm.lib.easyplugin.gui.paged.AutoPagedGUI;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.value.item.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessage;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessageList;
import com.artformgames.plugin.votepass.api.data.request.RequestAnswer;
Expand Down Expand Up @@ -131,7 +131,7 @@ public static final class ITEMS extends ConfigurationRoot {
.defaultLore(
" ",
"&fThis answer contains &e%(words) &fletters.",
"#contents#{1}",
"{ &f&o}#contents#{1}",
"#more-contents#{1}",
" ",
"&a ▶ Click &8|&f View full answer contents"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import cc.carm.lib.easyplugin.gui.GUIItem;
import cc.carm.lib.easyplugin.gui.GUIType;
import cc.carm.lib.easyplugin.gui.paged.AutoPagedGUI;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.value.item.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessage;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessageList;
import com.artformgames.plugin.votepass.api.data.request.RequestInformation;
Expand Down Expand Up @@ -56,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", GUIUtils.formatCommentLine(vote.comment()))
.insertLore("comment", GUIUtils.formatCommentLine(vote.comment()), true)
.get(player)
));
} else {
addItem(new GUIItem(CONFIG.ITEMS.REJECTED
.prepare(vote.voter().getDisplayName(), vote.getTimeString())
.insertLore("comment", GUIUtils.formatCommentLine(vote.comment()))
.insertLore("comment", GUIUtils.formatCommentLine(vote.comment()), true)
.get(player)
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import cc.carm.lib.easyplugin.gui.GUI;
import cc.carm.lib.easyplugin.gui.GUIItem;
import cc.carm.lib.easyplugin.gui.GUIType;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.value.item.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessage;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessageList;
import com.artformgames.plugin.votepass.api.data.request.RequestInformation;
Expand Down Expand Up @@ -129,6 +129,7 @@ public void executeCommands(Player player, ConfiguredMessageList<String> list, O
}

public void executeCommands(@Nullable List<String> commands) {
if (commands == null) return;
for (String command : commands) {
try {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import cc.carm.lib.easyplugin.gui.GUIItem;
import cc.carm.lib.easyplugin.gui.GUIType;
import cc.carm.lib.easyplugin.gui.paged.AutoPagedGUI;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.value.item.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessage;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessageList;
import com.artformgames.plugin.votepass.api.data.request.RequestAnswer;
Expand Down Expand Up @@ -197,7 +197,7 @@ public static final class ITEMS extends ConfigurationRoot {
.defaultLore(
" ",
"&fThis answer contains &e%(words) &fletters.",
"#contents#{1}",
"{ &f&o}#contents#{1}",
"#more-contents#{1}",
" ",
"&a ▶ Click &8|&f View full answer contents"
Expand Down
2 changes: 1 addition & 1 deletion game/plugin/src/test/java/AnswerFormatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void onTest() {
"answer3 %player_name% %%player%%"
);

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

System.out.println("Formatted ");
formatted.forEach(System.out::println);
Expand Down
2 changes: 1 addition & 1 deletion lobby/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.artformgames</groupId>
<artifactId>votepass-parent</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>
Expand Down
2 changes: 1 addition & 1 deletion lobby/plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.artformgames</groupId>
<artifactId>votepass-parent</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import cc.carm.lib.configuration.core.ConfigurationRoot;
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessage;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessageList;
import com.artformgames.plugin.votepass.core.conf.CommonConfig;
import cc.carm.lib.mineconfiguration.bukkit.value.item.ConfiguredItem;
import org.bukkit.Material;

public class PluginConfig extends ConfigurationRoot {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import cc.carm.lib.easyplugin.gui.paged.AutoPagedGUI;
import cc.carm.lib.easyplugin.utils.ColorParser;
import cc.carm.lib.easyplugin.utils.ItemStackFactory;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.value.item.ConfiguredItem;
import com.artformgames.plugin.votepass.api.data.request.RequestAnswer;
import com.artformgames.plugin.votepass.api.data.request.RequestInformation;
import com.artformgames.plugin.votepass.core.conf.CommonConfig;
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

<deps.easyplugin.version>1.5.8</deps.easyplugin.version>
<deps.easysql.version>0.4.7</deps.easysql.version>
<deps.mineconfig.version>2.8.4</deps.mineconfig.version>
<deps.mineconfig.version>2.8.5</deps.mineconfig.version>

<deps.book-api.version>1.6</deps.book-api.version>
<deps.minedown.version>1.7.1-SNAPSHOT</deps.minedown.version>
</properties>
<groupId>com.artformgames</groupId>
<artifactId>votepass-parent</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
<modules>
<module>api</module>
<module>lobby/api</module>
Expand Down

0 comments on commit 96948a8

Please sign in to comment.