make code more beautiful

This commit is contained in:
zhangyuheng 2024-01-10 10:51:46 +08:00
parent 2a92fdafc2
commit 2e293d89ad
12 changed files with 184 additions and 193 deletions

View File

@ -6,7 +6,7 @@
<groupId>cn.lunadeer</groupId> <groupId>cn.lunadeer</groupId>
<artifactId>MiniPlayerTitle</artifactId> <artifactId>MiniPlayerTitle</artifactId>
<version>1.12-beta</version> <version>1.13-beta</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>MiniPlayerTitle</name> <name>MiniPlayerTitle</name>

View File

@ -7,7 +7,6 @@ import cn.lunadeer.miniplayertitle.utils.STUI.Line;
import cn.lunadeer.miniplayertitle.utils.STUI.View; import cn.lunadeer.miniplayertitle.utils.STUI.View;
import cn.lunadeer.miniplayertitle.utils.XLogger; import cn.lunadeer.miniplayertitle.utils.XLogger;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor; import org.bukkit.command.TabExecutor;
@ -105,7 +104,7 @@ public class Commands implements TabExecutor {
Component shop = Button.create("称号商店", "/mplt shop"); Component shop = Button.create("称号商店", "/mplt shop");
Line line = Line.create(); Line line = Line.create();
line.append(backpack).append(shop); line.append(backpack).append(shop);
view.set(View.Slot.ACTIONBAR, line); view.actionBar(line);
view.showOn((Player) sender); view.showOn((Player) sender);
} }

View File

@ -21,7 +21,17 @@ public final class MiniPlayerTitle extends JavaPlugin {
Objects.requireNonNull(Bukkit.getPluginCommand("MiniPlayerTitle")).setExecutor(new Commands()); Objects.requireNonNull(Bukkit.getPluginCommand("MiniPlayerTitle")).setExecutor(new Commands());
Objects.requireNonNull(Bukkit.getPluginCommand("MiniPlayerTitle")).setTabCompleter(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 @Override

View File

@ -4,7 +4,7 @@ import cn.lunadeer.miniplayertitle.utils.Database;
import cn.lunadeer.miniplayertitle.utils.Notification; import cn.lunadeer.miniplayertitle.utils.Notification;
import cn.lunadeer.miniplayertitle.utils.STUI.Button; import cn.lunadeer.miniplayertitle.utils.STUI.Button;
import cn.lunadeer.miniplayertitle.utils.STUI.Line; 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 cn.lunadeer.miniplayertitle.utils.XLogger;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TextComponent;
@ -20,39 +20,30 @@ public class Shop {
Map<Integer, SaleTitle> titles = getSaleTitles(); Map<Integer, SaleTitle> titles = getSaleTitles();
if (!(sender instanceof Player)) { if (!(sender instanceof Player)) {
for (SaleTitle title : titles.values()) { for (SaleTitle title : titles.values()) {
Notification.info(sender, "[" + title.getSaleId() + "]"); Component idx = Component.text("[" + title.getSaleId() + "] ");
Notification.info(sender, title.getTitle()); Notification.info(sender, idx.append(title.getTitle()));
} }
return; return;
} }
Player player = (Player) sender; Player player = (Player) sender;
int offset = (page - 1) * 4; ListView view = ListView.create(5, "/mplt shop");
if (offset >= titles.size() || offset < 0) {
Notification.error(player, "页数超出范围");
return;
}
View view = View.create();
view.title("称号商店"); view.title("称号商店");
view.subtitle("当前余额: " + XPlayer.getCoin(player) + "金币"); view.subtitle("当前余额: " + XPlayer.getCoin(player) + "金币");
for (int i = offset; i < offset + 4; i++) { for (Map.Entry<Integer, SaleTitle> entry : titles.entrySet()) {
if (i >= titles.size()) { Integer title_sale_id = entry.getKey();
break;
}
Integer title_sale_id = (Integer) titles.keySet().toArray()[i];
TextComponent idx = Component.text("[" + title_sale_id + "] "); TextComponent idx = Component.text("[" + title_sale_id + "] ");
SaleTitle title = titles.get(title_sale_id); SaleTitle title = entry.getValue();
Line line = Line.create(); Line line = Line.create();
Component button = Button.create("购买", "/mplt buy " + title_sale_id); Component button = Button.create("购买", "/mplt buy " + title_sale_id);
line.append(idx) line.append(idx)
.append(title.getTitle()) .append(title.getTitle())
.append("价格:" + title.getPrice() + " 有效期:" + (title.getDays()==-1?"永久":title.getDays() + "")) .append("价格:" + title.getPrice() + " 有效期:" + (title.getDays() == -1 ? "永久" : title.getDays() + ""))
.append("售卖截止:" + title.getSaleEndAt()) .append("售卖截止:" + title.getSaleEndAt())
.append("剩余:" + ((title.getAmount() == -1) ? "无限" : title.getAmount())) .append("剩余:" + ((title.getAmount() == -1) ? "无限" : title.getAmount()))
.append(button); .append(button);
view.set(i, line); view.add(line);
} }
view.set(View.Slot.ACTIONBAR, View.pagination(page, titles.size(), "/mplt shop")); view.showOn(player, page);
view.showOn(player);
} }
public static void deleteTitle(Integer id) { public static void deleteTitle(Integer id) {

View File

@ -3,10 +3,9 @@ package cn.lunadeer.miniplayertitle;
import cn.lunadeer.miniplayertitle.utils.Database; import cn.lunadeer.miniplayertitle.utils.Database;
import cn.lunadeer.miniplayertitle.utils.Notification; import cn.lunadeer.miniplayertitle.utils.Notification;
import cn.lunadeer.miniplayertitle.utils.STUI.Line; 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 cn.lunadeer.miniplayertitle.utils.XLogger;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.event.HoverEvent;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -21,7 +20,6 @@ public class Title {
protected String _title; protected String _title;
protected String _description; protected String _description;
protected Boolean _enabled; protected Boolean _enabled;
JoinConfiguration join = JoinConfiguration.separator(Component.text(" "));
public static Title create(String title, String description) { public static Title create(String title, String description) {
String sql = ""; String sql = "";
@ -62,30 +60,21 @@ public class Title {
List<Title> titles = all(); List<Title> titles = all();
if (!(sender instanceof Player)) { if (!(sender instanceof Player)) {
for (Title title : titles) { for (Title title : titles) {
Notification.info(sender, "[" + title.getId() + "]"); Component idx = Component.text("[" + title.getId() + "]");
Notification.info(sender, title.getTitle()); Notification.info(sender, idx.append(title.getTitle()));
} }
return; return;
} }
Player player = (Player) sender; Player player = (Player) sender;
int offset = (page - 1) * 4; ListView view = ListView.create(5, "/mplt listall");
if (offset >= titles.size() || offset < 0) {
Notification.error(player, "页数超出范围");
return;
}
View view = View.create();
view.title("所有称号"); view.title("所有称号");
for (int i = offset; i < offset + 4; i++) { for (Title title : titles) {
if (i >= titles.size()) { TextComponent idx = Component.text("[" + title.getId() + "] ");
break;
}
TextComponent idx = Component.text("[" + titles.get(i).getId() + "] ");
Line line = Line.create(); Line line = Line.create();
line.append(idx).append(titles.get(i).getTitle()); line.append(idx).append(title.getTitle());
view.set(i, line); view.add(line);
} }
view.set(View.Slot.ACTIONBAR, View.pagination(page, titles.size(), "/mplt listall")); view.showOn(player, page);
view.showOn(player);
} }
public Title(Integer id) { public Title(Integer id) {

View File

@ -4,7 +4,7 @@ import cn.lunadeer.miniplayertitle.utils.Database;
import cn.lunadeer.miniplayertitle.utils.Notification; import cn.lunadeer.miniplayertitle.utils.Notification;
import cn.lunadeer.miniplayertitle.utils.STUI.Button; import cn.lunadeer.miniplayertitle.utils.STUI.Button;
import cn.lunadeer.miniplayertitle.utils.STUI.Line; 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.Time;
import cn.lunadeer.miniplayertitle.utils.XLogger; import cn.lunadeer.miniplayertitle.utils.XLogger;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -12,10 +12,7 @@ import net.kyori.adventure.text.TextComponent;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.util.HashMap; import java.util.*;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
public class XPlayer { public class XPlayer {
private final Player _player; private final Player _player;
@ -39,21 +36,12 @@ public class XPlayer {
} }
public void openBackpack(Integer page) { public void openBackpack(Integer page) {
Map<Integer, PlayerTitle> titles = getTitles(_player.getUniqueId()); Collection<PlayerTitle> titles = getTitles(_player.getUniqueId()).values();
int offset = (page - 1) * 4; ListView view = ListView.create(4, "/mplt list");
if (offset >= titles.size() || offset < 0) {
Notification.error(_player, "页数超出范围");
return;
}
View view = View.create();
view.title("我的称号"); view.title("我的称号");
for (int i = offset; i < offset + 4; i++) { for (PlayerTitle title : titles) {
if (i >= titles.size()) { int title_id = title.getId();
break;
}
int title_id = (int) titles.keySet().toArray()[i];
TextComponent idx = Component.text("[" + title_id + "] "); TextComponent idx = Component.text("[" + title_id + "] ");
PlayerTitle title = titles.get(title_id);
Line line = Line.create(); Line line = Line.create();
boolean is_using = Objects.equals(title.getId(), _current_title_id); boolean is_using = Objects.equals(title.getId(), _current_title_id);
Component button = Button.create(is_using ? "卸下" : "使用", "/mplt use " + (is_using ? -1 : title.getId())); Component button = Button.create(is_using ? "卸下" : "使用", "/mplt use " + (is_using ? -1 : title.getId()));
@ -61,10 +49,9 @@ public class XPlayer {
.append(title.getTitle()) .append(title.getTitle())
.append(Component.text("有效期至:" + title.getExpireAtStr())) .append(Component.text("有效期至:" + title.getExpireAtStr()))
.append(button); .append(button);
view.set(i, line); view.add(line);
} }
view.set(View.Slot.ACTIONBAR, View.pagination(page, titles.size(), "/mplt list")); view.showOn(_player, page);
view.showOn(_player);
} }
public void updateUsingTitle(Integer title_id) { public void updateUsingTitle(Integer title_id) {
@ -149,12 +136,11 @@ public class XPlayer {
sql += "SELECT title_id "; sql += "SELECT title_id ";
sql += "FROM mplt_player_using_title "; sql += "FROM mplt_player_using_title ";
sql += "WHERE uuid = '" + uuid.toString() + "';"; sql += "WHERE uuid = '" + uuid.toString() + "';";
Integer current_title_id = null; int current_title_id = -1;
try (ResultSet rs = Database.query(sql)) { try (ResultSet rs = Database.query(sql)) {
if (rs != null && rs.next()) { if (rs != null && rs.next()) {
current_title_id = rs.getInt("title_id"); current_title_id = rs.getInt("title_id");
} else { } else {
current_title_id = -1;
sql = ""; sql = "";
sql += "INSERT INTO mplt_player_using_title (uuid, title_id) VALUES ("; sql += "INSERT INTO mplt_player_using_title (uuid, title_id) VALUES (";
sql += "'" + uuid + "', "; sql += "'" + uuid + "', ";

View File

@ -1,11 +1,12 @@
package cn.lunadeer.miniplayertitle.utils.STUI; package cn.lunadeer.miniplayertitle.utils.STUI;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
public class Button { public class Button {
public static Component create(String text, String command) { public static TextComponent create(String text, String command) {
return Component.text("[" + text + "]", View.action_color) 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)); .clickEvent(net.kyori.adventure.text.event.ClickEvent.clickEvent(net.kyori.adventure.text.event.ClickEvent.Action.RUN_COMMAND, command));
} }
} }

View File

@ -8,9 +8,9 @@ import java.util.List;
public class Line { 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() { public Line() {
} }

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -2,72 +2,30 @@ package cn.lunadeer.miniplayertitle.utils.STUI;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static cn.lunadeer.miniplayertitle.utils.STUI.ViewStyles.main_color;
public class View { 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 title_decorate = Component.text("===========", main_color);
protected TextComponent sub_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 line_decorate = Component.text("- ", main_color);
protected TextComponent action_decorate = Component.text("> ", main_color); protected TextComponent action_decorate = Component.text("> ", main_color);
protected TextComponent title = Component.text(" "); protected TextComponent title = Component.text(" ");
protected TextComponent subtitle = Component.text(" "); protected TextComponent subtitle = Component.text(" ");
protected TextComponent content_line1 = Component.text(" "); protected List<TextComponent> content_lines = new ArrayList<>();
protected TextComponent content_line2 = Component.text(" ");
protected TextComponent content_line3 = Component.text(" ");
protected TextComponent content_line4 = Component.text(" ");
protected TextComponent actionbar = Component.text(" "); protected TextComponent actionbar = Component.text(" ");
protected TextComponent bottom_decorate = Component.text("=================================", main_color); 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) { public void showOn(Player player) {
player.sendMessage(Component.text().append(title_decorate).append(title).append(title_decorate).build()); 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(sub_title_decorate).append(subtitle).build());
player.sendMessage(Component.text().append(line_decorate).append(content_line1).build()); for (TextComponent content_line : content_lines) {
player.sendMessage(Component.text().append(line_decorate).append(content_line2).build()); player.sendMessage(Component.text().append(line_decorate).append(content_line).build());
player.sendMessage(Component.text().append(line_decorate).append(content_line3).build()); }
player.sendMessage(Component.text().append(line_decorate).append(content_line4).build());
player.sendMessage(Component.text().append(action_decorate).append(actionbar).build()); player.sendMessage(Component.text().append(action_decorate).append(actionbar).build());
player.sendMessage(bottom_decorate); player.sendMessage(bottom_decorate);
player.sendMessage(Component.text(" ")); player.sendMessage(Component.text(" "));
@ -97,90 +55,33 @@ public class View {
return this; return this;
} }
public View set(Slot line, TextComponent component) { public View actionBar(TextComponent actionbar) {
switch (line) { this.actionbar = actionbar;
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;
}
return this; return this;
} }
public View set(Slot line, String component) { public View actionBar(String actionbar) {
switch (line) { this.actionbar = Component.text(actionbar);
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;
}
return this; return this;
} }
public View set(int index, Line component) { public View actionBar(Line actionbar) {
if (index % 4 == 0) { this.actionbar = actionbar.build();
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");
}
return this; return this;
} }
public View set(Slot line, Line component) { public View addLine(TextComponent component) {
switch (line) { this.content_lines.add(component);
case SUBTITLE: return this;
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(String component) {
this.content_lines.add(Component.text(component));
return this;
}
public View addLine(Line component) {
this.content_lines.add(component.build());
return this; return this;
} }
} }

View File

@ -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);
}