diff --git a/src/main/java/com/mmlsystem/PlayerExpSystem/ExpAPI.java b/src/main/java/com/mmlsystem/PlayerExpSystem/ExpAPI.java new file mode 100644 index 0000000..3501455 --- /dev/null +++ b/src/main/java/com/mmlsystem/PlayerExpSystem/ExpAPI.java @@ -0,0 +1,46 @@ +package com.mmlsystem.PlayerExpSystem; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.function.Consumer; + +public class ExpAPI { + private final Connection connection; + private final Foundation plugin; + + public ExpAPI(Foundation plugin) { + this.plugin = plugin; + plugin.setupDatabase(); + this.connection = plugin.getConnection(); + } + + public void getPlayerOnlineTime(Player player, Consumer callback) { + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + int time; + try { + PreparedStatement ps = connection.prepareStatement("SELECT total FROM playtime WHERE uuid=?"); + ps.setString(1, player.getUniqueId().toString()); + ResultSet rs = ps.executeQuery(); + if (rs.next()) { + time = rs.getInt("total"); + } else { + plugin.getLogger().warning("读取数据库失败!"); + time = -1; + } + rs.close(); + ps.close(); + } catch (SQLException e) { + plugin.getLogger().severe(e.getMessage()); + time = -1; + } + + int finalMessage = time; + Bukkit.getScheduler().runTask(plugin, () -> callback.accept(finalMessage)); + }); + } +}