<commit_before>package cat.nyaa.playtimetracker.command;

import cat.nyaa.nyaacore.cmdreceiver.Arguments;
import cat.nyaa.nyaacore.cmdreceiver.CommandReceiver;
import cat.nyaa.nyaacore.cmdreceiver.SubCommand;
import cat.nyaa.nyaacore.utils.OfflinePlayerUtils;
import cat.nyaa.playtimetracker.*;
import cat.nyaa.playtimetracker.command.sub.ResetCommand;
import cat.nyaa.playtimetracker.db.model.TimeTrackerDbModel;
import cat.nyaa.playtimetracker.utils.CommandUtils;
import cat.nyaa.playtimetracker.utils.TimeUtils;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import java.io.File;
import java.util.Set;
import java.util.UUID;

public class CommandHandler extends CommandReceiver {
    private final I18n i18n;
    private final PlayTimeTracker plugin;


    @SubCommand(value = "reset", permission = "ptt.command.reset")
    public ResetCommand resetCommand;

    /**
     * @param plugin for logging purpose only
     * @param _i18n  i18n
     */
    public CommandHandler(PlayTimeTracker plugin, I18n _i18n) {
        super(plugin, _i18n);
        this.i18n = _i18n;
        this.plugin = plugin;
    }

    @SubCommand(value = "view", permission = "ptt.command.view", isDefaultCommand = true)
    public void view(CommandSender sender, Arguments args) {
        if (PlayTimeTracker.getInstance() == null || PlayTimeTracker.getInstance().getTimeRecordManager() == null) {
            I18n.send(sender, "command.view.err");
            return;
        }
        String targetName = args.next();
        UUID targetUUID = null;
        if (targetName != null) {
            String otherPermission = "ptt.command.view.other";
            if (!sender.hasPermission(otherPermission)) {
                I18n.send(sender, "command.view.no_view_other_permission", otherPermission);
                return;
            }
            targetUUID = CommandUtils.getPlayerUUIDByStr(targetName, sender);
        } else {
            if (sender instanceof Player) {
                targetUUID = ((Player) sender).getUniqueId();
                targetName = sender.getName();
            }
        }
        if (targetUUID == null) {
            I18n.send(sender, "command.view.invalid_target", targetName);
            return;
        }

        TimeTrackerDbModel timeTrackerDbModel = PlayTimeTracker.getInstance().getTimeRecordManager().getPlayerTimeTrackerDbModel(targetUUID);
        if (timeTrackerDbModel == null) {
            I18n.send(sender, "command.view.no_record", targetName);
            return;
        }
        I18n.send(sender, "command.view.query_title", targetName, targetUUID.toString());
        I18n.send(sender, "command.view.last_seen", TimeUtils.dateFormat(timeTrackerDbModel.getLastSeen()));
        I18n.send(sender, "command.view.daily_time", TimeUtils.timeFormat(timeTrackerDbModel.getDailyTime()));
        I18n.send(sender, "command.view.weekly_time", TimeUtils.timeFormat(timeTrackerDbModel.getWeeklyTime()));
        I18n.send(sender, "command.view.monthly_time", TimeUtils.timeFormat(timeTrackerDbModel.getMonthlyTime()));
        I18n.send(sender, "command.view.total_time", TimeUtils.timeFormat(timeTrackerDbModel.getTotalTime()));
    }

    @SubCommand(value = "migration", permission = "ptt.command.migration")
    public void migration(CommandSender sender, Arguments args) {
        if (!args.nextString("").equals("confirm")) {
            I18n.send(sender, "command.migration.confirm");
            return;
        }
        TimeRecordManager timeRecordManager = plugin.getTimeRecordManager();
        if (timeRecordManager == null) {
            I18n.send(sender, "command.migration.err");
            return;
        }
        File dbFile = new File(getPlugin().getDataFolder(), "database.yml");


        if (!dbFile.canRead()) {
            I18n.send(sender, "command.migration.can_not_read");
            return;
        }
        if (!dbFile.exists()) {
            I18n.send(sender, "command.migration.does_not_exist");
            return;
        }
        if (!dbFile.isFile()) {
            I18n.send(sender, "command.migration.nor_a_file");
            return;
        }

        ConfigurationSection cfg = YamlConfiguration.loadConfiguration(dbFile);
        for (String uuid_str : cfg.getKeys(false)) {
            try {
                ConfigurationSection sec = cfg.getConfigurationSection(uuid_str);
                //ZonedDateTime rec_lastSeen = ZonedDateTime.parse(sec.getString("last_seen"));
                UUID rec_id = UUID.fromString(uuid_str);
                long rec_dailyTime = sec.getLong("daily_play_time");
                long rec_weeklyTime = sec.getLong("weekly_play_time");
                long rec_monthlyTime = sec.getLong("monthly_play_time");
                long rec_totalTime = sec.getLong("total_play_time");
                TimeTrackerDbModel trackerDbModel = new TimeTrackerDbModel();
                trackerDbModel.setLastSeen(TimeUtils.getUnixTimeStampNow());
                trackerDbModel.setDailyTime(rec_dailyTime);
                trackerDbModel.setWeeklyTime(rec_weeklyTime);
                trackerDbModel.setMonthlyTime(rec_monthlyTime);
                trackerDbModel.setTotalTime(rec_totalTime);
                trackerDbModel.setPlayerUniqueId(rec_id);
                timeRecordManager.insertOrResetPlayer(trackerDbModel);
                OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(rec_id);

                I18n.send(sender, "command.migration.insert", offlinePlayer.getName());
            } catch (Exception e) {
                e.printStackTrace();
                sender.sendMessage(e.getLocalizedMessage());
            }
            I18n.send(sender, "command.migration.finish");
        }
    }

    @SubCommand(value = "afkstatus", permission = "ptt.command.afkstatus")
    public void afkStatus(CommandSender sender, Arguments args) {
        Player player = args.nextPlayer();

        if (!PlayerAFKManager.isAFK(player.getUniqueId())) {
            I18n.send(sender, "command.afkstatus.no_afk", player.getName());
            return;
        }
        if (PlayTimeTracker.getInstance() == null || PlayTimeTracker.getInstance().getAfkManager() == null) {
            I18n.send(sender, "command.afkstatus.not_found", player.getName());
            return;
        }
        if (PlayerAFKManager.isEssAfk(player.getUniqueId())) {<commit_msg>fix(migration): player name will not be null<commit_after>package cat.nyaa.playtimetracker.command;

import cat.nyaa.nyaacore.cmdreceiver.Arguments;
import cat.nyaa.nyaacore.cmdreceiver.CommandReceiver;
import cat.nyaa.nyaacore.cmdreceiver.SubCommand;
import cat.nyaa.playtimetracker.*;
import cat.nyaa.playtimetracker.command.sub.ResetCommand;
import cat.nyaa.playtimetracker.db.model.TimeTrackerDbModel;
import cat.nyaa.playtimetracker.utils.CommandUtils;
import cat.nyaa.playtimetracker.utils.TimeUtils;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import java.io.File;
import java.util.Set;
import java.util.UUID;

public class CommandHandler extends CommandReceiver {
    private final I18n i18n;
    private final PlayTimeTracker plugin;


    @SubCommand(value = "reset", permission = "ptt.command.reset")
    public ResetCommand resetCommand;

    /**
     * @param plugin for logging purpose only
     * @param _i18n  i18n
     */
    public CommandHandler(PlayTimeTracker plugin, I18n _i18n) {
        super(plugin, _i18n);
        this.i18n = _i18n;
        this.plugin = plugin;
    }

    @SubCommand(value = "view", permission = "ptt.command.view", isDefaultCommand = true)
    public void view(CommandSender sender, Arguments args) {
        if (PlayTimeTracker.getInstance() == null || PlayTimeTracker.getInstance().getTimeRecordManager() == null) {
            I18n.send(sender, "command.view.err");
            return;
        }
        String targetName = args.next();
        UUID targetUUID = null;
        if (targetName != null) {
            String otherPermission = "ptt.command.view.other";
            if (!sender.hasPermission(otherPermission)) {
                I18n.send(sender, "command.view.no_view_other_permission", otherPermission);
                return;
            }
            targetUUID = CommandUtils.getPlayerUUIDByStr(targetName, sender);
        } else {
            if (sender instanceof Player) {
                targetUUID = ((Player) sender).getUniqueId();
                targetName = sender.getName();
            }
        }
        if (targetUUID == null) {
            I18n.send(sender, "command.view.invalid_target", targetName);
            return;
        }

        TimeTrackerDbModel timeTrackerDbModel = PlayTimeTracker.getInstance().getTimeRecordManager().getPlayerTimeTrackerDbModel(targetUUID);
        if (timeTrackerDbModel == null) {
            I18n.send(sender, "command.view.no_record", targetName);
            return;
        }
        I18n.send(sender, "command.view.query_title", targetName, targetUUID.toString());
        I18n.send(sender, "command.view.last_seen", TimeUtils.dateFormat(timeTrackerDbModel.getLastSeen()));
        I18n.send(sender, "command.view.daily_time", TimeUtils.timeFormat(timeTrackerDbModel.getDailyTime()));
        I18n.send(sender, "command.view.weekly_time", TimeUtils.timeFormat(timeTrackerDbModel.getWeeklyTime()));
        I18n.send(sender, "command.view.monthly_time", TimeUtils.timeFormat(timeTrackerDbModel.getMonthlyTime()));
        I18n.send(sender, "command.view.total_time", TimeUtils.timeFormat(timeTrackerDbModel.getTotalTime()));
    }

    @SubCommand(value = "migration", permission = "ptt.command.migration")
    public void migration(CommandSender sender, Arguments args) {
        if (!args.nextString("").equals("confirm")) {
            I18n.send(sender, "command.migration.confirm");
            return;
        }
        TimeRecordManager timeRecordManager = plugin.getTimeRecordManager();
        if (timeRecordManager == null) {
            I18n.send(sender, "command.migration.err");
            return;
        }
        File dbFile = new File(getPlugin().getDataFolder(), "database.yml");


        if (!dbFile.canRead()) {
            I18n.send(sender, "command.migration.can_not_read");
            return;
        }
        if (!dbFile.exists()) {
            I18n.send(sender, "command.migration.does_not_exist");
            return;
        }
        if (!dbFile.isFile()) {
            I18n.send(sender, "command.migration.nor_a_file");
            return;
        }

        ConfigurationSection cfg = YamlConfiguration.loadConfiguration(dbFile);
        for (String uuid_str : cfg.getKeys(false)) {
            try {
                ConfigurationSection sec = cfg.getConfigurationSection(uuid_str);
                //ZonedDateTime rec_lastSeen = ZonedDateTime.parse(sec.getString("last_seen"));
                UUID rec_id = UUID.fromString(uuid_str);
                long rec_dailyTime = sec.getLong("daily_play_time");
                long rec_weeklyTime = sec.getLong("weekly_play_time");
                long rec_monthlyTime = sec.getLong("monthly_play_time");
                long rec_totalTime = sec.getLong("total_play_time");
                TimeTrackerDbModel trackerDbModel = new TimeTrackerDbModel();
                trackerDbModel.setLastSeen(TimeUtils.getUnixTimeStampNow());
                trackerDbModel.setDailyTime(rec_dailyTime);
                trackerDbModel.setWeeklyTime(rec_weeklyTime);
                trackerDbModel.setMonthlyTime(rec_monthlyTime);
                trackerDbModel.setTotalTime(rec_totalTime);
                trackerDbModel.setPlayerUniqueId(rec_id);
                timeRecordManager.insertOrResetPlayer(trackerDbModel);
                OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(rec_id);
                var playerName = offlinePlayer.getName();
                I18n.send(sender, "command.migration.insert", playerName == null ? "{" + rec_id + "}" : playerName);
            } catch (Exception e) {
                e.printStackTrace();
                sender.sendMessage(e.getLocalizedMessage());
            }
            I18n.send(sender, "command.migration.finish");
        }
    }

    @SubCommand(value = "afkstatus", permission = "ptt.command.afkstatus")
    public void afkStatus(CommandSender sender, Arguments args) {
        Player player = args.nextPlayer();

        if (!PlayerAFKManager.isAFK(player.getUniqueId())) {
            I18n.send(sender, "command.afkstatus.no_afk", player.getName());
            return;
        }
        if (PlayTimeTracker.getInstance() == null || PlayTimeTracker.getInstance().getAfkManager() == null) {
            I18n.send(sender, "command.afkstatus.not_found", player.getName());
            return;
        }
        if (PlayerAFKManager.isEssAfk(player.getUniqueId())) {
{'fraction_non_alphanumeric': 0.11300121506682867, 'fraction_numerical': 0.00850546780072904, 'mean_word_length': 2.7625714285714285, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '16202', 'n_tokens_mistral': 3892, 'n_tokens_neox': 3744, 'n_words': 594}