diff --git a/pom.xml b/pom.xml index 5721786..b2e3082 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ cn.lunadeer MiniPlayerTitle - 1.2-beta + 1.12-beta jar MiniPlayerTitle diff --git a/src/main/java/cn/lunadeer/miniplayertitle/Commands.java b/src/main/java/cn/lunadeer/miniplayertitle/Commands.java index 9bd0abd..8fb5380 100644 --- a/src/main/java/cn/lunadeer/miniplayertitle/Commands.java +++ b/src/main/java/cn/lunadeer/miniplayertitle/Commands.java @@ -2,7 +2,12 @@ package cn.lunadeer.miniplayertitle; 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.XLogger; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; @@ -35,7 +40,7 @@ public class Commands implements TabExecutor { switch (label) { case "mplt": if (args.length == 0) { - printHelp(sender); + home_view(sender); return true; } switch (args[0]) { @@ -89,6 +94,21 @@ public class Commands implements TabExecutor { } } + private void home_view(CommandSender sender){ + if (!(sender instanceof Player)){ + printHelp(sender); + return; + } + View view = View.create(); + view.title("称号系统"); + Component backpack = Button.create("称号背包", "/mplt list"); + Component shop = Button.create("称号商店", "/mplt shop"); + Line line = Line.create(); + line.append(backpack).append(shop); + view.set(View.Slot.ACTIONBAR, line); + view.showOn((Player) sender); + } + /** * Requests a list of possible completions for a command argument. * @@ -146,7 +166,7 @@ public class Commands implements TabExecutor { case "setamount": return Collections.singletonList("<商品ID> <数量> (-1为无限)"); case "setendat": - return Collections.singletonList("<商品ID> <结束时间戳>(-1为永久)"); + return Collections.singletonList("<商品ID> <结束时间YYYYMMDD>(-1为永久)"); default: return Arrays.asList("use", "list", "shop", "buy"); } diff --git a/src/main/java/cn/lunadeer/miniplayertitle/PlayerTitle.java b/src/main/java/cn/lunadeer/miniplayertitle/PlayerTitle.java index e402fe3..d4dd242 100644 --- a/src/main/java/cn/lunadeer/miniplayertitle/PlayerTitle.java +++ b/src/main/java/cn/lunadeer/miniplayertitle/PlayerTitle.java @@ -1,6 +1,7 @@ package cn.lunadeer.miniplayertitle; import cn.lunadeer.miniplayertitle.utils.Database; +import cn.lunadeer.miniplayertitle.utils.Time; import java.sql.ResultSet; import java.util.UUID; @@ -30,29 +31,25 @@ public class PlayerTitle extends Title { return null; } - public String getExpireAt() { + public String getExpireAtStr() { if (this._expire_at == -1L) { return "永久"; - } else if (this._expire_at < System.currentTimeMillis()) { + } else if (this._expire_at < Time.getCurrent()) { return "已过期"; } else { - return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date(this._expire_at)); + return this._expire_at.toString(); } } - public Long getExpireAtTimestamp() { - return this._expire_at; - } - public Boolean isExpired() { if (this._expire_at == -1L) { return false; } else { - return this._expire_at < System.currentTimeMillis(); + return this._expire_at < Time.getCurrent(); } } - public void setExpireAtTimestamp(Long expire_at) { + public void setExpireAt(Long expire_at) { this._expire_at = expire_at; this.save(); } @@ -62,7 +59,7 @@ public class PlayerTitle extends Title { 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 += "WHERE player_uuid = '" + _player_uuid.toString() + "' "; sql += "AND title_id = " + this._id + ";"; Database.query(sql); } diff --git a/src/main/java/cn/lunadeer/miniplayertitle/SaleTitle.java b/src/main/java/cn/lunadeer/miniplayertitle/SaleTitle.java index eba1f00..af86d94 100644 --- a/src/main/java/cn/lunadeer/miniplayertitle/SaleTitle.java +++ b/src/main/java/cn/lunadeer/miniplayertitle/SaleTitle.java @@ -2,6 +2,7 @@ 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; @@ -31,7 +32,7 @@ public class SaleTitle extends Title { try (ResultSet rs = Database.query(sql)) { if (rs != null && rs.next()) { Integer id = rs.getInt("id"); - return new SaleTitle(id, title_id, 0, 0, -1, System.currentTimeMillis()); + return new SaleTitle(id, title_id, 0, 0, 0, -1L); } } catch (Exception e) { XLogger.err("SaleTitle create failed: " + e.getMessage()); @@ -84,10 +85,10 @@ public class SaleTitle extends Title { public String getSaleEndAt() { if (this._sale_end_at == -1L) { return "常驻"; - } else if (this._sale_end_at < System.currentTimeMillis()) { + } else if (this._sale_end_at < Time.getCurrent()) { return "已停售"; } else { - return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date(this._sale_end_at)); + return this._sale_end_at.toString(); } } @@ -105,7 +106,7 @@ public class SaleTitle extends Title { if (this._sale_end_at == -1L) { return false; } else { - return this._sale_end_at < System.currentTimeMillis(); + return this._sale_end_at < Time.getCurrent(); } } diff --git a/src/main/java/cn/lunadeer/miniplayertitle/XPlayer.java b/src/main/java/cn/lunadeer/miniplayertitle/XPlayer.java index ad72552..7bb799a 100644 --- a/src/main/java/cn/lunadeer/miniplayertitle/XPlayer.java +++ b/src/main/java/cn/lunadeer/miniplayertitle/XPlayer.java @@ -5,6 +5,7 @@ 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.Time; import cn.lunadeer.miniplayertitle.utils.XLogger; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; @@ -58,7 +59,7 @@ public class XPlayer { Component button = Button.create(is_using ? "卸下" : "使用", "/mplt use " + (is_using ? -1 : title.getId())); line.append(idx) .append(title.getTitle()) - .append(Component.text("有效期至:" + title.getExpireAt())) + .append(Component.text("有效期至:" + title.getExpireAtStr())) .append(button); view.set(i, line); } @@ -69,15 +70,16 @@ public class XPlayer { public void updateUsingTitle(Integer title_id) { _current_title_id = title_id; checkTitleValid(); - if (_current_title_id == -1) { - return; - } String sql = ""; sql += "UPDATE mplt_player_using_title "; sql += "SET title_id = " + _current_title_id + ", "; sql += "updated_at = CURRENT_TIMESTAMP "; sql += "WHERE uuid = '" + _player.getUniqueId().toString() + "';"; Database.query(sql); + if (_current_title_id == -1) { + Notification.info(_player, "成功卸下称号"); + return; + } Notification.info(_player, "成功使用称号: "); Notification.info(_player, _titles.get(_current_title_id).getTitle()); } @@ -201,30 +203,35 @@ public class XPlayer { Notification.error(_player, "你的余额不足"); return; } - if (!_titles.containsKey(title.getId())) { - _titles.put(title.getId(), PlayerTitle.create(title.getId(), _player.getUniqueId())); - } else if (_titles.get(title.getId()).getExpireAtTimestamp() == -1) { - Notification.warn(_player, "你已经拥有此称号"); + 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; } - PlayerTitle title_bought = _titles.get(title.getId()); set_coin(_coin - title.getPrice()); SaleTitle.setAmount(title.getId(), title.getAmount() - 1); Notification.info(_player, "成功购买称号: "); Notification.info(_player, title.getTitle()); + if (title.getDays() == -1) { - title_bought.setExpireAtTimestamp(-1L); + title_bought.setExpireAt(-1L); + Notification.info(_player, "称号已购买至永久"); return; } - if (title_bought.isExpired()) { - title_bought.setExpireAtTimestamp(System.currentTimeMillis() + title.getDays() * 24 * 60 * 60 * 1000L); - Notification.info(_player, title.getTitle()); - Notification.info(_player, "称号已激活,有效期至 " + title_bought.getExpireAt()); - } else { - title_bought.setExpireAtTimestamp(title_bought.getExpireAtTimestamp() + title.getDays() * 24 * 60 * 60 * 1000L); - Notification.info(_player, title.getTitle()); - Notification.info(_player, "称号已续期至 " + title_bought.getExpireAt()); - } + Long timestamp = System.currentTimeMillis() + title.getDays() * 24 * 60 * 60 * 1000L; + title_bought.setExpireAt((long) Time.getFromTimestamp(timestamp)); + Notification.info(_player, title.getTitle()); + Notification.info(_player, "称号已购买至 " + title_bought.getExpireAtStr()); + } public void setTitle(Integer title_id, Long expire_at) { @@ -232,8 +239,8 @@ public class XPlayer { _titles.put(title_id, PlayerTitle.create(title_id, _player.getUniqueId())); } PlayerTitle title = _titles.get(title_id); - title.setExpireAtTimestamp(expire_at); + title.setExpireAt(expire_at); Notification.info(_player, title.getTitle()); - Notification.info(_player, "获得称号,有效期至 " + title.getExpireAt()); + Notification.info(_player, "获得称号,有效期至 " + title.getExpireAtStr()); } } diff --git a/src/main/java/cn/lunadeer/miniplayertitle/commands/AdminCommands.java b/src/main/java/cn/lunadeer/miniplayertitle/commands/AdminCommands.java index 6156d8b..37a3c34 100644 --- a/src/main/java/cn/lunadeer/miniplayertitle/commands/AdminCommands.java +++ b/src/main/java/cn/lunadeer/miniplayertitle/commands/AdminCommands.java @@ -179,19 +179,13 @@ public class AdminCommands { return true; } long time_stamp; - if (Integer.parseInt(args[2]) == -1) { - time_stamp = -1; - } else { - // 字符串转时间戳 - java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat("yyyyMMdd"); - try { - java.util.Date date = simpleDateFormat.parse(args[2]); - time_stamp = date.getTime(); - } catch (Exception e) { - Notification.error(sender, "时间格式错误"); - return true; - } + try { + time_stamp = Long.parseLong(args[2]); + } catch (Exception e) { + Notification.error(sender, "时间格式错误"); + return true; } + SaleTitle.setSaleEndAt(Integer.parseInt(args[1]), time_stamp); return true; } diff --git a/src/main/java/cn/lunadeer/miniplayertitle/utils/Notification.java b/src/main/java/cn/lunadeer/miniplayertitle/utils/Notification.java index b02cf63..c0a7255 100644 --- a/src/main/java/cn/lunadeer/miniplayertitle/utils/Notification.java +++ b/src/main/java/cn/lunadeer/miniplayertitle/utils/Notification.java @@ -42,11 +42,11 @@ public class Notification { } public static void warn(Player player, Component msg) { - player.sendMessage(Component.text(prefix + msg, w_style).append(msg)); + player.sendMessage(Component.text(prefix, w_style).append(msg)); } public static void error(Player player, Component msg) { - player.sendMessage(Component.text(prefix + msg, e_style).append(msg)); + player.sendMessage(Component.text(prefix, e_style).append(msg)); } public static void info(CommandSender player, Component msg) { @@ -54,10 +54,10 @@ public class Notification { } public static void warn(CommandSender player, Component msg) { - player.sendMessage(Component.text(prefix + msg, w_style).append(msg)); + player.sendMessage(Component.text(prefix, w_style).append(msg)); } public static void error(CommandSender player, Component msg) { - player.sendMessage(Component.text(prefix + msg, e_style).append(msg)); + player.sendMessage(Component.text(prefix, e_style).append(msg)); } } diff --git a/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/View.java b/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/View.java index 0746d31..1ec2f53 100644 --- a/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/View.java +++ b/src/main/java/cn/lunadeer/miniplayertitle/utils/STUI/View.java @@ -70,6 +70,7 @@ public class View { player.sendMessage(Component.text().append(line_decorate).append(content_line4).build()); player.sendMessage(Component.text().append(action_decorate).append(actionbar).build()); player.sendMessage(bottom_decorate); + player.sendMessage(Component.text(" ")); } public static View create() { diff --git a/src/main/java/cn/lunadeer/miniplayertitle/utils/Time.java b/src/main/java/cn/lunadeer/miniplayertitle/utils/Time.java new file mode 100644 index 0000000..8d49b7d --- /dev/null +++ b/src/main/java/cn/lunadeer/miniplayertitle/utils/Time.java @@ -0,0 +1,13 @@ +package cn.lunadeer.miniplayertitle.utils; + +public class Time { + + public static Integer getCurrent() { + // return YYYYMMDD + return Integer.parseInt(new java.text.SimpleDateFormat("yyyyMMdd").format(new java.util.Date())); + } + + public static Integer getFromTimestamp(Long timestamp_ms) { + return Integer.parseInt(new java.text.SimpleDateFormat("yyyyMMdd").format(new java.util.Date(timestamp_ms))); + } +}