Ryan Bonner

Can you make a plugin that tracks when people kill other people and a command to see that

Kodari AI
KodariNov 12 at 4:03 am 1
PvPKillTracker
A Minecraft plugin that tracks player-versus-player kills in real-time. When a player kills another player, the plugin records the kill event, storing the killer's name, victim's name, and timestamp. Includes a command to view kill statistics, allowing players to check their own kill count or another player's kill history. The plugin maintains persistent data across server restarts, providing a comprehensive kill tracking system for PvP-focused servers.

Created the player-kill tracker. Use /kills to see your total or /kills <player> to see another player’s total. Counts persist across server restarts.

Ryan Bonner

Make it so that only operators can use it, also make it so that using /serverkills, pulls up a page that shows who has killed who

Kodari AI
KodariNov 12 at 4:05 am 1
  1. Restrict kill commands to operators.

  2. Add a paginated /serverkills history showing who killed whom.

Both /kills and /serverkills [page] are now operator-only. The server kill history is paginated and persists across restarts.

History starts recording from this change; earlier kill totals don’t include victim details.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
Code
3 171
src/main/java/com/kodari/playerkills/PlayerKillsPlugin.java
package com.kodari.playerkills;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.plugin.java.JavaPlugin;

public final class PlayerKillsPlugin extends JavaPlugin implements Listener {
    private File dataFile;
    private YamlConfiguration data;

    @Override
    public void onEnable() {
        dataFile = new File(getDataFolder(), "kills.yml");
        data = YamlConfiguration.loadConfiguration(dataFile);
        getServer().getPluginManager().registerEvents(this, this);
    }

    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) {
        Player victim = event.getEntity();
        Player killer = victim.getKiller();
        if (killer == null || killer.getUniqueId().equals(victim.getUniqueId())) {
            return;
        }

        String path = "players." + killer.getUniqueId();
        data.set(path + ".name", killer.getName());
        data.set(path + ".kills", data.getLong(path + ".kills", 0L) + 1L);
        List<String> history = data.getStringList("history");
        history.add(killer.getName() + " killed " + victim.getName());
        data.set("history", history);
        if (!getDataFolder().exists() && !getDataFolder().mkdirs()) {
            getLogger().warning("Could not create the plugin data folder; kill counts could not be saved.");
            return;
        }
        try {
            data.save(dataFile);
        } catch (IOException exception) {
            getLogger().log(Level.SEVERE, "Could not save kill counts", exception);
        }
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!sender.isOp()) {
            sender.sendMessage(ChatColor.RED + "Only operators can use this command.");
            return true;
        }
        if (command.getName().equalsIgnoreCase("serverkills")) {
            if (args.length > 1) {
                sender.sendMessage(ChatColor.RED + "Usage: /" + label + " [page]");
                return true;
            }
            List<String> history = data.getStringList("history");
            int pages = Math.max(1, (int) ((history.size() + 9L) / 10));
            int page = 1;
            if (args.length == 1) {
                try {
                    page = Integer.parseInt(args[0]);
                } catch (NumberFormatException exception) {
                    sender.sendMessage(ChatColor.RED + "Enter a valid page number.");
                    return true;
                }
            }
            if (page < 1 || page > pages) {
                sender.sendMessage(ChatColor.RED + "Page must be between 1 and " + pages + ".");
                return true;
            }
            sender.sendMessage(ChatColor.GOLD + "Server kill history - page " + page + "/" + pages);
            if (history.isEmpty()) {
                sender.sendMessage(ChatColor.GRAY + "No kills recorded yet.");
            } else {
                int start = history.size() - 1 - (page - 1) * 10;
                for (int i = start; i >= 0 && i > start - 10; i--) {
                    sender.sendMessage(ChatColor.GRAY + history.get(i));
                }
            }
            return true;
        }
        if (args.length > 1) {
            sender.sendMessage(ChatColor.RED + "Usage: /" + label + " [player]");
            return true;
        }

        UUID id = null;
        String name = null;
        if (args.length == 0) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "Specify a player: /" + label + " <player>");
                return true;
            }
            Player player = (Player) sender;
            id = player.getUniqueId();
            name = player.getName();
        } else {
            Player online = Bukkit.getPlayerExact(args[0]);
            if (online != null) {
                id = online.getUniqueId();
                name = online.getName();
            } else {
                ConfigurationSection players = data.getConfigurationSection("players");
                if (players != null) {
                    for (String key : players.getKeys(false)) {
                        String storedName = players.getString(key + ".name");
                        if (storedName != null && storedName.equalsIgnoreCase(args[0])) {
                            try {
                                id = UUID.fromString(key);
                                name = storedName;
                            } catch (IllegalArgumentException ignored) {
                                continue;
                            }
                            break;
                        }
                    }
                }
            }
        }

        if (id == null) {
            sender.sendMessage(ChatColor.RED + "No online player or recorded killer found with that name.");
            return true;
        }
        long kills = data.getLong("players." + id + ".kills", 0L);
        sender.sendMessage(ChatColor.GOLD + name + " has " + kills + " player kill" + (kills == 1 ? "" : "s") + ".");
        return true;
    }
}