<commit_before>package fr.aumgn.bukkitutils.playerref;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

/**
 * Hold a reference to a player.
 */
public final class PlayerRef {

    private static final Map<String, PlayerRef> playersRef =
                new HashMap<String, PlayerRef>();

    public static PlayerRef get(OfflinePlayer player) {
        return get(player.getName());
    }

    public static PlayerRef get(String name) {
        String lname = name.toLowerCase(Locale.ENGLISH);
        if (!playersRef.containsKey(lname)) {
            playersRef.put(lname, new PlayerRef(lname));
        }

        return playersRef.get(lname);
    }

    private final String name;

    private PlayerRef(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public String getDisplayName() {
        Player player = getPlayer();
        if (player != null) {
            return player.getDisplayName();
        } else {
            return getName();
        }
    }

    public boolean isOnline() {
        return (getPlayer() != null);
    }

    public boolean isOffline() {
        return (getPlayer() == null);
    }

    public Player getPlayer() {
        return Bukkit.getPlayerExact(name);
    }

    public OfflinePlayer getOfflinePlayer() {
        return Bukkit.getOfflinePlayer(name);
    }

    @Override
    public boolean equals(Object other) {
        if (!(other instanceof PlayerRef)) {
            return false;
        }

        return name.equals(((PlayerRef) other).name);
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }

    @Override
    public String toString() {
        return name;
    }
}
<commit_msg>[PlayerRef] Keep a weak reference to the bukkit player<commit_after>package fr.aumgn.bukkitutils.playerref;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

/**
 * Hold a reference to a player.
 */
public final class PlayerRef {

    private static final Map<String, PlayerRef> playersRef =
                new HashMap<String, PlayerRef>();

    public static PlayerRef get(OfflinePlayer player) {
        return get(player.getName());
    }

    public static PlayerRef get(String name) {
        String lname = name.toLowerCase(Locale.ENGLISH);
        if (!playersRef.containsKey(lname)) {
            playersRef.put(lname, new PlayerRef(lname));
        }

        return playersRef.get(lname);
    }

    private final String name;
    private transient WeakReference<Player> ref;

    private PlayerRef(String name) {
        this.name = name;
        this.ref = new WeakReference<Player>(null);
    }

    public String getName() {
        return name;
    }

    public String getDisplayName() {
        Player player = getPlayer();
        if (player != null) {
            return player.getDisplayName();
        } else {
            return getName();
        }
    }

    public boolean isOnline() {
        return (getPlayer() != null);
    }

    public boolean isOffline() {
        return (getPlayer() == null);
    }

    public Player getPlayer() {
        Player player = ref.get();
        if (player == null) {
            player = Bukkit.getPlayerExact(name);
            ref = new WeakReference<Player>(player);
        }

        return player;
    }

    public OfflinePlayer getOfflinePlayer() {
        return Bukkit.getOfflinePlayer(name);
    }

    @Override
    public boolean equals(Object other) {
        if (!(other instanceof PlayerRef)) {
            return false;
        }

        return name.equals(((PlayerRef) other).name);
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }

    @Override
    public String toString() {
        return name;
    }
}

{'fraction_non_alphanumeric': 0.1054590570719603, 'fraction_numerical': 0.0, 'mean_word_length': 2.256058158319871, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '2979507', 'n_tokens_mistral': 1173, 'n_tokens_neox': 1131, 'n_words': 290}