|
| 1 | +package dev.teraprath.stats.api; |
| 2 | + |
| 3 | +import dev.teraprath.points.api.PointsAPI; |
| 4 | +import dev.teraprath.stats.sql.SQLAdapter; |
| 5 | +import dev.teraprath.stats.sql.SQLAuthentication; |
| 6 | +import org.bukkit.configuration.file.FileConfiguration; |
| 7 | +import org.bukkit.entity.Player; |
| 8 | +import org.bukkit.plugin.Plugin; |
| 9 | +import org.bukkit.plugin.java.JavaPlugin; |
| 10 | + |
| 11 | +import javax.annotation.Nonnull; |
| 12 | +import java.sql.ResultSet; |
| 13 | +import java.sql.SQLException; |
| 14 | +import java.util.UUID; |
| 15 | + |
| 16 | +public class StatsAPI { |
| 17 | + |
| 18 | + private final JavaPlugin plugin; |
| 19 | + private static SQLAuthentication authentication; |
| 20 | + private static boolean usePointsAPI = false; |
| 21 | + |
| 22 | + public StatsAPI(@Nonnull JavaPlugin plugin) { |
| 23 | + this.plugin = plugin; |
| 24 | + } |
| 25 | + |
| 26 | + public void init() { |
| 27 | + Plugin depend = plugin.getServer().getPluginManager().getPlugin("StatsAPI"); |
| 28 | + if (depend != null) { |
| 29 | + FileConfiguration cfg = depend.getConfig(); |
| 30 | + authentication = new SQLAuthentication(cfg.getString("mysql.host"), cfg.getInt("mysql.port"), cfg.getString("mysql.database"), cfg.getString("mysql.user"), cfg.getString("mysql.password")); |
| 31 | + depend.getLogger().info("Plugin registered: " + plugin.getName() + "-" + plugin.getDescription().getVersion()); |
| 32 | + if (cfg.getBoolean("use_points_api")) { |
| 33 | + if (plugin.getServer().getPluginManager().getPlugin("PointsAPI") != null) { |
| 34 | + usePointsAPI = true; |
| 35 | + new PointsAPI((JavaPlugin) depend).init(); |
| 36 | + } else { |
| 37 | + depend.getLogger().warning("Can't use PointsAPI. Please install PointsAPI on your server to fix this: https://github.com/teraprath/PointsAPI"); |
| 38 | + } |
| 39 | + } |
| 40 | + } else { |
| 41 | + plugin.getLogger().warning("StatsAPI is not installed! Instructions: https://github.com/teraprath/StatsAPI/wiki/1.-Getting-Started"); |
| 42 | + plugin.getLogger().warning("Download on GitHub: https://github.com/teraprath/StatsAPI/releases/latest"); |
| 43 | + } |
| 44 | + plugin.getServer().getScheduler().runTaskAsynchronously(plugin, task -> { |
| 45 | + new SQLAdapter(authentication).init(); |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + public static PlayerStats getPlayer(@Nonnull Player player) { |
| 51 | + return getPlayer(player.getUniqueId()); |
| 52 | + } |
| 53 | + |
| 54 | + public static PlayerStats getPlayer(@Nonnull UUID uuid) { |
| 55 | + SQLAdapter adapter = new SQLAdapter(authentication); |
| 56 | + |
| 57 | + adapter.connect(); |
| 58 | + |
| 59 | + try { |
| 60 | + ResultSet res = adapter.query(String.format("SELECT * FROM stats_players WHERE uuid = '%s'", uuid)); |
| 61 | + if (res.next()) { |
| 62 | + PlayerStats stats = new PlayerStats(uuid); |
| 63 | + stats.setKills(res.getInt("kills")); |
| 64 | + stats.setDeaths(res.getInt("deaths")); |
| 65 | + stats.setWins(res.getInt("wins")); |
| 66 | + stats.setLoses(res.getInt("loses")); |
| 67 | + stats.setGamePoints(res.getInt("games_played")); |
| 68 | + stats.setStreak(res.getInt("streak")); |
| 69 | + stats.setGamePoints(usePointsAPI ? PointsAPI.getPoints(uuid) : res.getInt("game_points")); |
| 70 | + return stats; |
| 71 | + } |
| 72 | + } catch (SQLException e) { |
| 73 | + e.printStackTrace(); |
| 74 | + } |
| 75 | + |
| 76 | + adapter.disconnect(); |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + public static void save(@Nonnull PlayerStats stats) { |
| 81 | + |
| 82 | + SQLAdapter adapter = new SQLAdapter(authentication); |
| 83 | + |
| 84 | + adapter.connect(); |
| 85 | + adapter.update(String.format("UPDATE stats_players SET kills = %d WHERE uuid = '%s'", stats.getKills(), stats.getUniqueId())); |
| 86 | + adapter.update(String.format("UPDATE stats_players SET deaths = %d WHERE uuid = '%s'", stats.getDeaths(), stats.getUniqueId())); |
| 87 | + adapter.update(String.format("UPDATE stats_players SET wins = %d WHERE uuid = '%s'", stats.getWins(), stats.getUniqueId())); |
| 88 | + adapter.update(String.format("UPDATE stats_players SET loses = %d WHERE uuid = '%s'", stats.getLoses(), stats.getUniqueId())); |
| 89 | + adapter.update(String.format("UPDATE stats_players SET games_played = %d WHERE uuid = '%s'", stats.getGamesPlayed(), stats.getUniqueId())); |
| 90 | + adapter.update(String.format("UPDATE stats_players SET streak = %d WHERE uuid = '%s'", stats.getStreak(), stats.getUniqueId())); |
| 91 | + adapter.update(String.format("UPDATE stats_players SET game_points = %d WHERE uuid = '%s'", stats.getGamePoints(), stats.getUniqueId())); |
| 92 | + |
| 93 | + if (usePointsAPI) { PointsAPI.setPoints(stats.getUniqueId(), stats.getGamePoints()); } |
| 94 | + |
| 95 | + adapter.disconnect(); |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + public static boolean hasRegistered(@Nonnull Player player) { |
| 100 | + return hasRegistered(player.getUniqueId()); |
| 101 | + } |
| 102 | + |
| 103 | + public static boolean hasRegistered(@Nonnull UUID uuid) { |
| 104 | + |
| 105 | + SQLAdapter adapter = new SQLAdapter(authentication); |
| 106 | + |
| 107 | + adapter.connect(); |
| 108 | + |
| 109 | + try { |
| 110 | + ResultSet res = adapter.query(String.format("SELECT * FROM stats_players WHERE uuid = '%s'", uuid)); |
| 111 | + if (res.next()) { |
| 112 | + return true; |
| 113 | + } |
| 114 | + } catch (SQLException e) { |
| 115 | + e.printStackTrace(); |
| 116 | + } |
| 117 | + |
| 118 | + adapter.disconnect(); |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + public static void register(@Nonnull UUID uuid) { |
| 123 | + |
| 124 | + SQLAdapter adapter = new SQLAdapter(authentication); |
| 125 | + |
| 126 | + adapter.connect(); |
| 127 | + adapter.update(String.format("INSERT IGNORE INTO stats_players (uuid, kills, deaths, wins, loses, games_played, streak, game_points) VALUES ('%s', 0, 0, 0, 0, 0, 0, 0)", uuid)); |
| 128 | + adapter.disconnect(); |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments