Skip to content
Closed
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 @@ -26,6 +26,7 @@ public Commander(ColdTracker plugin) {
this.registerCommand(new DumpCommand());
this.registerCommand(new ExportCommand());
this.registerCommand(new ShowVotesCommand());
this.registerCommand(new StatsCommand());

}

Expand Down
110 changes: 110 additions & 0 deletions src/main/java/dev/padrewin/coldtracker/commands/StatsCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package dev.padrewin.coldtracker.commands;

import dev.padrewin.coldtracker.ColdTracker;
import dev.padrewin.coldtracker.manager.CommandManager;
import dev.padrewin.coldtracker.manager.LocaleManager;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

public class StatsCommand extends BaseCommand {

public StatsCommand() {
super("stats", CommandManager.CommandAliases.STATS);
}

@Override
public void execute(@NotNull ColdTracker plugin, @NotNull CommandSender sender, @NotNull String[] args) {
LocaleManager localeManager = plugin.getManager(LocaleManager.class);
String prefix = localeManager.getLocaleMessage("prefix");

// Console handling
if (!(sender instanceof Player)) {
if (args.length == 1) {
String targetName = args[0];
OfflinePlayer targetPlayer = Bukkit.getOfflinePlayer(targetName);

if (!targetPlayer.hasPlayedBefore()) {
sender.sendMessage(prefix + localeManager.getLocaleMessage("player-not-found").replace("{player}", targetName));
return;
}

UUID targetUUID = targetPlayer.getUniqueId();
showStats(plugin, localeManager, sender, targetUUID, targetPlayer.getName());
} else {
sender.sendMessage(prefix + localeManager.getLocaleMessage("command-stats-console-only"));
}
return;
}

// Player handling
Player player = (Player) sender;
UUID playerUUID = player.getUniqueId();

if (args.length == 0) {
// Self stats
showStats(plugin, localeManager, sender, playerUUID, player.getName());
} else if (args.length == 1) {
// Target stats
if (!sender.hasPermission("coldtracker.stats.others")) {
sender.sendMessage(prefix + localeManager.getLocaleMessage("no-permission"));
return;
}

String targetName = args[0];
OfflinePlayer targetPlayer = Bukkit.getOfflinePlayer(targetName);

if (!targetPlayer.hasPlayedBefore()) {
sender.sendMessage(prefix + localeManager.getLocaleMessage("player-not-found").replace("{player}", targetName));
return;
}

UUID targetUUID = targetPlayer.getUniqueId();
boolean trackTime = targetPlayer.isOnline() && targetPlayer.getPlayer().hasPermission("coldtracker.tracktime");
boolean trackVotes = targetPlayer.isOnline() && targetPlayer.getPlayer().hasPermission("coldtracker.trackvote");

if (!trackTime && !trackVotes) {
sender.sendMessage(prefix + localeManager.getLocaleMessage("no-staff-member").replace("{player}", targetName));
return;
}

showStats(plugin, localeManager, sender, targetUUID, targetPlayer.getName());
} else {
sender.sendMessage(prefix + localeManager.getLocaleMessage("invalid-command-usage"));
}
}

private void showStats(ColdTracker plugin, LocaleManager localeManager, CommandSender sender, UUID playerUUID, String playerName) {
String prefix = localeManager.getLocaleMessage("prefix");
long totalTime = plugin.getDatabaseManager().getTotalTime(playerUUID);
long hours = (totalTime / 1000) / 3600;
long minutes = ((totalTime / 1000) % 3600) / 60;
long seconds = (totalTime / 1000) % 60;
long days = hours / 24;
hours = hours % 24;
String timeFormatted = String.format("%dd %dh %dm %ds", days, hours, minutes, seconds);

String statsMessage = prefix + localeManager.getLocaleMessage("command-stats-playtime").replace("{time}", timeFormatted);

if (plugin.getConfig().getBoolean("track-votes", false)) {
int totalVotes = plugin.getDatabaseManager().getTotalVotes(playerUUID);
statsMessage += " " + localeManager.getLocaleMessage("command-stats-votes").replace("{votes}", String.valueOf(totalVotes));
}

sender.sendMessage(statsMessage);
}

@Override
public List<String> tabComplete(@NotNull ColdTracker plugin, @NotNull CommandSender sender, @NotNull String[] args) {
if (args.length == 1 && sender.hasPermission("coldtracker.stats.others")) {
return null;
}
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public List<String> get() {
},
SHOWTIME,
SHOWVOTES,
STATS,
RELOAD,
WIPE,
VERSION,
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/locale/en_US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ showvotes-message: "&c{player} &7has a total of &c{votes}&7 votes."
command-showvotes-disabled: '&7Vote tracker &cdisabled&7. Please consult configuration file.'
command-votes-not-available: '&7Voting plugin not detected. Vote tracking is &cunavailable&7.'

# Stats Command
command-stats-description: '&8 - &c/coldtracker stats &7- View your personal stats'
command-stats-player-only: '&cOnly players can use this command.'
command-stats-console-only: '&7Console can''t have stats.'
command-stats-playtime: '&7You have a total time of &c{time}&7.'
command-stats-votes: '&7and &c{votes} votes&7.'

# Database message warnings
command-wipe-description: '&8 - &c/coldtracker wipe &7- Wipe current SQLite database'
command-wipe-usage: '&7Usage: &c/coldtracker wipe'
Expand Down
12 changes: 12 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ commands:
permission: "coldtracker.dump"
showvotes:
permission: "coldtracker.showvotes"
stats:
permission: "coldtracker.stats"

permissions:
coldtracker.*:
Expand All @@ -32,6 +34,8 @@ permissions:
coldtracker.dump: true
coldtracker.export: true
coldtracker.showvotes: true
coldtracker.stats: true
coldtracker.stats.others: true

coldtracker.version:
description: Gives access to the version command
Expand Down Expand Up @@ -68,3 +72,11 @@ permissions:
coldtracker.trackvote:
description: Gives access to track the votes
default: op

coldtracker.stats:
description: Gives access to the stats command
default: op

coldtracker.stats.others:
description: Gives access to view stats of other staff members
default: op
Loading