Compare commits
No commits in common. "master" and "v2.10.2" have entirely different histories.
@ -35,9 +35,8 @@ jobs:
|
||||
ln -s /usr/local/go/bin/go /usr/bin/go
|
||||
go version
|
||||
- name: "Release"
|
||||
uses: https://ssl.lunadeer.cn:14446/zhangyuheng/release-action@main
|
||||
uses: https://ssl.lunadeer.cn:14446/actions/release-action@main
|
||||
with:
|
||||
note: "带 `original-` 前缀的文件无法用于运行,请下载不带此前缀的版本。"
|
||||
files: |-
|
||||
staging/*.jar
|
||||
api_key: '${{secrets.RELEASE_TOKEN}}'
|
183
README.md
183
README.md
@ -1,13 +1,10 @@
|
||||
<div align="center">
|
||||
|
||||
<div style="text-align: center;">
|
||||
|
||||
<img src="https://ssl.lunadeer.cn:14437/i/2024/03/28/6604de7db2e6f.png" alt="" width="70%">
|
||||
|
||||
### [开源地址](https://ssl.lunadeer.cn:14446/zhangyuheng/MiniPlayerTitle) | [文档地址](https://ssl.lunadeer.cn:14448/doc/2/)
|
||||
|
||||
### [下载页面](https://ssl.lunadeer.cn:14446/zhangyuheng/MiniPlayerTitle/releases)
|
||||
|
||||
### [统计页面](https://bstats.org/plugin/bukkit/MiniPlayerTitle/21444) | [Hangar](https://hangar.papermc.io/zhangyuheng/MiniPlayerTitle)
|
||||
|
||||
</div>
|
||||
|
||||
## 简易玩家称号插件
|
||||
@ -28,12 +25,176 @@
|
||||
- 本插件使用 **TUI** 作为简易的交互方式,非 PlayerTitle 的箱子UI
|
||||
- 支持设置称号**限量销售、限时销售**
|
||||
- 支持玩家使用称号币自定义称号
|
||||
- 支持外部经济系统(需要 Vault 前置支持)(3.0.6+)
|
||||
- 支持 PlaceholderAPI (4.0.0+)
|
||||
|
||||
## 支持版本
|
||||
|
||||
- 1.20.1+ (Bukkit、Spigot、Paper、Folia)
|
||||
- 1.20.1+ (Paper、Folia)
|
||||
|
||||
## 安装方法
|
||||
|
||||
1. 将插件放入服务器的 `plugins` 目录下
|
||||
2. 重启服务器
|
||||
3. 在 `plugins/MiniPlayerTitle/config.yml` 中配置
|
||||
4. 重启服务器
|
||||
|
||||
## 使用方法
|
||||
|
||||
使用 `/mplt` 指令打开 TUI 界面
|
||||
|
||||
### 主页
|
||||
|
||||
- 点击【称号背包】可查看自己拥有的所有称号;
|
||||
- 点击【称号商店】可查看商店中出售的称号;
|
||||
- 点击【自定义称号】可查看自定义称号的相关帮助信息;
|
||||
|
||||
![image-20240110164757834](https://ssl.lunadeer.cn:14437/i/2024/01/10/659e59bfc8f7b.png)
|
||||
|
||||
### 称号背包
|
||||
|
||||
- 称号背包中会显示当前拥有的所有称号以及相应到到期时间。
|
||||
- 点击【卸下】可以取消当前正在使用的称号,点击【使用】会装备对应称号或替换当前称号。
|
||||
- 每页最多显示5条信息,可以点击【下一页】向后翻页。
|
||||
- 鼠标移动到称号上可以显示相应称号的描述信息。
|
||||
- 称号有效期只能精确到天,例如:20240115表示此称号将在2024年1月16日开始无法使用。
|
||||
|
||||
![image-20240110170535331](https://ssl.lunadeer.cn:14437/i/2024/01/10/659e5ddfc16dd.png)
|
||||
|
||||
### 称号商店
|
||||
|
||||
- 称号商店会显示称号销售列表、拥有的称号币余额、出售称号的价格、可购买天数、销售截止日期、可购买余量信息。
|
||||
|
||||
- 每页最多显示5条信息,可以点击【下一页】向后翻页。
|
||||
|
||||
- 点击【购买】即可购买相应的称号,称号购买后不会自动装备,需要前往背包手动使用。
|
||||
|
||||
- 在一个称号没有过期时无法再次购买此称号。
|
||||
- 售卖截止日期只能精确到天。
|
||||
|
||||
![image-20240110170621073](https://ssl.lunadeer.cn:14437/i/2024/01/10/659e5e0d772ce.png)
|
||||
|
||||
### 自定义称号
|
||||
|
||||
- 在自定义称号帮助页面会显示当前管理员配置的自定义称号规则。
|
||||
- 如果自定义称号处于【开启】状态,那么玩家可以消耗称号币自定义喜欢的称号。
|
||||
- 自定义称号支持RGB彩色字,需要使用 `&#`为开头的十六进制RGB颜色代码表示颜色,例如`�`表示黑色。可以使用 [Minecraft 渐变颜色生成器](https://ssl.lunadeer.cn:14440/) 来生成具有渐变效果的称号。
|
||||
- 创建成功后会自动扣除所需的花费。
|
||||
|
||||
![image-20240111093331929](https://ssl.lunadeer.cn:14437/i/2024/01/11/659f456c4a88b.png)
|
||||
|
||||
## 管理员指南
|
||||
|
||||
### 0. 数据库
|
||||
|
||||
如果使用 Postgresql 数据库,需要手动创建数据库。
|
||||
|
||||
如果使用 sqlite 数据库,插件会自动在插件目录下创建数据库文件。配置文件内的 Host、Port、User、Pass 字段不会被使用。
|
||||
|
||||
### 1. 创建称号
|
||||
|
||||
使用 `/mplt create [称号名称] [称号描述]` 创建称号,称号支持RGB彩色字,需要使用 `&#`为开头的十六进制RGB颜色代码表示颜色,例如`�`表示黑色。可以使用 [Minecraft 渐变颜色生成器](https://ssl.lunadeer.cn:14440/) 来生成具有渐变效果的称号。
|
||||
|
||||
![image-20240110172102245](https://ssl.lunadeer.cn:14437/i/2024/01/10/659e617ea7351.png)
|
||||
|
||||
创建成功后会显示创建称号的 ID 以及称号名。
|
||||
|
||||
### 2. 添加到商店
|
||||
|
||||
使用`/mplt addshop [称号ID]` 添加称号到商店,添加成功后会显示添加的商品ID。
|
||||
|
||||
添加到商店的默认销售信息为:**0元购买0天、常驻销售、0库存**,需要使用相应指令设置称号的销售信息,此默认配置是无法购买的(因为库存和销售天数都是0)。
|
||||
|
||||
### 3. 设置销售信息
|
||||
|
||||
首先使用`/mplt setprice [商品ID] [价格] [天数]` 设置称号的价格信息,例如可以设置成 1 个称号币购买 7 天`/mplt setprice [商品ID] 1 7` 。如果你希望这是一个永久称号,那么你可以将天数设置为-1。
|
||||
|
||||
接着需要使用`/mplt setamount [商品ID] [数量]` 设置称号出售数量。同理,如果你希望这是一个无限供应的称号,那么可以将数量设置为-1则表示无限量销售。
|
||||
|
||||
假如你希望这个称号是一个限时销售的称号可以使用`/mplt setendat [商品ID] [结束时间]` 设置称号结束销售时间,时间格式为 `YYYYMMDD` ,在超过这个日期后玩家将无法购买此称号(但是仍然会显示在商店列表里)。
|
||||
|
||||
将销售结束时间设置为-1可以将其重新修改为常驻销售。
|
||||
|
||||
### 4. 其他操作
|
||||
|
||||
其他操作详见下方的管理员指令说明。
|
||||
|
||||
提示:
|
||||
|
||||
- 你可以安全的删除一个称号,此操作会自动删除玩家拥有的此称号记录以及商店的相关数据。
|
||||
- 修改称号的名称、描述也会同步变动玩家已经购买的称号信息。
|
||||
- 修改商店的购买有效期不会改变玩家已购买的时间。
|
||||
- 一个称号可以多次被添加到商店,因此你可以将一个称号以多种不同的销售方式添加到商店,例如:1币购买1天,5币购买7天,100币购买永久。
|
||||
|
||||
## 指令
|
||||
|
||||
### 玩家指令
|
||||
|
||||
`/mplt` 打开 TUI 界面
|
||||
|
||||
`/mplt use [称号ID]` 使用称号
|
||||
|
||||
`/mplt list [页数(可选)]` 查看已拥有的称号
|
||||
|
||||
`/mplt shop [页数(可选)]` 查看商店
|
||||
|
||||
`/mplt buy [商品ID]` 购买称号
|
||||
|
||||
`/mplt custom [称号名称]` 自定义称号
|
||||
|
||||
`/mplt custominfo` 查看自定义称号帮助
|
||||
|
||||
### 管理员指令
|
||||
|
||||
`/mplt create [称号名称] [称号描述]` 创建称号
|
||||
|
||||
`/mplt delete [称号ID]` 删除称号
|
||||
|
||||
`/mplt setdesc [称号ID] [描述]` 设置称号描述
|
||||
|
||||
`/mplt setname [称号ID] [名称]` 设置称号名称
|
||||
|
||||
`/mplt addshop [称号ID]` 添加称号到商店
|
||||
|
||||
`/mplt removeshop [商品ID]` 从商店移除称号
|
||||
|
||||
`/mplt setprice [商品ID] [价格] [天数(-1为永久)]` 设置称号价格
|
||||
|
||||
`/mplt setamount [商品ID] [数量(-1为无限)]` 设置称号出售数量
|
||||
|
||||
`/mplt setendat [商品ID] [结束时间(格式:YYYYMMDD -1为永久)]` 设置称号销售结束时间
|
||||
|
||||
`/mplt listall [页数(可选)]` 查看所有称号
|
||||
|
||||
`/mplt addcoin [玩家名称] [数量]` 给玩家添加称号币
|
||||
|
||||
`/mplt setcoin [玩家名称] [数量]` 设置玩家称号币数量
|
||||
|
||||
## 配置文件参考
|
||||
|
||||
```yaml
|
||||
# 数据库配置
|
||||
Database:
|
||||
Type: sqlite # pgsql, sqlite
|
||||
Host: localhost
|
||||
Port: 5432
|
||||
Name: miniplayertitle
|
||||
User: miniplayertitle
|
||||
Pass: miniplayertitle
|
||||
|
||||
# 称号前缀后缀
|
||||
Prefix: "["
|
||||
Suffix: "]"
|
||||
|
||||
# 自定义称号配置
|
||||
CustomCost:
|
||||
Enabled: true
|
||||
Cost: 1000
|
||||
MaxLength: 8
|
||||
|
||||
# 玩家称号币初始值
|
||||
DefaultCoin: 0
|
||||
|
||||
Debug: false
|
||||
```
|
||||
|
||||
## TODO
|
||||
|
||||
@ -41,8 +202,4 @@
|
||||
|
||||
Mail: [zhangyuheng@lunadeer.cn](mailto:zhangyuheng@lunadeer.cn)
|
||||
|
||||
QQ群:309428300
|
||||
|
||||
## 统计
|
||||
|
||||
![bstats](https://bstats.org/signatures/bukkit/MiniPlayerTitle.svg)
|
||||
QQ: 2751268851
|
||||
|
23
pom.xml
23
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>cn.lunadeer</groupId>
|
||||
<artifactId>MiniPlayerTitle</artifactId>
|
||||
<version>4.7.1</version>
|
||||
<version>2.10.2</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>MiniPlayerTitle</name>
|
||||
@ -62,14 +62,6 @@
|
||||
<id>papermc</id>
|
||||
<url>https://repo.papermc.io/repository/maven-public/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>lunadeer-repo</id>
|
||||
<url>https://ssl.lunadeer.cn:14454/repository/maven-snapshots/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>placeholderapi</id>
|
||||
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
@ -80,15 +72,14 @@
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.lunadeer</groupId>
|
||||
<artifactId>MinecraftPluginUtils</artifactId>
|
||||
<version>1.3.7-SNAPSHOT</version>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>42.6.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>me.clip</groupId>
|
||||
<artifactId>placeholderapi</artifactId>
|
||||
<version>2.11.6</version>
|
||||
<scope>provided</scope>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>42.6.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -1,6 +1,6 @@
|
||||
package cn.lunadeer.miniplayertitle;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.XLogger;
|
||||
import cn.lunadeer.miniplayertitle.utils.XLogger;
|
||||
import net.kyori.adventure.text.format.Style;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
|
||||
|
@ -1,169 +1,282 @@
|
||||
package cn.lunadeer.miniplayertitle;
|
||||
|
||||
import cn.lunadeer.miniplayertitle.commands.PlayerManage;
|
||||
import cn.lunadeer.miniplayertitle.commands.TitleCard;
|
||||
import cn.lunadeer.miniplayertitle.commands.TitleManage;
|
||||
import cn.lunadeer.miniplayertitle.commands.TitleShopSale;
|
||||
import cn.lunadeer.miniplayertitle.dtos.PlayerInfoDTO;
|
||||
import cn.lunadeer.miniplayertitle.tuis.*;
|
||||
import cn.lunadeer.miniplayertitle.commands.AdminCommands;
|
||||
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.ViewStyles;
|
||||
import cn.lunadeer.miniplayertitle.utils.XLogger;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.event.ClickEvent;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class Commands implements TabExecutor {
|
||||
import static cn.lunadeer.miniplayertitle.commands.PlayerCommands.*;
|
||||
|
||||
public class Commands implements TabExecutor {
|
||||
/**
|
||||
* Executes the given command, returning its success.
|
||||
* <br>
|
||||
* If false is returned, then the "usage" plugin.yml entry for this command
|
||||
* (if defined) will be sent to the player.
|
||||
*
|
||||
* @param sender Source of the command
|
||||
* @param command Command which was executed
|
||||
* @param label Alias of the command which was used
|
||||
* @param args Passed command arguments
|
||||
* @return true if a valid command, otherwise false
|
||||
*/
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if (args.length == 0) {
|
||||
Menu.show(sender, args);
|
||||
return true;
|
||||
}
|
||||
switch (args[0]) {
|
||||
case "menu": // mplt menu [页数]
|
||||
Menu.show(sender, args);
|
||||
break;
|
||||
case "all_titles": // mplt all_titles [页数]
|
||||
AllTitles.show(sender, args);
|
||||
break;
|
||||
case "my_titles": // mplt my_titles [页数]
|
||||
MyTitles.show(sender, args);
|
||||
break;
|
||||
case "shop": // mplt shop [页数]
|
||||
Shop.show(sender, args);
|
||||
break;
|
||||
case "custom_info": // mplt custom_info
|
||||
CustomInfo.show(sender, args);
|
||||
break;
|
||||
case "sale_info": // mplt sale_info <商品ID>
|
||||
SaleInfo.show(sender, args);
|
||||
break;
|
||||
case "create_sale": // mplt create_sale <称号ID>
|
||||
TitleShopSale.createSale(sender, args);
|
||||
break;
|
||||
case "set_sale": // mplt set_sale <price|days|amount|end_at|more_end_at|less_end_at> <商品ID> <值> [页数]
|
||||
TitleShopSale.setSale(sender, args);
|
||||
break;
|
||||
case "delete_sale": // mplt delete_sale <商品ID> [页数]
|
||||
TitleShopSale.deleteSale(sender, args);
|
||||
break;
|
||||
case "buy_sale": // mplt buy_sale <商品ID>
|
||||
TitleShopSale.buySale(sender, args);
|
||||
break;
|
||||
case "use_title": // mplt use_title <背包ID> [页码]
|
||||
TitleManage.useTitle(sender, args);
|
||||
break;
|
||||
case "create_title": // mplt create_title <称号名称> [称号描述]
|
||||
TitleManage.createTitle(sender, args);
|
||||
break;
|
||||
case "delete_title": // mplt delete_title <称号ID> [页码]
|
||||
TitleManage.deleteTitle(sender, args);
|
||||
break;
|
||||
case "edit_title_name": // mplt edit_title_name <称号ID> <称号名称>
|
||||
TitleManage.editTitleName(sender, args);
|
||||
break;
|
||||
case "edit_title_desc": // mplt edit_title_desc <称号ID> <称号描述>
|
||||
TitleManage.editTitleDescription(sender, args);
|
||||
break;
|
||||
case "custom_title": // mplt custom_title <称号>
|
||||
TitleManage.customTitle(sender, args);
|
||||
break;
|
||||
case "add_coin": // mplt add_coin <玩家名称> <称号币数量>
|
||||
PlayerManage.addCoin(sender, args);
|
||||
break;
|
||||
case "set_coin": // mplt set_coin <玩家名称> <称号币数量>
|
||||
PlayerManage.setCoin(sender, args);
|
||||
break;
|
||||
case "grant_title": // mplt grant_title <玩家名称> <称号> <描述> [天数]
|
||||
PlayerManage.grantTitle(sender, args);
|
||||
break;
|
||||
case "get_card": // mplt get_card <商品ID>
|
||||
TitleCard.getTitleCard(sender, args);
|
||||
break;
|
||||
switch (label) {
|
||||
case "mplt":
|
||||
if (args.length == 0) {
|
||||
home_view(sender);
|
||||
return true;
|
||||
}
|
||||
switch (args[0]) {
|
||||
case "use":
|
||||
use(sender, args);
|
||||
return true;
|
||||
case "list":
|
||||
list(sender, args);
|
||||
return true;
|
||||
case "shop":
|
||||
shop(sender, args);
|
||||
return true;
|
||||
case "buy":
|
||||
buy(sender, args);
|
||||
return true;
|
||||
case "custom":
|
||||
custom(sender, args);
|
||||
return true;
|
||||
case "custominfo":
|
||||
custom_info(sender);
|
||||
return true;
|
||||
case "create":
|
||||
AdminCommands.createTitle(sender, args);
|
||||
return true;
|
||||
case "delete":
|
||||
AdminCommands.deleteTitle(sender, args);
|
||||
return true;
|
||||
case "setdesc":
|
||||
AdminCommands.setTitleDescription(sender, args);
|
||||
return true;
|
||||
case "setname":
|
||||
AdminCommands.setTitleName(sender, args);
|
||||
return true;
|
||||
case "addshop":
|
||||
AdminCommands.addShop(sender, args);
|
||||
return true;
|
||||
case "removeshop":
|
||||
AdminCommands.removeShop(sender, args);
|
||||
return true;
|
||||
case "setprice":
|
||||
AdminCommands.setPrice(sender, args);
|
||||
return true;
|
||||
case "setamount":
|
||||
AdminCommands.setAmount(sender, args);
|
||||
return true;
|
||||
case "setendat":
|
||||
AdminCommands.setSaleEndAt(sender, args);
|
||||
return true;
|
||||
case "listall":
|
||||
AdminCommands.listAllTitle(sender, args);
|
||||
return true;
|
||||
case "addcoin":
|
||||
AdminCommands.addCoin(sender, args);
|
||||
return true;
|
||||
case "setcoin":
|
||||
AdminCommands.setCoin(sender, args);
|
||||
return true;
|
||||
default:
|
||||
printHelp(sender);
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void printHelp(@NotNull CommandSender sender) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
Notification.warn(player, "用法: /mplt <use|list|shop|buy|custom|custominfo>");
|
||||
if (player.isOp()) {
|
||||
Notification.warn(player, "用法: /mplt <create|delete|setdesc|setname|addshop|removeshop|setprice|setamount|setendat|listall>");
|
||||
}
|
||||
} else {
|
||||
XLogger.info("用法: /mplt <use|list|shop|buy|custom>");
|
||||
XLogger.info("用法: /mplt <create|delete|setdesc|setname|addshop|removeshop|setprice|setamount|setendat|listall>");
|
||||
}
|
||||
}
|
||||
|
||||
public static void home_view(CommandSender sender) {
|
||||
if (!(sender instanceof Player)) {
|
||||
printHelp(sender);
|
||||
return;
|
||||
}
|
||||
View view = View.create();
|
||||
view.title("称号系统");
|
||||
Line line_1 = Line.create();
|
||||
line_1.append(Component.text("当前版本:"));
|
||||
line_1.append(Component.text(MiniPlayerTitle.instance.getPluginMeta().getVersion()));
|
||||
Line line_2 = Line.create();
|
||||
line_2.append(Component.text("帮助文档:"));
|
||||
line_2.append(Component.text("[点击在浏览器中打开]").clickEvent(ClickEvent.clickEvent(ClickEvent.Action.OPEN_URL, "https://ssl.lunadeer.cn:14446/zhangyuheng/MiniPlayerTitle")));
|
||||
Component backpack = Button.create("称号背包", "/mplt list");
|
||||
Component shop = Button.create("称号商店", "/mplt shop");
|
||||
Component custom = Button.create("自定义称号", "/mplt custominfo");
|
||||
Line line = Line.create();
|
||||
line.append(backpack).append(shop).append(custom);
|
||||
view.actionBar(line).addLine(line_1).addLine(line_2);
|
||||
view.showOn((Player) sender);
|
||||
}
|
||||
|
||||
private void custom_info(CommandSender sender) {
|
||||
if (!(sender instanceof Player)) {
|
||||
printHelp(sender);
|
||||
return;
|
||||
}
|
||||
Player player = (Player) sender;
|
||||
View view = View.create();
|
||||
view.title("自定义称号帮助");
|
||||
view.subtitle("当前余额: " + XPlayer.getCoin(player) + "称号币");
|
||||
Line line_1 = Line.create();
|
||||
line_1.append("当前自定义称号状态:")
|
||||
.append(MiniPlayerTitle.config.isEnableCustom() ?
|
||||
Component.text("开启", ViewStyles.success_color) :
|
||||
Component.text("关闭", ViewStyles.error_color));
|
||||
Line line_2 = Line.create();
|
||||
line_2.append("自定义称号花费:")
|
||||
.append(MiniPlayerTitle.config.getCustomCost().toString());
|
||||
Line line_3 = Line.create();
|
||||
line_3.append("自定义方法:")
|
||||
.append(Component.text("在聊天框输入 /mplt custom <称号>"));
|
||||
Line line_4 = Line.create();
|
||||
line_4.append("可以使用 Minecraft渐变颜色生成器 来生成具有渐变效果的称号")
|
||||
.append(Component.text("[点击在浏览器中打开生成器]", ViewStyles.action_color)
|
||||
.clickEvent(ClickEvent.clickEvent(ClickEvent.Action.OPEN_URL, "https://ssl.lunadeer.cn:14440/")));
|
||||
Line line_5 = Line.create();
|
||||
line_5.append("最大长度(不含颜色代码):")
|
||||
.append(MiniPlayerTitle.config.getMaxLength().toString());
|
||||
view.addLine(line_1)
|
||||
.addLine(line_2)
|
||||
.addLine(line_5)
|
||||
.addLine(line_3)
|
||||
.addLine(line_4);
|
||||
view.showOn(player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests a list of possible completions for a command argument.
|
||||
*
|
||||
* @param sender Source of the command. For players tab-completing a
|
||||
* command inside a command block, this will be the player, not
|
||||
* the command block.
|
||||
* @param command Command which was executed
|
||||
* @param label Alias of the command which was used
|
||||
* @param args The arguments passed to the command, including final
|
||||
* partial argument to be completed
|
||||
* @return A List of possible completions for the final argument, or null
|
||||
* to default to the command executor
|
||||
*/
|
||||
@Override
|
||||
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if (args.length == 1) {
|
||||
return Arrays.asList("menu", "all_titles", "my_titles", "shop", "custom_info", "sale_info",
|
||||
"create_sale", "set_sale", "delete_sale", "buy_sale", "use_title", "create_title",
|
||||
"delete_title", "edit_title_name", "edit_title_desc", "custom_title", "add_coin", "set_coin",
|
||||
"grant_title", "get_card"
|
||||
);
|
||||
String[] player_cmd = {"use", "list", "shop", "buy", "custom", "custominfo"};
|
||||
String[] admin_cmd = {"create", "delete", "setdesc", "setname", "addshop", "removeshop", "setprice", "setamount", "setendat", "listall", "addcoin", "setcoin"};
|
||||
List<String> res = new ArrayList<>();
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if (player.isOp()) {
|
||||
res.addAll(Arrays.asList(player_cmd));
|
||||
res.addAll(Arrays.asList(admin_cmd));
|
||||
} else {
|
||||
res.addAll(Arrays.asList(player_cmd));
|
||||
}
|
||||
} else {
|
||||
res.addAll(Arrays.asList(player_cmd));
|
||||
res.addAll(Arrays.asList(admin_cmd));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
if (args.length == 2) {
|
||||
switch (args[0]) {
|
||||
case "sale_info":
|
||||
case "delete_sale":
|
||||
case "buy_sale":
|
||||
case "get_card":
|
||||
return Collections.singletonList("<商品ID>");
|
||||
case "create_sale":
|
||||
case "delete_title":
|
||||
case "edit_title_name":
|
||||
case "edit_title_desc":
|
||||
case "use":
|
||||
return Collections.singletonList("要使用的称号ID");
|
||||
case "custom":
|
||||
return Collections.singletonList("自定义称号");
|
||||
case "list":
|
||||
case "shop":
|
||||
case "listall":
|
||||
return Collections.singletonList("页数(可选)");
|
||||
case "buy":
|
||||
return Collections.singletonList("要购买的条目ID");
|
||||
case "create":
|
||||
return Collections.singletonList("<称号名称> <称号描述>");
|
||||
case "delete":
|
||||
case "addshop":
|
||||
return Collections.singletonList("<称号ID>");
|
||||
case "use_title":
|
||||
return Collections.singletonList("<背包ID>");
|
||||
case "set_sale":
|
||||
return Arrays.asList("price", "days", "amount", "end_at", "more_end_at", "less_end_at");
|
||||
case "custom_title":
|
||||
case "create_title":
|
||||
return Collections.singletonList("<称号内容>");
|
||||
case "add_coin":
|
||||
case "set_coin":
|
||||
case "grant_title":
|
||||
return PlayerInfoDTO.playerNameList();
|
||||
case "setdesc":
|
||||
return Collections.singletonList("<称号ID> <称号描述>");
|
||||
case "setname":
|
||||
return Collections.singletonList("<称号ID> <称号名称>");
|
||||
case "removeshop":
|
||||
return Collections.singletonList("<商品ID>");
|
||||
case "setprice":
|
||||
return Collections.singletonList("<商品ID> <价格> <天数>(-1为永久)");
|
||||
case "setamount":
|
||||
return Collections.singletonList("<商品ID> <数量>(-1为无限)");
|
||||
case "setendat":
|
||||
return Collections.singletonList("<商品ID> <结束时间YYYYMMDD>(-1为永久)");
|
||||
case "addcoin":
|
||||
case "setcoin":
|
||||
// return player list
|
||||
List<String> res = new ArrayList<>();
|
||||
for (Player player : MiniPlayerTitle.instance.getServer().getOnlinePlayers()) {
|
||||
res.add(player.getName());
|
||||
}
|
||||
return res;
|
||||
default:
|
||||
return null;
|
||||
return Arrays.asList("use", "list", "shop", "buy");
|
||||
}
|
||||
}
|
||||
if (args.length == 3) {
|
||||
switch (args[0]) {
|
||||
case "set_sale":
|
||||
return Collections.singletonList("<商品ID>");
|
||||
case "edit_title_desc":
|
||||
return Collections.singletonList("<新的称号描述>");
|
||||
case "edit_title_name":
|
||||
return Collections.singletonList("<新的称号名称>");
|
||||
case "create_title":
|
||||
case "addcoin":
|
||||
case "setcoin":
|
||||
return Collections.singletonList("<金币数量>");
|
||||
case "create":
|
||||
case "setdesc":
|
||||
return Collections.singletonList("<称号描述>");
|
||||
case "add_coin":
|
||||
case "set_coin":
|
||||
return Collections.singletonList("<数量>");
|
||||
case "grant_title":
|
||||
return Collections.singletonList("<称号>");
|
||||
default:
|
||||
return null;
|
||||
case "setname":
|
||||
return Collections.singletonList("<称号名称>");
|
||||
case "setprice":
|
||||
return Collections.singletonList("<价格> <天数>(-1为永久)");
|
||||
case "setamount":
|
||||
return Collections.singletonList("<数量>(-1为无限)");
|
||||
case "setendat":
|
||||
return Collections.singletonList("<结束时间YYYYMMDD>(-1为永久)");
|
||||
}
|
||||
}
|
||||
if (args.length == 4) {
|
||||
switch (args[0]) {
|
||||
case "set_sale":
|
||||
return Collections.singletonList("<值>");
|
||||
case "grant_title":
|
||||
return Collections.singletonList("<描述>");
|
||||
default:
|
||||
return null;
|
||||
if (args[0].equals("setprice")) {
|
||||
return Collections.singletonList("<天数>(-1为永久)");
|
||||
}
|
||||
}
|
||||
if (args.length == 5) {
|
||||
switch (args[0]) {
|
||||
case "grant_title":
|
||||
return Collections.singletonList("<天数(默认永久)>");
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return Arrays.asList("use", "list", "shop", "buy", "custom", "custominfo");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,18 +1,25 @@
|
||||
package cn.lunadeer.miniplayertitle.events;
|
||||
package cn.lunadeer.miniplayertitle;
|
||||
|
||||
import cn.lunadeer.miniplayertitle.utils.Notification;
|
||||
import io.papermc.paper.event.player.AsyncChatEvent;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
import static cn.lunadeer.miniplayertitle.MiniPlayerTitle.usingPapi;
|
||||
public class Events implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
Player bukkitPlayer = event.getPlayer();
|
||||
XPlayer player = new XPlayer(bukkitPlayer);
|
||||
Commands.home_view(bukkitPlayer);
|
||||
player.updateName();
|
||||
}
|
||||
|
||||
public class PaperChat implements Listener {
|
||||
@EventHandler
|
||||
public void onPlayerSendChat(AsyncChatEvent event) {
|
||||
if (usingPapi()) {
|
||||
return;
|
||||
}
|
||||
Component nameComponent = event.getPlayer().displayName();
|
||||
Component chatComponent = event.message();
|
||||
Component newChatComponent = Component.text()
|
@ -1,57 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.XLogger;
|
||||
import cn.lunadeer.miniplayertitle.dtos.TitleDTO;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import static cn.lunadeer.miniplayertitle.MiniPlayerTitle.usingPapi;
|
||||
|
||||
public class Expansion extends PlaceholderExpansion {
|
||||
|
||||
private final JavaPlugin plugin;
|
||||
|
||||
public static Expansion instance;
|
||||
|
||||
public Expansion(JavaPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
if (usingPapi()) {
|
||||
XLogger.info("PlaceholderAPI is enabled, registering expansion...");
|
||||
this.register();
|
||||
} else {
|
||||
XLogger.warn("PlaceholderAPI is not enabled, using self impl of placeholders...");
|
||||
}
|
||||
instance = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String onPlaceholderRequest(Player bukkitPlayer, @NotNull String params) {
|
||||
if (params.equalsIgnoreCase("player_title")) {
|
||||
TitleDTO t = MiniPlayerTitle.instance.getPlayerUsingTitle(bukkitPlayer.getUniqueId());
|
||||
if (t == null) {
|
||||
return "";
|
||||
}
|
||||
return t.getTitleColoredBukkit();
|
||||
}
|
||||
|
||||
return null; //
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getIdentifier() {
|
||||
return "mplt";
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getAuthor() {
|
||||
return "zhangyuheng";
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getVersion() {
|
||||
return MiniPlayerTitle.instance.getDescription().getVersion();
|
||||
}
|
||||
}
|
863
src/main/java/cn/lunadeer/miniplayertitle/Metrics.java
Normal file
863
src/main/java/cn/lunadeer/miniplayertitle/Metrics.java
Normal file
@ -0,0 +1,863 @@
|
||||
package cn.lunadeer.miniplayertitle;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
public class Metrics {
|
||||
private final Plugin plugin;
|
||||
|
||||
private final MetricsBase metricsBase;
|
||||
|
||||
/**
|
||||
* Creates a new Metrics instance.
|
||||
*
|
||||
* @param plugin Your plugin instance.
|
||||
* @param serviceId The id of the service. It can be found at <a
|
||||
* href="https://bstats.org/what-is-my-plugin-id">What is my plugin id?</a>
|
||||
*/
|
||||
public Metrics(JavaPlugin plugin, int serviceId) {
|
||||
this.plugin = plugin;
|
||||
// Get the config file
|
||||
File bStatsFolder = new File(plugin.getDataFolder().getParentFile(), "bStats");
|
||||
File configFile = new File(bStatsFolder, "config.yml");
|
||||
YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);
|
||||
if (!config.isSet("serverUuid")) {
|
||||
config.addDefault("enabled", true);
|
||||
config.addDefault("serverUuid", UUID.randomUUID().toString());
|
||||
config.addDefault("logFailedRequests", false);
|
||||
config.addDefault("logSentData", false);
|
||||
config.addDefault("logResponseStatusText", false);
|
||||
// Inform the server owners about bStats
|
||||
config
|
||||
.options()
|
||||
.header(
|
||||
"bStats (https://bStats.org) collects some basic information for plugin authors, like how\n"
|
||||
+ "many people use their plugin and their total player count. It's recommended to keep bStats\n"
|
||||
+ "enabled, but if you're not comfortable with this, you can turn this setting off. There is no\n"
|
||||
+ "performance penalty associated with having metrics enabled, and data sent to bStats is fully\n"
|
||||
+ "anonymous.")
|
||||
.copyDefaults(true);
|
||||
try {
|
||||
config.save(configFile);
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
// Load the data
|
||||
boolean enabled = config.getBoolean("enabled", true);
|
||||
String serverUUID = config.getString("serverUuid");
|
||||
boolean logErrors = config.getBoolean("logFailedRequests", false);
|
||||
boolean logSentData = config.getBoolean("logSentData", false);
|
||||
boolean logResponseStatusText = config.getBoolean("logResponseStatusText", false);
|
||||
metricsBase =
|
||||
new MetricsBase(
|
||||
"bukkit",
|
||||
serverUUID,
|
||||
serviceId,
|
||||
enabled,
|
||||
this::appendPlatformData,
|
||||
this::appendServiceData,
|
||||
null,
|
||||
//submitDataTask -> Bukkit.getScheduler().runTask(plugin, submitDataTask),
|
||||
plugin::isEnabled,
|
||||
(message, error) -> this.plugin.getLogger().log(Level.WARNING, message, error),
|
||||
(message) -> this.plugin.getLogger().log(Level.INFO, message),
|
||||
logErrors,
|
||||
logSentData,
|
||||
logResponseStatusText);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shuts down the underlying scheduler service.
|
||||
*/
|
||||
public void shutdown() {
|
||||
metricsBase.shutdown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a custom chart.
|
||||
*
|
||||
* @param chart The chart to add.
|
||||
*/
|
||||
public void addCustomChart(CustomChart chart) {
|
||||
metricsBase.addCustomChart(chart);
|
||||
}
|
||||
|
||||
private void appendPlatformData(JsonObjectBuilder builder) {
|
||||
builder.appendField("playerAmount", getPlayerAmount());
|
||||
builder.appendField("onlineMode", Bukkit.getOnlineMode() ? 1 : 0);
|
||||
builder.appendField("bukkitVersion", Bukkit.getVersion());
|
||||
builder.appendField("bukkitName", Bukkit.getName());
|
||||
builder.appendField("javaVersion", System.getProperty("java.version"));
|
||||
builder.appendField("osName", System.getProperty("os.name"));
|
||||
builder.appendField("osArch", System.getProperty("os.arch"));
|
||||
builder.appendField("osVersion", System.getProperty("os.version"));
|
||||
builder.appendField("coreCount", Runtime.getRuntime().availableProcessors());
|
||||
}
|
||||
|
||||
private void appendServiceData(JsonObjectBuilder builder) {
|
||||
builder.appendField("pluginVersion", plugin.getDescription().getVersion());
|
||||
}
|
||||
|
||||
private int getPlayerAmount() {
|
||||
try {
|
||||
// Around MC 1.8 the return type was changed from an array to a collection,
|
||||
// This fixes java.lang.NoSuchMethodError:
|
||||
// org.bukkit.Bukkit.getOnlinePlayers()Ljava/util/Collection;
|
||||
Method onlinePlayersMethod = Class.forName("org.bukkit.Server").getMethod("getOnlinePlayers");
|
||||
return onlinePlayersMethod.getReturnType().equals(Collection.class)
|
||||
? ((Collection<?>) onlinePlayersMethod.invoke(Bukkit.getServer())).size()
|
||||
: ((Player[]) onlinePlayersMethod.invoke(Bukkit.getServer())).length;
|
||||
} catch (Exception e) {
|
||||
// Just use the new method if the reflection failed
|
||||
return Bukkit.getOnlinePlayers().size();
|
||||
}
|
||||
}
|
||||
|
||||
public static class MetricsBase {
|
||||
|
||||
/**
|
||||
* The version of the Metrics class.
|
||||
*/
|
||||
public static final String METRICS_VERSION = "3.0.2";
|
||||
|
||||
private static final String REPORT_URL = "https://bStats.org/api/v2/data/%s";
|
||||
|
||||
private final ScheduledExecutorService scheduler;
|
||||
|
||||
private final String platform;
|
||||
|
||||
private final String serverUuid;
|
||||
|
||||
private final int serviceId;
|
||||
|
||||
private final Consumer<JsonObjectBuilder> appendPlatformDataConsumer;
|
||||
|
||||
private final Consumer<JsonObjectBuilder> appendServiceDataConsumer;
|
||||
|
||||
private final Consumer<Runnable> submitTaskConsumer;
|
||||
|
||||
private final Supplier<Boolean> checkServiceEnabledSupplier;
|
||||
|
||||
private final BiConsumer<String, Throwable> errorLogger;
|
||||
|
||||
private final Consumer<String> infoLogger;
|
||||
|
||||
private final boolean logErrors;
|
||||
|
||||
private final boolean logSentData;
|
||||
|
||||
private final boolean logResponseStatusText;
|
||||
|
||||
private final Set<CustomChart> customCharts = new HashSet<>();
|
||||
|
||||
private final boolean enabled;
|
||||
|
||||
/**
|
||||
* Creates a new MetricsBase class instance.
|
||||
*
|
||||
* @param platform The platform of the service.
|
||||
* @param serviceId The id of the service.
|
||||
* @param serverUuid The server uuid.
|
||||
* @param enabled Whether or not data sending is enabled.
|
||||
* @param appendPlatformDataConsumer A consumer that receives a {@code JsonObjectBuilder} and
|
||||
* appends all platform-specific data.
|
||||
* @param appendServiceDataConsumer A consumer that receives a {@code JsonObjectBuilder} and
|
||||
* appends all service-specific data.
|
||||
* @param submitTaskConsumer A consumer that takes a runnable with the submit task. This can be
|
||||
* used to delegate the data collection to a another thread to prevent errors caused by
|
||||
* concurrency. Can be {@code null}.
|
||||
* @param checkServiceEnabledSupplier A supplier to check if the service is still enabled.
|
||||
* @param errorLogger A consumer that accepts log message and an error.
|
||||
* @param infoLogger A consumer that accepts info log messages.
|
||||
* @param logErrors Whether or not errors should be logged.
|
||||
* @param logSentData Whether or not the sent data should be logged.
|
||||
* @param logResponseStatusText Whether or not the response status text should be logged.
|
||||
*/
|
||||
public MetricsBase(
|
||||
String platform,
|
||||
String serverUuid,
|
||||
int serviceId,
|
||||
boolean enabled,
|
||||
Consumer<JsonObjectBuilder> appendPlatformDataConsumer,
|
||||
Consumer<JsonObjectBuilder> appendServiceDataConsumer,
|
||||
Consumer<Runnable> submitTaskConsumer,
|
||||
Supplier<Boolean> checkServiceEnabledSupplier,
|
||||
BiConsumer<String, Throwable> errorLogger,
|
||||
Consumer<String> infoLogger,
|
||||
boolean logErrors,
|
||||
boolean logSentData,
|
||||
boolean logResponseStatusText) {
|
||||
ScheduledThreadPoolExecutor scheduler =
|
||||
new ScheduledThreadPoolExecutor(1, task -> new Thread(task, "bStats-Metrics"));
|
||||
// We want delayed tasks (non-periodic) that will execute in the future to be
|
||||
// cancelled when the scheduler is shutdown.
|
||||
// Otherwise, we risk preventing the server from shutting down even when
|
||||
// MetricsBase#shutdown() is called
|
||||
scheduler.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
|
||||
this.scheduler = scheduler;
|
||||
this.platform = platform;
|
||||
this.serverUuid = serverUuid;
|
||||
this.serviceId = serviceId;
|
||||
this.enabled = enabled;
|
||||
this.appendPlatformDataConsumer = appendPlatformDataConsumer;
|
||||
this.appendServiceDataConsumer = appendServiceDataConsumer;
|
||||
this.submitTaskConsumer = submitTaskConsumer;
|
||||
this.checkServiceEnabledSupplier = checkServiceEnabledSupplier;
|
||||
this.errorLogger = errorLogger;
|
||||
this.infoLogger = infoLogger;
|
||||
this.logErrors = logErrors;
|
||||
this.logSentData = logSentData;
|
||||
this.logResponseStatusText = logResponseStatusText;
|
||||
checkRelocation();
|
||||
if (enabled) {
|
||||
// WARNING: Removing the option to opt-out will get your plugin banned from
|
||||
// bStats
|
||||
startSubmitting();
|
||||
}
|
||||
}
|
||||
|
||||
public void addCustomChart(CustomChart chart) {
|
||||
this.customCharts.add(chart);
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
scheduler.shutdown();
|
||||
}
|
||||
|
||||
private void startSubmitting() {
|
||||
final Runnable submitTask =
|
||||
() -> {
|
||||
if (!enabled || !checkServiceEnabledSupplier.get()) {
|
||||
// Submitting data or service is disabled
|
||||
scheduler.shutdown();
|
||||
return;
|
||||
}
|
||||
if (submitTaskConsumer != null) {
|
||||
submitTaskConsumer.accept(this::submitData);
|
||||
} else {
|
||||
this.submitData();
|
||||
}
|
||||
};
|
||||
// Many servers tend to restart at a fixed time at xx:00 which causes an uneven
|
||||
// distribution of requests on the
|
||||
// bStats backend. To circumvent this problem, we introduce some randomness into
|
||||
// the initial and second delay.
|
||||
// WARNING: You must not modify and part of this Metrics class, including the
|
||||
// submit delay or frequency!
|
||||
// WARNING: Modifying this code will get your plugin banned on bStats. Just
|
||||
// don't do it!
|
||||
long initialDelay = (long) (1000 * 60 * (3 + Math.random() * 3));
|
||||
long secondDelay = (long) (1000 * 60 * (Math.random() * 30));
|
||||
scheduler.schedule(submitTask, initialDelay, TimeUnit.MILLISECONDS);
|
||||
scheduler.scheduleAtFixedRate(
|
||||
submitTask, initialDelay + secondDelay, 1000 * 60 * 30, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
private void submitData() {
|
||||
final JsonObjectBuilder baseJsonBuilder = new JsonObjectBuilder();
|
||||
appendPlatformDataConsumer.accept(baseJsonBuilder);
|
||||
final JsonObjectBuilder serviceJsonBuilder = new JsonObjectBuilder();
|
||||
appendServiceDataConsumer.accept(serviceJsonBuilder);
|
||||
JsonObjectBuilder.JsonObject[] chartData =
|
||||
customCharts.stream()
|
||||
.map(customChart -> customChart.getRequestJsonObject(errorLogger, logErrors))
|
||||
.filter(Objects::nonNull)
|
||||
.toArray(JsonObjectBuilder.JsonObject[]::new);
|
||||
serviceJsonBuilder.appendField("id", serviceId);
|
||||
serviceJsonBuilder.appendField("customCharts", chartData);
|
||||
baseJsonBuilder.appendField("service", serviceJsonBuilder.build());
|
||||
baseJsonBuilder.appendField("serverUUID", serverUuid);
|
||||
baseJsonBuilder.appendField("metricsVersion", METRICS_VERSION);
|
||||
JsonObjectBuilder.JsonObject data = baseJsonBuilder.build();
|
||||
scheduler.execute(
|
||||
() -> {
|
||||
try {
|
||||
// Send the data
|
||||
sendData(data);
|
||||
} catch (Exception e) {
|
||||
// Something went wrong! :(
|
||||
if (logErrors) {
|
||||
errorLogger.accept("Could not submit bStats metrics data", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void sendData(JsonObjectBuilder.JsonObject data) throws Exception {
|
||||
if (logSentData) {
|
||||
infoLogger.accept("Sent bStats metrics data: " + data.toString());
|
||||
}
|
||||
String url = String.format(REPORT_URL, platform);
|
||||
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
|
||||
// Compress the data to save bandwidth
|
||||
byte[] compressedData = compress(data.toString());
|
||||
connection.setRequestMethod("POST");
|
||||
connection.addRequestProperty("Accept", "application/json");
|
||||
connection.addRequestProperty("Connection", "close");
|
||||
connection.addRequestProperty("Content-Encoding", "gzip");
|
||||
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
connection.setRequestProperty("User-Agent", "Metrics-Service/1");
|
||||
connection.setDoOutput(true);
|
||||
try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
|
||||
outputStream.write(compressedData);
|
||||
}
|
||||
StringBuilder builder = new StringBuilder();
|
||||
try (BufferedReader bufferedReader =
|
||||
new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
|
||||
String line;
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
builder.append(line);
|
||||
}
|
||||
}
|
||||
if (logResponseStatusText) {
|
||||
infoLogger.accept("Sent data to bStats and received response: " + builder);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that the class was properly relocated.
|
||||
*/
|
||||
private void checkRelocation() {
|
||||
// You can use the property to disable the check in your test environment
|
||||
if (System.getProperty("bstats.relocatecheck") == null
|
||||
|| !System.getProperty("bstats.relocatecheck").equals("false")) {
|
||||
// Maven's Relocate is clever and changes strings, too. So we have to use this
|
||||
// little "trick" ... :D
|
||||
final String defaultPackage =
|
||||
new String(new byte[]{'o', 'r', 'g', '.', 'b', 's', 't', 'a', 't', 's'});
|
||||
final String examplePackage =
|
||||
new String(new byte[]{'y', 'o', 'u', 'r', '.', 'p', 'a', 'c', 'k', 'a', 'g', 'e'});
|
||||
// We want to make sure no one just copy & pastes the example and uses the wrong
|
||||
// package names
|
||||
if (MetricsBase.class.getPackage().getName().startsWith(defaultPackage)
|
||||
|| MetricsBase.class.getPackage().getName().startsWith(examplePackage)) {
|
||||
throw new IllegalStateException("bStats Metrics class has not been relocated correctly!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gzips the given string.
|
||||
*
|
||||
* @param str The string to gzip.
|
||||
* @return The gzipped string.
|
||||
*/
|
||||
private static byte[] compress(final String str) throws IOException {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
try (GZIPOutputStream gzip = new GZIPOutputStream(outputStream)) {
|
||||
gzip.write(str.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
return outputStream.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
public static class SimplePie extends CustomChart {
|
||||
|
||||
private final Callable<String> callable;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param chartId The id of the chart.
|
||||
* @param callable The callable which is used to request the chart data.
|
||||
*/
|
||||
public SimplePie(String chartId, Callable<String> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
|
||||
String value = callable.call();
|
||||
if (value == null || value.isEmpty()) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
return new JsonObjectBuilder().appendField("value", value).build();
|
||||
}
|
||||
}
|
||||
|
||||
public static class MultiLineChart extends CustomChart {
|
||||
|
||||
private final Callable<Map<String, Integer>> callable;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param chartId The id of the chart.
|
||||
* @param callable The callable which is used to request the chart data.
|
||||
*/
|
||||
public MultiLineChart(String chartId, Callable<Map<String, Integer>> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
|
||||
JsonObjectBuilder valuesBuilder = new JsonObjectBuilder();
|
||||
Map<String, Integer> map = callable.call();
|
||||
if (map == null || map.isEmpty()) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
boolean allSkipped = true;
|
||||
for (Map.Entry<String, Integer> entry : map.entrySet()) {
|
||||
if (entry.getValue() == 0) {
|
||||
// Skip this invalid
|
||||
continue;
|
||||
}
|
||||
allSkipped = false;
|
||||
valuesBuilder.appendField(entry.getKey(), entry.getValue());
|
||||
}
|
||||
if (allSkipped) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
|
||||
}
|
||||
}
|
||||
|
||||
public static class AdvancedPie extends CustomChart {
|
||||
|
||||
private final Callable<Map<String, Integer>> callable;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param chartId The id of the chart.
|
||||
* @param callable The callable which is used to request the chart data.
|
||||
*/
|
||||
public AdvancedPie(String chartId, Callable<Map<String, Integer>> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
|
||||
JsonObjectBuilder valuesBuilder = new JsonObjectBuilder();
|
||||
Map<String, Integer> map = callable.call();
|
||||
if (map == null || map.isEmpty()) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
boolean allSkipped = true;
|
||||
for (Map.Entry<String, Integer> entry : map.entrySet()) {
|
||||
if (entry.getValue() == 0) {
|
||||
// Skip this invalid
|
||||
continue;
|
||||
}
|
||||
allSkipped = false;
|
||||
valuesBuilder.appendField(entry.getKey(), entry.getValue());
|
||||
}
|
||||
if (allSkipped) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
|
||||
}
|
||||
}
|
||||
|
||||
public static class SimpleBarChart extends CustomChart {
|
||||
|
||||
private final Callable<Map<String, Integer>> callable;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param chartId The id of the chart.
|
||||
* @param callable The callable which is used to request the chart data.
|
||||
*/
|
||||
public SimpleBarChart(String chartId, Callable<Map<String, Integer>> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
|
||||
JsonObjectBuilder valuesBuilder = new JsonObjectBuilder();
|
||||
Map<String, Integer> map = callable.call();
|
||||
if (map == null || map.isEmpty()) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
for (Map.Entry<String, Integer> entry : map.entrySet()) {
|
||||
valuesBuilder.appendField(entry.getKey(), new int[]{entry.getValue()});
|
||||
}
|
||||
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
|
||||
}
|
||||
}
|
||||
|
||||
public static class AdvancedBarChart extends CustomChart {
|
||||
|
||||
private final Callable<Map<String, int[]>> callable;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param chartId The id of the chart.
|
||||
* @param callable The callable which is used to request the chart data.
|
||||
*/
|
||||
public AdvancedBarChart(String chartId, Callable<Map<String, int[]>> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
|
||||
JsonObjectBuilder valuesBuilder = new JsonObjectBuilder();
|
||||
Map<String, int[]> map = callable.call();
|
||||
if (map == null || map.isEmpty()) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
boolean allSkipped = true;
|
||||
for (Map.Entry<String, int[]> entry : map.entrySet()) {
|
||||
if (entry.getValue().length == 0) {
|
||||
// Skip this invalid
|
||||
continue;
|
||||
}
|
||||
allSkipped = false;
|
||||
valuesBuilder.appendField(entry.getKey(), entry.getValue());
|
||||
}
|
||||
if (allSkipped) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
|
||||
}
|
||||
}
|
||||
|
||||
public static class DrilldownPie extends CustomChart {
|
||||
|
||||
private final Callable<Map<String, Map<String, Integer>>> callable;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param chartId The id of the chart.
|
||||
* @param callable The callable which is used to request the chart data.
|
||||
*/
|
||||
public DrilldownPie(String chartId, Callable<Map<String, Map<String, Integer>>> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonObjectBuilder.JsonObject getChartData() throws Exception {
|
||||
JsonObjectBuilder valuesBuilder = new JsonObjectBuilder();
|
||||
Map<String, Map<String, Integer>> map = callable.call();
|
||||
if (map == null || map.isEmpty()) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
boolean reallyAllSkipped = true;
|
||||
for (Map.Entry<String, Map<String, Integer>> entryValues : map.entrySet()) {
|
||||
JsonObjectBuilder valueBuilder = new JsonObjectBuilder();
|
||||
boolean allSkipped = true;
|
||||
for (Map.Entry<String, Integer> valueEntry : map.get(entryValues.getKey()).entrySet()) {
|
||||
valueBuilder.appendField(valueEntry.getKey(), valueEntry.getValue());
|
||||
allSkipped = false;
|
||||
}
|
||||
if (!allSkipped) {
|
||||
reallyAllSkipped = false;
|
||||
valuesBuilder.appendField(entryValues.getKey(), valueBuilder.build());
|
||||
}
|
||||
}
|
||||
if (reallyAllSkipped) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
return new JsonObjectBuilder().appendField("values", valuesBuilder.build()).build();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract static class CustomChart {
|
||||
|
||||
private final String chartId;
|
||||
|
||||
protected CustomChart(String chartId) {
|
||||
if (chartId == null) {
|
||||
throw new IllegalArgumentException("chartId must not be null");
|
||||
}
|
||||
this.chartId = chartId;
|
||||
}
|
||||
|
||||
public JsonObjectBuilder.JsonObject getRequestJsonObject(
|
||||
BiConsumer<String, Throwable> errorLogger, boolean logErrors) {
|
||||
JsonObjectBuilder builder = new JsonObjectBuilder();
|
||||
builder.appendField("chartId", chartId);
|
||||
try {
|
||||
JsonObjectBuilder.JsonObject data = getChartData();
|
||||
if (data == null) {
|
||||
// If the data is null we don't send the chart.
|
||||
return null;
|
||||
}
|
||||
builder.appendField("data", data);
|
||||
} catch (Throwable t) {
|
||||
if (logErrors) {
|
||||
errorLogger.accept("Failed to get data for custom chart with id " + chartId, t);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
protected abstract JsonObjectBuilder.JsonObject getChartData() throws Exception;
|
||||
}
|
||||
|
||||
public static class SingleLineChart extends CustomChart {
|
||||
|
||||
private final Callable<Integer> callable;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param chartId The id of the chart.
|
||||
* @param callable The callable which is used to request the chart data.
|
||||
*/
|
||||
public SingleLineChart(String chartId, Callable<Integer> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonObjectBuilder.JsonObject getChartData() throws Exception {
|
||||
int value = callable.call();
|
||||
if (value == 0) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
return new JsonObjectBuilder().appendField("value", value).build();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An extremely simple JSON builder.
|
||||
*
|
||||
* <p>While this class is neither feature-rich nor the most performant one, it's sufficient enough
|
||||
* for its use-case.
|
||||
*/
|
||||
public static class JsonObjectBuilder {
|
||||
|
||||
private StringBuilder builder = new StringBuilder();
|
||||
|
||||
private boolean hasAtLeastOneField = false;
|
||||
|
||||
public JsonObjectBuilder() {
|
||||
builder.append("{");
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a null field to the JSON.
|
||||
*
|
||||
* @param key The key of the field.
|
||||
* @return A reference to this object.
|
||||
*/
|
||||
public JsonObjectBuilder appendNull(String key) {
|
||||
appendFieldUnescaped(key, "null");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a string field to the JSON.
|
||||
*
|
||||
* @param key The key of the field.
|
||||
* @param value The value of the field.
|
||||
* @return A reference to this object.
|
||||
*/
|
||||
public JsonObjectBuilder appendField(String key, String value) {
|
||||
if (value == null) {
|
||||
throw new IllegalArgumentException("JSON value must not be null");
|
||||
}
|
||||
appendFieldUnescaped(key, "\"" + escape(value) + "\"");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends an integer field to the JSON.
|
||||
*
|
||||
* @param key The key of the field.
|
||||
* @param value The value of the field.
|
||||
* @return A reference to this object.
|
||||
*/
|
||||
public JsonObjectBuilder appendField(String key, int value) {
|
||||
appendFieldUnescaped(key, String.valueOf(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends an object to the JSON.
|
||||
*
|
||||
* @param key The key of the field.
|
||||
* @param object The object.
|
||||
* @return A reference to this object.
|
||||
*/
|
||||
public JsonObjectBuilder appendField(String key, JsonObject object) {
|
||||
if (object == null) {
|
||||
throw new IllegalArgumentException("JSON object must not be null");
|
||||
}
|
||||
appendFieldUnescaped(key, object.toString());
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a string array to the JSON.
|
||||
*
|
||||
* @param key The key of the field.
|
||||
* @param values The string array.
|
||||
* @return A reference to this object.
|
||||
*/
|
||||
public JsonObjectBuilder appendField(String key, String[] values) {
|
||||
if (values == null) {
|
||||
throw new IllegalArgumentException("JSON values must not be null");
|
||||
}
|
||||
String escapedValues =
|
||||
Arrays.stream(values)
|
||||
.map(value -> "\"" + escape(value) + "\"")
|
||||
.collect(Collectors.joining(","));
|
||||
appendFieldUnescaped(key, "[" + escapedValues + "]");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends an integer array to the JSON.
|
||||
*
|
||||
* @param key The key of the field.
|
||||
* @param values The integer array.
|
||||
* @return A reference to this object.
|
||||
*/
|
||||
public JsonObjectBuilder appendField(String key, int[] values) {
|
||||
if (values == null) {
|
||||
throw new IllegalArgumentException("JSON values must not be null");
|
||||
}
|
||||
String escapedValues =
|
||||
Arrays.stream(values).mapToObj(String::valueOf).collect(Collectors.joining(","));
|
||||
appendFieldUnescaped(key, "[" + escapedValues + "]");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends an object array to the JSON.
|
||||
*
|
||||
* @param key The key of the field.
|
||||
* @param values The integer array.
|
||||
* @return A reference to this object.
|
||||
*/
|
||||
public JsonObjectBuilder appendField(String key, JsonObject[] values) {
|
||||
if (values == null) {
|
||||
throw new IllegalArgumentException("JSON values must not be null");
|
||||
}
|
||||
String escapedValues =
|
||||
Arrays.stream(values).map(JsonObject::toString).collect(Collectors.joining(","));
|
||||
appendFieldUnescaped(key, "[" + escapedValues + "]");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a field to the object.
|
||||
*
|
||||
* @param key The key of the field.
|
||||
* @param escapedValue The escaped value of the field.
|
||||
*/
|
||||
private void appendFieldUnescaped(String key, String escapedValue) {
|
||||
if (builder == null) {
|
||||
throw new IllegalStateException("JSON has already been built");
|
||||
}
|
||||
if (key == null) {
|
||||
throw new IllegalArgumentException("JSON key must not be null");
|
||||
}
|
||||
if (hasAtLeastOneField) {
|
||||
builder.append(",");
|
||||
}
|
||||
builder.append("\"").append(escape(key)).append("\":").append(escapedValue);
|
||||
hasAtLeastOneField = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the JSON string and invalidates this builder.
|
||||
*
|
||||
* @return The built JSON string.
|
||||
*/
|
||||
public JsonObject build() {
|
||||
if (builder == null) {
|
||||
throw new IllegalStateException("JSON has already been built");
|
||||
}
|
||||
JsonObject object = new JsonObject(builder.append("}").toString());
|
||||
builder = null;
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes the given string like stated in https://www.ietf.org/rfc/rfc4627.txt.
|
||||
*
|
||||
* <p>This method escapes only the necessary characters '"', '\'. and '\u0000' - '\u001F'.
|
||||
* Compact escapes are not used (e.g., '\n' is escaped as "\u000a" and not as "\n").
|
||||
*
|
||||
* @param value The value to escape.
|
||||
* @return The escaped value.
|
||||
*/
|
||||
private static String escape(String value) {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
for (int i = 0; i < value.length(); i++) {
|
||||
char c = value.charAt(i);
|
||||
if (c == '"') {
|
||||
builder.append("\\\"");
|
||||
} else if (c == '\\') {
|
||||
builder.append("\\\\");
|
||||
} else if (c <= '\u000F') {
|
||||
builder.append("\\u000").append(Integer.toHexString(c));
|
||||
} else if (c <= '\u001F') {
|
||||
builder.append("\\u00").append(Integer.toHexString(c));
|
||||
} else {
|
||||
builder.append(c);
|
||||
}
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A super simple representation of a JSON object.
|
||||
*
|
||||
* <p>This class only exists to make methods of the {@link JsonObjectBuilder} type-safe and not
|
||||
* allow a raw string inputs for methods like {@link JsonObjectBuilder#appendField(String,
|
||||
* JsonObject)}.
|
||||
*/
|
||||
public static class JsonObject {
|
||||
|
||||
private final String value;
|
||||
|
||||
private JsonObject(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,23 +1,13 @@
|
||||
package cn.lunadeer.miniplayertitle;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.*;
|
||||
import cn.lunadeer.minecraftpluginutils.VaultConnect.VaultConnect;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.DatabaseManager;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.DatabaseType;
|
||||
import cn.lunadeer.miniplayertitle.commands.TitleCard;
|
||||
import cn.lunadeer.miniplayertitle.dtos.TitleDTO;
|
||||
import cn.lunadeer.miniplayertitle.events.Events;
|
||||
import cn.lunadeer.miniplayertitle.events.PaperChat;
|
||||
import cn.lunadeer.miniplayertitle.events.SpigotChat;
|
||||
import cn.lunadeer.miniplayertitle.utils.ConfigManager;
|
||||
import cn.lunadeer.miniplayertitle.utils.DatabaseTables;
|
||||
import cn.lunadeer.miniplayertitle.utils.Database;
|
||||
import cn.lunadeer.miniplayertitle.utils.XLogger;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.sql.Connection;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class MiniPlayerTitle extends JavaPlugin {
|
||||
|
||||
@ -25,48 +15,18 @@ public final class MiniPlayerTitle extends JavaPlugin {
|
||||
public void onEnable() {
|
||||
// Plugin startup logic
|
||||
instance = this;
|
||||
new Scheduler(this);
|
||||
new Notification(this);
|
||||
new XLogger(instance);
|
||||
config = new ConfigManager(instance);
|
||||
XLogger.setDebug(config.isDebug());
|
||||
new DatabaseManager(this,
|
||||
DatabaseType.valueOf(config.getDbType().toUpperCase()),
|
||||
config.getDbHost(),
|
||||
config.getDbPort(),
|
||||
config.getDbName(),
|
||||
config.getDbUser(),
|
||||
config.getDbPass());
|
||||
DatabaseTables.migrate();
|
||||
if (config.isExternalEco()) {
|
||||
XLogger.info("已启用外部经济插件");
|
||||
new VaultConnect(this);
|
||||
}
|
||||
|
||||
if (usingPapi()) {
|
||||
new Expansion(this);
|
||||
}
|
||||
dbConnection = Database.createConnection();
|
||||
Database.migrate();
|
||||
|
||||
Bukkit.getPluginManager().registerEvents(new Events(), this);
|
||||
Bukkit.getPluginManager().registerEvents(new TitleCard(), this);
|
||||
if (Common.isPaper()) {
|
||||
Bukkit.getPluginManager().registerEvents(new PaperChat(), this);
|
||||
} else {
|
||||
Bukkit.getPluginManager().registerEvents(new SpigotChat(), this);
|
||||
}
|
||||
Objects.requireNonNull(Bukkit.getPluginCommand("MiniPlayerTitle")).setExecutor(new Commands());
|
||||
Objects.requireNonNull(Bukkit.getPluginCommand("MiniPlayerTitle")).setTabCompleter(new Commands());
|
||||
|
||||
bStatsMetrics metrics = new bStatsMetrics(this, 21444);
|
||||
if (config.isCheckUpdate()) {
|
||||
giteaReleaseCheck = new GiteaReleaseCheck(this,
|
||||
"https://ssl.lunadeer.cn:14446",
|
||||
"zhangyuheng",
|
||||
"MiniPlayerTitle");
|
||||
}
|
||||
Metrics metrics = new Metrics(this, 21444);
|
||||
|
||||
XLogger.info("称号插件已加载");
|
||||
XLogger.info("版本: " + this.getDescription().getVersion());
|
||||
XLogger.info("版本: " + getPluginMeta().getVersion());
|
||||
// http://patorjk.com/software/taag/#p=display&f=Big&t=MiniPlayerTitle
|
||||
XLogger.info(" __ __ _ _ _____ _ _______ _ _ _");
|
||||
XLogger.info(" | \\/ (_) (_) __ \\| | |__ __(_) | | |");
|
||||
@ -81,23 +41,9 @@ public final class MiniPlayerTitle extends JavaPlugin {
|
||||
@Override
|
||||
public void onDisable() {
|
||||
// Plugin shutdown logic
|
||||
DatabaseManager.instance.close();
|
||||
}
|
||||
|
||||
public static MiniPlayerTitle instance;
|
||||
public static ConfigManager config;
|
||||
private GiteaReleaseCheck giteaReleaseCheck;
|
||||
private Map<UUID, TitleDTO> playerUsingTitle = new HashMap<>();
|
||||
|
||||
public static boolean usingPapi() {
|
||||
return Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI");
|
||||
}
|
||||
|
||||
public void setPlayerUsingTitle(UUID uuid, TitleDTO title) {
|
||||
playerUsingTitle.put(uuid, title);
|
||||
}
|
||||
|
||||
public TitleDTO getPlayerUsingTitle(UUID uuid) {
|
||||
return playerUsingTitle.get(uuid);
|
||||
}
|
||||
public static Connection dbConnection;
|
||||
}
|
||||
|
66
src/main/java/cn/lunadeer/miniplayertitle/PlayerTitle.java
Normal file
66
src/main/java/cn/lunadeer/miniplayertitle/PlayerTitle.java
Normal file
@ -0,0 +1,66 @@
|
||||
package cn.lunadeer.miniplayertitle;
|
||||
|
||||
import cn.lunadeer.miniplayertitle.utils.Database;
|
||||
import cn.lunadeer.miniplayertitle.utils.Time;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PlayerTitle extends Title {
|
||||
private Long _expire_at = -1L;
|
||||
private final UUID _player_uuid;
|
||||
|
||||
public PlayerTitle(Integer title_id, UUID player_uuid, Long expire_at) {
|
||||
super(title_id);
|
||||
this._player_uuid = player_uuid;
|
||||
this._expire_at = expire_at;
|
||||
}
|
||||
|
||||
public static PlayerTitle create(Integer title_id, UUID player_uuid) {
|
||||
String sql = "";
|
||||
sql += "INSERT INTO mplt_player_title (title_id, player_uuid, expire_at) ";
|
||||
sql += "VALUES (" + title_id + ", '" + player_uuid.toString() + "', " + System.currentTimeMillis() + ") ";
|
||||
sql += "RETURNING id;";
|
||||
try (ResultSet rs = Database.query(sql)) {
|
||||
if (rs != null && rs.next()) {
|
||||
return new PlayerTitle(title_id, player_uuid, -1L);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
cn.lunadeer.miniplayertitle.utils.XLogger.err("PlayerTitle create failed: " + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getExpireAtStr() {
|
||||
if (this._expire_at == -1L) {
|
||||
return "永久";
|
||||
} else if (this._expire_at < Time.getCurrent()) {
|
||||
return "已过期";
|
||||
} else {
|
||||
return this._expire_at.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean isExpired() {
|
||||
if (this._expire_at == -1L) {
|
||||
return false;
|
||||
} else {
|
||||
return this._expire_at < Time.getCurrent();
|
||||
}
|
||||
}
|
||||
|
||||
public void setExpireAt(Long expire_at) {
|
||||
this._expire_at = expire_at;
|
||||
this.save();
|
||||
}
|
||||
|
||||
private void save() {
|
||||
String sql = "";
|
||||
sql += "UPDATE mplt_player_title ";
|
||||
sql += "SET expire_at = " + this._expire_at + ", ";
|
||||
sql += "updated_at = CURRENT_TIMESTAMP ";
|
||||
sql += "WHERE player_uuid = '" + _player_uuid.toString() + "' ";
|
||||
sql += "AND title_id = " + this._id + ";";
|
||||
Database.query(sql);
|
||||
}
|
||||
}
|
130
src/main/java/cn/lunadeer/miniplayertitle/SaleTitle.java
Normal file
130
src/main/java/cn/lunadeer/miniplayertitle/SaleTitle.java
Normal file
@ -0,0 +1,130 @@
|
||||
package cn.lunadeer.miniplayertitle;
|
||||
|
||||
|
||||
import cn.lunadeer.miniplayertitle.utils.Database;
|
||||
import cn.lunadeer.miniplayertitle.utils.Time;
|
||||
import cn.lunadeer.miniplayertitle.utils.XLogger;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
|
||||
public class SaleTitle extends Title {
|
||||
private Integer _sale_id;
|
||||
private Integer _price;
|
||||
private Integer _days;
|
||||
private Integer _amount;
|
||||
private Long _sale_end_at;
|
||||
|
||||
public SaleTitle(Integer id, Integer title_id, Integer price, Integer days, Integer amount, Long sale_end_at) {
|
||||
super(title_id);
|
||||
this._sale_id = id;
|
||||
this._id = title_id;
|
||||
this._price = price;
|
||||
this._days = days;
|
||||
this._amount = amount;
|
||||
this._sale_end_at = sale_end_at;
|
||||
}
|
||||
|
||||
public static SaleTitle create(Integer title_id) {
|
||||
String sql = "";
|
||||
sql += "INSERT INTO mplt_title_shop (title_id, price, days, amount, sale_end_at) ";
|
||||
sql += "VALUES (" + title_id + ", 0, 0, 0, -1) ";
|
||||
sql += "RETURNING id;";
|
||||
try (ResultSet rs = Database.query(sql)) {
|
||||
if (rs != null && rs.next()) {
|
||||
Integer id = rs.getInt("id");
|
||||
return new SaleTitle(id, title_id, 0, 0, 0, -1L);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
XLogger.err("SaleTitle create failed: " + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Integer getPrice() {
|
||||
return this._price;
|
||||
}
|
||||
|
||||
private void setPrice(Integer price) {
|
||||
this._price = price;
|
||||
this.save();
|
||||
}
|
||||
|
||||
public static void setPrice(Integer id, Integer price) {
|
||||
SaleTitle title = Shop.getSaleTitles().get(id);
|
||||
title.setPrice(price);
|
||||
}
|
||||
|
||||
public Integer getDays() {
|
||||
return this._days;
|
||||
}
|
||||
|
||||
private void setDays(Integer days) {
|
||||
this._days = days;
|
||||
this.save();
|
||||
}
|
||||
|
||||
public static void setDays(Integer id, Integer days) {
|
||||
SaleTitle title = Shop.getSaleTitles().get(id);
|
||||
title.setDays(days);
|
||||
}
|
||||
|
||||
public Integer getAmount() {
|
||||
return this._amount;
|
||||
}
|
||||
|
||||
private void setAmount(Integer amount) {
|
||||
this._amount = amount;
|
||||
this.save();
|
||||
}
|
||||
|
||||
public static void setAmount(Integer id, Integer amount) {
|
||||
SaleTitle title = Shop.getSaleTitles().get(id);
|
||||
title.setAmount(amount);
|
||||
}
|
||||
|
||||
public String getSaleEndAt() {
|
||||
if (this._sale_end_at == -1L) {
|
||||
return "常驻";
|
||||
} else if (this._sale_end_at < Time.getCurrent()) {
|
||||
return "已停售";
|
||||
} else {
|
||||
return this._sale_end_at.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private void setSaleEndAt(Long sale_end_at) {
|
||||
this._sale_end_at = sale_end_at;
|
||||
this.save();
|
||||
}
|
||||
|
||||
public static void setSaleEndAt(Integer id, Long sale_end_at) {
|
||||
SaleTitle title = Shop.getSaleTitles().get(id);
|
||||
title.setSaleEndAt(sale_end_at);
|
||||
}
|
||||
|
||||
public Boolean isSaleExpired() {
|
||||
if (this._sale_end_at == -1L) {
|
||||
return false;
|
||||
} else {
|
||||
return this._sale_end_at < Time.getCurrent();
|
||||
}
|
||||
}
|
||||
|
||||
public Integer getSaleId(){
|
||||
return this._sale_id;
|
||||
}
|
||||
|
||||
private void save() {
|
||||
String sql = "";
|
||||
sql += "UPDATE mplt_title_shop ";
|
||||
sql += "SET title_id = " + this._id + ", ";
|
||||
sql += "price = " + this._price + ", ";
|
||||
sql += "days = " + this._days + ", ";
|
||||
sql += "amount = " + this._amount + ", ";
|
||||
sql += "sale_end_at = " + this._sale_end_at + ", ";
|
||||
sql += "updated_at = CURRENT_TIMESTAMP ";
|
||||
sql += "WHERE id = " + this._sale_id + ";";
|
||||
|
||||
Database.query(sql);
|
||||
}
|
||||
}
|
82
src/main/java/cn/lunadeer/miniplayertitle/Shop.java
Normal file
82
src/main/java/cn/lunadeer/miniplayertitle/Shop.java
Normal file
@ -0,0 +1,82 @@
|
||||
package cn.lunadeer.miniplayertitle;
|
||||
|
||||
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.ListView;
|
||||
import cn.lunadeer.miniplayertitle.utils.XLogger;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Shop {
|
||||
public static void open(CommandSender sender, Integer page) {
|
||||
Map<Integer, SaleTitle> titles = getSaleTitles();
|
||||
if (!(sender instanceof Player)) {
|
||||
for (SaleTitle title : titles.values()) {
|
||||
Component idx = Component.text("[" + title.getSaleId() + "] ");
|
||||
Notification.info(sender, idx.append(title.getTitle()));
|
||||
}
|
||||
return;
|
||||
}
|
||||
Player player = (Player) sender;
|
||||
ListView view = ListView.create(5, "/mplt shop");
|
||||
view.title("称号商店");
|
||||
view.subtitle("当前余额: " + XPlayer.getCoin(player) + "称号币");
|
||||
for (Map.Entry<Integer, SaleTitle> entry : titles.entrySet()) {
|
||||
Integer title_sale_id = entry.getKey();
|
||||
TextComponent idx = Component.text("[" + 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.getSaleEndAt())
|
||||
.append("剩余:" + ((title.getAmount() == -1) ? "无限" : title.getAmount()))
|
||||
.append(button);
|
||||
view.add(line);
|
||||
}
|
||||
view.showOn(player, page);
|
||||
}
|
||||
|
||||
public static void deleteTitle(Integer id) {
|
||||
String sql = "";
|
||||
sql += "DELETE FROM mplt_title_shop WHERE id = " + id + ";";
|
||||
Database.query(sql);
|
||||
}
|
||||
|
||||
public static Map<Integer, SaleTitle> getSaleTitles() {
|
||||
String sql = "";
|
||||
sql += "SELECT ";
|
||||
sql += "id, ";
|
||||
sql += "title_id, ";
|
||||
sql += "price, ";
|
||||
sql += "days, ";
|
||||
sql += "amount, ";
|
||||
sql += "sale_end_at ";
|
||||
sql += "FROM mplt_title_shop;";
|
||||
Map<Integer, SaleTitle> titles = new HashMap<>();
|
||||
try (ResultSet rs = Database.query(sql)) {
|
||||
while (rs != null && rs.next()) {
|
||||
Integer id = rs.getInt("id");
|
||||
Integer title_id = rs.getInt("title_id");
|
||||
Integer price = rs.getInt("price");
|
||||
Integer days = rs.getInt("days");
|
||||
Integer amount = rs.getInt("amount");
|
||||
Long sale_end_at = rs.getLong("sale_end_at");
|
||||
SaleTitle title = new SaleTitle(id, title_id, price, days, amount, sale_end_at);
|
||||
titles.put(id, title);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
XLogger.err("XPlayer getTitles failed: " + e.getMessage());
|
||||
}
|
||||
return titles;
|
||||
}
|
||||
}
|
196
src/main/java/cn/lunadeer/miniplayertitle/Title.java
Normal file
196
src/main/java/cn/lunadeer/miniplayertitle/Title.java
Normal file
@ -0,0 +1,196 @@
|
||||
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.ListView;
|
||||
import cn.lunadeer.miniplayertitle.utils.XLogger;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.event.HoverEvent;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Title {
|
||||
protected Integer _id = null;
|
||||
protected String _title;
|
||||
protected String _description;
|
||||
protected Boolean _enabled;
|
||||
|
||||
public static Title create(String title, String description) {
|
||||
String sql = "";
|
||||
sql += "INSERT INTO mplt_title (title, description, enabled) VALUES (";
|
||||
sql += "'" + title + "', ";
|
||||
sql += "'" + description + "', ";
|
||||
sql += "true ";
|
||||
sql += ") RETURNING id;";
|
||||
try (ResultSet rs = Database.query(sql)) {
|
||||
if (rs != null && rs.next()) {
|
||||
int titleId = rs.getInt("id");
|
||||
return new Title(titleId);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
XLogger.err("Title create failed: " + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<Title> all() {
|
||||
List<Title> titles = new ArrayList<>();
|
||||
String sql = "";
|
||||
sql += "SELECT id FROM mplt_title;";
|
||||
try (ResultSet rs = Database.query(sql)) {
|
||||
if (rs != null) {
|
||||
while (rs.next()) {
|
||||
Integer id = rs.getInt("id");
|
||||
titles.add(new Title(id));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
XLogger.err("Title all failed: " + e.getMessage());
|
||||
}
|
||||
return titles;
|
||||
}
|
||||
|
||||
public static void listAllTitle(CommandSender sender, Integer page) {
|
||||
List<Title> titles = all();
|
||||
if (!(sender instanceof Player)) {
|
||||
for (Title title : titles) {
|
||||
Component idx = Component.text("[" + title.getId() + "]");
|
||||
Notification.info(sender, idx.append(title.getTitle()));
|
||||
}
|
||||
return;
|
||||
}
|
||||
Player player = (Player) sender;
|
||||
ListView view = ListView.create(5, "/mplt listall");
|
||||
view.title("所有称号");
|
||||
for (Title title : titles) {
|
||||
TextComponent idx = Component.text("[" + title.getId() + "] ");
|
||||
Line line = Line.create();
|
||||
line.append(idx).append(title.getTitle());
|
||||
view.add(line);
|
||||
}
|
||||
view.showOn(player, page);
|
||||
}
|
||||
|
||||
public Title(Integer id) {
|
||||
this._id = id;
|
||||
String sql = "";
|
||||
sql += "SELECT id, title, description, enabled ";
|
||||
sql += "FROM mplt_title ";
|
||||
sql += "WHERE id = " + id + ";";
|
||||
try (ResultSet rs = Database.query(sql)) {
|
||||
if (rs != null && rs.next()) {
|
||||
this._title = rs.getString("title");
|
||||
this._description = rs.getString("description");
|
||||
this._enabled = rs.getBoolean("enabled");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
XLogger.err("Title load failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void delete(Integer id) {
|
||||
String sql = "";
|
||||
sql += "DELETE FROM mplt_title WHERE id = " + id + ";";
|
||||
Database.query(sql);
|
||||
}
|
||||
|
||||
private void save() {
|
||||
String sql = "";
|
||||
if (this._id == null) {
|
||||
sql += "INSERT INTO mplt_title (title, description, enabled) VALUES (";
|
||||
sql += "'" + this._title + "', ";
|
||||
sql += "'" + this._description + "', ";
|
||||
sql += this._enabled + " ";
|
||||
sql += ");";
|
||||
} else {
|
||||
sql += "UPDATE mplt_title SET ";
|
||||
sql += "title = '" + this._title + "', ";
|
||||
sql += "description = '" + this._description + "', ";
|
||||
sql += "enabled = " + this._enabled + " ";
|
||||
sql += "updated_at = CURRENT_TIMESTAMP ";
|
||||
sql += "WHERE id = " + this._id + ";";
|
||||
}
|
||||
Database.query(sql);
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return this._id;
|
||||
}
|
||||
|
||||
public TextComponent getTitle() {
|
||||
TextComponent prefix = Component.text(MiniPlayerTitle.config.getPrefix());
|
||||
TextComponent suffix = Component.text(MiniPlayerTitle.config.getSuffix());
|
||||
String[] parts = this._title.split("&#");
|
||||
List<TextComponent> components = new ArrayList<>();
|
||||
components.add(prefix);
|
||||
for (String part : parts) {
|
||||
if (part.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
// match hex regx ^[0-9a-fA-F]{6}$
|
||||
Color color = new Color("#ffffff");
|
||||
String content;
|
||||
if (part.length() > 6 && part.substring(0, 6).matches("^[0-9a-fA-F]{6}$")) {
|
||||
String color_str = part.substring(0, 6);
|
||||
color = new Color("#" + color_str);
|
||||
content = part.substring(6);
|
||||
} else {
|
||||
content = part;
|
||||
}
|
||||
components.add(Component.text(content, color.getStyle()));
|
||||
}
|
||||
components.add(suffix);
|
||||
TextComponent.Builder title_component = Component.text();
|
||||
for (TextComponent component : components) {
|
||||
title_component.append(component);
|
||||
}
|
||||
return title_component.build().hoverEvent(HoverEvent.hoverEvent(HoverEvent.Action.SHOW_TEXT, Component.text(this._description)));
|
||||
}
|
||||
|
||||
public String getTitleContent() {
|
||||
String[] parts = this._title.split("&#");
|
||||
StringBuilder res = new StringBuilder();
|
||||
for (String part : parts) {
|
||||
if (part.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
String content;
|
||||
if (part.length() > 6 && part.substring(0, 6).matches("^[0-9a-fA-F]{6}$")) {
|
||||
content = part.substring(6);
|
||||
} else {
|
||||
content = part;
|
||||
}
|
||||
res.append(content);
|
||||
}
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this._title = title;
|
||||
this.save();
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return this._description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this._description = description;
|
||||
this.save();
|
||||
}
|
||||
|
||||
public Boolean getEnabled() {
|
||||
return this._enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(Boolean enabled) {
|
||||
this._enabled = enabled;
|
||||
this.save();
|
||||
}
|
||||
}
|
263
src/main/java/cn/lunadeer/miniplayertitle/XPlayer.java
Normal file
263
src/main/java/cn/lunadeer/miniplayertitle/XPlayer.java
Normal file
@ -0,0 +1,263 @@
|
||||
package cn.lunadeer.miniplayertitle;
|
||||
|
||||
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.ListView;
|
||||
import cn.lunadeer.miniplayertitle.utils.Time;
|
||||
import cn.lunadeer.miniplayertitle.utils.XLogger;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.util.*;
|
||||
|
||||
public class XPlayer {
|
||||
private final Player _player;
|
||||
private Integer _coin;
|
||||
private final Map<Integer, PlayerTitle> _titles;
|
||||
private Integer _current_title_id = -1;
|
||||
|
||||
public XPlayer(Player player) {
|
||||
_player = player;
|
||||
_titles = getTitles(player.getUniqueId());
|
||||
getInfo();
|
||||
checkTitleValid();
|
||||
}
|
||||
|
||||
public PlayerTitle getTitle() {
|
||||
if (_current_title_id == -1) {
|
||||
return null;
|
||||
}
|
||||
return _titles.get(_current_title_id);
|
||||
}
|
||||
|
||||
public void openBackpack(Integer page) {
|
||||
Collection<PlayerTitle> titles = getTitles(_player.getUniqueId()).values();
|
||||
ListView view = ListView.create(5, "/mplt list");
|
||||
view.title("我的称号");
|
||||
for (PlayerTitle title : titles) {
|
||||
int title_id = title.getId();
|
||||
TextComponent idx = Component.text("[" + 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()));
|
||||
line.append(idx)
|
||||
.append(title.getTitle())
|
||||
.append(Component.text("有效期至:" + title.getExpireAtStr()))
|
||||
.append(button);
|
||||
view.add(line);
|
||||
}
|
||||
view.showOn(_player, page);
|
||||
}
|
||||
|
||||
public void updateUsingTitle(Integer title_id) {
|
||||
_current_title_id = title_id;
|
||||
checkTitleValid();
|
||||
String sql = "";
|
||||
sql += "UPDATE mplt_player_info ";
|
||||
sql += "SET using_title_id = " + _current_title_id + ", ";
|
||||
sql += "updated_at = CURRENT_TIMESTAMP ";
|
||||
sql += "WHERE uuid = '" + _player.getUniqueId() + "';";
|
||||
Database.query(sql);
|
||||
updateName();
|
||||
if (_current_title_id == -1) {
|
||||
Notification.info(_player, "成功卸下称号");
|
||||
return;
|
||||
}
|
||||
Notification.info(_player, Component.text("成功使用称号: ").append(_titles.get(_current_title_id).getTitle()));
|
||||
}
|
||||
|
||||
private void checkTitleValid() {
|
||||
if (_current_title_id == -1) {
|
||||
return;
|
||||
}
|
||||
if (!_titles.containsKey(_current_title_id)) {
|
||||
Notification.error(_player, "称号 " + _current_title_id + " 不存在");
|
||||
_current_title_id = -1;
|
||||
return;
|
||||
}
|
||||
PlayerTitle title = _titles.get(_current_title_id);
|
||||
if (title.isExpired()) {
|
||||
Notification.error(_player, title.getTitle().append(Component.text(" 称号已过期")));
|
||||
_current_title_id = -1;
|
||||
}
|
||||
}
|
||||
|
||||
public void set_coin(Integer coin) {
|
||||
_coin = coin;
|
||||
String sql = "";
|
||||
sql += "UPDATE mplt_player_info ";
|
||||
sql += "SET coin = " + coin + ", ";
|
||||
sql += "updated_at = CURRENT_TIMESTAMP ";
|
||||
sql += "WHERE uuid = '" + _player.getUniqueId().toString() + "';";
|
||||
Database.query(sql);
|
||||
}
|
||||
|
||||
public void add_coin(Integer coin) {
|
||||
set_coin(_coin + coin);
|
||||
}
|
||||
|
||||
public Integer get_coin() {
|
||||
return _coin;
|
||||
}
|
||||
|
||||
public static Integer getCoin(Player player) {
|
||||
XPlayer xplayer = new XPlayer(player);
|
||||
return xplayer.get_coin();
|
||||
}
|
||||
|
||||
private static Map<Integer, PlayerTitle> getTitles(UUID uuid) {
|
||||
String sql = "";
|
||||
sql += "SELECT ";
|
||||
sql += "title_id, expire_at ";
|
||||
sql += "FROM mplt_player_title ";
|
||||
sql += "WHERE player_uuid = '" + uuid.toString() + "';";
|
||||
Map<Integer, PlayerTitle> titles = new HashMap<>();
|
||||
try (ResultSet rs = Database.query(sql)) {
|
||||
while (rs != null && rs.next()) {
|
||||
Integer title_id = rs.getInt("title_id");
|
||||
Long expire_at = rs.getLong("expire_at");
|
||||
PlayerTitle title = new PlayerTitle(title_id, uuid, expire_at);
|
||||
titles.put(title_id, title);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
XLogger.err("XPlayer getTitles failed: " + e.getMessage());
|
||||
}
|
||||
return titles;
|
||||
}
|
||||
|
||||
private void getInfo() {
|
||||
UUID uuid = _player.getUniqueId();
|
||||
String sql = "";
|
||||
sql += "SELECT coin, using_title_id ";
|
||||
sql += "FROM mplt_player_info ";
|
||||
sql += "WHERE uuid = '" + uuid.toString() + "';";
|
||||
this._coin = MiniPlayerTitle.config.getDefaultCoin();
|
||||
this._current_title_id = -1;
|
||||
try (ResultSet rs = Database.query(sql)) {
|
||||
if (rs != null && rs.next()) {
|
||||
this._coin = rs.getInt("coin");
|
||||
this._current_title_id = rs.getInt("using_title_id");
|
||||
} else {
|
||||
sql = "";
|
||||
sql += "INSERT INTO mplt_player_info (uuid, coin, using_title_id) VALUES (";
|
||||
sql += "'" + uuid + "', ";
|
||||
sql += this._coin + ", ";
|
||||
sql += this._current_title_id + ");";
|
||||
Database.query(sql);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
XLogger.err("XPlayer getInfo failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void buyTitle(SaleTitle title) {
|
||||
if (title.isSaleExpired() || title.getDays() == 0) {
|
||||
Notification.error(_player, "此称号已停止销售");
|
||||
return;
|
||||
}
|
||||
if (title.getAmount() != -1 && title.getAmount() <= 0) {
|
||||
Notification.error(_player, "此称号已售罄");
|
||||
return;
|
||||
}
|
||||
if (title.getPrice() > _coin) {
|
||||
Notification.error(_player, "你的余额不足");
|
||||
return;
|
||||
}
|
||||
PlayerTitle title_bought = null;
|
||||
if (_titles.containsKey(title.getId())) {
|
||||
if (!_titles.get(title.getId()).isExpired()) {
|
||||
Notification.warn(_player, "你已经拥有此称号");
|
||||
return;
|
||||
}
|
||||
title_bought = _titles.get(title.getId());
|
||||
} else {
|
||||
title_bought = PlayerTitle.create(title.getId(), _player.getUniqueId());
|
||||
}
|
||||
if (title_bought == null) {
|
||||
Notification.error(_player, "购买失败");
|
||||
return;
|
||||
}
|
||||
set_coin(_coin - title.getPrice());
|
||||
if (title.getAmount() > 0) {
|
||||
SaleTitle.setAmount(title.getId(), title.getAmount() - 1);
|
||||
}
|
||||
Notification.info(_player, Component.text("成功购买称号: ").append(title.getTitle()));
|
||||
Notification.info(_player, "花费: " + title.getPrice() + "称号币,余额: " + _coin + "称号币");
|
||||
|
||||
if (title.getDays() == -1) {
|
||||
title_bought.setExpireAt(-1L);
|
||||
} else {
|
||||
Long timestamp = System.currentTimeMillis() + title.getDays() * 24 * 60 * 60 * 1000L;
|
||||
title_bought.setExpireAt((long) Time.getFromTimestamp(timestamp));
|
||||
}
|
||||
Notification.info(_player, title.getTitle().append(Component.text(" 已购买至 " + title_bought.getExpireAtStr())));
|
||||
}
|
||||
|
||||
public void custom(String title_str) {
|
||||
if (this.get_coin() < MiniPlayerTitle.config.getCustomCost()) {
|
||||
Notification.error(this._player, "称号币不足");
|
||||
return;
|
||||
}
|
||||
List<String> exist_titles = new ArrayList<>();
|
||||
for (Title title : Title.all()) {
|
||||
exist_titles.add(title.getTitleContent());
|
||||
}
|
||||
Title title = Title.create(title_str, this._player.getName() + "的自定义称号");
|
||||
if (title == null) {
|
||||
Notification.error(this._player, "创建称号失败");
|
||||
return;
|
||||
}
|
||||
if (title.getTitleContent().length() > MiniPlayerTitle.config.getMaxLength()) {
|
||||
Notification.error(this._player, "称号长度超过限制,最大字符长度(不含颜色代码): " + MiniPlayerTitle.config.getMaxLength());
|
||||
Title.delete(title.getId());
|
||||
return;
|
||||
}
|
||||
if (exist_titles.contains(title.getTitleContent())) {
|
||||
Notification.error(this._player, "已存在同名称号");
|
||||
Title.delete(title.getId());
|
||||
return;
|
||||
}
|
||||
PlayerTitle playerTitle = PlayerTitle.create(title.getId(), this._player.getUniqueId());
|
||||
if (playerTitle == null) {
|
||||
Notification.error(this._player, "创建称号失败");
|
||||
Title.delete(title.getId());
|
||||
return;
|
||||
}
|
||||
playerTitle.setExpireAt(-1L);
|
||||
this.set_coin(this.get_coin() - MiniPlayerTitle.config.getCustomCost());
|
||||
Notification.info(this._player, Component.text("成功创建自定义称号: ").append(title.getTitle()));
|
||||
Notification.info(this._player, "花费: " + MiniPlayerTitle.config.getCustomCost() + "称号币,余额: " + this.get_coin() + "称号币");
|
||||
}
|
||||
|
||||
public void updateName() {
|
||||
PlayerTitle title = getTitle();
|
||||
if (title == null) {
|
||||
Component newDisplayName = Component.text()
|
||||
.append(Component.text("<"))
|
||||
.append(_player.name())
|
||||
.append(Component.text("> ")).build();
|
||||
Component newListName = Component.text()
|
||||
.append(_player.name()).build();
|
||||
_player.displayName(newDisplayName);
|
||||
_player.playerListName(newListName);
|
||||
return;
|
||||
}
|
||||
|
||||
Component titleComponent = title.getTitle();
|
||||
Component newDisplayName = Component.text()
|
||||
.append(titleComponent)
|
||||
.append(Component.text("<"))
|
||||
.append(_player.name())
|
||||
.append(Component.text(">")).build();
|
||||
Component newListName = Component.text()
|
||||
.append(titleComponent)
|
||||
.append(Component.text(" "))
|
||||
.append(_player.name()).build();
|
||||
_player.displayName(newDisplayName);
|
||||
_player.playerListName(newListName);
|
||||
}
|
||||
}
|
@ -0,0 +1,276 @@
|
||||
package cn.lunadeer.miniplayertitle.commands;
|
||||
|
||||
import cn.lunadeer.miniplayertitle.SaleTitle;
|
||||
import cn.lunadeer.miniplayertitle.Title;
|
||||
import cn.lunadeer.miniplayertitle.XPlayer;
|
||||
import cn.lunadeer.miniplayertitle.utils.Notification;
|
||||
import cn.lunadeer.miniplayertitle.utils.XLogger;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class AdminCommands {
|
||||
public static void createTitle(CommandSender sender, String[] args) {
|
||||
if (sender instanceof org.bukkit.entity.Player) {
|
||||
Player player = (org.bukkit.entity.Player) sender;
|
||||
if (!player.isOp()) {
|
||||
XLogger.warn(player, "你没有权限执行该命令");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (args.length != 3) {
|
||||
Notification.warn(sender, "用法: /mplt create <称号名称> <称号描述>");
|
||||
return;
|
||||
}
|
||||
Title title = Title.create(args[1], args[2]);
|
||||
if (title != null) {
|
||||
Notification.info(sender, Component.text("成功创建称号: [" + title.getId() + "]").append(title.getTitle()));
|
||||
} else {
|
||||
Notification.error(sender, "创建称号失败");
|
||||
}
|
||||
}
|
||||
|
||||
public static void deleteTitle(CommandSender sender, String[] args) {
|
||||
if (sender instanceof org.bukkit.entity.Player) {
|
||||
Player player = (org.bukkit.entity.Player) sender;
|
||||
if (!player.isOp()) {
|
||||
XLogger.warn(player, "你没有权限执行该命令");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (args.length != 2) {
|
||||
Notification.warn(sender, "用法: /mplt delete <称号ID>");
|
||||
return;
|
||||
}
|
||||
Title.delete(Integer.parseInt(args[1]));
|
||||
Notification.info(sender, "已删除称号");
|
||||
}
|
||||
|
||||
public static void listAllTitle(CommandSender sender, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if (!player.isOp()) {
|
||||
XLogger.warn(player, "你没有权限执行该命令");
|
||||
return;
|
||||
}
|
||||
}
|
||||
int page = 1;
|
||||
if (args.length == 2) {
|
||||
try {
|
||||
page = Integer.parseInt(args[1]);
|
||||
} catch (Exception e) {
|
||||
Notification.error(sender, "页数格式错误");
|
||||
return;
|
||||
}
|
||||
}
|
||||
Title.listAllTitle(sender, page);
|
||||
}
|
||||
|
||||
public static void setTitleDescription(CommandSender sender, String[] args) {
|
||||
if (sender instanceof org.bukkit.entity.Player) {
|
||||
Player player = (org.bukkit.entity.Player) sender;
|
||||
if (!player.isOp()) {
|
||||
XLogger.warn(player, "你没有权限执行该命令");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (args.length != 3) {
|
||||
Notification.warn(sender, "用法: /mplt setdesc <称号ID> <称号描述>");
|
||||
return;
|
||||
}
|
||||
Title title = new Title(Integer.parseInt(args[1]));
|
||||
title.setDescription(args[2]);
|
||||
Notification.info(sender, "已设置称号描述");
|
||||
}
|
||||
|
||||
public static void setTitleName(CommandSender sender, String[] args) {
|
||||
if (sender instanceof org.bukkit.entity.Player) {
|
||||
Player player = (org.bukkit.entity.Player) sender;
|
||||
if (!player.isOp()) {
|
||||
XLogger.warn(player, "你没有权限执行该命令");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (args.length != 3) {
|
||||
Notification.warn(sender, "用法: /mplt setname <称号ID> <称号名称>");
|
||||
return;
|
||||
}
|
||||
Title title = new Title(Integer.parseInt(args[1]));
|
||||
title.setTitle(args[2]);
|
||||
Notification.info(sender, "已设置称号名称");
|
||||
}
|
||||
|
||||
public static void addShop(CommandSender sender, String[] args) {
|
||||
if (sender instanceof org.bukkit.entity.Player) {
|
||||
Player player = (org.bukkit.entity.Player) sender;
|
||||
if (!player.isOp()) {
|
||||
XLogger.warn(player, "你没有权限执行该命令");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (args.length != 2) {
|
||||
Notification.warn(sender, "用法: /mplt addshop <称号ID>");
|
||||
return;
|
||||
}
|
||||
SaleTitle title = SaleTitle.create(Integer.parseInt(args[1]));
|
||||
if (title == null) {
|
||||
Notification.error(sender, "添加商品失败");
|
||||
} else {
|
||||
Notification.info(sender, "已添加称号到商店, 商品ID: " + title.getSaleId());
|
||||
Notification.info(sender, title.getTitle());
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeShop(CommandSender sender, String[] args) {
|
||||
if (sender instanceof org.bukkit.entity.Player) {
|
||||
Player player = (org.bukkit.entity.Player) sender;
|
||||
if (!player.isOp()) {
|
||||
XLogger.warn(player, "你没有权限执行该命令");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (args.length != 2) {
|
||||
Notification.warn(sender, "用法: /mplt removeshop <商品ID>");
|
||||
return;
|
||||
}
|
||||
SaleTitle.delete(Integer.parseInt(args[1]));
|
||||
Notification.info(sender, "已从商店移除商品");
|
||||
}
|
||||
|
||||
public static void setPrice(CommandSender sender, String[] args) {
|
||||
if (sender instanceof org.bukkit.entity.Player) {
|
||||
Player player = (org.bukkit.entity.Player) sender;
|
||||
if (!player.isOp()) {
|
||||
XLogger.warn(player, "你没有权限执行该命令");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (args.length != 4) {
|
||||
Notification.warn(sender, "用法: /mplt setprice <商品ID> <价格> <天数>(-1为永久)");
|
||||
return;
|
||||
}
|
||||
int price;
|
||||
int days;
|
||||
int id;
|
||||
try {
|
||||
price = Integer.parseInt(args[2]);
|
||||
days = Integer.parseInt(args[3]);
|
||||
id = Integer.parseInt(args[1]);
|
||||
} catch (Exception e) {
|
||||
Notification.error(sender, "价格或天数格式错误");
|
||||
return;
|
||||
}
|
||||
if (price < 0 || days < -1) {
|
||||
Notification.error(sender, "价格或天数格式错误");
|
||||
return;
|
||||
}
|
||||
SaleTitle.setPrice(id, price);
|
||||
SaleTitle.setDays(id, days);
|
||||
Notification.info(sender, "已设置商品价格");
|
||||
}
|
||||
|
||||
public static void setAmount(CommandSender sender, String[] args) {
|
||||
if (sender instanceof org.bukkit.entity.Player) {
|
||||
Player player = (org.bukkit.entity.Player) sender;
|
||||
if (!player.isOp()) {
|
||||
XLogger.warn(player, "你没有权限执行该命令");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (args.length != 3) {
|
||||
Notification.warn(sender, "用法: /mplt setamount <商品ID> <数量>(-1为无限)");
|
||||
return;
|
||||
}
|
||||
int amount;
|
||||
int id;
|
||||
try {
|
||||
amount = Integer.parseInt(args[2]);
|
||||
id = Integer.parseInt(args[1]);
|
||||
} catch (Exception e) {
|
||||
Notification.error(sender, "数量格式错误");
|
||||
return;
|
||||
}
|
||||
SaleTitle.setAmount(id, amount);
|
||||
Notification.info(sender, "已设置商品数量");
|
||||
}
|
||||
|
||||
public static void setSaleEndAt(CommandSender sender, String[] args) {
|
||||
if (sender instanceof org.bukkit.entity.Player) {
|
||||
Player player = (org.bukkit.entity.Player) sender;
|
||||
if (!player.isOp()) {
|
||||
XLogger.warn(player, "你没有权限执行该命令");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (args.length != 3) {
|
||||
Notification.warn(sender, "用法: /mplt setendat <商品ID> <时间YYYYMMDD>(-1为永久)");
|
||||
return;
|
||||
}
|
||||
long time_stamp;
|
||||
try {
|
||||
time_stamp = Long.parseLong(args[2]);
|
||||
} catch (Exception e) {
|
||||
Notification.error(sender, "时间格式错误");
|
||||
return;
|
||||
}
|
||||
|
||||
SaleTitle.setSaleEndAt(Integer.parseInt(args[1]), time_stamp);
|
||||
Notification.info(sender, "已设置商品结束时间");
|
||||
}
|
||||
|
||||
public static void addCoin(CommandSender sender, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if (!player.isOp()) {
|
||||
XLogger.warn(player, "你没有权限执行该命令");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (args.length != 3) {
|
||||
Notification.warn(sender, "用法: /mplt addcoin <玩家> <数量>");
|
||||
return;
|
||||
}
|
||||
Player target = sender.getServer().getPlayer(args[1]);
|
||||
if (target == null) {
|
||||
Notification.error(sender, "玩家不在线");
|
||||
return;
|
||||
}
|
||||
int amount;
|
||||
try {
|
||||
amount = Integer.parseInt(args[2]);
|
||||
} catch (Exception e) {
|
||||
Notification.error(sender, "数量格式错误");
|
||||
return;
|
||||
}
|
||||
new XPlayer(target).add_coin(amount);
|
||||
Notification.info(sender, "已给予玩家 " + target.getName() + " " + amount + " 称号币");
|
||||
}
|
||||
|
||||
public static void setCoin(CommandSender sender, String[] args) {
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
if (!player.isOp()) {
|
||||
XLogger.warn(player, "你没有权限执行该命令");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (args.length != 3) {
|
||||
Notification.warn(sender, "用法: /mplt setcoin <玩家> <数量>");
|
||||
return;
|
||||
}
|
||||
Player target = sender.getServer().getPlayer(args[1]);
|
||||
if (target == null) {
|
||||
Notification.error(sender, "玩家不在线");
|
||||
return;
|
||||
}
|
||||
int amount;
|
||||
try {
|
||||
amount = Integer.parseInt(args[2]);
|
||||
} catch (Exception e) {
|
||||
Notification.error(sender, "数量格式错误");
|
||||
return;
|
||||
}
|
||||
new XPlayer(target).set_coin(amount);
|
||||
Notification.info(sender, "已设置玩家 " + target.getName() + " 称号币为 " + amount);
|
||||
}
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle.commands;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.Common;
|
||||
import cn.lunadeer.miniplayertitle.MiniPlayerTitle;
|
||||
import cn.lunadeer.miniplayertitle.dtos.TitleDTO;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static cn.lunadeer.miniplayertitle.MiniPlayerTitle.usingPapi;
|
||||
|
||||
|
||||
public class Apis {
|
||||
|
||||
public static void updateName(Player player, @Nullable TitleDTO title) {
|
||||
MiniPlayerTitle.instance.setPlayerUsingTitle(player.getUniqueId(), title);
|
||||
if (usingPapi()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Component titleComponent = Component.text("");
|
||||
String titleBukkit = "";
|
||||
|
||||
if (title != null && title.getId() != -1) {
|
||||
titleComponent = title.getTitleColored();
|
||||
titleBukkit = ChatColor.translateAlternateColorCodes('&', title.getTitleColoredBukkit());
|
||||
}
|
||||
|
||||
if (Common.isPaper()) {
|
||||
Component newDisplayName = Component.text()
|
||||
.append(titleComponent)
|
||||
.append(Component.text("<"))
|
||||
.append(player.name())
|
||||
.append(Component.text(">")).build();
|
||||
Component newListName = Component.text()
|
||||
.append(titleComponent)
|
||||
.append(Component.text(" "))
|
||||
.append(player.name()).build();
|
||||
player.displayName(newDisplayName);
|
||||
player.playerListName(newListName);
|
||||
} else {
|
||||
player.setDisplayName(titleBukkit + "<" + player.getName() + ">");
|
||||
player.setPlayerListName(titleBukkit + " " + player.getName());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
package cn.lunadeer.miniplayertitle.commands;
|
||||
|
||||
import cn.lunadeer.miniplayertitle.MiniPlayerTitle;
|
||||
import cn.lunadeer.miniplayertitle.SaleTitle;
|
||||
import cn.lunadeer.miniplayertitle.Shop;
|
||||
import cn.lunadeer.miniplayertitle.XPlayer;
|
||||
import cn.lunadeer.miniplayertitle.utils.Notification;
|
||||
import cn.lunadeer.miniplayertitle.utils.XLogger;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class PlayerCommands {
|
||||
public static void use(CommandSender sender, String[] args) {
|
||||
if (!(sender instanceof org.bukkit.entity.Player)) {
|
||||
XLogger.warn("该命令只能由玩家执行");
|
||||
return;
|
||||
}
|
||||
org.bukkit.entity.Player player = (org.bukkit.entity.Player) sender;
|
||||
if (args.length != 2) {
|
||||
Notification.warn(player, "用法: /mplt use <称号ID>");
|
||||
return;
|
||||
}
|
||||
|
||||
XPlayer xPlayer = new XPlayer(player);
|
||||
Integer title_id = Integer.parseInt(args[1]);
|
||||
xPlayer.updateUsingTitle(title_id);
|
||||
}
|
||||
|
||||
public static void list(CommandSender sender, String[] args) {
|
||||
if (!(sender instanceof org.bukkit.entity.Player)) {
|
||||
XLogger.warn("该命令只能由玩家执行");
|
||||
return;
|
||||
}
|
||||
org.bukkit.entity.Player player = (org.bukkit.entity.Player) sender;
|
||||
int page = 1;
|
||||
if (args.length == 2) {
|
||||
try {
|
||||
page = Integer.parseInt(args[1]);
|
||||
} catch (Exception e) {
|
||||
Notification.error(player, "页数格式错误");
|
||||
return;
|
||||
}
|
||||
}
|
||||
XPlayer xPlayer = new XPlayer(player);
|
||||
xPlayer.openBackpack(page);
|
||||
}
|
||||
|
||||
public static void shop(CommandSender sender, String[] args) {
|
||||
int page = 1;
|
||||
if (args.length == 2) {
|
||||
try {
|
||||
page = Integer.parseInt(args[1]);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
Shop.open(sender, page);
|
||||
}
|
||||
|
||||
public static void buy(CommandSender sender, String[] args) {
|
||||
if (!(sender instanceof org.bukkit.entity.Player)) {
|
||||
XLogger.warn("该命令只能由玩家执行");
|
||||
return;
|
||||
}
|
||||
org.bukkit.entity.Player player = (org.bukkit.entity.Player) sender;
|
||||
if (args.length != 2) {
|
||||
Notification.warn(player, "用法: /mplt buy <称号ID>");
|
||||
return;
|
||||
}
|
||||
XPlayer xPlayer = new XPlayer(player);
|
||||
Integer sale_id = Integer.parseInt(args[1]);
|
||||
SaleTitle saleTitle = Shop.getSaleTitles().get(sale_id);
|
||||
if (saleTitle == null) {
|
||||
Notification.error(player, "该称号不存在");
|
||||
return;
|
||||
}
|
||||
xPlayer.buyTitle(saleTitle);
|
||||
}
|
||||
|
||||
public static void custom(CommandSender sender, String[] args) {
|
||||
if (!(sender instanceof org.bukkit.entity.Player)) {
|
||||
XLogger.warn("该命令只能由玩家执行");
|
||||
return;
|
||||
}
|
||||
Player player = (Player) sender;
|
||||
if (args.length != 2) {
|
||||
Notification.warn(player, "用法: /mplt custom <称号>");
|
||||
return;
|
||||
}
|
||||
if (!MiniPlayerTitle.config.isEnableCustom()) {
|
||||
Notification.error(player, "自定义称号功能已关闭");
|
||||
return;
|
||||
}
|
||||
XPlayer xPlayer = new XPlayer(player);
|
||||
xPlayer.custom(args[1]);
|
||||
}
|
||||
}
|
@ -1,101 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle.commands;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.Notification;
|
||||
import cn.lunadeer.miniplayertitle.dtos.PlayerInfoDTO;
|
||||
import cn.lunadeer.miniplayertitle.dtos.PlayerTitleDTO;
|
||||
import cn.lunadeer.miniplayertitle.dtos.TitleDTO;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
|
||||
public class PlayerManage {
|
||||
|
||||
/**
|
||||
* 给玩家添加称号币
|
||||
* mplt add_coin <玩家名称> <称号币数量>
|
||||
*
|
||||
* @param sender CommandSender
|
||||
* @param args String[]
|
||||
*/
|
||||
public static void addCoin(CommandSender sender, String[] args) {
|
||||
if (!sender.hasPermission("mplt.admin")) return;
|
||||
try {
|
||||
PlayerInfoDTO playerInfo = PlayerInfoDTO.get(args[1]);
|
||||
if (playerInfo == null) {
|
||||
Notification.error(sender, "玩家不存在");
|
||||
return;
|
||||
}
|
||||
if (playerInfo.addCoin(Integer.parseInt(args[2]))) {
|
||||
Notification.info(sender, "成功给玩家 %s 添加 %s 称号币", playerInfo.getLastUseName(), args[2]);
|
||||
Notification.info(sender, "玩家 %s 当前余额 %f 称号币", playerInfo.getLastUseName(), playerInfo.getCoin());
|
||||
} else {
|
||||
Notification.error(sender, "给玩家添加称号币失败,详细错误请查看控制台日志");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Notification.error(sender, "给玩家添加称号币时出错:%s", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 给玩家设置称号币余额
|
||||
* mplt set_coin <玩家名称> <称号币数量>
|
||||
*
|
||||
* @param sender CommandSender
|
||||
* @param args String[]
|
||||
*/
|
||||
public static void setCoin(CommandSender sender, String[] args) {
|
||||
if (!sender.hasPermission("mplt.admin")) return;
|
||||
try {
|
||||
PlayerInfoDTO playerInfo = PlayerInfoDTO.get(args[1]);
|
||||
if (playerInfo == null) {
|
||||
Notification.error(sender, "玩家不存在");
|
||||
return;
|
||||
}
|
||||
if (playerInfo.setCoin(Integer.parseInt(args[2]))) {
|
||||
Notification.info(sender, "成功给玩家 %s 设置 %s 称号币", playerInfo.getLastUseName(), args[2]);
|
||||
Notification.info(sender, "玩家 %s 当前余额 %f 称号币", playerInfo.getLastUseName(), playerInfo.getCoin());
|
||||
} else {
|
||||
Notification.error(sender, "给玩家设置称号币失败,详细错误请查看控制台日志");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Notification.error(sender, "给玩家设置称号币时出错:%s", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接授予玩家某称号
|
||||
* mplt grant_title <玩家名称> <称号> <描述> [天数]
|
||||
*
|
||||
* @param sender CommandSender
|
||||
* @param args String[]
|
||||
*/
|
||||
public static void grantTitle(CommandSender sender, String[] args) {
|
||||
if (!sender.hasPermission("mplt.admin")) return;
|
||||
try {
|
||||
PlayerInfoDTO playerInfo = PlayerInfoDTO.get(args[1]);
|
||||
if (playerInfo == null) {
|
||||
Notification.error(sender, "玩家不存在");
|
||||
return;
|
||||
}
|
||||
String title = args[2];
|
||||
String description = args[3];
|
||||
int days = args.length == 5 ? Integer.parseInt(args[4]) : -1;
|
||||
TitleDTO titleDTO = TitleDTO.create(title, description);
|
||||
if (titleDTO == null) {
|
||||
Notification.error(sender, "创建称号失败,详细错误请查看控制台日志");
|
||||
return;
|
||||
}
|
||||
LocalDateTime expire = days == -1 ? null : LocalDateTime.now().plusDays(days);
|
||||
PlayerTitleDTO playerTitle = PlayerTitleDTO.create(playerInfo.getUuid(), titleDTO, expire);
|
||||
if (playerTitle == null) {
|
||||
Notification.error(sender, "授予玩家称号失败,详细错误请查看控制台日志");
|
||||
return;
|
||||
}
|
||||
playerInfo.setUsingTitle(titleDTO);
|
||||
Notification.info(sender, "成功给玩家 %s 授予称号 %s", playerInfo.getLastUseName(), titleDTO.getTitlePlainText());
|
||||
} catch (Exception e) {
|
||||
Notification.error(sender, "授予玩家称号时出错:%s", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
@ -1,135 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle.commands;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.Notification;
|
||||
import cn.lunadeer.miniplayertitle.dtos.PlayerTitleDTO;
|
||||
import cn.lunadeer.miniplayertitle.dtos.TitleDTO;
|
||||
import cn.lunadeer.miniplayertitle.dtos.TitleShopDTO;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class TitleCard implements Listener {
|
||||
|
||||
public static void getTitleCard(CommandSender sender, String[] args) {
|
||||
try {
|
||||
if (!sender.hasPermission("mplt.admin")) return;
|
||||
if (!(sender instanceof Player)) {
|
||||
Notification.error(sender, "该命令只能由玩家执行");
|
||||
return;
|
||||
}
|
||||
if (args.length != 2) {
|
||||
Notification.warn(sender, "用法: /mplt get_card <销售ID>");
|
||||
return;
|
||||
}
|
||||
int saleId = Integer.parseInt(args[1]);
|
||||
TitleShopDTO titleShop = TitleShopDTO.get(saleId);
|
||||
if (titleShop == null) {
|
||||
Notification.error(sender, "获取销售详情时出现错误");
|
||||
return;
|
||||
}
|
||||
if (titleShop.getDays() == 0) {
|
||||
Notification.error(sender, "不可以生成天数为0的称号卡!");
|
||||
return;
|
||||
}
|
||||
ItemStack card = TitleCard.create(titleShop);
|
||||
Player player = (Player) sender;
|
||||
player.getInventory().addItem(card);
|
||||
Notification.info(player, "成功创建称号卡");
|
||||
} catch (Exception e) {
|
||||
Notification.error(sender, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void useTitleCard(PlayerInteractEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
if (player.getInventory().getItemInMainHand().getType() != Material.NAME_TAG) {
|
||||
return;
|
||||
}
|
||||
ItemStack item = player.getInventory().getItemInMainHand();
|
||||
if (item.getItemMeta() == null || item.getItemMeta().getLore() == null || item.getItemMeta().getLore().size() != 5) {
|
||||
return;
|
||||
}
|
||||
use(player, item);
|
||||
player.getInventory().removeItem(item);
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
private static ItemStack create(@NotNull TitleShopDTO saleInfo) {
|
||||
ItemStack card = new ItemStack(Material.NAME_TAG);
|
||||
TitleDTO title = saleInfo.getTitle();
|
||||
card.editMeta(meta -> {
|
||||
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', title.getTitleColoredBukkit()));
|
||||
meta.setLore(
|
||||
Arrays.asList(
|
||||
"称号ID: " + title.getId(),
|
||||
"使用后获得天数: " + (saleInfo.getDays() == -1 ? "永久" : saleInfo.getDays()),
|
||||
"称号描述: " + title.getDescription(),
|
||||
"",
|
||||
ChatColor.GRAY + "【右键使用】"
|
||||
|
||||
)
|
||||
);
|
||||
});
|
||||
return card;
|
||||
}
|
||||
|
||||
private static void use(@NotNull Player player, @NotNull ItemStack item) {
|
||||
try {
|
||||
if (item.getType() != Material.NAME_TAG) {
|
||||
return;
|
||||
}
|
||||
List<String> lore = item.getLore();
|
||||
if (lore == null || lore.size() != 5) {
|
||||
return;
|
||||
}
|
||||
int titleId = Integer.parseInt(lore.get(0).split(": ")[1]);
|
||||
int day = lore.get(1).split(": ")[1].equals("永久") ? -1 : Integer.parseInt(lore.get(1).split(": ")[1]);
|
||||
|
||||
List<PlayerTitleDTO> playerTitles = PlayerTitleDTO.getAllOf(player.getUniqueId());
|
||||
PlayerTitleDTO had = null;
|
||||
for (PlayerTitleDTO playerTitle : playerTitles) {
|
||||
if (Objects.equals(playerTitle.getTitle().getId(), titleId)) {
|
||||
had = playerTitle;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
TitleDTO title = TitleDTO.get(titleId);
|
||||
if (title == null) {
|
||||
Notification.error(player, "称号不存在");
|
||||
return;
|
||||
}
|
||||
|
||||
if (had == null) {
|
||||
had = PlayerTitleDTO.create(player.getUniqueId(), title, day == -1 ? null : LocalDateTime.now().plusDays(day));
|
||||
if (had == null) {
|
||||
Notification.error(player, "购买称号时出现错误,详情请查看控制台日志");
|
||||
return;
|
||||
}
|
||||
Notification.info(player, Component.text("成功使用称号卡: ").append(had.getTitle().getTitleColored()));
|
||||
} else if (!had.isExpired()) {
|
||||
Notification.warn(player, "你已拥有此称号,在过期前无法使用称号卡");
|
||||
} else {
|
||||
had.setExpireAt(day == -1 ? null : LocalDateTime.now().plusDays(day));
|
||||
Notification.info(player, Component.text("成功续续期称号: ").append(had.getTitle().getTitleColored()));
|
||||
}
|
||||
TitleManage.useTitle(player, new String[]{"use_title", String.valueOf(had.getId())});
|
||||
} catch (Exception e) {
|
||||
Notification.error(player, "使用称号卡时出现错误: %s", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,246 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle.commands;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.Notification;
|
||||
import cn.lunadeer.miniplayertitle.MiniPlayerTitle;
|
||||
import cn.lunadeer.miniplayertitle.dtos.PlayerInfoDTO;
|
||||
import cn.lunadeer.miniplayertitle.dtos.PlayerTitleDTO;
|
||||
import cn.lunadeer.miniplayertitle.dtos.TitleDTO;
|
||||
import cn.lunadeer.miniplayertitle.tuis.AllTitles;
|
||||
import cn.lunadeer.miniplayertitle.tuis.MyTitles;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import static cn.lunadeer.miniplayertitle.commands.Apis.updateName;
|
||||
|
||||
public class TitleManage {
|
||||
/**
|
||||
* 创建称号
|
||||
* mplt create_title <称号名称> [称号描述]
|
||||
*
|
||||
* @param sender CommandSender
|
||||
* @param args String[]
|
||||
*/
|
||||
public static void createTitle(CommandSender sender, String[] args) {
|
||||
if (!sender.hasPermission("mplt.admin")) return;
|
||||
if (args.length < 2) {
|
||||
Notification.warn(sender, "用法: /mplt create_title <称号名称> [称号描述]");
|
||||
return;
|
||||
}
|
||||
TitleDTO title = TitleDTO.create(args[1], args.length == 3 ? args[2] : "这是一个管理员创建的称号");
|
||||
if (title != null) {
|
||||
Notification.info(sender, Component.text("成功创建称号: [" + title.getId() + "]").append(title.getTitleColored()));
|
||||
AllTitles.show(sender, new String[]{"all_titles"});
|
||||
} else {
|
||||
Notification.error(sender, "创建称号失败,具体请查看控制台日志");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除称号
|
||||
* mplt delete_title <称号ID> [页码]
|
||||
*
|
||||
* @param sender CommandSender
|
||||
* @param args String[]
|
||||
*/
|
||||
public static void deleteTitle(CommandSender sender, String[] args) {
|
||||
try {
|
||||
if (!sender.hasPermission("mplt.admin")) return;
|
||||
if (args.length < 2) {
|
||||
Notification.warn(sender, "用法: /mplt delete_title <称号ID>");
|
||||
return;
|
||||
}
|
||||
TitleDTO title = TitleDTO.get(Integer.parseInt(args[1]));
|
||||
if (title == null) {
|
||||
Notification.error(sender, "称号不存在");
|
||||
return;
|
||||
}
|
||||
boolean success = title.delete();
|
||||
if (!success) {
|
||||
Notification.error(sender, "删除称号失败,具体请查看控制台日志");
|
||||
return;
|
||||
}
|
||||
Notification.info(sender, "已删除称号");
|
||||
if (args.length == 3) {
|
||||
int page = Integer.parseInt(args[2]);
|
||||
AllTitles.show(sender, new String[]{"all_titles", String.valueOf(page)});
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Notification.error(sender, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置称号名称
|
||||
* mplt edit_title_name <称号ID> <称号名称>
|
||||
*
|
||||
* @param sender CommandSender
|
||||
* @param args String[]
|
||||
*/
|
||||
public static void editTitleName(CommandSender sender, String[] args) {
|
||||
try {
|
||||
if (!sender.hasPermission("mplt.admin")) return;
|
||||
if (args.length != 3) {
|
||||
Notification.warn(sender, "用法: /mplt set_title <称号ID> <称号名称>");
|
||||
return;
|
||||
}
|
||||
TitleDTO title = TitleDTO.get(Integer.parseInt(args[1]));
|
||||
if (title == null) {
|
||||
Notification.error(sender, "称号不存在");
|
||||
return;
|
||||
}
|
||||
boolean success = title.updateTitle(args[2]);
|
||||
if (success) {
|
||||
Notification.info(sender, "已更新称号名称");
|
||||
} else {
|
||||
Notification.error(sender, "更新称号名称失败,具体请查看控制台日志");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Notification.error(sender, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置称号描述
|
||||
* mplt edit_title_desc <称号ID> <称号描述>
|
||||
*
|
||||
* @param sender CommandSender
|
||||
* @param args String[]
|
||||
*/
|
||||
public static void editTitleDescription(CommandSender sender, String[] args) {
|
||||
try {
|
||||
if (!sender.hasPermission("mplt.admin")) return;
|
||||
if (args.length != 3) {
|
||||
Notification.warn(sender, "用法: /mplt set_desc <称号ID> <称号描述>");
|
||||
return;
|
||||
}
|
||||
TitleDTO title = TitleDTO.get(Integer.parseInt(args[1]));
|
||||
if (title == null) {
|
||||
Notification.error(sender, "称号不存在");
|
||||
return;
|
||||
}
|
||||
boolean success = title.updateDescription(args[2]);
|
||||
if (success) {
|
||||
Notification.info(sender, "已更新称号描述");
|
||||
} else {
|
||||
Notification.error(sender, "更新称号描述失败,具体请查看控制台日志");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Notification.error(sender, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用称号
|
||||
* mplt use_title <背包ID> [页码]
|
||||
*
|
||||
* @param sender CommandSender
|
||||
* @param args String[]
|
||||
*/
|
||||
public static void useTitle(CommandSender sender, String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
Notification.error(sender, "该命令只能由玩家执行");
|
||||
return;
|
||||
}
|
||||
if (!sender.hasPermission("mplt.command")) return;
|
||||
if (args.length < 2) {
|
||||
Notification.warn(sender, "用法: /mplt use_title <背包ID> [页码]");
|
||||
return;
|
||||
}
|
||||
Player player = (Player) sender;
|
||||
PlayerInfoDTO playerInfo = PlayerInfoDTO.get((player).getUniqueId());
|
||||
if (playerInfo == null) {
|
||||
Notification.error(sender, "获取玩家信息时出现错误");
|
||||
return;
|
||||
}
|
||||
int id = Integer.parseInt(args[1]);
|
||||
if (id == -1) {
|
||||
Notification.info(sender, "已卸下称号");
|
||||
playerInfo.setUsingTitle(null);
|
||||
updateName(player, null);
|
||||
} else {
|
||||
PlayerTitleDTO title = PlayerTitleDTO.get(id);
|
||||
if (title == null) {
|
||||
Notification.error(sender, "称号不存在");
|
||||
return;
|
||||
}
|
||||
if (!title.getPlayerUuid().equals(player.getUniqueId())) {
|
||||
Notification.error(sender, "该称号不属于你");
|
||||
return;
|
||||
}
|
||||
if (title.isExpired()) {
|
||||
Notification.error(sender, "称号 %s 已过期", title.getTitle().getTitlePlainText());
|
||||
playerInfo.setUsingTitle(null);
|
||||
updateName(player, null);
|
||||
return;
|
||||
}
|
||||
boolean success = playerInfo.setUsingTitle(title.getTitle());
|
||||
if (success) {
|
||||
updateName((Player) sender, title.getTitle());
|
||||
Notification.info(sender, "已使用称号");
|
||||
} else {
|
||||
Notification.error(sender, "使用称号失败,具体请查看控制台日志");
|
||||
}
|
||||
}
|
||||
|
||||
if (args.length == 3) {
|
||||
int page = Integer.parseInt(args[2]);
|
||||
MyTitles.show(sender, new String[]{"my_titles", String.valueOf(page)});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建自定义称号
|
||||
* mplt custom_title <称号内容>
|
||||
*
|
||||
* @param sender CommandSender
|
||||
* @param args String[]
|
||||
*/
|
||||
public static void customTitle(CommandSender sender, String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
Notification.error(sender, "该命令只能由玩家执行");
|
||||
return;
|
||||
}
|
||||
if (!sender.hasPermission("mplt.custom") && !sender.hasPermission("mplt.admin")) {
|
||||
Notification.error(sender, "你没有权限使用该命令");
|
||||
return;
|
||||
}
|
||||
Player player = (Player) sender;
|
||||
if (!MiniPlayerTitle.config.isEnableCustom()) {
|
||||
Notification.error(sender, "自定义称号功能已关闭");
|
||||
return;
|
||||
}
|
||||
PlayerInfoDTO playerInfo = PlayerInfoDTO.get(player.getUniqueId());
|
||||
if (playerInfo == null) {
|
||||
Notification.error(sender, "获取玩家信息时出现错误");
|
||||
return;
|
||||
}
|
||||
if (MiniPlayerTitle.config.getCustomCost() > playerInfo.getCoin()) {
|
||||
Notification.error(sender, "称号币不足");
|
||||
return;
|
||||
}
|
||||
if (args.length < 2) {
|
||||
Notification.warn(sender, "用法: /mplt custom_title <称号>");
|
||||
return;
|
||||
}
|
||||
TitleDTO title = TitleDTO.create(args[1], player.getName() + "的自定义称号");
|
||||
if (title == null) {
|
||||
Notification.error(sender, "创建称号失败,具体请查看控制台日志");
|
||||
return;
|
||||
}
|
||||
if (title.getTitlePlainText().length() > MiniPlayerTitle.config.getMaxLength()) {
|
||||
Notification.error(sender, "称号长度超过限制");
|
||||
title.delete();
|
||||
return;
|
||||
}
|
||||
PlayerTitleDTO created_rec = PlayerTitleDTO.create(player.getUniqueId(), title, null);
|
||||
if (created_rec == null) {
|
||||
Notification.error(sender, "创建称号记录失败,具体请查看控制台日志");
|
||||
title.delete();
|
||||
return;
|
||||
}
|
||||
playerInfo.setCoin(playerInfo.getCoin() - MiniPlayerTitle.config.getCustomCost());
|
||||
Notification.info(sender, "成功创建自定义称号");
|
||||
MyTitles.show(sender, new String[]{"my_titles"});
|
||||
}
|
||||
}
|
@ -1,202 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle.commands;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.Notification;
|
||||
import cn.lunadeer.miniplayertitle.dtos.PlayerInfoDTO;
|
||||
import cn.lunadeer.miniplayertitle.dtos.PlayerTitleDTO;
|
||||
import cn.lunadeer.miniplayertitle.dtos.TitleDTO;
|
||||
import cn.lunadeer.miniplayertitle.dtos.TitleShopDTO;
|
||||
import cn.lunadeer.miniplayertitle.tuis.MyTitles;
|
||||
import cn.lunadeer.miniplayertitle.tuis.SaleInfo;
|
||||
import cn.lunadeer.miniplayertitle.tuis.Shop;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import static cn.lunadeer.miniplayertitle.tuis.Apis.getArgPage;
|
||||
import static cn.lunadeer.miniplayertitle.tuis.Apis.getLastArgsPage;
|
||||
|
||||
public class TitleShopSale {
|
||||
|
||||
/**
|
||||
* 设置商品信息
|
||||
* mplt set_sale <price|days|amount|end_at|more_end_at|less_end_at> <商品ID> <值> [页数]
|
||||
*
|
||||
* @param sender CommandSender
|
||||
* @param args String[]
|
||||
*/
|
||||
public static void setSale(CommandSender sender, String[] args) {
|
||||
if (!sender.hasPermission("mplt.admin")) return;
|
||||
TitleShopDTO titleShop = TitleShopDTO.get(Integer.valueOf(args[2]));
|
||||
if (titleShop == null) {
|
||||
Notification.error(sender, "获取详情时出现错误,详情请查看控制台日志");
|
||||
return;
|
||||
}
|
||||
boolean success;
|
||||
switch (args[1]) {
|
||||
case "price":
|
||||
success = titleShop.setPrice(Double.parseDouble(args[3]));
|
||||
break;
|
||||
case "days":
|
||||
success = titleShop.setDays(Integer.parseInt(args[3]));
|
||||
break;
|
||||
case "amount":
|
||||
success = titleShop.setAmount(Integer.parseInt(args[3]));
|
||||
break;
|
||||
case "end_at":
|
||||
String[] date = args[3].split(":");
|
||||
int year = Integer.parseInt(date[0]);
|
||||
int month = Integer.parseInt(date[1]);
|
||||
int day = Integer.parseInt(date[2]);
|
||||
success = titleShop.setSaleEndAt(year, month, day);
|
||||
break;
|
||||
case "more_end_at":
|
||||
int days = Integer.parseInt(args[3]);
|
||||
success = titleShop.setSaleEndAt(titleShop.getSaleEndAt().plusDays(days));
|
||||
break;
|
||||
case "less_end_at":
|
||||
int days2 = Integer.parseInt(args[3]);
|
||||
success = titleShop.setSaleEndAt(titleShop.getSaleEndAt().minusDays(days2));
|
||||
break;
|
||||
default:
|
||||
Notification.warn(sender, "用法: /mplt set_sale <price|days|amount|end_at|end_at_y|end_at_m|end_at_d> <商品ID> <值> [页数]");
|
||||
return;
|
||||
}
|
||||
if (!success) {
|
||||
Notification.error(sender, "设置商品信息时出现错误,详情请查看控制台日志");
|
||||
}
|
||||
if (args.length == 5) {
|
||||
int page = getLastArgsPage(args);
|
||||
SaleInfo.show(sender, new String[]{"sale_info", args[2], String.valueOf(page)});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建商品
|
||||
* mplt create_sale <称号ID>
|
||||
*
|
||||
* @param sender CommandSender
|
||||
* @param args String[]
|
||||
*/
|
||||
public static void createSale(CommandSender sender, String[] args) {
|
||||
if (!sender.hasPermission("mplt.admin")) return;
|
||||
TitleDTO title = TitleDTO.get(Integer.parseInt(args[1]));
|
||||
if (title == null) {
|
||||
Notification.error(sender, "获取称号详情时出现错误,详情请查看控制台日志");
|
||||
return;
|
||||
}
|
||||
TitleShopDTO sale = TitleShopDTO.create(title);
|
||||
if (sale == null) {
|
||||
Notification.error(sender, "创建商品时出现错误,详情请查看控制台日志");
|
||||
return;
|
||||
}
|
||||
Notification.info(sender, "已创建称号商品");
|
||||
if (sender instanceof Player) {
|
||||
SaleInfo.show(sender, new String[]{"sale_info", String.valueOf(sale.getId())});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除商品
|
||||
* mplt delete_sale <商品ID> [页数]
|
||||
*
|
||||
* @param sender CommandSender
|
||||
* @param args String[]
|
||||
*/
|
||||
public static void deleteSale(CommandSender sender, String[] args) {
|
||||
if (!sender.hasPermission("mplt.admin")) return;
|
||||
TitleShopDTO titleShop = TitleShopDTO.get(Integer.valueOf(args[1]));
|
||||
if (titleShop == null) {
|
||||
Notification.error(sender, "获取详情时出现错误");
|
||||
return;
|
||||
}
|
||||
boolean success = titleShop.delete();
|
||||
if (success) {
|
||||
Notification.info(sender, "已删除商品");
|
||||
} else {
|
||||
Notification.error(sender, "删除商品时出现错误,详情请查看控制台日志");
|
||||
}
|
||||
|
||||
if (args.length == 3) {
|
||||
Shop.show(sender, new String[]{"shop"});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 购买商品
|
||||
* mplt buy_sale <商品ID>
|
||||
*
|
||||
* @param sender CommandSender
|
||||
* @param args String[]
|
||||
*/
|
||||
public static void buySale(CommandSender sender, String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
Notification.error(sender, "该命令只能由玩家执行");
|
||||
return;
|
||||
}
|
||||
if (!sender.hasPermission("mplt.command")) return;
|
||||
Player player = (Player) sender;
|
||||
PlayerInfoDTO playerInfo = PlayerInfoDTO.get(player.getUniqueId());
|
||||
if (playerInfo == null) {
|
||||
Notification.error(player, "获取玩家信息时出现错误,详情请查看控制台日志");
|
||||
return;
|
||||
}
|
||||
TitleShopDTO titleShop = TitleShopDTO.get(Integer.valueOf(args[1]));
|
||||
if (titleShop == null) {
|
||||
Notification.error(player, "获取详情时出现错误,详情请查看控制台日志");
|
||||
return;
|
||||
}
|
||||
|
||||
if (titleShop.isExpired() || titleShop.getDays() == 0) {
|
||||
Notification.error(player, "此称号已停止销售");
|
||||
return;
|
||||
}
|
||||
if (titleShop.getAmount() != -1 && titleShop.getAmount() <= 0) {
|
||||
Notification.error(player, "此称号已售罄");
|
||||
return;
|
||||
}
|
||||
if (titleShop.getPrice() > playerInfo.getCoin()) {
|
||||
Notification.error(player, "你的余额不足");
|
||||
return;
|
||||
}
|
||||
|
||||
List<PlayerTitleDTO> playerTitles = PlayerTitleDTO.getAllOf(player.getUniqueId());
|
||||
PlayerTitleDTO had = null;
|
||||
for (PlayerTitleDTO playerTitle : playerTitles) {
|
||||
if (Objects.equals(playerTitle.getTitle().getId(), titleShop.getTitle().getId())) {
|
||||
had = playerTitle;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (had == null) {
|
||||
had = PlayerTitleDTO.create(player.getUniqueId(), titleShop.getTitle(), titleShop.getDays() == -1 ? null : LocalDateTime.now().plusDays(titleShop.getDays()));
|
||||
if (had == null) {
|
||||
Notification.error(player, "购买称号时出现错误,详情请查看控制台日志");
|
||||
return;
|
||||
}
|
||||
if (titleShop.getAmount() >= 1) {
|
||||
titleShop.setAmount(titleShop.getAmount() - 1);
|
||||
}
|
||||
playerInfo.setCoin(playerInfo.getCoin() - titleShop.getPrice());
|
||||
Notification.info(player, Component.text("成功购买称号: ").append(had.getTitle().getTitleColored()));
|
||||
} else if (!had.isExpired()) {
|
||||
Notification.warn(player, "你已拥有此称号,在过期前无法再次购买");
|
||||
} else {
|
||||
had.setExpireAt(titleShop.getDays() == -1 ? null : LocalDateTime.now().plusDays(titleShop.getDays()));
|
||||
if (titleShop.getAmount() >= 1) {
|
||||
titleShop.setAmount(titleShop.getAmount() - 1);
|
||||
}
|
||||
playerInfo.setCoin(playerInfo.getCoin() - titleShop.getPrice());
|
||||
Notification.info(player, Component.text("成功续续期称号: ").append(had.getTitle().getTitleColored()));
|
||||
}
|
||||
|
||||
TitleManage.useTitle(player, new String[]{"use_title", String.valueOf(had.getId())});
|
||||
int page = getArgPage(args, 3);
|
||||
MyTitles.show(sender, new String[]{"my_titles", String.valueOf(page)});
|
||||
}
|
||||
|
||||
}
|
@ -1,178 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle.dtos;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.VaultConnect.VaultConnect;
|
||||
import cn.lunadeer.minecraftpluginutils.XLogger;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.DatabaseManager;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.Field;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.FieldType;
|
||||
import cn.lunadeer.miniplayertitle.MiniPlayerTitle;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static cn.lunadeer.minecraftpluginutils.databse.DatabaseManager.handleDatabaseError;
|
||||
|
||||
public class PlayerInfoDTO {
|
||||
private final Field uuid = new Field("uuid", FieldType.UUID);
|
||||
private final Field coin = new Field("coin_d", FieldType.DOUBLE);
|
||||
private TitleDTO using_title;
|
||||
private final Field last_use_name = new Field("last_use_name", FieldType.STRING);
|
||||
|
||||
public static PlayerInfoDTO get(UUID uuid) {
|
||||
String sql = "";
|
||||
sql = "SELECT uuid, coin_d, using_title_id, last_use_name FROM mplt_player_info WHERE uuid = ?;";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, uuid)) {
|
||||
if (rs.next()) return getPlayerInfoDTO(rs);
|
||||
else return null;
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("获取玩家信息失败", e, sql);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static PlayerInfoDTO get(Player player) {
|
||||
PlayerInfoDTO info = get(player.getUniqueId());
|
||||
if (info == null) {
|
||||
info = create(player);
|
||||
if (info == null) {
|
||||
XLogger.err("创建玩家信息时失败");
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
info = updateName(player);
|
||||
if (info == null) {
|
||||
XLogger.err("更新玩家名称时失败");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
public static PlayerInfoDTO get(String name) {
|
||||
String sql = "";
|
||||
sql = "SELECT uuid, coin_d, using_title_id, last_use_name FROM mplt_player_info WHERE last_use_name = ?;";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, name)) {
|
||||
if (rs.next()) return getPlayerInfoDTO(rs);
|
||||
else return null;
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("获取玩家信息失败", e, sql);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static PlayerInfoDTO create(Player player) {
|
||||
String sql = "";
|
||||
sql = "INSERT INTO mplt_player_info (uuid, coin_d, last_use_name) " +
|
||||
"VALUES (?, ?, ?) " +
|
||||
"ON CONFLICT DO NOTHING;";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, player.getUniqueId(), MiniPlayerTitle.config.getDefaultCoin(), player.getName())) {
|
||||
return get(player.getUniqueId());
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("创建玩家信息失败", e, sql);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static PlayerInfoDTO updateName(Player player) {
|
||||
String sql = "";
|
||||
sql = "UPDATE mplt_player_info SET last_use_name = ? WHERE uuid = ?;";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, player.getName(), player.getUniqueId())) {
|
||||
return get(player.getUniqueId());
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("更新玩家名称失败", e, sql);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static PlayerInfoDTO getPlayerInfoDTO(ResultSet rs) throws SQLException {
|
||||
PlayerInfoDTO playerInfoDTO = new PlayerInfoDTO();
|
||||
playerInfoDTO.uuid.value = UUID.fromString(rs.getString("uuid"));
|
||||
playerInfoDTO.coin.value = rs.getDouble("coin_d");
|
||||
playerInfoDTO.using_title = TitleDTO.get(rs.getInt("using_title_id"));
|
||||
playerInfoDTO.last_use_name.value = rs.getString("last_use_name");
|
||||
return playerInfoDTO;
|
||||
}
|
||||
|
||||
public Double getCoin() {
|
||||
if (MiniPlayerTitle.config.isExternalEco()) {
|
||||
Player player = MiniPlayerTitle.instance.getServer().getPlayer(getUuid());
|
||||
return VaultConnect.instance.getBalance(player);
|
||||
}
|
||||
return (Double) coin.value;
|
||||
}
|
||||
|
||||
public TitleDTO getUsingTitle() {
|
||||
return using_title;
|
||||
}
|
||||
|
||||
public String getLastUseName() {
|
||||
return (String) last_use_name.value;
|
||||
}
|
||||
|
||||
public UUID getUuid() {
|
||||
return (UUID) uuid.value;
|
||||
}
|
||||
|
||||
public boolean setUsingTitle(@Nullable TitleDTO title) {
|
||||
String sql = "";
|
||||
sql = "UPDATE mplt_player_info SET using_title_id = ? WHERE uuid = ?;";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, title == null ? -1 : title.getId(), getUuid())) {
|
||||
this.using_title = title == null ? TitleDTO.get(-1) : title;
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("设置玩家使用称号失败", e, sql);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean addCoin(double coin) {
|
||||
if (MiniPlayerTitle.config.isExternalEco()) {
|
||||
Player player = MiniPlayerTitle.instance.getServer().getPlayer(getUuid());
|
||||
VaultConnect.instance.depositPlayer(player, coin);
|
||||
return true;
|
||||
}
|
||||
return setCoin(getCoin() + coin);
|
||||
}
|
||||
|
||||
public boolean setCoin(double coin) {
|
||||
if (MiniPlayerTitle.config.isExternalEco()) {
|
||||
Player player = MiniPlayerTitle.instance.getServer().getPlayer(getUuid());
|
||||
double balance = VaultConnect.instance.getBalance(player);
|
||||
if (balance < coin) {
|
||||
VaultConnect.instance.depositPlayer(player, coin - balance);
|
||||
} else {
|
||||
VaultConnect.instance.withdrawPlayer(player, balance - coin);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
String sql = "";
|
||||
sql = "UPDATE mplt_player_info SET coin_d = ? WHERE uuid = ?;";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, coin, getUuid())) {
|
||||
this.coin.value = coin;
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("设置玩家金币失败", e, sql);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static List<String> playerNameList() {
|
||||
String sql = "";
|
||||
sql = "SELECT last_use_name FROM mplt_player_info;";
|
||||
List<String> names = new ArrayList<>();
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql)) {
|
||||
while (rs.next()) {
|
||||
names.add(rs.getString("last_use_name"));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("获取玩家名称列表失败", e, sql);
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
}
|
@ -1,141 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle.dtos;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.databse.DatabaseManager;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.Field;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.FieldType;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static cn.lunadeer.minecraftpluginutils.databse.DatabaseManager.handleDatabaseError;
|
||||
|
||||
public class PlayerTitleDTO {
|
||||
private final Field id = new Field("id", FieldType.INT);
|
||||
private final Field player_uuid = new Field("player_uuid", FieldType.UUID);
|
||||
private TitleDTO title;
|
||||
private LocalDateTime expire_at;
|
||||
|
||||
public Integer getId() {
|
||||
return (Integer) id.value;
|
||||
}
|
||||
|
||||
public TitleDTO getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public UUID getPlayerUuid() {
|
||||
return (UUID) player_uuid.value;
|
||||
}
|
||||
|
||||
public LocalDateTime getExpireAt() {
|
||||
return expire_at;
|
||||
}
|
||||
|
||||
public boolean setExpireAt(LocalDateTime dateTime) {
|
||||
String sql = "";
|
||||
if (dateTime == null) {
|
||||
sql += "UPDATE mplt_player_title SET expire_at_y = -1, expire_at_m = -1, expire_at_d = -1 WHERE id = " + getId() + ";";
|
||||
} else {
|
||||
sql += "UPDATE mplt_player_title SET expire_at_y = " + dateTime.getYear() + ", expire_at_m = " + dateTime.getMonthValue() + ", expire_at_d = " + dateTime.getDayOfMonth() + " WHERE id = " + getId() + ";";
|
||||
}
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql)) {
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("设置玩家称号过期时间失败", e, sql);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static PlayerTitleDTO create(UUID player_uuid, TitleDTO title, @Nullable LocalDateTime expire_at) {
|
||||
String sql = "";
|
||||
sql += "INSERT INTO mplt_player_title (player_uuid, title_id, expire_at_y, expire_at_m, expire_at_d) ";
|
||||
|
||||
if (expire_at == null) {
|
||||
sql += "VALUES (?, ? , -1, -1, -1) ";
|
||||
} else {
|
||||
sql += "VALUES (?, ?, " + expire_at.getYear() + ", " + expire_at.getMonthValue() + ", " + expire_at.getDayOfMonth() + ") ";
|
||||
}
|
||||
sql += "RETURNING " +
|
||||
"id, player_uuid, title_id, expire_at_y, expire_at_m, expire_at_d;";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, player_uuid, title.getId())) {
|
||||
if (rs.next()) {
|
||||
return getRs(rs);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("创建玩家称号失败", e, sql);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static PlayerTitleDTO get(Integer id) {
|
||||
String sql = "";
|
||||
sql += "SELECT id, player_uuid, title_id, expire_at_y, expire_at_m, expire_at_d FROM mplt_player_title " +
|
||||
"WHERE id = ?;";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, id)) {
|
||||
if (rs.next()) {
|
||||
return getRs(rs);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("获取玩家称号失败", e, sql);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static PlayerTitleDTO get(UUID player, Integer title) {
|
||||
String sql = "";
|
||||
sql += "SELECT id, player_uuid, title_id, expire_at_y, expire_at_m, expire_at_d FROM mplt_player_title " +
|
||||
"WHERE player_uuid = ? AND title_id = ?;";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, player, title)) {
|
||||
if (rs.next()) {
|
||||
return getRs(rs);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("获取玩家称号失败", e, sql);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static PlayerTitleDTO getRs(ResultSet rs) throws SQLException {
|
||||
PlayerTitleDTO playerTitle = new PlayerTitleDTO();
|
||||
playerTitle.id.value = rs.getInt("id");
|
||||
playerTitle.player_uuid.value = UUID.fromString(rs.getString("player_uuid"));
|
||||
playerTitle.title = TitleDTO.get(rs.getInt("title_id"));
|
||||
int y = rs.getInt("expire_at_y");
|
||||
int m = rs.getInt("expire_at_m");
|
||||
int d = rs.getInt("expire_at_d");
|
||||
if (y == -1 && m == -1 && d == -1) {
|
||||
playerTitle.expire_at = null;
|
||||
} else {
|
||||
playerTitle.expire_at = LocalDateTime.of(y, m, d, 0, 0, 0);
|
||||
}
|
||||
return playerTitle;
|
||||
}
|
||||
|
||||
public static List<PlayerTitleDTO> getAllOf(UUID player_uuid) {
|
||||
String sql = "";
|
||||
sql += "SELECT id, player_uuid, title_id, expire_at_y, expire_at_m, expire_at_d FROM mplt_player_title " +
|
||||
"WHERE player_uuid = ?;";
|
||||
List<PlayerTitleDTO> playerTitles = new ArrayList<>();
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, player_uuid)) {
|
||||
while (rs.next()) {
|
||||
playerTitles.add(getRs(rs));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("获取玩家称号失败", e, sql);
|
||||
}
|
||||
return playerTitles;
|
||||
}
|
||||
|
||||
public boolean isExpired() {
|
||||
if (expire_at == null) {
|
||||
return false;
|
||||
} else {
|
||||
return expire_at.isBefore(LocalDateTime.now());
|
||||
}
|
||||
}
|
||||
}
|
@ -1,145 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle.dtos;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.ColorParser;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.DatabaseManager;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.Field;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.FieldType;
|
||||
import cn.lunadeer.miniplayertitle.MiniPlayerTitle;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.lunadeer.minecraftpluginutils.databse.DatabaseManager.handleDatabaseError;
|
||||
|
||||
public class TitleDTO {
|
||||
private final Field id = new Field("id", FieldType.INT);
|
||||
private final Field title = new Field("title", FieldType.STRING);
|
||||
private final Field description = new Field("description", FieldType.STRING);
|
||||
|
||||
|
||||
public static TitleDTO get(int id) {
|
||||
String sql = "";
|
||||
sql += "SELECT id, title, description FROM mplt_title WHERE id = " + id + ";";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql)) {
|
||||
if (rs.next()) return getTitleDTO(rs);
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("获取称号失败", e, sql);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static TitleDTO create(String title, String description) {
|
||||
String sql = "";
|
||||
sql += "INSERT INTO mplt_title (title, description) " +
|
||||
"VALUES (?, ?) " +
|
||||
"RETURNING " +
|
||||
"id, title, description;";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, title, description)) {
|
||||
if (rs.next()) return getTitleDTO(rs);
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("创建称号失败", e, sql);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean delete() {
|
||||
String updateSql = "UPDATE mplt_player_info SET using_title_id = -1 WHERE using_title_id = ?;";
|
||||
String deleteSql = "DELETE FROM mplt_title WHERE id = ?;";
|
||||
try {
|
||||
// 执行更新操作
|
||||
DatabaseManager.instance.query(updateSql, getId());
|
||||
// 执行删除操作
|
||||
DatabaseManager.instance.query(deleteSql, getId());
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("删除称号失败", e, updateSql + " " + deleteSql);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<TitleDTO> getAll() {
|
||||
String sql = "";
|
||||
sql += "SELECT id, title, description FROM mplt_title;";
|
||||
List<TitleDTO> titleDTOs = new ArrayList<>();
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql)) {
|
||||
while (rs.next()) {
|
||||
titleDTOs.add(getTitleDTO(rs));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("获取称号列表失败", e, sql);
|
||||
}
|
||||
return titleDTOs;
|
||||
}
|
||||
|
||||
public TextComponent getTitleColored() {
|
||||
String with_pre_suf = "&#ffffff" + MiniPlayerTitle.config.getPrefix() + getTitleRaw() + "&#ffffff" + MiniPlayerTitle.config.getSuffix();
|
||||
return ColorParser.getComponentType(with_pre_suf);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取称号的颜色化字符串
|
||||
* 需要使用 ChatColor.translateAlternateColorCodes 方法对返回字符串进行处理
|
||||
* &#FFFFFF -> &x&f&f&f&f&f
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String getTitleColoredBukkit() {
|
||||
String with_pre_suf = "&#ffffff" + MiniPlayerTitle.config.getPrefix() + getTitleRaw() + "&#ffffff" + MiniPlayerTitle.config.getSuffix();
|
||||
return ColorParser.getBukkitType(with_pre_suf);
|
||||
}
|
||||
|
||||
public String getTitlePlainText() {
|
||||
return ColorParser.getPlainText(getTitleRaw());
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return (Integer) this.id.value;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return (String) this.description.value;
|
||||
}
|
||||
|
||||
public String getTitleRaw() {
|
||||
return (String) this.title.value;
|
||||
}
|
||||
|
||||
public boolean updateTitle(String title) {
|
||||
String sql = "";
|
||||
sql += "UPDATE mplt_title SET title = ? WHERE id = ?;";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, title, getId())) {
|
||||
if (rs != null && rs.next()) {
|
||||
this.title.value = title;
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("更新称号失败", e, sql);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean updateDescription(String description) {
|
||||
String sql = "";
|
||||
sql += "UPDATE mplt_title SET description = ? WHERE id = ?;";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, description, getId())) {
|
||||
if (rs != null && rs.next()) {
|
||||
this.description.value = description;
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("更新称号失败", e, sql);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static TitleDTO getTitleDTO(ResultSet rs) throws SQLException {
|
||||
TitleDTO titleDTO = new TitleDTO();
|
||||
titleDTO.id.value = rs.getInt("id");
|
||||
titleDTO.title.value = rs.getString("title");
|
||||
titleDTO.description.value = rs.getString("description");
|
||||
return titleDTO;
|
||||
}
|
||||
}
|
@ -1,178 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle.dtos;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.databse.DatabaseManager;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.Field;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.FieldType;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.lunadeer.minecraftpluginutils.databse.DatabaseManager.handleDatabaseError;
|
||||
|
||||
public class TitleShopDTO {
|
||||
private TitleDTO title;
|
||||
private final Field id = new Field("id", FieldType.INT);
|
||||
private final Field price = new Field("price_d", FieldType.DOUBLE);
|
||||
private final Field days = new Field("days", FieldType.INT);
|
||||
private final Field amount = new Field("amount", FieldType.INT);
|
||||
|
||||
private LocalDateTime sale_end_at;
|
||||
|
||||
public Integer getId() {
|
||||
return (Integer) id.value;
|
||||
}
|
||||
|
||||
public TitleDTO getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return (Double) price.value;
|
||||
}
|
||||
|
||||
public boolean setPrice(Double price) {
|
||||
String sql = "";
|
||||
sql += "UPDATE mplt_title_shop SET price_d = ? WHERE id = ?;";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, price, getId())) {
|
||||
this.price.value = price;
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("设置称号商店价格失败", e, sql);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Integer getDays() {
|
||||
return (Integer) days.value;
|
||||
}
|
||||
|
||||
public boolean setDays(int days) {
|
||||
String sql = "";
|
||||
sql += "UPDATE mplt_title_shop SET days = ? WHERE id = ?;";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, days, getId())) {
|
||||
this.days.value = days;
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("设置称号商店天数失败", e, sql);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Integer getAmount() {
|
||||
return (Integer) amount.value;
|
||||
}
|
||||
|
||||
public boolean setAmount(int amount) {
|
||||
String sql = "";
|
||||
sql += "UPDATE mplt_title_shop SET amount = ? WHERE id = ?;";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, amount, getId())) {
|
||||
this.amount.value = amount;
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("设置称号商店数量失败", e, sql);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public LocalDateTime getSaleEndAt() {
|
||||
return sale_end_at;
|
||||
}
|
||||
|
||||
public boolean setSaleEndAt(LocalDateTime dateTime) {
|
||||
return setSaleEndAt(dateTime.getYear(), dateTime.getMonthValue(), dateTime.getDayOfMonth());
|
||||
}
|
||||
|
||||
public boolean setSaleEndAt(int y, int m, int d) {
|
||||
String sql = "";
|
||||
sql += "UPDATE mplt_title_shop SET sale_end_at_y = ?, sale_end_at_m = ?, sale_end_at_d = ? WHERE id = ?;";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, y, m, d, getId())) {
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("设置称号商店销售结束时间失败", e, sql);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static TitleShopDTO get(Integer id) {
|
||||
String sql = "";
|
||||
sql += "SELECT id, title_id, price_d, days, amount, sale_end_at_y, sale_end_at_m, sale_end_at_d " +
|
||||
"FROM mplt_title_shop WHERE id = ?;";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, id)) {
|
||||
if (rs.next()) {
|
||||
return getTitleShop(rs);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("获取称号商店信息失败", e, sql);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<TitleShopDTO> getAll() {
|
||||
String sql = "";
|
||||
sql += "SELECT id, title_id, price_d, days, amount, sale_end_at_y, sale_end_at_m, sale_end_at_d " +
|
||||
"FROM mplt_title_shop;";
|
||||
List<TitleShopDTO> titleShops = new ArrayList<>();
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql)) {
|
||||
while (rs != null && rs.next()) {
|
||||
TitleShopDTO titleShop = getTitleShop(rs);
|
||||
titleShops.add(titleShop);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("获取称号商店列表失败", e, sql);
|
||||
}
|
||||
return titleShops;
|
||||
}
|
||||
|
||||
private static TitleShopDTO getTitleShop(ResultSet rs) throws Exception {
|
||||
TitleShopDTO titleShop = new TitleShopDTO();
|
||||
titleShop.id.value = rs.getInt("id");
|
||||
titleShop.title = TitleDTO.get(rs.getInt("title_id"));
|
||||
titleShop.price.value = rs.getDouble("price_d");
|
||||
titleShop.days.value = rs.getInt("days");
|
||||
titleShop.amount.value = rs.getInt("amount");
|
||||
int y = rs.getInt("sale_end_at_y");
|
||||
int m = rs.getInt("sale_end_at_m");
|
||||
int d = rs.getInt("sale_end_at_d");
|
||||
if (y == -1 && m == -1 && d == -1) {
|
||||
titleShop.sale_end_at = null;
|
||||
} else {
|
||||
titleShop.sale_end_at = LocalDateTime.of(y, m, d, 0, 0, 0);
|
||||
}
|
||||
return titleShop;
|
||||
}
|
||||
|
||||
public static TitleShopDTO create(TitleDTO title) {
|
||||
String sql = "";
|
||||
sql += "INSERT INTO mplt_title_shop (title_id, price_d, days, amount, sale_end_at_y, sale_end_at_m, sale_end_at_d) " +
|
||||
"VALUES (?, 0, -1, 0, -1, -1, -1) " +
|
||||
"RETURNING " +
|
||||
"id, title_id, price_d, days, amount, sale_end_at_y, sale_end_at_m, sale_end_at_d;";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, title.getId())) {
|
||||
if (rs.next()) {
|
||||
return getTitleShop(rs);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("创建称号商店失败", e, sql);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean delete() {
|
||||
String sql = "";
|
||||
sql += "DELETE FROM mplt_title_shop WHERE id = ?;";
|
||||
try (ResultSet rs = DatabaseManager.instance.query(sql, getId())) {
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
handleDatabaseError("删除称号商店失败", e, sql);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isExpired() {
|
||||
if (sale_end_at == null) return false;
|
||||
return LocalDateTime.now().isAfter(sale_end_at);
|
||||
}
|
||||
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle.events;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.Notification;
|
||||
import cn.lunadeer.miniplayertitle.dtos.PlayerInfoDTO;
|
||||
import cn.lunadeer.miniplayertitle.dtos.PlayerTitleDTO;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
import static cn.lunadeer.miniplayertitle.commands.Apis.updateName;
|
||||
|
||||
public class Events implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
Player bukkitPlayer = event.getPlayer();
|
||||
PlayerInfoDTO player = PlayerInfoDTO.get(bukkitPlayer);
|
||||
if (player == null) {
|
||||
Notification.error(bukkitPlayer, "获取玩家信息时出现错误,请联系管理员");
|
||||
return;
|
||||
}
|
||||
if (player.getUsingTitle().getId() == -1) {
|
||||
updateName(bukkitPlayer, null);
|
||||
return;
|
||||
}
|
||||
PlayerTitleDTO title = PlayerTitleDTO.get(bukkitPlayer.getUniqueId(), player.getUsingTitle().getId());
|
||||
if (title == null || title.isExpired()) {
|
||||
Notification.warn(bukkitPlayer, "你当前使用的称号 %s 已过期", player.getUsingTitle().getTitlePlainText());
|
||||
player.setUsingTitle(null);
|
||||
updateName(bukkitPlayer, null);
|
||||
} else {
|
||||
updateName(bukkitPlayer, title.getTitle());
|
||||
}
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle.events;
|
||||
|
||||
import cn.lunadeer.miniplayertitle.MiniPlayerTitle;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerChatEvent;
|
||||
|
||||
import static cn.lunadeer.miniplayertitle.MiniPlayerTitle.usingPapi;
|
||||
|
||||
public class SpigotChat implements Listener {
|
||||
@EventHandler
|
||||
public void onPlayerSendChat(PlayerChatEvent event) {
|
||||
if (usingPapi()) {
|
||||
return;
|
||||
}
|
||||
event.setCancelled(true);
|
||||
MiniPlayerTitle.instance.getServer().broadcastMessage(event.getPlayer().getDisplayName() + " " + event.getMessage());
|
||||
}
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle.tuis;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.stui.ListView;
|
||||
import cn.lunadeer.minecraftpluginutils.stui.components.Button;
|
||||
import cn.lunadeer.minecraftpluginutils.stui.components.Line;
|
||||
import cn.lunadeer.miniplayertitle.dtos.TitleDTO;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static cn.lunadeer.miniplayertitle.tuis.Apis.getLastArgsPage;
|
||||
import static cn.lunadeer.miniplayertitle.tuis.Apis.playerOnly;
|
||||
|
||||
public class AllTitles {
|
||||
public static void show(CommandSender sender, String[] args) {
|
||||
Player player = playerOnly(sender);
|
||||
if (player == null) return;
|
||||
int page = getLastArgsPage(args);
|
||||
List<TitleDTO> titles = TitleDTO.getAll();
|
||||
|
||||
ListView view = ListView.create(10, "/mplt all_titles");
|
||||
view.title("所有称号");
|
||||
view.navigator(Line.create()
|
||||
.append(Button.create("主菜单").setExecuteCommand("/mplt menu").build())
|
||||
.append("所有称号"));
|
||||
|
||||
for (TitleDTO title : titles) {
|
||||
if (title.getId() == -1){
|
||||
continue;
|
||||
}
|
||||
Line line = Line.create()
|
||||
.append(title.getId().toString())
|
||||
.append(title.getTitleColored());
|
||||
if (player.hasPermission("mplt.admin")) {
|
||||
line.append(Button.createRed("删除").setExecuteCommand("/mplt delete_title " + title.getId() + " " + page).build());
|
||||
line.append(Button.createGreen("创建商品").setExecuteCommand("/mplt create_sale " + title.getId()).build());
|
||||
}
|
||||
view.add(line);
|
||||
}
|
||||
view.showOn(player, page);
|
||||
}
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle.tuis;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.Notification;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Apis {
|
||||
public static Player playerOnly(CommandSender sender) {
|
||||
if (!(sender instanceof Player)) {
|
||||
Notification.error(sender, "该命令只能由玩家执行");
|
||||
return null;
|
||||
}
|
||||
return (Player) sender;
|
||||
}
|
||||
|
||||
public static int getLastArgsPage(String[] args) {
|
||||
if (args.length == 0) return 1;
|
||||
int page = 1;
|
||||
try {
|
||||
page = Integer.parseInt(args[args.length - 1]);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return page;
|
||||
}
|
||||
|
||||
public static int getArgPage(String[] args, int pos) {
|
||||
int page = 1;
|
||||
if (args.length > pos - 1) {
|
||||
try {
|
||||
page = Integer.parseInt(args[pos - 1]);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
return page;
|
||||
}
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle.tuis;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.stui.ListView;
|
||||
import cn.lunadeer.minecraftpluginutils.stui.components.Button;
|
||||
import cn.lunadeer.minecraftpluginutils.stui.components.Line;
|
||||
import cn.lunadeer.miniplayertitle.MiniPlayerTitle;
|
||||
import cn.lunadeer.miniplayertitle.dtos.PlayerInfoDTO;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import static cn.lunadeer.miniplayertitle.tuis.Apis.getLastArgsPage;
|
||||
import static cn.lunadeer.miniplayertitle.tuis.Apis.playerOnly;
|
||||
|
||||
public class CustomInfo {
|
||||
public static void show(CommandSender sender, String[] args) {
|
||||
Player player = playerOnly(sender);
|
||||
if (player == null) return;
|
||||
int page = getLastArgsPage(args);
|
||||
PlayerInfoDTO playerInfo = PlayerInfoDTO.get(player.getUniqueId());
|
||||
if (playerInfo == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
ListView view = ListView.create(10, "/mplt custom_info");
|
||||
view.title("自定义称号帮助");
|
||||
view.navigator(Line.create()
|
||||
.append(Button.create("主菜单").setExecuteCommand("/mplt menu").build())
|
||||
.append("自定义称号"));
|
||||
|
||||
Line line_1 = Line.create()
|
||||
.append("称号币余额:").append(playerInfo.getCoin().toString());
|
||||
Line line_2 = Line.create()
|
||||
.append("自定义称号状态:")
|
||||
.append(MiniPlayerTitle.config.isEnableCustom() ? "开启" : "关闭");
|
||||
Line line_3 = Line.create()
|
||||
.append("自定义称号花费:").append(MiniPlayerTitle.config.getCustomCost().toString());
|
||||
Line line_4 = Line.create()
|
||||
.append("自定义称号最大长度(不含颜色代码):").append(MiniPlayerTitle.config.getMaxLength().toString());
|
||||
Line line_5 = Line.create()
|
||||
.append("自定义方法:")
|
||||
.append("在聊天框输入 /mplt custom_title <称号>");
|
||||
Line line_6 = Line.create()
|
||||
.append("可以使用 Minecraft渐变颜色生成器 来生成具有渐变效果的称号")
|
||||
.append(Button.create("点击在浏览器中打开生成器").setOpenURL("https://ssl.lunadeer.cn:14440/").build());
|
||||
|
||||
view.add(line_1);
|
||||
view.add(line_2);
|
||||
view.add(line_3);
|
||||
view.add(line_4);
|
||||
view.add(line_5);
|
||||
view.add(line_6);
|
||||
|
||||
view.showOn(player, page);
|
||||
}
|
||||
}
|
@ -1,69 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle.tuis;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.Notification;
|
||||
import cn.lunadeer.minecraftpluginutils.VaultConnect.VaultConnect;
|
||||
import cn.lunadeer.minecraftpluginutils.stui.ListView;
|
||||
import cn.lunadeer.minecraftpluginutils.stui.components.Button;
|
||||
import cn.lunadeer.minecraftpluginutils.stui.components.Line;
|
||||
import cn.lunadeer.miniplayertitle.MiniPlayerTitle;
|
||||
import cn.lunadeer.miniplayertitle.dtos.PlayerInfoDTO;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import static cn.lunadeer.miniplayertitle.tuis.Apis.getLastArgsPage;
|
||||
import static cn.lunadeer.miniplayertitle.tuis.Apis.playerOnly;
|
||||
|
||||
public class Menu {
|
||||
public static void show(CommandSender sender, String[] args) {
|
||||
int page = getLastArgsPage(args);
|
||||
|
||||
Player player = playerOnly(sender);
|
||||
if (player == null) return;
|
||||
|
||||
PlayerInfoDTO playerInfo = PlayerInfoDTO.get(player.getUniqueId());
|
||||
if (playerInfo == null) {
|
||||
Notification.error(player, "获取玩家信息时出现错误");
|
||||
return;
|
||||
}
|
||||
Line balance;
|
||||
if (!MiniPlayerTitle.config.isExternalEco()) {
|
||||
balance = Line.create()
|
||||
.append("称号币余额: ").append(playerInfo.getCoin().toString());
|
||||
} else {
|
||||
balance = Line.create()
|
||||
.append("余额: ").append(playerInfo.getCoin().toString()).append(VaultConnect.instance.currencyNamePlural());
|
||||
}
|
||||
Line backpack = Line.create()
|
||||
.append(Button.create("称号背包").setExecuteCommand("/mplt my_titles").build()).append("查看你拥有的称号");
|
||||
Line shop = Line.create()
|
||||
.append(Button.create("称号商店").setExecuteCommand("/mplt shop").build()).append("购买在售称号");
|
||||
Line custom = Line.create()
|
||||
.append(Button.create("自定义称号").setExecuteCommand("/mplt custom_info").build()).append("查看如何自定义称号");
|
||||
Line manual = Line.create()
|
||||
.append(Button.create("帮助文档").setOpenURL("https://ssl.lunadeer.cn:14448/doc/2/").build()).append("在浏览器打开帮助文档");
|
||||
|
||||
ListView view = ListView.create(10, "/mplt");
|
||||
view.title("称号系统")
|
||||
.navigator(Line.create().append("主菜单"))
|
||||
.add(balance)
|
||||
.add(Line.create())
|
||||
.add(backpack)
|
||||
.add(shop)
|
||||
.add(manual);
|
||||
|
||||
if (MiniPlayerTitle.config.isEnableCustom()) {
|
||||
view.add(custom);
|
||||
}
|
||||
|
||||
Line all = Line.create()
|
||||
.append(Button.create("所有称号").setExecuteCommand("/mplt all_titles").build()).append("查看所有称号");
|
||||
|
||||
if (player.hasPermission("mplt.admin")) {
|
||||
view.add(Line.create().append(""));
|
||||
view.add(Line.create().append("---以下选项仅OP可见---"));
|
||||
view.add(all);
|
||||
}
|
||||
|
||||
view.showOn(player, page);
|
||||
}
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle.tuis;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.Notification;
|
||||
import cn.lunadeer.minecraftpluginutils.stui.ListView;
|
||||
import cn.lunadeer.minecraftpluginutils.stui.components.Button;
|
||||
import cn.lunadeer.minecraftpluginutils.stui.components.Line;
|
||||
import cn.lunadeer.miniplayertitle.dtos.PlayerInfoDTO;
|
||||
import cn.lunadeer.miniplayertitle.dtos.PlayerTitleDTO;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import static cn.lunadeer.miniplayertitle.tuis.Apis.getLastArgsPage;
|
||||
import static cn.lunadeer.miniplayertitle.tuis.Apis.playerOnly;
|
||||
|
||||
public class MyTitles {
|
||||
public static void show(CommandSender sender, String[] args) {
|
||||
Player player = playerOnly(sender);
|
||||
if (player == null) return;
|
||||
int page = getLastArgsPage(args);
|
||||
|
||||
PlayerInfoDTO playerInfo = PlayerInfoDTO.get(player.getUniqueId());
|
||||
if (playerInfo == null) {
|
||||
Notification.error(player, "获取玩家信息时出现错误,详情请查看控制台日志");
|
||||
return;
|
||||
}
|
||||
List<PlayerTitleDTO> titles = PlayerTitleDTO.getAllOf(player.getUniqueId());
|
||||
ListView view = ListView.create(10, "/mplt my_titles");
|
||||
view.title("称号背包");
|
||||
view.navigator(Line.create()
|
||||
.append(Button.create("主菜单").setExecuteCommand("/mplt menu").build())
|
||||
.append("称号背包"));
|
||||
|
||||
for (PlayerTitleDTO title : titles) {
|
||||
if (title == null) {
|
||||
continue;
|
||||
}
|
||||
Line line = Line.create()
|
||||
.append(title.getTitle().getTitleColored());
|
||||
if (title.getExpireAt() == null) {
|
||||
line.append("永久");
|
||||
} else if (title.getExpireAt().isBefore(LocalDateTime.now())) {
|
||||
line.append("已过期");
|
||||
} else {
|
||||
line.append("有效期至: " + title.getExpireAt().getYear() + "年" + title.getExpireAt().getMonthValue() + "月" + title.getExpireAt().getDayOfMonth() + "日");
|
||||
}
|
||||
if (Objects.equals(playerInfo.getUsingTitle().getId(), title.getTitle().getId())) {
|
||||
line.append(Button.createRed("卸下").setExecuteCommand("/mplt use_title -1 " + page).build());
|
||||
} else {
|
||||
line.append(Button.createGreen("使用").setExecuteCommand("/mplt use_title " + title.getId() + " " + page).build());
|
||||
}
|
||||
view.add(line);
|
||||
}
|
||||
|
||||
view.showOn(player, page);
|
||||
}
|
||||
}
|
@ -1,131 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle.tuis;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.Notification;
|
||||
import cn.lunadeer.minecraftpluginutils.stui.ListView;
|
||||
import cn.lunadeer.minecraftpluginutils.stui.components.Button;
|
||||
import cn.lunadeer.minecraftpluginutils.stui.components.Line;
|
||||
import cn.lunadeer.minecraftpluginutils.stui.components.NumChanger;
|
||||
import cn.lunadeer.miniplayertitle.dtos.TitleDTO;
|
||||
import cn.lunadeer.miniplayertitle.dtos.TitleShopDTO;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static cn.lunadeer.miniplayertitle.tuis.Apis.getArgPage;
|
||||
import static cn.lunadeer.miniplayertitle.tuis.Apis.playerOnly;
|
||||
|
||||
public class SaleInfo {
|
||||
public static void show(CommandSender sender, String[] args) {
|
||||
Player player = playerOnly(sender);
|
||||
if (player == null) return;
|
||||
int page = getArgPage(args, 3);
|
||||
TitleShopDTO titleShop = TitleShopDTO.get(Integer.valueOf(args[1]));
|
||||
if (titleShop == null) {
|
||||
Notification.error(player, "获取详情时出现错误");
|
||||
return;
|
||||
}
|
||||
TitleDTO title = titleShop.getTitle();
|
||||
if (title == null) {
|
||||
Notification.error(player, "获取详情时出现错误");
|
||||
return;
|
||||
}
|
||||
int now_year = LocalDateTime.now().getYear();
|
||||
int now_month = LocalDateTime.now().getMonthValue();
|
||||
int now_day = LocalDateTime.now().getDayOfMonth();
|
||||
ListView view = ListView.create(10, "/mplt sale_info " + args[1]);
|
||||
view.title("销售详情");
|
||||
view.navigator(Line.create()
|
||||
.append(Button.create("主菜单").setExecuteCommand("/mplt menu").build())
|
||||
.append(Button.create("称号商店").setExecuteCommand("/mplt shop").build())
|
||||
.append("销售详情"));
|
||||
view.add(Line.create().append("内容: ").append(titleShop.getTitle().getTitleColored()));
|
||||
view.add(Line.create().append("描述: ").append(titleShop.getTitle().getDescription()));
|
||||
|
||||
Line price = Line.create().append("价格: ");
|
||||
if (titleShop.getPrice() <= 0) {
|
||||
price.append("免费");
|
||||
if (player.hasPermission("mplt.admin")) {
|
||||
price.append(Button.create("设置价格").setExecuteCommand("/mplt set_sale price " + titleShop.getId() + " 100 " + page).build());
|
||||
}
|
||||
} else {
|
||||
if (player.hasPermission("mplt.admin")) {
|
||||
price.append(NumChanger.create(titleShop.getPrice(), "/mplt set_sale price " + titleShop.getId()).setPageNumber(page).build());
|
||||
price.append(Button.create("设置为免费").setExecuteCommand("/mplt set_sale price " + titleShop.getId() + " 0 " + page).build());
|
||||
} else {
|
||||
price.append(titleShop.getPrice().toString());
|
||||
}
|
||||
}
|
||||
view.add(price);
|
||||
|
||||
Line day = Line.create().append("购买天数: ");
|
||||
if (titleShop.getDays() <= 0) {
|
||||
day.append("永久");
|
||||
if (player.hasPermission("mplt.admin")) {
|
||||
day.append(Button.create("转为限时").setExecuteCommand("/mplt set_sale days " + titleShop.getId() + " 7 " + page).build());
|
||||
}
|
||||
} else {
|
||||
if (player.hasPermission("mplt.admin")) {
|
||||
day.append(NumChanger.create(titleShop.getDays(), "/mplt set_sale days " + titleShop.getId()).setPageNumber(page).build());
|
||||
day.append(Button.create("转为永久").setExecuteCommand("/mplt set_sale days " + titleShop.getId() + " -1 " + page).build());
|
||||
} else {
|
||||
day.append(titleShop.getDays().toString());
|
||||
}
|
||||
}
|
||||
view.add(day);
|
||||
|
||||
Line amount = Line.create().append("剩余数量: ");
|
||||
if (titleShop.getAmount() == -1) {
|
||||
amount.append("无限");
|
||||
if (player.hasPermission("mplt.admin")) {
|
||||
amount.append(Button.create("转为限量").setExecuteCommand("/mplt set_sale amount " + titleShop.getId() + " 0 " + page).build());
|
||||
}
|
||||
} else {
|
||||
|
||||
if (player.hasPermission("mplt.admin")) {
|
||||
amount.append(NumChanger.create(titleShop.getAmount(), "/mplt set_sale amount " + titleShop.getId()).setPageNumber(page).build());
|
||||
amount.append(Button.create("转为无限").setExecuteCommand("/mplt set_sale amount " + titleShop.getId() + " -1 " + page).build());
|
||||
} else {
|
||||
amount.append(titleShop.getAmount().toString());
|
||||
}
|
||||
}
|
||||
view.add(amount);
|
||||
|
||||
Line end_at = Line.create().append("售卖结束时间: ");
|
||||
if (titleShop.getSaleEndAt() == null) {
|
||||
end_at.append("常驻");
|
||||
if (player.hasPermission("mplt.admin")) {
|
||||
end_at.append(Button.create("转为限时").setExecuteCommand("/mplt set_sale end_at " + titleShop.getId() + " " + now_year + ":" + now_month + ":" + now_day + " " + page).build());
|
||||
}
|
||||
} else {
|
||||
if (player.hasPermission("mplt.admin")) {
|
||||
end_at.append(Button.create("<<").setPreSufIx("", "").setHoverText("提前10天").setExecuteCommand("/mplt set_sale less_end_at " + titleShop.getId() + " 10 " + page).build()
|
||||
.append(Button.create("-").setPreSufIx("", "").setHoverText("提前1天").setExecuteCommand("/mplt set_sale less_end_at " + titleShop.getId() + " 1 " + page).build())
|
||||
.append(Component.text(titleShop.getSaleEndAt().getYear() + "年" + titleShop.getSaleEndAt().getMonthValue() + "月" + titleShop.getSaleEndAt().getDayOfMonth() + "日"))
|
||||
.append(Button.create("+").setPreSufIx("", "").setHoverText("延后1天").setExecuteCommand("/mplt set_sale more_end_at " + titleShop.getId() + " 1 " + page).build())
|
||||
.append(Button.create(">>").setPreSufIx("", "").setHoverText("延后10天").setExecuteCommand("/mplt set_sale more_end_at " + titleShop.getId() + " 10 " + page).build()));
|
||||
end_at.append(Button.create("转为常驻").setExecuteCommand("/mplt set_sale end_at " + titleShop.getId() + " -1:-1:-1 " + page).build());
|
||||
} else {
|
||||
end_at.append(titleShop.getSaleEndAt().getYear() + "年" + titleShop.getSaleEndAt().getMonthValue() + "月" + titleShop.getSaleEndAt().getDayOfMonth() + "日");
|
||||
}
|
||||
}
|
||||
view.add(end_at);
|
||||
|
||||
Line operate = Line.create().append("操作: ");
|
||||
if (titleShop.isExpired()) {
|
||||
operate.append(Button.createRed("已结束").build());
|
||||
} else if (titleShop.getAmount() == 0) {
|
||||
operate.append(Button.createRed("已售罄").build());
|
||||
} else {
|
||||
operate.append(Button.createGreen("购买").setExecuteCommand("/mplt buy_sale " + titleShop.getId()).build());
|
||||
}
|
||||
if (player.hasPermission("mplt.admin")) {
|
||||
operate.append(Button.create("删除").setExecuteCommand("/mplt delete_sale " + args[1] + " b").build());
|
||||
operate.append(Button.createGreen("导出称号卡").setExecuteCommand("/mplt get_card " + args[1]).build());
|
||||
}
|
||||
view.add(Line.create().append("---------------------"));
|
||||
view.add(operate);
|
||||
view.showOn(player, page);
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle.tuis;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.stui.ListView;
|
||||
import cn.lunadeer.minecraftpluginutils.stui.components.Button;
|
||||
import cn.lunadeer.minecraftpluginutils.stui.components.Line;
|
||||
import cn.lunadeer.miniplayertitle.dtos.TitleShopDTO;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static cn.lunadeer.miniplayertitle.tuis.Apis.getLastArgsPage;
|
||||
import static cn.lunadeer.miniplayertitle.tuis.Apis.playerOnly;
|
||||
|
||||
public class Shop {
|
||||
public static void show(CommandSender sender, String[] args) {
|
||||
Player player = playerOnly(sender);
|
||||
if (player == null) return;
|
||||
int page = getLastArgsPage(args);
|
||||
List<TitleShopDTO> titles = TitleShopDTO.getAll();
|
||||
|
||||
ListView view = ListView.create(10, "/mplt shop");
|
||||
view.title("称号商店");
|
||||
view.navigator(Line.create()
|
||||
.append(Button.create("主菜单").setExecuteCommand("/mplt menu").build())
|
||||
.append("称号商店"));
|
||||
|
||||
for (TitleShopDTO title : titles) {
|
||||
if (title == null) {
|
||||
continue;
|
||||
}
|
||||
if (title.getTitle().getId() == -1) {
|
||||
continue;
|
||||
}
|
||||
Line line = Line.create()
|
||||
.append(title.getId().toString())
|
||||
.append(title.getTitle().getTitleColored())
|
||||
.append(Button.createGreen("详情").setExecuteCommand("/mplt sale_info " + title.getId()).build());
|
||||
view.add(line);
|
||||
}
|
||||
|
||||
view.showOn(player, page);
|
||||
}
|
||||
}
|
@ -16,6 +16,10 @@ public class ConfigManager {
|
||||
_file = _plugin.getConfig();
|
||||
_debug = _file.getBoolean("Debug", false);
|
||||
_db_type = _file.getString("Database.Type", "sqlite");
|
||||
if (!_db_type.equals("pgsql") && !_db_type.equals("sqlite")) {
|
||||
XLogger.err("当前数据库只支持 pgsql 或 sqlite,已重置为 sqlite");
|
||||
setDbType("sqlite");
|
||||
}
|
||||
_db_host = _file.getString("Database.Host", "localhost");
|
||||
_db_port = _file.getString("Database.Port", "5432");
|
||||
_db_name = _file.getString("Database.Name", "miniplayertitle");
|
||||
@ -27,8 +31,6 @@ public class ConfigManager {
|
||||
_enable_custom = _file.getBoolean("CustomCost.Enabled", true);
|
||||
_custom_cost = _file.getInt("CustomCost.Cost", 1000);
|
||||
_max_length = _file.getInt("CustomCost.MaxLength", 8);
|
||||
_check_update = _file.getBoolean("CheckUpdate", true);
|
||||
_external_eco = _file.getBoolean("ExternalEco", false);
|
||||
}
|
||||
|
||||
public Boolean isDebug() {
|
||||
@ -116,9 +118,15 @@ public class ConfigManager {
|
||||
return _max_length;
|
||||
}
|
||||
|
||||
public void setCustom(boolean enable) {
|
||||
public void enableCustom(){
|
||||
_enable_custom = true;
|
||||
_file.set("CustomCost.Enabled", enable);
|
||||
_file.set("CustomCost.Enabled", true);
|
||||
_plugin.saveConfig();
|
||||
}
|
||||
|
||||
public void disableCustom(){
|
||||
_enable_custom = false;
|
||||
_file.set("CustomCost.Enabled", false);
|
||||
_plugin.saveConfig();
|
||||
}
|
||||
|
||||
@ -134,14 +142,6 @@ public class ConfigManager {
|
||||
_plugin.saveConfig();
|
||||
}
|
||||
|
||||
public Boolean isCheckUpdate() {
|
||||
return _check_update;
|
||||
}
|
||||
|
||||
public Boolean isExternalEco() {
|
||||
return _external_eco;
|
||||
}
|
||||
|
||||
|
||||
private final MiniPlayerTitle _plugin;
|
||||
private FileConfiguration _file;
|
||||
@ -158,7 +158,6 @@ public class ConfigManager {
|
||||
private Integer _default_coin;
|
||||
private Boolean _enable_custom;
|
||||
private Integer _custom_cost;
|
||||
|
||||
private Integer _max_length;
|
||||
private Boolean _check_update;
|
||||
private Boolean _external_eco;
|
||||
}
|
||||
|
158
src/main/java/cn/lunadeer/miniplayertitle/utils/Database.java
Normal file
158
src/main/java/cn/lunadeer/miniplayertitle/utils/Database.java
Normal file
@ -0,0 +1,158 @@
|
||||
package cn.lunadeer.miniplayertitle.utils;
|
||||
|
||||
import cn.lunadeer.miniplayertitle.MiniPlayerTitle;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
public class Database {
|
||||
|
||||
public static Connection createConnection() {
|
||||
try {
|
||||
String connectionUrl;
|
||||
if (MiniPlayerTitle.config.getDbType().equals("pgsql")) {
|
||||
XLogger.info("正在连接到 PostgreSQL 数据库");
|
||||
Class.forName("org.postgresql.Driver");
|
||||
connectionUrl = "jdbc:postgresql://" + MiniPlayerTitle.config.getDbHost() + ":" + MiniPlayerTitle.config.getDbPort();
|
||||
connectionUrl += "/" + MiniPlayerTitle.config.getDbName();
|
||||
return DriverManager.getConnection(connectionUrl, MiniPlayerTitle.config.getDbUser(), MiniPlayerTitle.config.getDbPass());
|
||||
} else if (MiniPlayerTitle.config.getDbType().equals("sqlite")) {
|
||||
XLogger.info("正在连接到 SQLite 数据库");
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
connectionUrl = "jdbc:sqlite:" + MiniPlayerTitle.instance.getDataFolder() + "/" + MiniPlayerTitle.config.getDbName() + ".db";
|
||||
return DriverManager.getConnection(connectionUrl);
|
||||
} else {
|
||||
XLogger.err("=== 严重错误 ===");
|
||||
XLogger.err("数据库类型错误,只能为 pgsql 或 sqlite");
|
||||
XLogger.err("===============");
|
||||
return null;
|
||||
}
|
||||
} catch (ClassNotFoundException | SQLException e) {
|
||||
XLogger.err("=== 严重错误 ===");
|
||||
XLogger.err("Database connection failed: " + e.getMessage());
|
||||
XLogger.err("===============");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static ResultSet query(String sql) {
|
||||
Connection conn = MiniPlayerTitle.dbConnection;
|
||||
if (conn == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
Statement stmt = conn.createStatement();
|
||||
if (sql.contains("SERIAL PRIMARY KEY") && MiniPlayerTitle.config.getDbType().equals("sqlite")) {
|
||||
sql = sql.replace("SERIAL PRIMARY KEY", "INTEGER PRIMARY KEY AUTOINCREMENT");
|
||||
}
|
||||
// if query with no result return null
|
||||
if (stmt.execute(sql)) {
|
||||
return stmt.getResultSet();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
handleDatabaseError("Database query failed: ", e, sql);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void handleDatabaseError(String errorMessage, SQLException e, String sql) {
|
||||
XLogger.err("=== 严重错误 ===");
|
||||
XLogger.err(errorMessage + e.getMessage());
|
||||
XLogger.err("SQL: " + sql);
|
||||
XLogger.err("===============");
|
||||
}
|
||||
|
||||
private static void addColumnIfNotExists(String tableName, String columnName, String columnDefinition) {
|
||||
if (MiniPlayerTitle.config.getDbType().equals("pgsql")) {
|
||||
String sql = "ALTER TABLE " + tableName + " ADD COLUMN IF NOT EXISTS " + columnName + " " + columnDefinition + ";";
|
||||
query(sql);
|
||||
} else if (MiniPlayerTitle.config.getDbType().equals("sqlite")) {
|
||||
try {
|
||||
ResultSet rs = query("PRAGMA table_info(" + tableName + ");");
|
||||
boolean columnExists = false;
|
||||
if (rs != null) {
|
||||
while (rs.next()) {
|
||||
if (columnName.equals(rs.getString("name"))) {
|
||||
columnExists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!columnExists) {
|
||||
query("ALTER TABLE " + tableName + " ADD COLUMN " + columnName + " " + columnDefinition + ";");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
handleDatabaseError("Database operation failed: ", e, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void migrate() {
|
||||
String sql = "";
|
||||
|
||||
// title table
|
||||
sql = "CREATE TABLE IF NOT EXISTS mplt_title (" +
|
||||
" id SERIAL PRIMARY KEY," +
|
||||
" title TEXT NOT NULL UNIQUE," +
|
||||
" description TEXT NOT NULL," +
|
||||
" enabled BOOLEAN NOT NULL DEFAULT TRUE," +
|
||||
" created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP," +
|
||||
" updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP" +
|
||||
");";
|
||||
query(sql);
|
||||
|
||||
// title shop table
|
||||
sql = "CREATE TABLE IF NOT EXISTS mplt_title_shop (" +
|
||||
" id SERIAL PRIMARY KEY," +
|
||||
" title_id INTEGER NOT NULL," +
|
||||
" price INTEGER NOT NULL DEFAULT 0," +
|
||||
" days INTEGER NOT NULL DEFAULT 0," +
|
||||
" amount INTEGER NOT NULL DEFAULT -1," +
|
||||
" sale_end_at BIGINT NOT NULL DEFAULT -1," +
|
||||
" created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP," +
|
||||
" updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP," +
|
||||
" FOREIGN KEY (title_id) REFERENCES mplt_title(id) ON DELETE CASCADE" +
|
||||
");";
|
||||
query(sql);
|
||||
|
||||
// player title info table
|
||||
sql = "CREATE TABLE IF NOT EXISTS mplt_player_info (" +
|
||||
" uuid UUID PRIMARY KEY," +
|
||||
" coin INTEGER NOT NULL DEFAULT 0," +
|
||||
" using_title_id INTEGER NOT NULL DEFAULT -1," +
|
||||
" created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP," +
|
||||
" updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP," +
|
||||
" FOREIGN KEY (using_title_id) REFERENCES mplt_title(id) ON DELETE CASCADE" +
|
||||
");";
|
||||
query(sql);
|
||||
|
||||
// player title table
|
||||
sql = "CREATE TABLE IF NOT EXISTS mplt_player_title (" +
|
||||
" id SERIAL PRIMARY KEY," +
|
||||
" player_uuid UUID NOT NULL," +
|
||||
" title_id INTEGER NOT NULL," +
|
||||
" expire_at BIGINT NOT NULL DEFAULT -1," +
|
||||
" created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP," +
|
||||
" updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP," +
|
||||
" FOREIGN KEY (title_id) REFERENCES mplt_title(id) ON DELETE CASCADE," +
|
||||
" FOREIGN KEY (player_uuid) REFERENCES mplt_player_info(uuid) ON DELETE CASCADE" +
|
||||
");";
|
||||
query(sql);
|
||||
|
||||
sql = "INSERT INTO mplt_title (" +
|
||||
"id, " +
|
||||
"title, " +
|
||||
"description," +
|
||||
"enabled, " +
|
||||
"created_at, " +
|
||||
"updated_at " +
|
||||
") VALUES (" +
|
||||
"-1, " +
|
||||
"'default', " +
|
||||
"'default', " +
|
||||
"TRUE, " +
|
||||
"CURRENT_TIMESTAMP, " +
|
||||
"CURRENT_TIMESTAMP " +
|
||||
") ON CONFLICT (id) DO NOTHING;";
|
||||
query(sql);
|
||||
}
|
||||
}
|
@ -1,172 +0,0 @@
|
||||
package cn.lunadeer.miniplayertitle.utils;
|
||||
|
||||
import cn.lunadeer.minecraftpluginutils.databse.DatabaseManager;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.Field;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.FieldType;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.TableColumn;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.syntax.AddColumn;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.syntax.CreateTable;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.syntax.InsertRow;
|
||||
import cn.lunadeer.minecraftpluginutils.databse.syntax.RemoveColumn;
|
||||
|
||||
import static cn.lunadeer.minecraftpluginutils.databse.Common.IsFieldExist;
|
||||
|
||||
public class DatabaseTables {
|
||||
|
||||
public static void migrate() {
|
||||
|
||||
// title table
|
||||
TableColumn mplt_title_id = new TableColumn("id", FieldType.INT, true, true, true, true, 0);
|
||||
TableColumn mplt_title_title = new TableColumn("title", FieldType.STRING, false, false, true, false, "'unknown'");
|
||||
TableColumn mplt_title_description = new TableColumn("description", FieldType.STRING, false, false, true, false, "'unknown'");
|
||||
TableColumn mplt_title_enabled = new TableColumn("enabled", FieldType.BOOLEAN, false, false, true, false, "true");
|
||||
TableColumn mplt_title_created_at = new TableColumn("created_at", FieldType.DATETIME, false, false, true, false, "CURRENT_TIMESTAMP");
|
||||
TableColumn mplt_title_updated_at = new TableColumn("updated_at", FieldType.DATETIME, false, false, true, false, "CURRENT_TIMESTAMP");
|
||||
CreateTable mplt_title = new CreateTable().ifNotExists();
|
||||
mplt_title.table("mplt_title")
|
||||
.field(mplt_title_id)
|
||||
.field(mplt_title_title)
|
||||
.field(mplt_title_description)
|
||||
.field(mplt_title_enabled)
|
||||
.field(mplt_title_created_at)
|
||||
.field(mplt_title_updated_at);
|
||||
mplt_title.execute();
|
||||
|
||||
|
||||
// title shop table
|
||||
TableColumn mplt_title_shop_id = new TableColumn("id", FieldType.INT, true, true, true, true, 0);
|
||||
TableColumn mplt_title_shop_title_id = new TableColumn("title_id", FieldType.INT, false, false, true, false, 0);
|
||||
TableColumn mplt_title_shop_price = new TableColumn("price", FieldType.INT, false, false, true, false, 0);
|
||||
TableColumn mplt_title_shop_days = new TableColumn("days", FieldType.INT, false, false, true, false, 0);
|
||||
TableColumn mplt_title_shop_amount = new TableColumn("amount", FieldType.INT, false, false, true, false, -1);
|
||||
TableColumn mplt_title_shop_sale_end_at = new TableColumn("sale_end_at", FieldType.LONG, false, false, true, false, -1);
|
||||
TableColumn mplt_title_shop_created_at = new TableColumn("created_at", FieldType.DATETIME, false, false, true, false, "CURRENT_TIMESTAMP");
|
||||
TableColumn mplt_title_shop_updated_at = new TableColumn("updated_at", FieldType.DATETIME, false, false, true, false, "CURRENT_TIMESTAMP");
|
||||
CreateTable.ForeignKey mplt_title_shop_title_id_fk = new CreateTable.ForeignKey(mplt_title_shop_title_id, "mplt_title", mplt_title_id, true);
|
||||
CreateTable mplt_title_shop = new CreateTable().ifNotExists();
|
||||
mplt_title_shop.table("mplt_title_shop")
|
||||
.field(mplt_title_shop_id)
|
||||
.field(mplt_title_shop_title_id)
|
||||
.field(mplt_title_shop_price)
|
||||
.field(mplt_title_shop_days)
|
||||
.field(mplt_title_shop_amount)
|
||||
.field(mplt_title_shop_sale_end_at)
|
||||
.field(mplt_title_shop_created_at)
|
||||
.field(mplt_title_shop_updated_at)
|
||||
.foreignKey(mplt_title_shop_title_id_fk);
|
||||
mplt_title_shop.execute();
|
||||
|
||||
// player title info table
|
||||
TableColumn mplt_player_info_uuid = new TableColumn("uuid", FieldType.UUID, true, false, true, false, "'00000000-0000-0000-0000-000000000000'");
|
||||
TableColumn mplt_player_info_coin = new TableColumn("coin", FieldType.INT, false, false, true, false, 0);
|
||||
TableColumn mplt_player_info_using_title_id = new TableColumn("using_title_id", FieldType.INT, false, false, true, false, -1);
|
||||
TableColumn mplt_player_info_created_at = new TableColumn("created_at", FieldType.DATETIME, false, false, true, false, "CURRENT_TIMESTAMP");
|
||||
TableColumn mplt_player_info_updated_at = new TableColumn("updated_at", FieldType.DATETIME, false, false, true, false, "CURRENT_TIMESTAMP");
|
||||
CreateTable.ForeignKey mplt_player_info_using_title_id_fk = new CreateTable.ForeignKey(mplt_player_info_using_title_id, "mplt_title", mplt_title_id, true);
|
||||
CreateTable mplt_player_info = new CreateTable().ifNotExists();
|
||||
mplt_player_info.table("mplt_player_info")
|
||||
.field(mplt_player_info_uuid)
|
||||
.field(mplt_player_info_coin)
|
||||
.field(mplt_player_info_using_title_id)
|
||||
.field(mplt_player_info_created_at)
|
||||
.field(mplt_player_info_updated_at)
|
||||
.foreignKey(mplt_player_info_using_title_id_fk);
|
||||
mplt_player_info.execute();
|
||||
|
||||
|
||||
// player title table
|
||||
TableColumn mplt_player_title_id = new TableColumn("id", FieldType.INT, true, true, true, true, 0);
|
||||
TableColumn mplt_player_title_player_uuid = new TableColumn("player_uuid", FieldType.UUID, false, false, true, false, "'00000000-0000-0000-0000-000000000000'");
|
||||
TableColumn mplt_player_title_title_id = new TableColumn("title_id", FieldType.INT, false, false, true, false, 0);
|
||||
TableColumn mplt_player_title_expire_at = new TableColumn("expire_at", FieldType.LONG, false, false, true, false, -1);
|
||||
TableColumn mplt_player_title_created_at = new TableColumn("created_at", FieldType.DATETIME, false, false, true, false, "CURRENT_TIMESTAMP");
|
||||
TableColumn mplt_player_title_updated_at = new TableColumn("updated_at", FieldType.DATETIME, false, false, true, false, "CURRENT_TIMESTAMP");
|
||||
CreateTable.ForeignKey mplt_player_title_title_id_fk = new CreateTable.ForeignKey(mplt_player_title_title_id, "mplt_title", mplt_title_id, true);
|
||||
CreateTable.ForeignKey mplt_player_title_player_uuid_fk = new CreateTable.ForeignKey(mplt_player_title_player_uuid, "mplt_player_info", mplt_player_info_uuid, true);
|
||||
CreateTable mplt_player_title = new CreateTable().ifNotExists();
|
||||
mplt_player_title.table("mplt_player_title")
|
||||
.field(mplt_player_title_id)
|
||||
.field(mplt_player_title_player_uuid)
|
||||
.field(mplt_player_title_title_id)
|
||||
.field(mplt_player_title_expire_at)
|
||||
.field(mplt_player_title_created_at)
|
||||
.field(mplt_player_title_updated_at)
|
||||
.foreignKey(mplt_player_title_title_id_fk)
|
||||
.foreignKey(mplt_player_title_player_uuid_fk);
|
||||
mplt_player_title.execute();
|
||||
|
||||
new RemoveColumn("expire_at").IfExists().table("mplt_title").execute();
|
||||
new RemoveColumn("sale_end_at").IfExists().table("mplt_title_shop").execute();
|
||||
new RemoveColumn("expire_at").IfExists().table("mplt_player_title").execute();
|
||||
new RemoveColumn("created_at").IfExists().table("mplt_title").execute();
|
||||
new RemoveColumn("updated_at").IfExists().table("mplt_title").execute();
|
||||
new RemoveColumn("created_at").IfExists().table("mplt_player_info").execute();
|
||||
new RemoveColumn("updated_at").IfExists().table("mplt_player_info").execute();
|
||||
new RemoveColumn("created_at").IfExists().table("mplt_player_title").execute();
|
||||
new RemoveColumn("updated_at").IfExists().table("mplt_player_title").execute();
|
||||
|
||||
TableColumn mplt_title_shop_sale_end_at_y = new TableColumn("sale_end_at_y", FieldType.INT, false, false, true, false, -1);
|
||||
TableColumn mplt_title_shop_sale_end_at_m = new TableColumn("sale_end_at_m", FieldType.INT, false, false, true, false, -1);
|
||||
TableColumn mplt_title_shop_sale_end_at_d = new TableColumn("sale_end_at_d", FieldType.INT, false, false, true, false, -1);
|
||||
new AddColumn(mplt_title_shop_sale_end_at_y).table("mplt_title_shop").ifNotExists().execute();
|
||||
new AddColumn(mplt_title_shop_sale_end_at_m).table("mplt_title_shop").ifNotExists().execute();
|
||||
new AddColumn(mplt_title_shop_sale_end_at_d).table("mplt_title_shop").ifNotExists().execute();
|
||||
// convert sale_end_at(YYYYMMDD) to sale_end_at_y, sale_end_at_m, sale_end_at_d if sale_end_at column exists
|
||||
if (IsFieldExist("mplt_title_shop", "sale_end_at")) {
|
||||
String sql = "UPDATE mplt_title_shop SET " +
|
||||
"sale_end_at_y = (sale_end_at / 10000), " +
|
||||
"sale_end_at_m = (sale_end_at % 10000 / 100), " +
|
||||
"sale_end_at_d = (sale_end_at % 100) " +
|
||||
"WHERE sale_end_at != -1;";
|
||||
DatabaseManager.instance.query(sql);
|
||||
new RemoveColumn("sale_end_at").IfExists().table("mplt_title_shop").execute();
|
||||
}
|
||||
|
||||
|
||||
TableColumn mplt_player_title_expire_at_y = new TableColumn("expire_at_y", FieldType.INT, false, false, true, false, -1);
|
||||
TableColumn mplt_player_title_expire_at_m = new TableColumn("expire_at_m", FieldType.INT, false, false, true, false, -1);
|
||||
TableColumn mplt_player_title_expire_at_d = new TableColumn("expire_at_d", FieldType.INT, false, false, true, false, -1);
|
||||
new AddColumn(mplt_player_title_expire_at_y).table("mplt_player_title").ifNotExists().execute();
|
||||
new AddColumn(mplt_player_title_expire_at_m).table("mplt_player_title").ifNotExists().execute();
|
||||
new AddColumn(mplt_player_title_expire_at_d).table("mplt_player_title").ifNotExists().execute();
|
||||
|
||||
if (IsFieldExist("mplt_player_title", "expire_at")) {
|
||||
String sql = "UPDATE mplt_player_title SET " +
|
||||
"expire_at_y = (expire_at / 10000), " +
|
||||
"expire_at_m = (expire_at % 10000 / 100), " +
|
||||
"expire_at_d = (expire_at % 100) " +
|
||||
"WHERE expire_at != -1;";
|
||||
DatabaseManager.instance.query(sql);
|
||||
new RemoveColumn("expire_at").IfExists().table("mplt_player_title").execute();
|
||||
}
|
||||
|
||||
Field mplt_title_id_field = new Field("id", -1);
|
||||
Field mplt_title_title_field = new Field("title", "default");
|
||||
Field mplt_title_description_field = new Field("description", "default");
|
||||
InsertRow mplt_title_default = new InsertRow().table("mplt_title");
|
||||
mplt_title_default.field(mplt_title_id_field)
|
||||
.field(mplt_title_title_field)
|
||||
.field(mplt_title_description_field).onConflictDoNothing(mplt_title_id_field);
|
||||
mplt_title_default.execute();
|
||||
|
||||
|
||||
TableColumn mplt_player_info_last_use_name = new TableColumn("last_use_name", FieldType.STRING, false, false, true, false, "'null'");
|
||||
new AddColumn(mplt_player_info_last_use_name).table("mplt_player_info").ifNotExists().execute();
|
||||
|
||||
// 3.0.6
|
||||
TableColumn mplt_player_info_coin_d = new TableColumn("coin_d", FieldType.DOUBLE, false, false, true, false, 0);
|
||||
TableColumn mplt_title_shop_price_d = new TableColumn("price_d", FieldType.DOUBLE, false, false, true, false, 0);
|
||||
new AddColumn(mplt_player_info_coin_d).table("mplt_player_info").ifNotExists().execute();
|
||||
new AddColumn(mplt_title_shop_price_d).table("mplt_title_shop").ifNotExists().execute();
|
||||
if (IsFieldExist("mplt_player_info", "coin")) {
|
||||
String sql = "UPDATE mplt_player_info SET coin_d = coin;";
|
||||
DatabaseManager.instance.query(sql);
|
||||
new RemoveColumn("coin").IfExists().table("mplt_player_info").execute();
|
||||
}
|
||||
if (IsFieldExist("mplt_title_shop", "price")) {
|
||||
String sql = "UPDATE mplt_title_shop SET price_d = price;";
|
||||
DatabaseManager.instance.query(sql);
|
||||
new RemoveColumn("price").IfExists().table("mplt_title_shop").execute();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package cn.lunadeer.miniplayertitle.utils;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.Style;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Notification {
|
||||
private static final Style i_style = Style.style(TextColor.color(139, 255, 123));
|
||||
private static final Style w_style = Style.style(TextColor.color(255, 185, 69));
|
||||
private static final Style e_style = Style.style(TextColor.color(255, 96, 72));
|
||||
|
||||
private static final String prefix = "[MiniPlayerTitle] ";
|
||||
|
||||
public static void info(Player player, String msg) {
|
||||
player.sendMessage(Component.text(prefix + msg, i_style));
|
||||
}
|
||||
|
||||
public static void warn(Player player, String msg) {
|
||||
player.sendMessage(Component.text(prefix + msg, w_style));
|
||||
}
|
||||
|
||||
public static void error(Player player, String msg) {
|
||||
player.sendMessage(Component.text(prefix + msg, e_style));
|
||||
}
|
||||
|
||||
public static void info(CommandSender sender, String msg) {
|
||||
sender.sendMessage(Component.text(prefix + msg, i_style));
|
||||
}
|
||||
|
||||
public static void warn(CommandSender sender, String msg) {
|
||||
sender.sendMessage(Component.text(prefix + msg, w_style));
|
||||
}
|
||||
|
||||
public static void error(CommandSender sender, String msg) {
|
||||
sender.sendMessage(Component.text(prefix + msg, e_style));
|
||||
}
|
||||
|
||||
public static void info(Player player, Component msg) {
|
||||
player.sendMessage(Component.text(prefix, i_style).append(msg));
|
||||
}
|
||||
|
||||
public static void warn(Player player, Component msg) {
|
||||
player.sendMessage(Component.text(prefix, w_style).append(msg));
|
||||
}
|
||||
|
||||
public static void error(Player player, Component msg) {
|
||||
player.sendMessage(Component.text(prefix, e_style).append(msg));
|
||||
}
|
||||
|
||||
public static void info(CommandSender player, Component msg) {
|
||||
player.sendMessage(Component.text(prefix, i_style).append(msg));
|
||||
}
|
||||
|
||||
public static void warn(CommandSender player, Component msg) {
|
||||
player.sendMessage(Component.text(prefix, w_style).append(msg));
|
||||
}
|
||||
|
||||
public static void error(CommandSender player, Component msg) {
|
||||
player.sendMessage(Component.text(prefix, e_style).append(msg));
|
||||
}
|
||||
}
|
@ -0,0 +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 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));
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
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;
|
||||
|
||||
|
||||
public class Line {
|
||||
private final List<Component> elements = new ArrayList<>();
|
||||
|
||||
private final TextComponent divider = Component.text(" - ", ViewStyles.sub_color);
|
||||
|
||||
public Line() {
|
||||
}
|
||||
|
||||
public TextComponent build() {
|
||||
TextComponent.Builder builder = Component.text();
|
||||
for (int i = 0; i < elements.size(); i++) {
|
||||
builder.append(elements.get(i));
|
||||
if (i != elements.size() - 1) {
|
||||
builder.append(divider);
|
||||
}
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
public static Line create() {
|
||||
return new Line();
|
||||
}
|
||||
|
||||
public Line append(TextComponent component) {
|
||||
elements.add(component);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Line append(Component component) {
|
||||
elements.add(component);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Line append(String component) {
|
||||
elements.add(Component.text(component));
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
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 (lines.isEmpty()) {
|
||||
Notification.warn(player, "没有数据");
|
||||
return;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
106
src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/View.java
Normal file
106
src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/View.java
Normal file
@ -0,0 +1,106 @@
|
||||
package cn.lunadeer.miniplayertitle.utils.STUI;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
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 {
|
||||
protected TextComponent title_decorate = Component.text("━", main_color);
|
||||
protected TextComponent space = Component.text(" ");
|
||||
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 List<TextComponent> content_lines = new ArrayList<>();
|
||||
protected TextComponent actionbar = Component.text(" ");
|
||||
protected TextComponent edge = Component.text("━━━━━━━━━━━━━━━━━━━━━━━━━━━━", main_color);
|
||||
protected TextComponent divide_line = Component.text("━━━━━━━━━━━━━━━━━━━━━━━━━━━━", main_color);
|
||||
|
||||
public void showOn(Player player) {
|
||||
player.sendMessage(edge);
|
||||
TextComponent.Builder builder = Component.text();
|
||||
int title_length = title.content().length();
|
||||
int title_width = title_length * 2 + 2;
|
||||
int decorate_count = divide_line.content().length() - title_width;
|
||||
for (int i = 0; i < decorate_count / 2; i++) {
|
||||
builder.append(title_decorate);
|
||||
}
|
||||
builder.append(space).append(title).append(space);
|
||||
for (int i = 0; i < decorate_count / 2; i++) {
|
||||
builder.append(title_decorate);
|
||||
}
|
||||
player.sendMessage(builder.build());
|
||||
if (subtitle.content().length() > 0) {
|
||||
player.sendMessage(divide_line);
|
||||
player.sendMessage(Component.text().append(sub_title_decorate).append(subtitle).build());
|
||||
}
|
||||
player.sendMessage(divide_line);
|
||||
for (TextComponent content_line : content_lines) {
|
||||
player.sendMessage(Component.text().append(line_decorate).append(content_line).build());
|
||||
}
|
||||
player.sendMessage(divide_line);
|
||||
player.sendMessage(Component.text().append(action_decorate).append(actionbar).build());
|
||||
player.sendMessage(edge);
|
||||
player.sendMessage(Component.text(" "));
|
||||
}
|
||||
|
||||
public static View create() {
|
||||
return new View();
|
||||
}
|
||||
|
||||
public View title(String title) {
|
||||
this.title = Component.text(title);
|
||||
return this;
|
||||
}
|
||||
|
||||
public View title(TextComponent title) {
|
||||
this.title = title;
|
||||
return this;
|
||||
}
|
||||
|
||||
public View subtitle(String subtitle) {
|
||||
this.subtitle = Component.text(subtitle);
|
||||
return this;
|
||||
}
|
||||
|
||||
public View subtitle(TextComponent subtitle) {
|
||||
this.subtitle = subtitle;
|
||||
return this;
|
||||
}
|
||||
|
||||
public View actionBar(TextComponent actionbar) {
|
||||
this.actionbar = actionbar;
|
||||
return this;
|
||||
}
|
||||
|
||||
public View actionBar(String actionbar) {
|
||||
this.actionbar = Component.text(actionbar);
|
||||
return this;
|
||||
}
|
||||
|
||||
public View actionBar(Line actionbar) {
|
||||
this.actionbar = actionbar.build();
|
||||
return this;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package cn.lunadeer.miniplayertitle.utils.STUI;
|
||||
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
|
||||
public class ViewStyles {
|
||||
public static TextColor main_color = TextColor.color(0, 148, 213);
|
||||
public static TextColor sub_color = TextColor.color(122, 122, 122);
|
||||
public static TextColor action_color = TextColor.color(251, 255, 139);
|
||||
public static TextColor error_color = TextColor.color(255, 96, 72);
|
||||
public static TextColor success_color = TextColor.color(139, 255, 123);
|
||||
}
|
56
src/main/java/cn/lunadeer/miniplayertitle/utils/XLogger.java
Normal file
56
src/main/java/cn/lunadeer/miniplayertitle/utils/XLogger.java
Normal file
@ -0,0 +1,56 @@
|
||||
package cn.lunadeer.miniplayertitle.utils;
|
||||
|
||||
import cn.lunadeer.miniplayertitle.MiniPlayerTitle;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class XLogger {
|
||||
private static final MiniPlayerTitle _plugin = MiniPlayerTitle.instance;
|
||||
private static final Logger _logger = _plugin.getLogger();
|
||||
|
||||
private static final String prefix = "[MiniPlayerTitle] ";
|
||||
|
||||
public static void info(Player player, String message) {
|
||||
Notification.info(player, prefix + "I | " + message);
|
||||
if (MiniPlayerTitle.config.isDebug())
|
||||
debug("来自玩家[ " + player.getName() + " ] 的信息 | " + message);
|
||||
}
|
||||
|
||||
public static void info(String message) {
|
||||
_logger.info(" I | " + message);
|
||||
}
|
||||
|
||||
public static void warn(Player player, String message) {
|
||||
Notification.warn(player, prefix + "W | " + message);
|
||||
if (MiniPlayerTitle.config.isDebug())
|
||||
debug("来自玩家[ " + player.getName() + " ] 的警告 | " + message);
|
||||
}
|
||||
|
||||
public static void warn(String message) {
|
||||
_logger.info(" W | " + message);
|
||||
}
|
||||
|
||||
public static void err(Player player, String message) {
|
||||
Notification.error(player, prefix + "E | " + message);
|
||||
if (MiniPlayerTitle.config.isDebug())
|
||||
debug("来自玩家[ " + player.getName() + " ] 的报错 | " + message);
|
||||
}
|
||||
|
||||
public static void err(String message) {
|
||||
_logger.info(" E | " + message);
|
||||
}
|
||||
|
||||
public static void debug(Player player, String message) {
|
||||
if (!MiniPlayerTitle.config.isDebug()) return;
|
||||
if (player.isOp())
|
||||
Notification.info(player, prefix + "D | " + message);
|
||||
else
|
||||
debug("来自玩家[ " + player.getName() + " ] 的调试 | " + message);
|
||||
}
|
||||
|
||||
public static void debug(String message) {
|
||||
if (!MiniPlayerTitle.config.isDebug()) return;
|
||||
_logger.info(" D | " + message);
|
||||
}
|
||||
}
|
@ -16,8 +16,4 @@ CustomCost:
|
||||
|
||||
DefaultCoin: 0
|
||||
|
||||
ExternalEco: false # 使用外部经济插件 需要Vault支持
|
||||
|
||||
CheckUpdate: true
|
||||
|
||||
Debug: false
|
@ -5,20 +5,8 @@ api-version: '1.20'
|
||||
description: 称号插件。
|
||||
website: https://lunadeer.cn
|
||||
folia-supported: true
|
||||
softdepend: [ PlaceholderAPI, Vault ]
|
||||
commands:
|
||||
MiniPlayerTitle:
|
||||
description: 称号插件命令
|
||||
usage: /mplt <cmd>
|
||||
aliases: [ mplt ]
|
||||
permission: mplt.command
|
||||
permissions:
|
||||
mplt.command:
|
||||
description: 普通命令
|
||||
default: true
|
||||
mplt.custom:
|
||||
description: 是否可以自定义称号
|
||||
default: true
|
||||
mplt.admin:
|
||||
description: 是否可以使用管理员命令
|
||||
default: op
|
||||
aliases: [mplt]
|
||||
|
Loading…
Reference in New Issue
Block a user