Untitled

 avatar
unknown
plain_text
a year ago
3.1 kB
5
Indexable
package net.thebridgemc.domainstats;

import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Main extends JavaPlugin implements Listener {

    private Map<String, Set<String>> joinedToday = new HashMap<>();
    private Set<String> newPlayersToday = new HashSet<>();
    private String todayDate;

    @Override
    public void onEnable() {
        Bukkit.getServer().getPluginManager().registerEvents(this, this);
        saveDefaultConfig();
        todayDate = getCurrentDate();
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        String playerName = event.getPlayer().getName();
        boolean hasPlayedBefore = event.getPlayer().hasPlayedBefore();

        if (!hasPlayedBefore) {
            newPlayersToday.add(playerName);
        }
    }

    @EventHandler
    public void onPlayerLogin(PlayerLoginEvent event) {
        String domain = event.getHostname().toLowerCase(); // Use the full hostname as a single string
        String playerName = event.getPlayer().getName();
        String currentDate = getCurrentDate();

        if (!currentDate.equals(todayDate)) {
            todayDate = currentDate;
            joinedToday.clear();
            newPlayersToday.clear();
        }

        String configPath = currentDate + "." + domain.replace(".", "_"); // Replace dots to prevent YAML nesting issues

        // Initialize config path if it doesn't exist
        if (!getConfig().contains(configPath)) {
            getConfig().set(configPath + ".Unique", 0);
            getConfig().set(configPath + ".Re-Join", 0);
        }

        // Initialize today's join set for this domain if it doesn't exist
        joinedToday.putIfAbsent(domain, new HashSet<>());

        boolean isFirstJoinToday = !joinedToday.get(domain).contains(playerName);
        boolean isNewPlayer = newPlayersToday.contains(playerName);

        // Handle unique counter
        if (isNewPlayer && isFirstJoinToday) {
            int uniqueCount = getConfig().getInt(configPath + ".Unique", 0);
            getConfig().set(configPath + ".Unique", uniqueCount + 1);
        }

        // Handle re-join counter
        if (!isNewPlayer && isFirstJoinToday) {
            int rejoinCount = getConfig().getInt(configPath + ".Re-Join", 0);
            getConfig().set(configPath + ".Re-Join", rejoinCount + 1);
        }

        // Add player to today's join set for this domain
        joinedToday.get(domain).add(playerName);

        saveConfig();
    }

    private String getCurrentDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        return sdf.format(new Date());
    }
}

Editor is loading...
Leave a Comment