Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,54 +22,62 @@
commandname = "hplb",
permission = "headsplus.leaderboards",
maincommand = false,
usage = "/hplb [Hunting|Selling|Crafting|Page No.] [Mob|Page No.] [ID|Page No.] ",
usage = "/hplb [Hunting|Selling|Crafting|Unique|Page No.] [Unique|Mob|Page No.] [ID|Page No.] ",
descriptionPath = "descriptions.hplb")
public class LeaderboardsCommand implements CommandExecutor, IHeadsPlusCommand, TabCompleter {

@Override
public boolean onCommand(@NotNull CommandSender cs, @NotNull Command command, @NotNull String s,
@NotNull String[] args) {
if (!MainConfig.get().getMainFeatures().LEADERBOARDS) {
return true;
}
if (!cs.hasPermission("headsplus.leaderboards")) {
return true;
}

// Ensure the command can be used
if (!MainConfig.get().getMainFeatures().LEADERBOARDS) return true;
if (!cs.hasPermission("headsplus.leaderboards")) return true;

// For /hplb
if (args.length == 0) {
StatisticsSQLManager.get().getLeaderboardTotal().thenAccept(list ->
StatisticsSQLManager.get().getLeaderboardTotal(false).thenAccept(list ->
cs.sendMessage(ConfigTextMenus.LeaderBoardTranslator.translate(cs, "Total", list, 1)));
return true;
}

// Check category
StatisticsSQLManager.CollectionType type = StatisticsSQLManager.CollectionType.getType(args[0].toUpperCase());
int page = 1;

if (type == null) {
StatisticsSQLManager.get().getLeaderboardTotal().thenAccept(list ->
cs.sendMessage(ConfigTextMenus.LeaderBoardTranslator.translate(cs, "Total", list, checkPage(args[0]))));
boolean unique = args[0].equalsIgnoreCase("unique");
StatisticsSQLManager.get().getLeaderboardTotal(unique).thenAccept(list ->
cs.sendMessage(ConfigTextMenus.LeaderBoardTranslator.translate(cs, unique ? "Unique" : "Total", list, checkPage(args[0]))));
return true;
}

if (args.length == 1) {
StatisticsSQLManager.get().getLeaderboardTotal(type).thenAccept(list ->
StatisticsSQLManager.get().getLeaderboardTotal(type, false).thenAccept(list ->
cs.sendMessage(ConfigTextMenus.LeaderBoardTranslator.translate(cs,
HeadsPlus.capitalize(type.name()), list, page)));
return true;
}

if (args.length == 2) {
if (args[1].matches("^[0-9]+$")) {
StatisticsSQLManager.get().getLeaderboardTotal(type).thenAccept(list ->
StatisticsSQLManager.get().getLeaderboardTotal(type, false).thenAccept(list ->
cs.sendMessage(ConfigTextMenus.LeaderBoardTranslator.translate(cs, "Total", list, checkPage(args[1]))));
} else {
String key = args[1];
if (args[0].equalsIgnoreCase("hunting")) {
StatisticsSQLManager.get().getLeaderboardTotalMetadata(type, "entity=" + key.toUpperCase()).thenAccept(list ->
cs.sendMessage(ConfigTextMenus.LeaderBoardTranslator.translate(cs, "Total", list, 1)));
} else if (args[0].equalsIgnoreCase("crafting")) {
StatisticsSQLManager.get().getLeaderboardTotal(type, "headsplus:crafting_" + key).thenAccept(list ->
cs.sendMessage(ConfigTextMenus.LeaderBoardTranslator.translate(cs, "Total", list, 1)));
if (key.equalsIgnoreCase("unique")) {
StatisticsSQLManager.get().getLeaderboardTotal(type, true).thenAccept(list ->
cs.sendMessage(ConfigTextMenus.LeaderBoardTranslator.translate(cs, "Unique", list, 1)));
} else {
if (args[0].equalsIgnoreCase("hunting")) {
StatisticsSQLManager.get().getLeaderboardTotalMetadata(type, "entity=" + key.toUpperCase(), false).thenAccept(list ->
cs.sendMessage(ConfigTextMenus.LeaderBoardTranslator.translate(cs, "Total", list, 1)));
} else if (args[0].equalsIgnoreCase("crafting")) {
StatisticsSQLManager.get().getLeaderboardTotal(type, "headsplus:crafting_" + key, false).thenAccept(list ->
cs.sendMessage(ConfigTextMenus.LeaderBoardTranslator.translate(cs, "Total", list, 1)));
}
}

}
return true;
}
Expand All @@ -78,18 +86,18 @@ public boolean onCommand(@NotNull CommandSender cs, @NotNull Command command, @N
String key = args[1].toUpperCase();
if (args[0].equalsIgnoreCase("hunting")) key = "entity=" + key;
if (args[2].matches("^[0-9]+$")) {
StatisticsSQLManager.get().getLeaderboardTotalMetadata(type, key).thenAccept(list ->
StatisticsSQLManager.get().getLeaderboardTotalMetadata(type, key, false).thenAccept(list ->
cs.sendMessage(ConfigTextMenus.LeaderBoardTranslator.translate(cs, "Total", list, checkPage(args[2]))));
} else {
StatisticsSQLManager.get().getLeaderboardTotal(type, args[2], key).thenAccept(list ->
StatisticsSQLManager.get().getLeaderboardTotal(type, args[2], key, false).thenAccept(list ->
cs.sendMessage(ConfigTextMenus.LeaderBoardTranslator.translate(cs, "Total", list, 1)));
}
return true;
}

String key = args[1].toUpperCase();
if (args[0].equalsIgnoreCase("hunting")) key = "entity=" + key;
StatisticsSQLManager.get().getLeaderboardTotal(type, args[2], key).thenAccept(list ->
StatisticsSQLManager.get().getLeaderboardTotal(type, args[2], key, false).thenAccept(list ->
cs.sendMessage(ConfigTextMenus.LeaderBoardTranslator.translate(cs, "Total", list, checkPage(args[3]))));

return true;
Expand All @@ -110,13 +118,16 @@ public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Comman
@NotNull String[] args) {
List<String> results = new ArrayList<>();
if (args.length == 1) {
StringUtil.copyPartialMatches(args[0], Arrays.asList("hunting", "crafting"), results);
StringUtil.copyPartialMatches(args[0], Arrays.asList("hunting", "crafting", "unique"), results);
} else if (args.length == 2) {
List<String> options = new ArrayList<>();
options.add("unique");
if (args[0].equalsIgnoreCase("hunting")) {
StringUtil.copyPartialMatches(args[1], EntityDataManager.ableEntities, results);
options.addAll(EntityDataManager.ableEntities);
} else {
StringUtil.copyPartialMatches(args[1], CraftingManager.get().getRegisteredKeys(), results);
options.addAll(CraftingManager.get().getRegisteredKeys());
}
StringUtil.copyPartialMatches(args[1], options, results);
} else if (args.length == 3) {
if (args[0].equalsIgnoreCase("hunting")) {
List<EntityDataManager.DroppedHeadInfo> list = EntityDataManager.getStoredHeads().get(args[1].toUpperCase() + ";default");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,24 @@ public int getTotalChallengesComplete(OfflinePlayer player) {
}

public List<StatisticsSQLManager.LeaderboardEntry> getEntries(StatisticsSQLManager.CollectionType type) {
return getEntries(type.name(), StatisticsSQLManager.get().getLeaderboardTotal(type));
return getEntries(type.name(), StatisticsSQLManager.get().getLeaderboardTotal(type, false));
}

public List<StatisticsSQLManager.LeaderboardEntry> getEntries(StatisticsSQLManager.CollectionType type,
String head) {
return getEntries(type.name() + "_" + head, StatisticsSQLManager.get().getLeaderboardTotal(type, head));
return getEntries(type.name() + "_" + head, StatisticsSQLManager.get().getLeaderboardTotal(type, head, false));
}

public List<StatisticsSQLManager.LeaderboardEntry> getEntriesMeta(StatisticsSQLManager.CollectionType type,
String metadata) {
return getEntries(type.name() + "_" + metadata, StatisticsSQLManager.get().getLeaderboardTotalMetadata(type,
metadata));
metadata, false));
}

public List<StatisticsSQLManager.LeaderboardEntry> getEntries(StatisticsSQLManager.CollectionType type,
String head, String metadata) {
return getEntries(type.name() + "_" + head + "_" + metadata,
StatisticsSQLManager.get().getLeaderboardTotal(type, head, metadata));
StatisticsSQLManager.get().getLeaderboardTotal(type, head, metadata, false));
}

public int getStat(OfflinePlayer player, StatisticsSQLManager.CollectionType type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,101 +183,93 @@ public CompletableFuture<Integer> getStat(UUID uuid, CollectionType type, String
}, async, "get stat " + type.name() + " with metadata " + metadata + " for head " + head + " and user " + uuid);
}

public CompletableFuture<List<LeaderboardEntry>> getLeaderboardTotal() {
return createConnection(connection -> {
PreparedStatement statement = connection.prepareStatement(
"SELECT SUM(count) as total, username FROM headsplus_stats, headsplus_players " +
"WHERE headsplus_stats.user_id = headsplus_players.id " +
"GROUP BY headsplus_stats.user_id ORDER BY total DESC");

ResultSet set = statement.executeQuery();
List<LeaderboardEntry> leaderboard = new ArrayList<>();
while (set.next()) {
leaderboard.add(new LeaderboardEntry(set.getString("username"), set.getInt("total")));
}
return leaderboard;
}, true, "get leaderboard total");
public CompletableFuture<List<LeaderboardEntry>> getLeaderboardTotal(boolean unique) {

return getLeaderboard("get leaderboard total", connection -> connection.prepareStatement(
"SELECT " + (unique ? "COUNT" : "SUM") + "(count) as total, username FROM headsplus_stats, headsplus_players " +
"WHERE headsplus_stats.user_id = headsplus_players.id " +
"GROUP BY headsplus_stats.user_id ORDER BY total DESC"));
}

public CompletableFuture<List<LeaderboardEntry>> getLeaderboardTotal(CollectionType type) {
return createConnection(connection -> {
public CompletableFuture<List<LeaderboardEntry>> getLeaderboardTotal(CollectionType type, boolean unique) {

return getLeaderboard("get leaderboard total for " + type.name(), connection -> {
PreparedStatement statement = connection.prepareStatement(
"SELECT SUM(count) as total, username FROM headsplus_stats, headsplus_players " +
"SELECT " + (unique ? "COUNT" : "SUM") + "(count) as total, username FROM headsplus_stats, headsplus_players " +
"WHERE headsplus_stats.user_id = headsplus_players.id AND collection_type = ?" +
"AND headsplus_stats.user_id = headsplus_players.id " +
"GROUP BY headsplus_stats.user_id ORDER BY total DESC");

statement.setString(1, type.name());

ResultSet set = statement.executeQuery();
List<LeaderboardEntry> leaderboard = new ArrayList<>();
while (set.next()) {
leaderboard.add(new LeaderboardEntry(set.getString("username"), set.getInt("total")));
}
return leaderboard;
}, true, "get leaderboard total for " + type.name());

return statement;
});
}

public CompletableFuture<List<LeaderboardEntry>> getLeaderboardTotal(CollectionType type, String head) {
return createConnection(connection -> {
public CompletableFuture<List<LeaderboardEntry>> getLeaderboardTotal(CollectionType type, String head, boolean unique) {

return getLeaderboard("get leaderboard total for " + type.name() + " and head " + head, connection -> {
PreparedStatement statement = connection.prepareStatement(
"SELECT SUM(count) as total, username FROM headsplus_stats, headsplus_players" +
"SELECT " + (unique ? "COUNT" : "SUM") + "(count) as total, username FROM headsplus_stats, headsplus_players" +
" WHERE collection_type = ? AND head = ? " +
"AND headsplus_stats.user_id = headsplus_players.id " +
"GROUP BY headsplus_stats.user_id ORDER BY total DESC");

statement.setString(1, type.name());
statement.setString(2, head);

ResultSet set = statement.executeQuery();
List<LeaderboardEntry> leaderboard = new ArrayList<>();
while (set.next()) {
leaderboard.add(new LeaderboardEntry(set.getString("username"), set.getInt("total")));
}
return leaderboard;
}, true, "get leaderboard total for " + type.name() + " and head " + head);

return statement;
});
}

public CompletableFuture<List<LeaderboardEntry>> getLeaderboardTotalMetadata(CollectionType type, String metadata) {
return createConnection(connection -> {
public CompletableFuture<List<LeaderboardEntry>> getLeaderboardTotalMetadata(CollectionType type, String metadata, boolean unique) {

return getLeaderboard("get leaderboard total for " + type.name() + " and metadata " + metadata, connection -> {
PreparedStatement statement = connection.prepareStatement(
"SELECT SUM(count) as total, username FROM headsplus_stats, headsplus_players " +
"SELECT " + (unique ? "COUNT" : "SUM") + "(count) as total, username FROM headsplus_stats, headsplus_players " +
"WHERE collection_type = ? AND metadata LIKE ? " +
"AND headsplus_stats.user_id = headsplus_players.id " +
"GROUP BY headsplus_stats.user_id ORDER BY total DESC");

statement.setString(1, type.name());
statement.setString(2, "%" + metadata + "%");

ResultSet set = statement.executeQuery();
List<LeaderboardEntry> leaderboard = new ArrayList<>();
while (set.next()) {
leaderboard.add(new LeaderboardEntry(set.getString("username"), set.getInt("total")));
}
return leaderboard;
}, true, "get leaderboard total for " + type.name() + " and metadata " + metadata);
return statement;
});
}

public CompletableFuture<List<LeaderboardEntry>> getLeaderboardTotal(CollectionType type, String head,
String metadata) {
return createConnection(connection -> {
String metadata, boolean unique) {

return getLeaderboard("get leaderboard total for " + type.name() + ", head " + head + " and metadata " + metadata, connection -> {
PreparedStatement statement =connection.prepareStatement(
"SELECT SUM(count) as total, username FROM headsplus_stats, headsplus_players " +
"SELECT " + (unique ? "COUNT" : "SUM") + "(count) as total, username FROM headsplus_stats, headsplus_players " +
"WHERE collection_type = ? AND head = ? AND metadata LIKE ? " +
"AND headsplus_stats.user_id = headsplus_players.id " +
"GROUP BY headsplus_stats.user_id ORDER BY total DESC");

statement.setString(1, type.name());
statement.setString(2, head);
statement.setString(3, "%" + metadata + "%");
return statement;
});
}

ResultSet set = statement.executeQuery();
public CompletableFuture<List<LeaderboardEntry>> getLeaderboardUniqueHeads() {
return getLeaderboard("get unique leaderboard total", connection -> connection.prepareStatement(
"SELECT COUNT(count) as total, username FROM headsplus_stats, headsplus_players " +
"WHERE headsplus_stats.user_id = headsplus_players.id " +
"GROUP BY headsplus_stats.user_id ORDER BY total DESC"));
}

private CompletableFuture<List<LeaderboardEntry>> getLeaderboard(String message, SQLFunction<PreparedStatement> statement) {
return createConnection(connection -> {
ResultSet set = statement.applyWithSQL(connection).executeQuery();
List<LeaderboardEntry> leaderboard = new ArrayList<>();
while (set.next()) {
leaderboard.add(new LeaderboardEntry(set.getString("username"), set.getInt("total")));
}
return leaderboard;
}, true, "get leaderboard total for " + type.name() + ", head " + head + " and metadata " + metadata);
}, true, message);
}

public void addToTotal(UUID uuid, CollectionType type, String head, String metadata, int amount, boolean async) {
Expand Down