From a4ec6935bd47d096c93dcd3422072a1c71bf7d3a Mon Sep 17 00:00:00 2001 From: tianyu <32282861@qq.com> Date: Wed, 24 Jul 2024 19:32:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 40 +++++ pom.xml | 42 +++++ src/main/java/me/Demon/DemonPay/Main.java | 151 ++++++++++++++++++ .../java/me/Demon/DemonPay/PLimitData.java | 59 +++++++ .../Demon/DemonPay/command/DpayCommand.java | 74 +++++++++ .../Demon/DemonPay/command/MpayCommand.java | 59 +++++++ .../java/me/Demon/DemonPay/util/SqlUtil.java | 108 +++++++++++++ .../DemonPay/util/database/MegumiSQL.java | 15 ++ .../DemonPay/util/database/SqlManager.java | 93 +++++++++++ src/main/resources/plugin.yml | 7 + 10 files changed, 648 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/me/Demon/DemonPay/Main.java create mode 100644 src/main/java/me/Demon/DemonPay/PLimitData.java create mode 100644 src/main/java/me/Demon/DemonPay/command/DpayCommand.java create mode 100644 src/main/java/me/Demon/DemonPay/command/MpayCommand.java create mode 100644 src/main/java/me/Demon/DemonPay/util/SqlUtil.java create mode 100644 src/main/java/me/Demon/DemonPay/util/database/MegumiSQL.java create mode 100644 src/main/java/me/Demon/DemonPay/util/database/SqlManager.java create mode 100644 src/main/resources/plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c037f46 --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store +/.idea/ +/out/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..715264e --- /dev/null +++ b/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + org.example + DemonPay + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + + + public-rpg + https://repo.aurora-pixels.com/repository/public-rpg/ + + + + + + org.spigotmc + spigot-api + 1.12.2 + + + me.Demon.DemonPlugin + DemonAPI + 1.2.0 + + + cn.hamster3.cdapi + CDTimeAPI + 1.0 + + + + \ No newline at end of file diff --git a/src/main/java/me/Demon/DemonPay/Main.java b/src/main/java/me/Demon/DemonPay/Main.java new file mode 100644 index 0000000..96d7e05 --- /dev/null +++ b/src/main/java/me/Demon/DemonPay/Main.java @@ -0,0 +1,151 @@ +package me.Demon.DemonPay; + +import me.Demon.DemonPay.command.DpayCommand; +import me.Demon.DemonPay.command.MpayCommand; +import me.Demon.DemonPay.util.SqlUtil; +import me.Demon.DemonPay.util.database.SqlManager; +import me.Demon.DemonPlugin.DemonAPI; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.HashMap; + +public class Main extends JavaPlugin { + + public static Main plugin; + public static String prefix = "§7[§6货币贸易§7] §f"; + public static HashMap dataAPI = new HashMap<>(); + private SqlUtil sqlUtil; + private SqlManager sqlManager; + + public SqlUtil getSqlUtil() { + return sqlUtil; + } + + public SqlManager getSqlManager() { + return sqlManager; + } + + public void onEnable() { + plugin = this; + getCommand("dpay").setExecutor(new DpayCommand()); + getCommand("pay").setExecutor(new MpayCommand()); + Bukkit.getConsoleSender().sendMessage("§b[DemonPay] §a插件成功载入Sever!"); + Bukkit.getConsoleSender().sendMessage("§b[DemonPay] §a作者QQ:1763917516"); + // 数据库配置 +// sqlManager = new SqlManager(this); +// String SQL_Host = DemonMySql.SQL_Host; +// String SQL_Port = DemonMySql.SQL_Port; +// String SQL_Database = DemonMySql.SQL_Database; +// String SQL_Users = DemonMySql.SQL_Users; +// String SQL_Password = DemonMySql.SQL_Password; +// sqlUtil = new SqlUtil(this, SQL_Host, SQL_Port, SQL_Database, SQL_Users, SQL_Password); +// sqlManager.createTable(); + } + + public void onDisable() { + Bukkit.getConsoleSender().sendMessage("§b[DemonPay] §c插件成功卸载!"); + } + + public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args) { + if ((CommandLabel.equalsIgnoreCase("eco")) && (sender.isOp())) { + if (args.length == 0) { + sender.sendMessage("======== " + prefix + "========"); + sender.sendMessage("§7/eco look §6: 查询某玩家的金币"); + sender.sendMessage("§7/eco give [money] §6: 给予某玩家金币"); + sender.sendMessage("§7/eco giveall [money] §6: 给予在线玩家金币"); + sender.sendMessage("§7/eco take [money] §6: 扣除某玩家的金币"); + sender.sendMessage("§7/eco set [money] §6: 设置某玩家的金币"); + sender.sendMessage("§7/eco reset §c: 重置某玩家的金币为0"); + return true; + } + if (args.length == 2) { + if (args[0].equalsIgnoreCase("reset")) { + OfflinePlayer player = Bukkit.getOfflinePlayer(args[1]); + DemonAPI.takeMoney(player, DemonAPI.seeMoney(player)); + sender.sendMessage(prefix + "成功清空玩家§a" + player.getName() + "§f的金币."); + } + } + if (args.length == 2) { + if (args[0].equalsIgnoreCase("look")) { + OfflinePlayer player = Bukkit.getOfflinePlayer(args[1]); + sender.sendMessage(prefix + "玩家 §a" + player.getName() + " §f有 §a" + DemonAPI.seeMoney(player) + " §f金币"); + } + if (args[0].equalsIgnoreCase("giveall")) { + int money = Integer.parseInt(args[1]); + int amount = 0; + for (Player allp : Bukkit.getOnlinePlayers()) { + allp.sendMessage(prefix + "你从 §c管理员 §f那收到了 §e" + money + " §f金币"); + DemonAPI.giveMoney(allp, money); + amount++; + } + sender.sendMessage(prefix + "成功给在线 §a" + amount + " §f名玩家发放金币 总计: §e" + (money * amount) + " §f金币"); + } + } + if (args.length == 3) { + if (args[0].equalsIgnoreCase("give")) { + OfflinePlayer player = Bukkit.getOfflinePlayer(args[1]); + int money = Integer.parseInt(args[2]); + if (money >= 1) { + DemonAPI.giveMoney(player, money); + sender.sendMessage(prefix + "玩家 §a" + player.getName() + " §f有 §a" + DemonAPI.seeMoney(player) + " §f金币"); + } + } + if (args[0].equalsIgnoreCase("take")) { + + OfflinePlayer player = Bukkit.getOfflinePlayer(args[1]); + int money = Integer.parseInt(args[2]); + if (money >= 1) { + if (DemonAPI.hasMoney(player, money)) { + DemonAPI.takeMoney(player, money); + sender.sendMessage(prefix + "玩家 §a" + player.getName() + " §f有 §a" + DemonAPI.seeMoney(player) + " §f金币"); + } else { + sender.sendMessage(prefix + "没有足够的金币用于扣除."); + } + } + } + if (args[0].equalsIgnoreCase("set")) { + OfflinePlayer player = Bukkit.getOfflinePlayer(args[1]); + //获取玩家要设置的金额数 + int money = Integer.parseInt(args[2]); + //判断玩家余额是否大于Money + if (DemonAPI.seeMoney(player) >= money) { + //获取玩家余额 + int Y = (int) DemonAPI.seeMoney(player); + // Y = 50000 + // X = 80000 + //我想得出 Z = 30000 + //也就是 X - Y = Z + int Z = money - Y; + DemonAPI.giveMoney(player, Z); + sender.sendMessage(prefix + "玩家 §a" + player.getName() + " §f有 §a" + DemonAPI.seeMoney(player) + " §f金币"); + } else { + //获取玩家余额 + int Y = (int) DemonAPI.seeMoney(player); + // Y = 50000 + // X = 1000 + //我想得出 Z = 49000 + //也就是 Y - (Y-X) = Z + int Z = Y - (Y - money); + DemonAPI.takeMoney(player, (Y - Z)); + sender.sendMessage(prefix + "玩家 §a" + player.getName() + " §f有 §a" + DemonAPI.seeMoney(player) + " §f金币"); + } + } + } + } + return true; + } + + public static boolean isInt(String s) { + try { + Integer.parseInt(s); + } catch (NumberFormatException nfe) { + return false; + } + return true; + } +} diff --git a/src/main/java/me/Demon/DemonPay/PLimitData.java b/src/main/java/me/Demon/DemonPay/PLimitData.java new file mode 100644 index 0000000..bd0c0d8 --- /dev/null +++ b/src/main/java/me/Demon/DemonPay/PLimitData.java @@ -0,0 +1,59 @@ +package me.Demon.DemonPay; + +import me.Demon.DemonPay.util.database.SqlManager; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import java.util.Date; + +public class PLimitData { + + private final Player p; + + private int limit; + + private String updata_time; + + public PLimitData(Player p){ + this.p = p; + SqlManager sqlManager = Main.plugin.getSqlManager(); + String playerData = sqlManager.getPlayerData(p.getName()); + if(playerData == null){ + sqlManager.createPlayerTableData(p); + playerData = sqlManager.getPlayerData(p.getName()); + } + // 获取数据库中上次数据刷新时间 + String Todaytime = playerData.split("#")[1]; + Date date = new Date(); + // 获取今天是几月几号 时间是否与数据库数据匹配 + String time = date.getMonth()+"."+date.getDate(); + if(Todaytime.equalsIgnoreCase(time)) { + this.limit = Integer.parseInt(playerData.split("#")[0]); + this.updata_time = playerData.split("#")[1]; + }else{ + this.updata_time = time; + this.limit = 200; + if(p.hasPermission("vip1.use")){ + this.limit = 800; + }else if(p.hasPermission("yuanzu.use")){ + this.limit = 600; + }else if(p.hasPermission("lianjinshi.use")){ + this.limit = 400; + } + Bukkit.getConsoleSender().sendMessage("§d[点券限额] §f玩家"+p.getName()+"的每日额度已刷新."); + Main.plugin.getSqlManager().SaverPlayerData(p); + } + } + + public int getLimit() { + return limit; + } + + public void setLimit(int limit) { + this.limit = limit; + } + + public String getUpdata_time() { + return updata_time; + } +} diff --git a/src/main/java/me/Demon/DemonPay/command/DpayCommand.java b/src/main/java/me/Demon/DemonPay/command/DpayCommand.java new file mode 100644 index 0000000..9060761 --- /dev/null +++ b/src/main/java/me/Demon/DemonPay/command/DpayCommand.java @@ -0,0 +1,74 @@ +package me.Demon.DemonPay.command; + +import cn.hamster3.cdapi.CDTimeAPI; +import me.Demon.DemonPay.Main; +import me.Demon.DemonPay.PLimitData; +import me.Demon.DemonPlugin.DemonAPI; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class DpayCommand implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + String prefix = Main.prefix; + Player p = (Player) sender; + if(!p.hasPermission("demonpay.points")){ + sender.sendMessage(prefix+"你需要成为 §c【领域炼金师】 §f才能使用此命令."); + return true; + } + if (args.length == 2) { + if(!Main.isInt(args[1])){ + sender.sendMessage(prefix+"正确用法: §a/dpay [玩家ID] [金额]"); + return true; + } + Player otherPlayer = Bukkit.getPlayer(args[0]); + if (otherPlayer == null) { + sender.sendMessage(prefix + "目标玩家不在线"); + return true; + } + if (p.getName().equalsIgnoreCase(otherPlayer.getName())){ + sender.sendMessage(prefix + "无法向自己发起转账"); + return true; + } + if (CDTimeAPI.getCD(p.getUniqueId(), "PayCD") > 0) { + sender.sendMessage(prefix + "支付系统正在冷却..."); + return true; + } + int points = Integer.parseInt(args[1]); + if (points < 5) { + sender.sendMessage(prefix + "支付金额必须大于 §d5.0点券"); + return true; + } + if (!DemonAPI.hasPoints(p, points)) { + sender.sendMessage(prefix + "没有足够的点券!"); + return true; + } + // 若无此权限将会进行额度检测 + if(!p.hasPermission("demonpay.pointspro")) { + if (Main.dataAPI.get(p) == null) { + Main.dataAPI.put(p, new PLimitData(p)); + } + PLimitData dataAPI = Main.dataAPI.get(p); + int limit = dataAPI.getLimit(); + int newLimit = limit - points; + if (newLimit <= -1) { + sender.sendMessage(prefix+"转账失败,您的今日点券额度不足."); + return true; + } + dataAPI.setLimit(newLimit); + } + DemonAPI.takePoints(p, points); + DemonAPI.givePoints(otherPlayer, points); + CDTimeAPI.setPlayerCD(p.getUniqueId(),"PayCD",1000 * 10); + sender.sendMessage(prefix+"你向 §a" + otherPlayer.getName() + " §f支付了 §a" + points + " §f点券"); + otherPlayer.sendMessage(prefix+"你从 §a" + sender.getName() + " §f收到了 §a" + points + " §f点券"); + } else { + sender.sendMessage(prefix+"正确用法: §a/dpay [玩家ID] [金额]"); + } + return false; + } +} diff --git a/src/main/java/me/Demon/DemonPay/command/MpayCommand.java b/src/main/java/me/Demon/DemonPay/command/MpayCommand.java new file mode 100644 index 0000000..98b44bb --- /dev/null +++ b/src/main/java/me/Demon/DemonPay/command/MpayCommand.java @@ -0,0 +1,59 @@ +package me.Demon.DemonPay.command; + +import cn.hamster3.cdapi.CDTimeAPI; +import me.Demon.DemonPay.Main; +import me.Demon.DemonPlugin.DemonAPI; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class MpayCommand implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + String prefix = Main.prefix; + Player p = (Player) sender; + if (!p.hasPermission("demonpay.money")) { + sender.sendMessage(prefix + "你需要成为 §2【领域农民】 §f才能使用此命令."); + return true; + } + if (args.length == 2) { + if (!Main.isInt(args[1])) { + sender.sendMessage(prefix + "正确用法: §a/pay [玩家ID] [金额]"); + return true; + } + Player otherPlayer = Bukkit.getPlayer(args[0]); + if (otherPlayer == null) { + sender.sendMessage(prefix + "目标玩家不在线"); + return true; + } + if (p.getName().equalsIgnoreCase(otherPlayer.getName())) { + sender.sendMessage(prefix + "无法向自己发起转账"); + return true; + } + if (CDTimeAPI.getCD(p.getUniqueId(), "PayCD") > 0) { + sender.sendMessage(prefix + "支付系统正在冷却..."); + return true; + } + int money = Integer.parseInt(args[1]); + if (money < 100) { + sender.sendMessage(prefix + "支付金额必须大于§6 $100.0"); + return true; + } + if (DemonAPI.hasMoney(p, money)) { + DemonAPI.takeMoney(p, money); + DemonAPI.giveMoney(otherPlayer, money); + CDTimeAPI.setPlayerCD(p.getUniqueId(), "PayCD", 1000 * 10); + sender.sendMessage(prefix + "你向 §a" + otherPlayer.getName() + " §f支付了 §a" + money + " §f金币"); + otherPlayer.sendMessage(prefix + "你从 §a" + sender.getName() + " §f收到了 §a" + money + " §f金币"); + } else { + sender.sendMessage(prefix + "没有足够的钱!"); + } + } else { + sender.sendMessage(prefix + "正确用法: §a/pay [玩家ID] [金额]"); + } + return false; + } +} diff --git a/src/main/java/me/Demon/DemonPay/util/SqlUtil.java b/src/main/java/me/Demon/DemonPay/util/SqlUtil.java new file mode 100644 index 0000000..3b4afc8 --- /dev/null +++ b/src/main/java/me/Demon/DemonPay/util/SqlUtil.java @@ -0,0 +1,108 @@ +package me.Demon.DemonPay.util; + +import me.Demon.DemonPay.Main; +import me.Demon.DemonPay.util.database.MegumiSQL; +import org.bukkit.Bukkit; + +import java.sql.*; + +public class SqlUtil extends MegumiSQL { + + private Main plugin; + private final String hostname; + private final String port; + private final String database; + private final String username; + private final String password; + private Connection connection; + + public SqlUtil(Main plugin, String hostname, String port, String database, String username, String password) { + this.plugin = plugin; + this.hostname = hostname; + this.port = port; + this.database = database; + this.username = username; + this.password = password; + this.connection = null; + } + + @Override + public void openConnection() { + try { + Class.forName("com.mysql.jdbc.Driver"); + this.connection = DriverManager.getConnection("jdbc:mysql://" + this.hostname + ":" + this.port + "/" + this.database + "?useSSL=false", this.username, this.password); + } catch (SQLException e) { + Bukkit.getConsoleSender().sendMessage("§6[点券限额] §c数据库连接失败!"); + this.connection = null; + } catch (ClassNotFoundException e) { + Bukkit.getConsoleSender().sendMessage("§6[点券限额] §c未找到JDBC驱动程序!"); + this.connection = null; + } + } + + @Override + public boolean checkConnection() { + return this.connection != null; + } + + @Override + public Connection getConnection() { + return this.connection; + } + + @Override + public void closeConnection() { + if (this.connection != null) { + try { + this.connection.close(); + this.connection = null; + } catch (SQLException e) { + Bukkit.getConsoleSender().sendMessage("§6[点券限额] §c关闭数据库失败!"); + e.printStackTrace(); + } + } + } + + public ResultSet querySQL(String query) { + Connection conn; + if (checkConnection()) + conn = getConnection(); + else + return null; + Statement stat = null; + try { + stat = conn.createStatement(); + } catch (SQLException e) { + e.printStackTrace(); + } + ResultSet result = null; + try { + if (stat != null) + result = stat.executeQuery(query); + else + return null; + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + return result; + } + + public boolean updateSQL(String data) { + Connection conn; + if (checkConnection()) { + conn = getConnection(); + } else { + return false; + } + Statement stat = null; + try { + stat = conn.createStatement(); + stat.executeUpdate(data); + return true; + } catch (SQLException e) { + e.printStackTrace(); + return false; + } + } +} diff --git a/src/main/java/me/Demon/DemonPay/util/database/MegumiSQL.java b/src/main/java/me/Demon/DemonPay/util/database/MegumiSQL.java new file mode 100644 index 0000000..8115ca8 --- /dev/null +++ b/src/main/java/me/Demon/DemonPay/util/database/MegumiSQL.java @@ -0,0 +1,15 @@ +package me.Demon.DemonPay.util.database; + +import java.sql.Connection; + +public abstract class MegumiSQL { + + public abstract void openConnection(); + + public abstract boolean checkConnection(); + + public abstract Connection getConnection(); + + public abstract void closeConnection(); + +} diff --git a/src/main/java/me/Demon/DemonPay/util/database/SqlManager.java b/src/main/java/me/Demon/DemonPay/util/database/SqlManager.java new file mode 100644 index 0000000..7f2de12 --- /dev/null +++ b/src/main/java/me/Demon/DemonPay/util/database/SqlManager.java @@ -0,0 +1,93 @@ +package me.Demon.DemonPay.util.database; + +import me.Demon.DemonPay.Main; +import me.Demon.DemonPay.PLimitData; +import me.Demon.DemonPay.util.SqlUtil; +import org.bukkit.entity.Player; + +import java.sql.ResultSet; +import java.util.Date; +import java.util.List; + +public class SqlManager { + + private Main plugin; + + public String table = "demonpay_points"; + + public SqlManager(Main plugin) { + this.plugin = plugin; + } + + private SqlUtil getSQL() { + return plugin.getSqlUtil(); + } + + // 创建数据库表格格式 + public void createTable() { + // 数据库结构组成 + // 玩家名(VARCHAR) + // UUID(VARCHAR) + // 已转账金额(VARCHAR) + // 刷新时间(VARCHAR) + String s = "CREATE TABLE IF NOT EXISTS " + table + "(" + + " `name` VARCHAR(64) NOT NULL," + + " `uuid` VARCHAR(64) NOT NULL," + + " `limit` VARCHAR(16) NOT NULL," + + " `date` VARCHAR(10) NOT NULL" + + ") ENGINE = InnoDB"; + getSQL().openConnection(); + getSQL().updateSQL(s); + getSQL().closeConnection(); + } + + // 创建玩家数据表格 + public void createPlayerTableData(Player p) { + String set = "INSERT INTO " + table + " (`name`, `uuid`, `limit`, `date`) VALUES ('%name%','%uuid%','%limit%','%date%')"; + getSQL().openConnection(); + Date date = new Date(); + getSQL().updateSQL(set.replace("%name%", p.getName()) + .replace("%uuid%", String.valueOf(p.getUniqueId())) + .replace("%limit%", "0") + .replace("%updata%", date.getMonth() + "." + date.getDate())); + getSQL().closeConnection(); + } + + // 从数据库中获取玩家所有数据 + public String getPlayerData(String playName) { + String string = null; + String select = "SELECT * FROM " + table + " WHERE name = '%playerName%'"; + try { + getSQL().openConnection(); + ResultSet set = getSQL().querySQL(select.replace("%playerName%", playName)); + if (set.next()) { + String jieshou = set.getString("limit"); + String update = set.getString("date"); + string = update + "#" + jieshou; + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + getSQL().closeConnection(); + } + return string; + } + + // 保存玩家在DataAPI中的数据 + public void SaverPlayerData(Player player) { + if (Main.dataAPI.get(player) == null) { + return; + } + PLimitData dataAPI = Main.dataAPI.get(player); + String update_time = dataAPI.getUpdata_time(); + String limit = String.valueOf(dataAPI.getLimit()); + String set = "UPDATE `" + table + "` SET " + + "`limit` = '%limit%'," + + "`date` = '%date%' WHERE `" + table + "`.`name` = '%player%'"; + getSQL().openConnection(); + getSQL().updateSQL(set.replace("%player%", player.getName()). + replace("%limit%", limit) + .replace("%date%", update_time)); + getSQL().closeConnection(); + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..5729375 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,7 @@ +main: me.Demon.DemonPay.Main +name: DemonPay +version: 1.0.2 +commands: + pay: + dpay: + eco: \ No newline at end of file