From a09fcd5f2a773f53adb585d5bba8db76f5b66991 Mon Sep 17 00:00:00 2001 From: Kyoukawa Meishin Date: Thu, 16 Oct 2025 20:44:57 +0800 Subject: [PATCH] =?UTF-8?q?CheckinSystem=E7=9A=84=E9=A6=96=E6=AC=A1?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 39 +++++++ .idea/.gitignore | 3 + .idea/encodings.xml | 7 ++ .idea/misc.xml | 14 +++ .idea/vcs.xml | 6 + pom.xml | 42 +++++++ .../CheckinSystem/CheckinCommand.java | 26 +++++ .../CheckinSystem/CheckinManager.java | 105 ++++++++++++++++++ .../mmlsystem/CheckinSystem/Foundation.java | 98 ++++++++++++++++ src/main/resources/plugin.yml | 11 ++ 10 files changed, 351 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 pom.xml create mode 100644 src/main/java/com/mmlsystem/CheckinSystem/CheckinCommand.java create mode 100644 src/main/java/com/mmlsystem/CheckinSystem/CheckinManager.java create mode 100644 src/main/java/com/mmlsystem/CheckinSystem/Foundation.java create mode 100644 src/main/resources/plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..480bdf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,39 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ +.kotlin + +### 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 \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..359bb53 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..eda147d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..09095f6 --- /dev/null +++ b/pom.xml @@ -0,0 +1,42 @@ + + 4.0.0 + com.mmlsystem + CheckinSystem + 1.0 + + + + central + https://repo.maven.apache.org/maven2/ + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + jitpack-io + https://jitpack.io/ + + + + + + org.spigotmc + spigot-api + 1.21.8-R0.1-SNAPSHOT + provided + + + com.github.MilkBowl + VaultAPI + 1.7 + provided + + + org.jetbrains + annotations + 24.1.0 + compile + + + diff --git a/src/main/java/com/mmlsystem/CheckinSystem/CheckinCommand.java b/src/main/java/com/mmlsystem/CheckinSystem/CheckinCommand.java new file mode 100644 index 0000000..04d3df2 --- /dev/null +++ b/src/main/java/com/mmlsystem/CheckinSystem/CheckinCommand.java @@ -0,0 +1,26 @@ +package com.mmlsystem.CheckinSystem; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +public class CheckinCommand implements CommandExecutor { + private final CheckinManager manager; + + public CheckinCommand(CheckinManager manager) { + this.manager = manager; + } + + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { + if (!(sender instanceof Player)) return false; + + Player player = (Player) sender; + + player.sendMessage("签到中,请稍候..."); + manager.checkinAsync(player, message -> player.sendMessage(message)); + return true; + } +} diff --git a/src/main/java/com/mmlsystem/CheckinSystem/CheckinManager.java b/src/main/java/com/mmlsystem/CheckinSystem/CheckinManager.java new file mode 100644 index 0000000..10a1e04 --- /dev/null +++ b/src/main/java/com/mmlsystem/CheckinSystem/CheckinManager.java @@ -0,0 +1,105 @@ +package com.mmlsystem.CheckinSystem; + +import net.milkbowl.vault.economy.Economy; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import java.sql.*; +import java.time.*; +import java.util.UUID; +import java.util.function.Consumer; + +public class CheckinManager { + private final Foundation plugin; + private final Economy econ; + + public CheckinManager(Foundation plugin) { + this.plugin = plugin; + this.econ = Foundation.getEconomy(); + } + + public void checkinAsync(Player player, Consumer callback) { + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + String message; + try { + message = execCheckin(player); + } catch (Exception e) { + plugin.getLogger().severe(e.getMessage()); + message = "§c未知错误。"; + } + + String finalMessage = message; + Bukkit.getScheduler().runTask(plugin, () -> callback.accept(finalMessage)); + }); + } + + private String execCheckin(Player player) { + if (!player.hasPermission("com.mmlsystem.checkin")) { + return "§c拒绝访问。"; + } + UUID uuid = player.getUniqueId(); + LocalDate today = LocalDate.now(); + + try { + PreparedStatement ps = plugin.getConnection().prepareStatement("SELECT last_checkin, streak, total_checkin FROM checkins WHERE uuid=?"); + ps.setString(1, uuid.toString()); + ResultSet rs = ps.executeQuery(); + + if (rs.next()) { + long lastCheckin = rs.getLong("last_checkin"); + int streak = rs.getInt("streak"); + int total = rs.getInt("total_checkin"); + + LocalDate lastDate = Instant.ofEpochMilli(lastCheckin) + .atZone(ZoneId.systemDefault()).toLocalDate(); + if (today.isEqual(lastDate)) { + rs.close(); + ps.close(); + return "§e你今天已经签到过了!"; + } else { + if (today.minusDays(1).isEqual(lastDate)) { + streak += 1; + } else { + streak = 1; + } + } + total += 1; + ps = plugin.getConnection().prepareStatement( + "UPDATE checkins SET last_checkin=?, streak=?, total_checkin=? WHERE uuid=?"); + ps.setLong(1, today.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()); + ps.setInt(2, streak); + ps.setInt(3, total); + ps.setString(4, uuid.toString()); + ps.executeUpdate(); + double reward = getReward(streak); + econ.depositPlayer(player, reward); + ps.close(); + rs.close(); + return "§a签到成功!已连续签到§b" + streak + "§a天,获得§c$" + reward + "§a奖励"; + } else { + ps = plugin.getConnection().prepareStatement( + "INSERT INTO checkins(last_checkin,streak,total_checkin,uuid) VALUES(?,?,?,?)"); + ps.setLong(1, today.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()); + ps.setInt(2, 1); + ps.setInt(3, 1); + ps.setString(4, uuid.toString()); + ps.executeUpdate(); + econ.depositPlayer(player, 25.0); + ps.close(); + rs.close(); + return "§a签到成功!已连续签到§b1§a天,获得§c$25§a奖励"; + } + } catch (SQLException e) { + plugin.getLogger().severe(e.getMessage()); + return "§c未知错误。"; + } + } + + public double getReward(int streak) { + if (streak >= 7) { + return 200.0; + } else { + return 25.0 * streak; + } + } +} diff --git a/src/main/java/com/mmlsystem/CheckinSystem/Foundation.java b/src/main/java/com/mmlsystem/CheckinSystem/Foundation.java new file mode 100644 index 0000000..fafe73a --- /dev/null +++ b/src/main/java/com/mmlsystem/CheckinSystem/Foundation.java @@ -0,0 +1,98 @@ +package com.mmlsystem.CheckinSystem; + +import net.milkbowl.vault.economy.Economy; +import org.bukkit.Bukkit; +import org.bukkit.permissions.PermissionDefault; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.permissions.Permission; +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.RegisteredServiceProvider; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Objects; + +public class Foundation extends JavaPlugin { + public static final Permission CHECKIN_PERM = new Permission( + "com.mmlsystem.checkin", + "允许玩家执行签到操作。", + PermissionDefault.TRUE + ); + private static Economy econ = null; + private static Connection connection; + + @Override + public void onEnable() { + getLogger().info("张华考上了北京大学"); + getLogger().info("李萍进了中等技术学校"); + getLogger().info("你却在这里看服务器启动日志里面有没有什么奇怪的东西"); + if (!setupEconomy()) { + getLogger().severe("什么杂鱼服务器连个经济插件都没有(大雾)"); + getLogger().severe("请先安装Vault 1.7+或VaultUnlocked插件以继续操作"); + getServer().getPluginManager().disablePlugin(this); + return; + } + setupDatabase(); + PluginManager pm = getServer().getPluginManager(); + pm.addPermission(CHECKIN_PERM); + Objects.requireNonNull(getCommand("checkin")).setExecutor(new CheckinCommand(new CheckinManager(this))); + getLogger().info("插件启用成功。"); + } + + @Override + public void onDisable() { + try { + connection.close(); + } catch (SQLException e) { + getLogger().severe(e.getMessage()); + throw new RuntimeException(e); + } + getLogger().info("插件禁用成功。"); + } + + public static Economy getEconomy() { + return econ; + } + + public Connection getConnection() { + return connection; + } + + private boolean setupEconomy() { + if (getServer().getPluginManager().getPlugin("Vault") == null) { + return false; + } + RegisteredServiceProvider rsp = Bukkit.getServicesManager().getRegistration(Economy.class); + if (rsp == null) { + return false; + } + econ = rsp.getProvider(); + return true; + } + + private void setupDatabase() { + try { + File dbFile = new File(getDataFolder(), "data.db"); + if (!getDataFolder().exists()) { + //noinspection ResultOfMethodCallIgnored + getDataFolder().mkdirs(); + } + + connection = DriverManager.getConnection("jdbc:sqlite:" + dbFile.getAbsolutePath()); + Statement stmt = connection.createStatement(); + stmt.executeUpdate("CREATE TABLE IF NOT EXISTS checkins (" + + "uuid TEXT PRIMARY KEY," + + "last_checkin INTEGER," + + "streak INTEGER," + + "total_checkin INTEGER)"); + stmt.close(); + + getLogger().info("SQLite 数据库初始化成功。"); + } catch (SQLException e) { + getLogger().severe(e.getMessage()); + } + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..aa1a5d8 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,11 @@ +name: HelloPlugin +main: com.mmlsystem.CheckinSystem.Foundation +version: 1.0 +api-version: 1.21 +author: 杏川铭心 +description: 提供服务器每日签到功能。 +depend: [Vault] +commands: + checkin: + description: 在服务器上签到并获取奖励。 + usage: /checkin \ No newline at end of file