Skip to content

Commit edff9b7

Browse files
authored
PointsAPI Support
1 parent 6141c4c commit edff9b7

File tree

15 files changed

+681
-3
lines changed

15 files changed

+681
-3
lines changed

StatsAPI.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.gson:gson:2.8.9" level="project" />
3333
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-chat:1.16-R0.4" level="project" />
3434
<orderEntry type="library" scope="PROVIDED" name="Maven: org.yaml:snakeyaml:1.30" level="project" />
35-
<orderEntry type="library" name="Maven: com.github.teraprath:PointsAPI:1.2.5-STABLE" level="project" />
35+
<orderEntry type="library" name="Maven: com.github.teraprath:PointsAPI:1.2.7-STABLE" level="project" />
3636
</component>
3737
</module>

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>dev.teraprath</groupId>
88
<artifactId>stats</artifactId>
9-
<version>1.2.0-STABLE</version>
9+
<version>1.2.1-STABLE</version>
1010
<packaging>jar</packaging>
1111

1212
<name>StatsAPI</name>
@@ -78,7 +78,7 @@
7878
<dependency>
7979
<groupId>com.github.teraprath</groupId>
8080
<artifactId>PointsAPI</artifactId>
81-
<version>1.2.5-STABLE</version>
81+
<version>1.2.7-STABLE</version>
8282
</dependency>
8383
</dependencies>
8484
</project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package dev.teraprath.stats;
2+
3+
import dev.teraprath.stats.api.StatsAPI;
4+
import dev.teraprath.stats.command.AdminCommand;
5+
import dev.teraprath.stats.command.StatsCommand;
6+
import dev.teraprath.stats.listener.LoginListener;
7+
import org.bukkit.Bukkit;
8+
import org.bukkit.plugin.PluginManager;
9+
import org.bukkit.plugin.java.JavaPlugin;
10+
11+
public final class Main extends JavaPlugin {
12+
13+
private static Main instance;
14+
15+
@Override
16+
public void onEnable() {
17+
18+
instance = this;
19+
20+
getConfig().options().copyDefaults(true);
21+
saveConfig();
22+
23+
new StatsAPI(this).init();
24+
25+
registerCommands();
26+
registerEvents();
27+
28+
}
29+
30+
@Override
31+
public void onDisable() {
32+
// Plugin shutdown logic
33+
}
34+
35+
private void registerCommands() {
36+
getCommand("stats").setExecutor(new StatsCommand());
37+
getCommand("astats").setExecutor(new AdminCommand());
38+
}
39+
40+
private void registerEvents() {
41+
final PluginManager pm = Bukkit.getServer().getPluginManager();
42+
pm.registerEvents(new LoginListener(), this);
43+
}
44+
45+
public static Main getInstance() {
46+
return instance;
47+
}
48+
49+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package dev.teraprath.stats.api;
2+
3+
public enum DataType {
4+
5+
KILLS,
6+
DEATHS,
7+
WINS,
8+
LOSES,
9+
GAMES_PLAYED,
10+
STREAK,
11+
GAME_POINTS
12+
13+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package dev.teraprath.stats.api;
2+
3+
import javax.annotation.Nonnegative;
4+
import javax.annotation.Nonnull;
5+
import java.util.UUID;
6+
7+
public class PlayerStats {
8+
9+
private final UUID uuid;
10+
private int kills;
11+
private int deaths;
12+
private int wins;
13+
private int loses;
14+
private int gamesPlayed;
15+
private int streak;
16+
private int gamePoints;
17+
18+
public PlayerStats(@Nonnull UUID uuid) {
19+
this.uuid = uuid;
20+
this.kills = 0;
21+
this.deaths = 0;
22+
this.wins = 0;
23+
this.loses = 0;
24+
this.gamesPlayed = 0;
25+
this.streak = 0;
26+
this.gamePoints = 0;
27+
}
28+
29+
public UUID getUniqueId() {
30+
return this.uuid;
31+
}
32+
33+
public void setKills(@Nonnegative int amount) {
34+
this.kills = amount;
35+
}
36+
37+
public int getKills() {
38+
return this.kills;
39+
}
40+
41+
public void setDeaths(@Nonnegative int amount) {
42+
this.deaths = amount;
43+
}
44+
45+
public int getDeaths() {
46+
return this.deaths;
47+
}
48+
49+
public void setWins(@Nonnegative int amount) {
50+
this.wins = amount;
51+
}
52+
53+
public int getWins() {
54+
return this.wins;
55+
}
56+
57+
public void setLoses(@Nonnegative int amount) {
58+
this.loses = amount;
59+
}
60+
61+
public int getLoses() {
62+
return this.loses;
63+
}
64+
65+
public void setGamesPlayed(@Nonnegative int amount) {
66+
this.gamesPlayed = amount;
67+
}
68+
69+
public int getGamesPlayed() {
70+
return this.gamesPlayed;
71+
}
72+
73+
public void setStreak(@Nonnegative int amount) {
74+
this.streak = amount;
75+
}
76+
77+
public int getStreak() {
78+
return this.streak;
79+
}
80+
81+
public void setGamePoints(@Nonnegative int amount) {
82+
this.gamePoints = amount;
83+
}
84+
85+
public int getGamePoints() {
86+
return this.gamePoints;
87+
}
88+
89+
public void reset() {
90+
this.kills = 0;
91+
this.deaths = 0;
92+
this.wins = 0;
93+
this.loses = 0;
94+
this.gamesPlayed = 0;
95+
this.streak = 0;
96+
this.gamePoints = 0;
97+
}
98+
99+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package dev.teraprath.stats.command;
2+
3+
import dev.teraprath.stats.Main;
4+
import dev.teraprath.stats.api.DataType;
5+
import dev.teraprath.stats.utils.SQLUtils;
6+
import org.bukkit.Bukkit;
7+
import org.bukkit.ChatColor;
8+
import org.bukkit.command.Command;
9+
import org.bukkit.command.CommandExecutor;
10+
import org.bukkit.command.CommandSender;
11+
import org.bukkit.command.TabCompleter;
12+
import org.bukkit.configuration.file.FileConfiguration;
13+
import org.bukkit.entity.Player;
14+
15+
import javax.annotation.Nonnull;
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
19+
public class AdminCommand implements CommandExecutor, TabCompleter {
20+
21+
private final FileConfiguration cfg = Main.getInstance().getConfig();
22+
final String prefix = ChatColor.translateAlternateColorCodes('&', cfg.getString("language.prefix") + "");
23+
24+
@Override
25+
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
26+
if (args.length > 2) {
27+
final Player target = Bukkit.getPlayer(args[0]);
28+
final DataType type = DataType.valueOf(args[1]);
29+
final int amount = Integer.parseInt(args[2]);
30+
String message;
31+
if (target != null && amount >= 0) {
32+
new SQLUtils().update(target.getUniqueId(), type, amount, sender, prefix + cfg.getString("language.player_stats_update").replace("%player%", target.getName()).replace("%datatype%", type.name().toLowerCase().replace("_", " ")));
33+
} else {
34+
wrongUsage(sender);
35+
}
36+
} else {
37+
wrongUsage(sender);
38+
}
39+
40+
return false;
41+
}
42+
43+
private void wrongUsage(@Nonnull CommandSender sender) {
44+
String wrongUsage = cfg.getString("language.wrong_usage");
45+
sender.sendMessage(prefix + ChatColor.translateAlternateColorCodes('&', wrongUsage.replace("%usage%", "/astats <player> <datatype> <amount>")));
46+
}
47+
48+
@Override
49+
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
50+
ArrayList<String> list = new ArrayList<>();
51+
String current = args[args.length - 1].toLowerCase();
52+
53+
switch (args.length) {
54+
case 1:
55+
Bukkit.getOnlinePlayers().forEach(player -> {
56+
list.add(player.getName());
57+
});
58+
break;
59+
case 2:
60+
for (DataType type : DataType.values()) {
61+
list.add(type.name());
62+
}
63+
break;
64+
case 3:
65+
list.add("amount");
66+
default:
67+
break;
68+
}
69+
70+
list.removeIf(s -> !s.toLowerCase().startsWith(current));
71+
return list;
72+
}
73+
}

0 commit comments

Comments
 (0)