diff --git a/pom.xml b/pom.xml index b2e3082..108e8e2 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ cn.lunadeer MiniPlayerTitle - 1.12-beta + 1.13-beta jar MiniPlayerTitle diff --git a/src/main/java/cn/lunadeer/miniplayertitle/Commands.java b/src/main/java/cn/lunadeer/miniplayertitle/Commands.java index 8fb5380..bd56ec8 100644 --- a/src/main/java/cn/lunadeer/miniplayertitle/Commands.java +++ b/src/main/java/cn/lunadeer/miniplayertitle/Commands.java @@ -7,7 +7,6 @@ import cn.lunadeer.miniplayertitle.utils.STUI.Line; import cn.lunadeer.miniplayertitle.utils.STUI.View; import cn.lunadeer.miniplayertitle.utils.XLogger; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.TextComponent; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; @@ -105,7 +104,7 @@ public class Commands implements TabExecutor { Component shop = Button.create("称号商店", "/mplt shop"); Line line = Line.create(); line.append(backpack).append(shop); - view.set(View.Slot.ACTIONBAR, line); + view.actionBar(line); view.showOn((Player) sender); } diff --git a/src/main/java/cn/lunadeer/miniplayertitle/MiniPlayerTitle.java b/src/main/java/cn/lunadeer/miniplayertitle/MiniPlayerTitle.java index 5924ec9..69ec5ad 100644 --- a/src/main/java/cn/lunadeer/miniplayertitle/MiniPlayerTitle.java +++ b/src/main/java/cn/lunadeer/miniplayertitle/MiniPlayerTitle.java @@ -21,7 +21,17 @@ public final class MiniPlayerTitle extends JavaPlugin { Objects.requireNonNull(Bukkit.getPluginCommand("MiniPlayerTitle")).setExecutor(new Commands()); Objects.requireNonNull(Bukkit.getPluginCommand("MiniPlayerTitle")).setTabCompleter(new Commands()); - XLogger.info("NewbTitle 称号插件已加载"); + String logo = "称号插件已加载\n"; + // http://patorjk.com/software/taag/#p=display&f=Big&t=MiniPlayerTitle + logo += " __ __ _ _ _____ _ _______ _ _ _\n" + + " | \\/ (_) (_) __ \\| | |__ __(_) | | |\n" + + " | \\ / |_ _ __ _| |__) | | __ _ _ _ ___ _ __| | _| |_| | ___\n" + + " | |\\/| | | '_ \\| | ___/| |/ _` | | | |/ _ \\ '__| | | | __| |/ _ \\\n" + + " | | | | | | | | | | | | (_| | |_| | __/ | | | | | |_| | __/\n" + + " |_| |_|_|_| |_|_|_| |_|\\__,_|\\__, |\\___|_| |_| |_|\\__|_|\\___|\n" + + " __/ |\n" + + " |___/"; + XLogger.info(logo); } @Override diff --git a/src/main/java/cn/lunadeer/miniplayertitle/Shop.java b/src/main/java/cn/lunadeer/miniplayertitle/Shop.java index af2df22..5845803 100644 --- a/src/main/java/cn/lunadeer/miniplayertitle/Shop.java +++ b/src/main/java/cn/lunadeer/miniplayertitle/Shop.java @@ -4,7 +4,7 @@ import cn.lunadeer.miniplayertitle.utils.Database; import cn.lunadeer.miniplayertitle.utils.Notification; import cn.lunadeer.miniplayertitle.utils.STUI.Button; import cn.lunadeer.miniplayertitle.utils.STUI.Line; -import cn.lunadeer.miniplayertitle.utils.STUI.View; +import cn.lunadeer.miniplayertitle.utils.STUI.ListView; import cn.lunadeer.miniplayertitle.utils.XLogger; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; @@ -20,39 +20,30 @@ public class Shop { Map titles = getSaleTitles(); if (!(sender instanceof Player)) { for (SaleTitle title : titles.values()) { - Notification.info(sender, "[" + title.getSaleId() + "]"); - Notification.info(sender, title.getTitle()); + Component idx = Component.text("[" + title.getSaleId() + "] "); + Notification.info(sender, idx.append(title.getTitle())); } return; } Player player = (Player) sender; - int offset = (page - 1) * 4; - if (offset >= titles.size() || offset < 0) { - Notification.error(player, "页数超出范围"); - return; - } - View view = View.create(); + ListView view = ListView.create(5, "/mplt shop"); view.title("称号商店"); view.subtitle("当前余额: " + XPlayer.getCoin(player) + "金币"); - for (int i = offset; i < offset + 4; i++) { - if (i >= titles.size()) { - break; - } - Integer title_sale_id = (Integer) titles.keySet().toArray()[i]; + for (Map.Entry entry : titles.entrySet()) { + Integer title_sale_id = entry.getKey(); TextComponent idx = Component.text("[" + title_sale_id + "] "); - SaleTitle title = titles.get(title_sale_id); + SaleTitle title = entry.getValue(); Line line = Line.create(); Component button = Button.create("购买", "/mplt buy " + title_sale_id); line.append(idx) .append(title.getTitle()) - .append("价格:" + title.getPrice() + " 有效期:" + (title.getDays()==-1?"永久":title.getDays() + "天")) + .append("价格:" + title.getPrice() + " 有效期:" + (title.getDays() == -1 ? "永久" : title.getDays() + "天")) .append("售卖截止:" + title.getSaleEndAt()) .append("剩余:" + ((title.getAmount() == -1) ? "无限" : title.getAmount())) .append(button); - view.set(i, line); + view.add(line); } - view.set(View.Slot.ACTIONBAR, View.pagination(page, titles.size(), "/mplt shop")); - view.showOn(player); + view.showOn(player, page); } public static void deleteTitle(Integer id) { diff --git a/src/main/java/cn/lunadeer/miniplayertitle/Title.java b/src/main/java/cn/lunadeer/miniplayertitle/Title.java index 8db16ab..1626323 100644 --- a/src/main/java/cn/lunadeer/miniplayertitle/Title.java +++ b/src/main/java/cn/lunadeer/miniplayertitle/Title.java @@ -3,10 +3,9 @@ package cn.lunadeer.miniplayertitle; import cn.lunadeer.miniplayertitle.utils.Database; import cn.lunadeer.miniplayertitle.utils.Notification; import cn.lunadeer.miniplayertitle.utils.STUI.Line; -import cn.lunadeer.miniplayertitle.utils.STUI.View; +import cn.lunadeer.miniplayertitle.utils.STUI.ListView; import cn.lunadeer.miniplayertitle.utils.XLogger; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.JoinConfiguration; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.event.HoverEvent; import org.bukkit.command.CommandSender; @@ -21,7 +20,6 @@ public class Title { protected String _title; protected String _description; protected Boolean _enabled; - JoinConfiguration join = JoinConfiguration.separator(Component.text(" ")); public static Title create(String title, String description) { String sql = ""; @@ -62,30 +60,21 @@ public class Title { List titles = all(); if (!(sender instanceof Player)) { for (Title title : titles) { - Notification.info(sender, "[" + title.getId() + "]"); - Notification.info(sender, title.getTitle()); + Component idx = Component.text("[" + title.getId() + "]"); + Notification.info(sender, idx.append(title.getTitle())); } return; } Player player = (Player) sender; - int offset = (page - 1) * 4; - if (offset >= titles.size() || offset < 0) { - Notification.error(player, "页数超出范围"); - return; - } - View view = View.create(); + ListView view = ListView.create(5, "/mplt listall"); view.title("所有称号"); - for (int i = offset; i < offset + 4; i++) { - if (i >= titles.size()) { - break; - } - TextComponent idx = Component.text("[" + titles.get(i).getId() + "] "); + for (Title title : titles) { + TextComponent idx = Component.text("[" + title.getId() + "] "); Line line = Line.create(); - line.append(idx).append(titles.get(i).getTitle()); - view.set(i, line); + line.append(idx).append(title.getTitle()); + view.add(line); } - view.set(View.Slot.ACTIONBAR, View.pagination(page, titles.size(), "/mplt listall")); - view.showOn(player); + view.showOn(player, page); } public Title(Integer id) { diff --git a/src/main/java/cn/lunadeer/miniplayertitle/XPlayer.java b/src/main/java/cn/lunadeer/miniplayertitle/XPlayer.java index d02b127..f086091 100644 --- a/src/main/java/cn/lunadeer/miniplayertitle/XPlayer.java +++ b/src/main/java/cn/lunadeer/miniplayertitle/XPlayer.java @@ -4,7 +4,7 @@ import cn.lunadeer.miniplayertitle.utils.Database; import cn.lunadeer.miniplayertitle.utils.Notification; import cn.lunadeer.miniplayertitle.utils.STUI.Button; import cn.lunadeer.miniplayertitle.utils.STUI.Line; -import cn.lunadeer.miniplayertitle.utils.STUI.View; +import cn.lunadeer.miniplayertitle.utils.STUI.ListView; import cn.lunadeer.miniplayertitle.utils.Time; import cn.lunadeer.miniplayertitle.utils.XLogger; import net.kyori.adventure.text.Component; @@ -12,10 +12,7 @@ import net.kyori.adventure.text.TextComponent; import org.bukkit.entity.Player; import java.sql.ResultSet; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; -import java.util.UUID; +import java.util.*; public class XPlayer { private final Player _player; @@ -39,21 +36,12 @@ public class XPlayer { } public void openBackpack(Integer page) { - Map<Integer, PlayerTitle> titles = getTitles(_player.getUniqueId()); - int offset = (page - 1) * 4; - if (offset >= titles.size() || offset < 0) { - Notification.error(_player, "页数超出范围"); - return; - } - View view = View.create(); + Collection<PlayerTitle> titles = getTitles(_player.getUniqueId()).values(); + ListView view = ListView.create(4, "/mplt list"); view.title("我的称号"); - for (int i = offset; i < offset + 4; i++) { - if (i >= titles.size()) { - break; - } - int title_id = (int) titles.keySet().toArray()[i]; + for (PlayerTitle title : titles) { + int title_id = title.getId(); TextComponent idx = Component.text("[" + title_id + "] "); - PlayerTitle title = titles.get(title_id); Line line = Line.create(); boolean is_using = Objects.equals(title.getId(), _current_title_id); Component button = Button.create(is_using ? "卸下" : "使用", "/mplt use " + (is_using ? -1 : title.getId())); @@ -61,10 +49,9 @@ public class XPlayer { .append(title.getTitle()) .append(Component.text("有效期至:" + title.getExpireAtStr())) .append(button); - view.set(i, line); + view.add(line); } - view.set(View.Slot.ACTIONBAR, View.pagination(page, titles.size(), "/mplt list")); - view.showOn(_player); + view.showOn(_player, page); } public void updateUsingTitle(Integer title_id) { @@ -149,12 +136,11 @@ public class XPlayer { sql += "SELECT title_id "; sql += "FROM mplt_player_using_title "; sql += "WHERE uuid = '" + uuid.toString() + "';"; - Integer current_title_id = null; + int current_title_id = -1; try (ResultSet rs = Database.query(sql)) { if (rs != null && rs.next()) { current_title_id = rs.getInt("title_id"); } else { - current_title_id = -1; sql = ""; sql += "INSERT INTO mplt_player_using_title (uuid, title_id) VALUES ("; sql += "'" + uuid + "', "; diff --git a/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/Button.java b/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/Button.java index 62da3bb..4f701e4 100644 --- a/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/Button.java +++ b/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/Button.java @@ -1,11 +1,12 @@ package cn.lunadeer.miniplayertitle.utils.STUI; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; public class Button { - public static Component create(String text, String command) { - return Component.text("[" + text + "]", View.action_color) + public static TextComponent create(String text, String command) { + return Component.text("[" + text + "]", ViewStyles.action_color) .clickEvent(net.kyori.adventure.text.event.ClickEvent.clickEvent(net.kyori.adventure.text.event.ClickEvent.Action.RUN_COMMAND, command)); } } diff --git a/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/Line.java b/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/Line.java index b8ba5f4..2c3e93f 100644 --- a/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/Line.java +++ b/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/Line.java @@ -8,9 +8,9 @@ import java.util.List; public class Line { - private List<Component> elements = new ArrayList<>(); + private final List<Component> elements = new ArrayList<>(); - private TextComponent divider = Component.text(" - ", View.sub_color); + private final TextComponent divider = Component.text(" - ", ViewStyles.sub_color); public Line() { } diff --git a/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/ListView.java b/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/ListView.java new file mode 100644 index 0000000..f61316b --- /dev/null +++ b/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/ListView.java @@ -0,0 +1,67 @@ +package cn.lunadeer.miniplayertitle.utils.STUI; + +import cn.lunadeer.miniplayertitle.utils.Notification; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.List; + +public class ListView { + + private final Integer page_size; + private final List<Line> lines = new ArrayList<>(); + private String command = ""; + private final View view = View.create(); + + public ListView(int page_size, String command) { + super(); + this.page_size = page_size; + this.command = command; + } + + public static ListView create(int page_size, String command) { + return new ListView(page_size, command); + } + + public ListView title(String title) { + view.title(title); + return this; + } + + public ListView title(String title, String subtitle) { + view.title(title); + view.subtitle(subtitle); + return this; + } + + public ListView subtitle(String subtitle) { + view.subtitle(subtitle); + return this; + } + + public ListView add(Line line) { + lines.add(line); + return this; + } + + public ListView addLines(List<Line> lines) { + this.lines.addAll(lines); + return this; + } + + public void showOn(Player player, Integer page) { + int offset = (page - 1) * page_size; + if (offset >= lines.size() || offset < 0) { + Notification.error(player, "页数超出范围"); + return; + } + for (int i = offset; i < offset + page_size; i++) { + if (i >= lines.size()) { + break; + } + view.addLine(lines.get(i)); + } + view.actionBar(Pagination.create(page, lines.size(), this.command)); + view.showOn(player); + } +} diff --git a/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/Pagination.java b/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/Pagination.java new file mode 100644 index 0000000..c0d8f68 --- /dev/null +++ b/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/Pagination.java @@ -0,0 +1,38 @@ +package cn.lunadeer.miniplayertitle.utils.STUI; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; + +import java.util.ArrayList; +import java.util.List; + +import static cn.lunadeer.miniplayertitle.utils.STUI.ViewStyles.main_color; +import static cn.lunadeer.miniplayertitle.utils.STUI.ViewStyles.sub_color; + +public class Pagination { + public static TextComponent create(int page, int item_size, String command) { + // 第 x/y 页 [上一页] [下一页] + int page_size = 4; + int page_count = (int) Math.ceil((double) item_size / page_size); + if (page_count == 0) { + page_count = 1; + } + List<Component> componentList = new ArrayList<>(); + componentList.add(Component.text("第 ", main_color)); + componentList.add(Component.text(page, sub_color)); + componentList.add(Component.text("/", main_color)); + componentList.add(Component.text(page_count, sub_color)); + componentList.add(Component.text(" 页 ", main_color)); + if (page > 1) { + componentList.add(Button.create("[上一页]", command + " " + (page - 1))); + } + if (page < page_count) { + componentList.add(Button.create("[下一页]", command + " " + (page + 1))); + } + TextComponent.Builder builder = Component.text(); + for (Component component : componentList) { + builder.append(component); + } + return builder.build(); + } +} diff --git a/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/View.java b/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/View.java index 1ec2f53..5f3d4f2 100644 --- a/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/View.java +++ b/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/View.java @@ -2,72 +2,30 @@ package cn.lunadeer.miniplayertitle.utils.STUI; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; -import net.kyori.adventure.text.format.TextColor; import org.bukkit.entity.Player; import java.util.ArrayList; import java.util.List; +import static cn.lunadeer.miniplayertitle.utils.STUI.ViewStyles.main_color; + public class View { - public enum Slot { - SUBTITLE, - LINE_1, - LINE_2, - LINE_3, - LINE_4, - ACTIONBAR - } - - public static TextColor main_color = TextColor.color(0, 179, 255); - public static TextColor sub_color = TextColor.color(143, 143, 143); - public static TextColor action_color = TextColor.color(251, 255, 139); - protected TextComponent title_decorate = Component.text("===========", main_color); protected TextComponent sub_title_decorate = Component.text("=====", main_color); protected TextComponent line_decorate = Component.text("- ", main_color); protected TextComponent action_decorate = Component.text("> ", main_color); protected TextComponent title = Component.text(" "); protected TextComponent subtitle = Component.text(" "); - protected TextComponent content_line1 = Component.text(" "); - protected TextComponent content_line2 = Component.text(" "); - protected TextComponent content_line3 = Component.text(" "); - protected TextComponent content_line4 = Component.text(" "); + protected List<TextComponent> content_lines = new ArrayList<>(); protected TextComponent actionbar = Component.text(" "); protected TextComponent bottom_decorate = Component.text("=================================", main_color); - public static TextComponent pagination(int page, int item_size, String command) { - // 第 x/y 页 [上一页] [下一页] - int page_size = 4; - int page_count = (int) Math.ceil((double) item_size / page_size); - if (page_count == 0) { - page_count = 1; - } - List<Component> componentList = new ArrayList<>(); - componentList.add(Component.text("第 ", main_color)); - componentList.add(Component.text(page, sub_color)); - componentList.add(Component.text("/", main_color)); - componentList.add(Component.text(page_count, sub_color)); - componentList.add(Component.text(" 页 ", main_color)); - if (page > 1) { - componentList.add(Button.create("[上一页]", command + " " + (page - 1))); - } - if (page < page_count) { - componentList.add(Button.create("[下一页]", command + " " + (page + 1))); - } - TextComponent.Builder builder = Component.text(); - for (Component component : componentList) { - builder.append(component); - } - return builder.build(); - } - public void showOn(Player player) { player.sendMessage(Component.text().append(title_decorate).append(title).append(title_decorate).build()); player.sendMessage(Component.text().append(sub_title_decorate).append(subtitle).build()); - player.sendMessage(Component.text().append(line_decorate).append(content_line1).build()); - player.sendMessage(Component.text().append(line_decorate).append(content_line2).build()); - player.sendMessage(Component.text().append(line_decorate).append(content_line3).build()); - player.sendMessage(Component.text().append(line_decorate).append(content_line4).build()); + for (TextComponent content_line : content_lines) { + player.sendMessage(Component.text().append(line_decorate).append(content_line).build()); + } player.sendMessage(Component.text().append(action_decorate).append(actionbar).build()); player.sendMessage(bottom_decorate); player.sendMessage(Component.text(" ")); @@ -97,90 +55,33 @@ public class View { return this; } - public View set(Slot line, TextComponent component) { - switch (line) { - case SUBTITLE: - this.subtitle = component; - break; - case LINE_1: - this.content_line1 = component; - break; - case LINE_2: - this.content_line2 = component; - break; - case LINE_3: - this.content_line3 = component; - break; - case LINE_4: - this.content_line4 = component; - break; - case ACTIONBAR: - this.actionbar = component; - break; - } + public View actionBar(TextComponent actionbar) { + this.actionbar = actionbar; return this; } - public View set(Slot line, String component) { - switch (line) { - case SUBTITLE: - this.subtitle = Component.text(component); - break; - case LINE_1: - this.content_line1 = Component.text(component); - break; - case LINE_2: - this.content_line2 = Component.text(component); - break; - case LINE_3: - this.content_line3 = Component.text(component); - break; - case LINE_4: - this.content_line4 = Component.text(component); - break; - case ACTIONBAR: - this.actionbar = Component.text(component); - break; - } + public View actionBar(String actionbar) { + this.actionbar = Component.text(actionbar); return this; } - public View set(int index, Line component) { - if (index % 4 == 0) { - this.set(View.Slot.LINE_1, component); - } else if (index % 4 == 1) { - this.set(View.Slot.LINE_2, component); - } else if (index % 4 == 2) { - this.set(View.Slot.LINE_3, component); - } else if (index % 4 == 3) { - this.set(View.Slot.LINE_4, component); - } else { - throw new IllegalArgumentException("index must be 0-3"); - } + public View actionBar(Line actionbar) { + this.actionbar = actionbar.build(); return this; } - public View set(Slot line, Line component) { - switch (line) { - case SUBTITLE: - this.subtitle = component.build(); - break; - case LINE_1: - this.content_line1 = component.build(); - break; - case LINE_2: - this.content_line2 = component.build(); - break; - case LINE_3: - this.content_line3 = component.build(); - break; - case LINE_4: - this.content_line4 = component.build(); - break; - case ACTIONBAR: - this.actionbar = component.build(); - break; - } + public View addLine(TextComponent component) { + this.content_lines.add(component); + return this; + } + + public View addLine(String component) { + this.content_lines.add(Component.text(component)); + return this; + } + + public View addLine(Line component) { + this.content_lines.add(component.build()); return this; } } diff --git a/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/ViewStyles.java b/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/ViewStyles.java new file mode 100644 index 0000000..b073b54 --- /dev/null +++ b/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/ViewStyles.java @@ -0,0 +1,9 @@ +package cn.lunadeer.miniplayertitle.utils.STUI; + +import net.kyori.adventure.text.format.TextColor; + +public class ViewStyles { + public static TextColor main_color = TextColor.color(0, 179, 255); + public static TextColor sub_color = TextColor.color(143, 143, 143); + public static TextColor action_color = TextColor.color(251, 255, 139); +}