暴露API
This commit is contained in:
parent
d7898e333f
commit
1d0951f6a8
46
src/main/java/com/mmlsystem/PlayerExpSystem/ExpAPI.java
Normal file
46
src/main/java/com/mmlsystem/PlayerExpSystem/ExpAPI.java
Normal file
@ -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<Integer> 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));
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user