Add leaderboard command for top 10
This commit is contained in:
parent
3c65988b54
commit
d00fa703c6
3 changed files with 88 additions and 3 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
package me.firephoenix.deathcounter;
|
package me.firephoenix.deathcounter;
|
||||||
|
|
||||||
import me.clip.placeholderapi.PlaceholderAPI;
|
import me.clip.placeholderapi.PlaceholderAPI;
|
||||||
|
import me.firephoenix.deathcounter.commands.DeathsCommand;
|
||||||
import me.firephoenix.deathcounter.listener.DeathListener;
|
import me.firephoenix.deathcounter.listener.DeathListener;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
@ -33,9 +34,11 @@ public final class DeathCounter extends JavaPlugin {
|
||||||
|
|
||||||
createSqLiteDatabase();
|
createSqLiteDatabase();
|
||||||
|
|
||||||
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) { //
|
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
|
||||||
new DeathPlaceholderExpansion(this).register(); //
|
new DeathPlaceholderExpansion(this).register();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getCommand("deaths").setExecutor(new DeathsCommand(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createSqLiteDatabase() {
|
private void createSqLiteDatabase() {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,78 @@
|
||||||
|
package me.firephoenix.deathcounter.commands;
|
||||||
|
|
||||||
|
import me.firephoenix.deathcounter.DeathCounter;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.command.Command;
|
||||||
|
import org.bukkit.command.CommandExecutor;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.jetbrains.annotations.Contract;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jspecify.annotations.NonNull;
|
||||||
|
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author NieGestorben
|
||||||
|
* Copyright© (c) 2026, All Rights Reserved.
|
||||||
|
*/
|
||||||
|
public class DeathsCommand implements CommandExecutor {
|
||||||
|
|
||||||
|
public DeathCounter plugin;
|
||||||
|
|
||||||
|
public DeathsCommand(DeathCounter plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String @NonNull [] args) {
|
||||||
|
if (args.length != 1) {
|
||||||
|
commandSender.sendMessage("§6Please enter in most or least deaths as an argument like so: /deaths most or /deaths least");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
String order;
|
||||||
|
|
||||||
|
if (args[0].equals("most")) {
|
||||||
|
order = "DESC";
|
||||||
|
} else if (args[0].equals("least")) {
|
||||||
|
order = "ASC";
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||||
|
try (var conn = DriverManager.getConnection(plugin.getDataBaseURL())) {
|
||||||
|
if (conn != null) {
|
||||||
|
String getFirstTenSQL = "SELECT * FROM deaths_counts ORDER BY deaths " + order + " LIMIT 10;";
|
||||||
|
|
||||||
|
Statement queryStatement = conn.createStatement();
|
||||||
|
ResultSet resultSet = queryStatement.executeQuery(getFirstTenSQL);
|
||||||
|
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
int place = 1;
|
||||||
|
|
||||||
|
while (resultSet.next()) {
|
||||||
|
String uuid = resultSet.getString("uuid");
|
||||||
|
int deaths = resultSet.getInt("deaths");
|
||||||
|
String name = Bukkit.getOfflinePlayer(UUID.fromString(uuid)).getName();
|
||||||
|
sb.append("§6" + place + ". " + name + ": " + deaths + " Deaths");
|
||||||
|
sb.append("\n");
|
||||||
|
place++;
|
||||||
|
}
|
||||||
|
|
||||||
|
commandSender.sendMessage(sb.toString());
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
Bukkit.getLogger().log(Level.WARNING, e.getMessage());
|
||||||
|
commandSender.sendMessage("There was an error querying the database!");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,3 +4,7 @@ main: me.firephoenix.deathcounter.DeathCounter
|
||||||
api-version: '1.21'
|
api-version: '1.21'
|
||||||
description: Counts Deaths of all players via Scoreboard and Tablist
|
description: Counts Deaths of all players via Scoreboard and Tablist
|
||||||
depend: ["PlaceholderAPI"]
|
depend: ["PlaceholderAPI"]
|
||||||
|
commands:
|
||||||
|
deaths:
|
||||||
|
usage: /deaths <most|least>
|
||||||
|
description: Shows players with the lowest death count in chat
|
||||||
Loading…
Add table
Add a link
Reference in a new issue