finish main function

This commit is contained in:
zhangyuheng 2024-01-08 00:46:18 +08:00
parent ecb3757c98
commit a06f88ecda
7 changed files with 356 additions and 96 deletions

View File

@ -1,5 +1,6 @@
package cn.lunadeer.newbtitle;
import cn.lunadeer.newbtitle.commands.AdminCommands;
import cn.lunadeer.newbtitle.utils.Notification;
import cn.lunadeer.newbtitle.utils.XLogger;
import org.bukkit.command.Command;
@ -11,6 +12,8 @@ import org.jetbrains.annotations.Nullable;
import java.util.*;
import static cn.lunadeer.newbtitle.commands.PlayerCommands.*;
public class Commands implements TabExecutor {
/**
* Executes the given command, returning its success.
@ -46,12 +49,34 @@ public class Commands implements TabExecutor {
return shop(sender, args);
case "buy":
return buy(sender, args);
case "create":
return AdminCommands.createTitle(sender, args);
case "delete":
return AdminCommands.deleteTitle(sender, args);
case "setdesc":
return AdminCommands.setTitleDescription(sender, args);
case "setname":
return AdminCommands.setTitleName(sender, args);
case "addshop":
return AdminCommands.addShop(sender, args);
case "removeshop":
return AdminCommands.removeShop(sender, args);
case "setprice":
return AdminCommands.setPrice(sender, args);
case "setamount":
return AdminCommands.setAmount(sender, args);
case "setendat":
return AdminCommands.setSaleEndAt(sender, args);
default:
if (sender instanceof Player) {
Player player = (Player) sender;
Notification.warn(player, "用法: /nt <use|list|shop|buy>");
if (player.isOp()){
Notification.warn(player, "用法: /nt <create|delete|setdesc|setname|addshop|removeshop|setprice|setamount|setendat>");
}
} else {
XLogger.info("用法: /nt <use|list|shop|buy>");
XLogger.info("用法: /nt <create|delete|setdesc|setname|addshop|removeshop|setprice|setamount|setendat>");
}
return true;
}
@ -78,7 +103,22 @@ public class Commands implements TabExecutor {
switch (label) {
case "nt":
if (args.length == 0) {
return Arrays.asList("use", "list", "shop", "buy");
String[] player_cmd = {"use", "list", "shop", "buy"};
String[] admin_cmd = {"create", "delete", "setdesc", "setname", "addshop", "removeshop", "setprice", "setamount", "setendat"};
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;
}
switch (args[0]) {
case "use":
@ -89,6 +129,24 @@ public class Commands implements TabExecutor {
return Collections.singletonList("页数(可选)");
case "buy":
return Collections.singletonList("要购买的条目ID");
case "create":
return Collections.singletonList("<称号名称> <称号描述>");
case "delete":
return Collections.singletonList("<称号ID>");
case "setdesc":
return Collections.singletonList("<称号ID> <称号描述>");
case "setname":
return Collections.singletonList("<称号ID> <称号名称>");
case "addshop":
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> <结束时间戳>(-1为永久)");
default:
return Arrays.asList("use", "list", "shop", "buy");
}
@ -97,79 +155,5 @@ public class Commands implements TabExecutor {
}
}
private static boolean use(CommandSender sender, String[] args) {
if (!(sender instanceof Player)) {
XLogger.warn("该命令只能由玩家执行");
return true;
}
Player player = (Player) sender;
if (args.length == 0) {
Notification.warn(player, "用法: /nt use <称号ID>");
return true;
}
XPlayer xPlayer = new XPlayer(player);
Integer title_id = Integer.parseInt(args[0]);
xPlayer.updateUsingTitle(title_id);
return true;
}
private static boolean list(CommandSender sender, String[] args) {
if (!(sender instanceof Player)) {
XLogger.warn("该命令只能由玩家执行");
return true;
}
Player player = (Player) sender;
int page = 1;
if (args.length != 0) {
try {
page = Integer.parseInt(args[0]);
} catch (Exception e) {
Notification.warn(player, "用法: /nt list <页数>");
return true;
}
}
XPlayer xPlayer = new XPlayer(player);
xPlayer.openBackpack(page);
return true;
}
private static boolean shop(CommandSender sender, String[] args) {
if (!(sender instanceof Player)) {
XLogger.warn("该命令只能由玩家执行");
return true;
}
Player player = (Player) sender;
int page = 1;
if (args.length != 0) {
try {
page = Integer.parseInt(args[0]);
} catch (Exception e) {
Notification.warn(player, "用法: /nt shop <页数>");
return true;
}
}
Shop.open(player, page);
return true;
}
private static boolean buy(CommandSender sender, String[] args) {
if (!(sender instanceof Player)) {
XLogger.warn("该命令只能由玩家执行");
return true;
}
Player player = (Player) sender;
if (args.length == 0) {
Notification.warn(player, "用法: /nt buy <称号ID>");
return true;
}
XPlayer xPlayer = new XPlayer(player);
Integer sale_id = Integer.parseInt(args[0]);
SaleTitle saleTitle = Shop.getSaleTitle(sale_id);
if (saleTitle == null) {
Notification.error(player, "该称号不存在");
return true;
}
xPlayer.buyTitle(saleTitle);
return true;
}
}

View File

@ -23,6 +23,24 @@ public class SaleTitle extends Title {
this._sale_end_at = sale_end_at;
}
public static SaleTitle create(Integer title_id) {
String sql = "";
sql += "INSERT INTO nt_title_shop (title_id, price, days, amount, sale_end_at) ";
sql += "VALUES (" + title_id + ", 0, 0, 0, -1) ";
sql += "RETURNING id;";
ResultSet rs = Database.query(sql);
try {
if (rs != null && rs.next()) {
Integer id = rs.getInt("id");
SaleTitle title = new SaleTitle(id, title_id, 0, 0, -1, System.currentTimeMillis());
return title;
}
} catch (Exception e) {
XLogger.err("SaleTitle create failed: " + e.getMessage());
}
return null;
}
public Integer getPrice() {
return this._price;
}

View File

@ -58,22 +58,6 @@ public class Shop {
view.showOn(player);
}
public static void addTitle(Integer title_id) {
String sql = "";
sql += "INSERT INTO nt_title_shop (title_id, price, days, amount, sale_end_at) ";
sql += "VALUES (" + title_id + ", 0, 0, -1, CURRENT_TIMESTAMP) ";
sql += "RETURNING id;";
ResultSet rs = Database.query(sql);
try {
if (rs != null && rs.next()) {
Integer id = rs.getInt("id");
SaleTitle title = new SaleTitle(id, title_id, 0, 0, -1, System.currentTimeMillis());
}
} catch (Exception e) {
XLogger.err("SaleTitle create failed: " + e.getMessage());
}
}
public static void deleteTitle(Integer id) {
String sql = "";
sql += "DELETE FROM nt_title_shop WHERE id = " + id + ";";

View File

@ -19,11 +19,22 @@ public class Title {
protected String _updated_at;
JoinConfiguration join = JoinConfiguration.separator(Component.text(" "));
public Title(String title, String description) {
this._title = title;
this._description = description;
this._enabled = true;
this.save();
public static Title create(String title, String description) {
String sql = "";
sql += "INSERT INTO nt_title (title, description, enabled) VALUES (";
sql += "'" + title + "', ";
sql += "'" + description + "', ";
sql += "true ";
sql += ") RETURNING id;";
ResultSet rs = Database.query(sql);
try {
if (rs != null && rs.next()) {
return new Title(rs.getInt("id"));
}
} catch (Exception e) {
XLogger.err("Title create failed: " + e.getMessage());
}
return null;
}
public Title(Integer id) {

View File

@ -0,0 +1,163 @@
package cn.lunadeer.newbtitle.commands;
import cn.lunadeer.newbtitle.SaleTitle;
import cn.lunadeer.newbtitle.Shop;
import cn.lunadeer.newbtitle.Title;
import org.bukkit.entity.Player;
import cn.lunadeer.newbtitle.utils.Notification;
import cn.lunadeer.newbtitle.utils.XLogger;
import org.bukkit.command.CommandSender;
public class AdminCommands {
public static boolean 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 true;
}
}
if (args.length != 2) {
Notification.warn(sender, "用法: /nt create <称号名称> <称号描述>");
return true;
}
Title.create(args[0], args[1]);
return true;
}
public static boolean 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 true;
}
}
if (args.length != 1) {
Notification.warn(sender, "用法: /nt delete <称号ID>");
return true;
}
Title.delete(Integer.parseInt(args[0]));
return true;
}
public static boolean 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 true;
}
}
if (args.length != 2) {
Notification.warn(sender, "用法: /nt setdesc <称号ID> <称号描述>");
return true;
}
Title title = new Title(Integer.parseInt(args[0]));
title.setDescription(args[1]);
return true;
}
public static boolean 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 true;
}
}
if (args.length != 2) {
Notification.warn(sender, "用法: /nt setname <称号ID> <称号名称>");
return true;
}
Title title = new Title(Integer.parseInt(args[0]));
title.setTitle(args[1]);
return true;
}
public static boolean 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 true;
}
}
if (args.length != 2) {
Notification.warn(sender, "用法: /nt addshop <称号ID>");
return true;
}
SaleTitle title = SaleTitle.create(Integer.parseInt(args[0]));
if (title == null) {
Notification.error(sender, "添加商品失败");
} else {
Notification.info(sender, "已添加称号 " + title.getTitle() + " 到商店, 商品ID: " + title.getId());
}
return true;
}
public static boolean 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 true;
}
}
if (args.length != 1) {
Notification.warn(sender, "用法: /nt removeshop <商品ID>");
return true;
}
SaleTitle.delete(Integer.parseInt(args[0]));
return true;
}
public static boolean 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 true;
}
}
if (args.length != 4) {
Notification.warn(sender, "用法: /nt setprice <商品ID> <价格> <天数>(-1为永久)");
return true;
}
SaleTitle.setPrice(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
SaleTitle.setDays(Integer.parseInt(args[0]), Integer.parseInt(args[2]));
return true;
}
public static boolean 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 true;
}
}
if (args.length != 2) {
Notification.warn(sender, "用法: /nt setamount <商品ID> <数量>(-1为无限)");
return true;
}
SaleTitle.setAmount(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
return true;
}
public static boolean 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 true;
}
}
if (args.length != 2) {
Notification.warn(sender, "用法: /nt setendat <商品ID> <时间戳>(-1为永久)");
return true;
}
SaleTitle.setSaleEndAt(Integer.parseInt(args[0]), Long.parseLong(args[1]));
return true;
}
}

View File

@ -0,0 +1,87 @@
package cn.lunadeer.newbtitle.commands;
import cn.lunadeer.newbtitle.SaleTitle;
import cn.lunadeer.newbtitle.Shop;
import cn.lunadeer.newbtitle.XPlayer;
import cn.lunadeer.newbtitle.utils.Notification;
import cn.lunadeer.newbtitle.utils.XLogger;
import org.bukkit.command.CommandSender;
public class PlayerCommands {
public static boolean use(CommandSender sender, String[] args) {
if (!(sender instanceof org.bukkit.entity.Player)) {
XLogger.warn("该命令只能由玩家执行");
return true;
}
org.bukkit.entity.Player player = (org.bukkit.entity.Player) sender;
if (args.length == 0) {
Notification.warn(player, "用法: /nt use <称号ID>");
return true;
}
XPlayer xPlayer = new XPlayer(player);
Integer title_id = Integer.parseInt(args[0]);
xPlayer.updateUsingTitle(title_id);
return true;
}
public static boolean list(CommandSender sender, String[] args) {
if (!(sender instanceof org.bukkit.entity.Player)) {
XLogger.warn("该命令只能由玩家执行");
return true;
}
org.bukkit.entity.Player player = (org.bukkit.entity.Player) sender;
int page = 1;
if (args.length != 0) {
try {
page = Integer.parseInt(args[0]);
} catch (Exception e) {
Notification.warn(player, "用法: /nt list <页数>");
return true;
}
}
XPlayer xPlayer = new XPlayer(player);
xPlayer.openBackpack(page);
return true;
}
public static boolean shop(CommandSender sender, String[] args) {
if (!(sender instanceof org.bukkit.entity.Player)) {
XLogger.warn("该命令只能由玩家执行");
return true;
}
org.bukkit.entity.Player player = (org.bukkit.entity.Player) sender;
int page = 1;
if (args.length != 0) {
try {
page = Integer.parseInt(args[0]);
} catch (Exception e) {
Notification.warn(player, "用法: /nt shop <页数>");
return true;
}
}
Shop.open(player, page);
return true;
}
public static boolean buy(CommandSender sender, String[] args) {
if (!(sender instanceof org.bukkit.entity.Player)) {
XLogger.warn("该命令只能由玩家执行");
return true;
}
org.bukkit.entity.Player player = (org.bukkit.entity.Player) sender;
if (args.length == 0) {
Notification.warn(player, "用法: /nt buy <称号ID>");
return true;
}
XPlayer xPlayer = new XPlayer(player);
Integer sale_id = Integer.parseInt(args[0]);
SaleTitle saleTitle = Shop.getSaleTitle(sale_id);
if (saleTitle == null) {
Notification.error(player, "该称号不存在");
return true;
}
xPlayer.buyTitle(saleTitle);
return true;
}
}

View File

@ -3,6 +3,7 @@ package cn.lunadeer.newbtitle.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 {
@ -21,4 +22,16 @@ public class Notification {
public static void error(Player player, String msg) {
player.sendMessage(Component.text("[NewbTitle] " + msg, e_style));
}
public static void info(CommandSender sender, String msg) {
sender.sendMessage(Component.text("[NewbTitle] " + msg, i_style));
}
public static void warn(CommandSender sender, String msg) {
sender.sendMessage(Component.text("[NewbTitle] " + msg, w_style));
}
public static void error(CommandSender sender, String msg) {
sender.sendMessage(Component.text("[NewbTitle] " + msg, e_style));
}
}