<commit_before>package net.md_5.itag;

import com.comphenix.protocol.Packets;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.ConnectionSide;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.Futures;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.Callable;
import lombok.Getter;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.kitteh.tag.PlayerReceiveNameTagEvent;
import org.kitteh.tag.TagAPI;

public class iTag extends JavaPlugin implements Listener
{

    @Getter
    private static iTag instance;
    private TagAPI tagAPI;
    private Map<Integer, Player> entityIdMap;

    @Override
    public void onEnable()
    {
        instance = this;
        entityIdMap = new WeakHashMap<Integer, Player>();
        tagAPI = new TagAPI();
        tagAPI.install( this );

        getServer().getPluginManager().registerEvents( this, this );
        ProtocolLibrary.getProtocolManager().addPacketListener( new PacketAdapter( this, ConnectionSide.SERVER_SIDE, ListenerPriority.NORMAL, Packets.Server.NAMED_ENTITY_SPAWN )
        {
            @Override
            public void onPacketSending(PacketEvent event)
            {
                event.getPacket().getSpecificModifier( String.class ).write( 0, getSentName(
                        event.getPacket().getSpecificModifier( int.class ).read( 0 ),
                        event.getPacket().getSpecificModifier( String.class ).read( 0 ),
                        event.getPlayer() ) );
            }
        } );
    }

    @EventHandler
    public void onJoin(PlayerJoinEvent event)
    {
        entityIdMap.put( event.getPlayer().getEntityId(), event.getPlayer() );
    }

    @EventHandler
    public void onQuit(PlayerQuitEvent event)
    {
        entityIdMap.remove( event.getPlayer().getEntityId() );
    }

    @Override
    public void onDisable()
    {
        ProtocolLibrary.getProtocolManager().removePacketListeners( this );
        tagAPI.uninstall();

        entityIdMap.clear();
        entityIdMap = null;
        tagAPI = null;
        instance = null;
    }

    private String getSentName(int sentEntityId, String sentName, Player destinationPlayer)
    {
        final PlayerReceiveNameTagEvent event = new PlayerReceiveNameTagEvent( destinationPlayer, entityIdMap.get( sentEntityId ), sentName );
        if ( getServer().isPrimaryThread() )
        {
            getServer().getPluginManager().callEvent( event );
        } else
        {
            Futures.getUnchecked( getServer().getScheduler().callSyncMethod( this, new Callable<Void>()
            {
                public Void call() throws Exception
                {
                    getServer().getPluginManager().callEvent( event );
                    return null;
                }
            } ) );
        }

        return event.getTag().substring( 0, Math.min( event.getTag().length(), 16 ) );
    }<commit_msg>Using a weakhashmap was a bad idea, also make more resiliant to reloads<commit_after>package net.md_5.itag;

import com.comphenix.protocol.Packets;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.ConnectionSide;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.Futures;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import lombok.Getter;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.kitteh.tag.PlayerReceiveNameTagEvent;
import org.kitteh.tag.TagAPI;

public class iTag extends JavaPlugin implements Listener
{

    @Getter
    private static iTag instance;
    private TagAPI tagAPI;
    private Map<Integer, Player> entityIdMap;

    @Override
    public void onEnable()
    {
        instance = this;
        entityIdMap = new HashMap<Integer, Player>();
        tagAPI = new TagAPI();
        tagAPI.install( this );

        for ( Player player : getServer().getOnlinePlayers() )
        {
            entityIdMap.put( player.getEntityId(), player );
        }

        getServer().getPluginManager().registerEvents( this, this );
        ProtocolLibrary.getProtocolManager().addPacketListener( new PacketAdapter( this, ConnectionSide.SERVER_SIDE, ListenerPriority.NORMAL, Packets.Server.NAMED_ENTITY_SPAWN )
        {
            @Override
            public void onPacketSending(PacketEvent event)
            {
                event.getPacket().getSpecificModifier( String.class ).write( 0, getSentName(
                        event.getPacket().getSpecificModifier( int.class ).read( 0 ),
                        event.getPacket().getSpecificModifier( String.class ).read( 0 ),
                        event.getPlayer() ) );
            }
        } );
    }

    @EventHandler
    public void onJoin(PlayerJoinEvent event)
    {
        entityIdMap.put( event.getPlayer().getEntityId(), event.getPlayer() );
    }

    @EventHandler
    public void onQuit(PlayerQuitEvent event)
    {
        entityIdMap.remove( event.getPlayer().getEntityId() );
    }

    @Override
    public void onDisable()
    {
        ProtocolLibrary.getProtocolManager().removePacketListeners( this );
        tagAPI.uninstall();

        entityIdMap.clear();
        entityIdMap = null;
        tagAPI = null;
        instance = null;
    }

    private String getSentName(int sentEntityId, String sentName, Player destinationPlayer)
    {
        Player namedPlayer = entityIdMap.get( sentEntityId );
        if ( namedPlayer == null )
        {
            // They probably were dead when we reloaded
            return sentName;
        }

        final PlayerReceiveNameTagEvent event = new PlayerReceiveNameTagEvent( destinationPlayer, namedPlayer, sentName );
        if ( getServer().isPrimaryThread() )
        {
            getServer().getPluginManager().callEvent( event );
        } else
        {
            Futures.getUnchecked( getServer().getScheduler().callSyncMethod( this, new Callable<Void>()
            {
                public Void call() throws Exception
                {
                    getServer().getPluginManager().callEvent( event );
                    return null;
                }
            } ) );
        }

        return event.getTag().substring( 0, Math.min( event.getTag().length(), 16 ) );
    }
{'fraction_non_alphanumeric': 0.10112994350282486, 'fraction_numerical': 0.0019774011299435027, 'mean_word_length': 2.6219948849104857, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '4424868', 'n_tokens_mistral': 1979, 'n_tokens_neox': 1922, 'n_words': 390}