<commit_before>package kernitus.plugin.OldCombatMechanics.utilities.reflection;

import kernitus.plugin.OldCombatMechanics.utilities.reflection.type.PacketType;
import org.bukkit.entity.Player;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.*;

/**
 * From <a href="https://www.spigotmc.org/resources/1-9-anti-collision.28770/">1.9 anti-collision plugin by Mentrixx</a>
 * Modified by kernitus to work with Rayzr's Reflector utility
 * <p>
 * I've tried to contact the author but didn't get an answer after months,
 * and as the plugin has no license decided to take some of the code
 */
public class TeamUtils {
    private static List<Player> securePlayers = new ArrayList<>();
    private static Constructor<?> packetScoreboardTeamConstructor;
    private static Field nameField;
    private static Field modeField;
    private static Field collisionRuleField;
    private static Field playersField;
    private static Map<Player, String> teamNameMap = new WeakHashMap<>();

    static {
        try {
            Class<?> packetTeamClass = Reflector.Packets.getPacket(PacketType.PlayOut, "ScoreboardTeam");
            packetScoreboardTeamConstructor = Reflector.getConstructor(packetTeamClass, 0);

            nameField = Reflector.getInaccessibleField(packetTeamClass, "a");
            modeField = Reflector.getInaccessibleField(packetTeamClass, "i");
            collisionRuleField = Reflector.getInaccessibleField(packetTeamClass, "f");
            playersField = Reflector.getInaccessibleField(packetTeamClass, "h");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static synchronized void sendTeamPacket(Player player) {
        if (getSecurePlayers().contains(player)) {
            return;
        }

        try {
            Object packetTeamObject = packetScoreboardTeamConstructor.newInstance();

            String teamName = UUID.randomUUID().toString().substring(0, 15);
            teamNameMap.put(player, teamName);

            nameField.set(packetTeamObject, teamName);
            modeField.set(packetTeamObject, 0);
            playersField.set(packetTeamObject, Collections.singletonList(player.getName()));

            changePacketCollisionType(packetTeamObject);

            Reflector.Packets.sendPacket(player, packetTeamObject);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Sends a packet that disbands the formerly formed team to re-enable collision.
     * <p>
     * If the player hasn't gotten a {@link #sendTeamPacket(Player)} yet, this method does nothing.
     *
     * @param player the player to send it to
     */
    public static synchronized void sendTeamRemovePacket(Player player) {
        if (!teamNameMap.containsKey(player)) {
            return;
        }
        String teamName = teamNameMap.get(player);

        try {
            Object packetTeamObject = packetScoreboardTeamConstructor.newInstance();

            nameField.set(packetTeamObject, teamName);
            modeField.set(packetTeamObject, 1);

            Reflector.Packets.sendPacket(player, packetTeamObject);

            teamNameMap.remove(player);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Sets the collision rule to never.
     *
     * @param packetTeamObject the packet to set it on
     * @throws ReflectiveOperationException if an error occurs
     */
    public static void changePacketCollisionType(Object packetTeamObject) throws ReflectiveOperationException {
        collisionRuleField.set(packetTeamObject, "never");
    }

    public static List<Player> getSecurePlayers() {
        return securePlayers;
    }
}<commit_msg>Remove some magic numbers in favour of an enum<commit_after>package kernitus.plugin.OldCombatMechanics.utilities.reflection;

import kernitus.plugin.OldCombatMechanics.utilities.reflection.type.PacketType;
import org.bukkit.entity.Player;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.WeakHashMap;

/**
 * From <a href="https://www.spigotmc.org/resources/1-9-anti-collision.28770/">1.9 anti-collision plugin by Mentrixx</a>
 * Modified by kernitus to work with Rayzr's Reflector utility
 * <p>
 * I've tried to contact the author but didn't get an answer after months,
 * and as the plugin has no license decided to take some of the code
 */
public class TeamUtils {
    private static List<Player> securePlayers = new ArrayList<>();
    private static Constructor<?> packetScoreboardTeamConstructor;
    private static Field nameField;
    private static Field modeField;
    private static Field collisionRuleField;
    private static Field playersField;
    private static Map<Player, String> teamNameMap = new WeakHashMap<>();

    static {
        try {
            Class<?> packetTeamClass = Reflector.Packets.getPacket(PacketType.PlayOut, "ScoreboardTeam");
            packetScoreboardTeamConstructor = Reflector.getConstructor(packetTeamClass, 0);

            nameField = Reflector.getInaccessibleField(packetTeamClass, "a");
            modeField = Reflector.getInaccessibleField(packetTeamClass, "i");
            collisionRuleField = Reflector.getInaccessibleField(packetTeamClass, "f");
            playersField = Reflector.getInaccessibleField(packetTeamClass, "h");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static synchronized void sendTeamPacket(Player player) {
        if (getSecurePlayers().contains(player)) {
            return;
        }

        try {
            Object packetTeamObject = packetScoreboardTeamConstructor.newInstance();

            String teamName = UUID.randomUUID().toString().substring(0, 15);
            teamNameMap.put(player, teamName);

            nameField.set(packetTeamObject, teamName);
            modeField.set(packetTeamObject, TeamAction.CREATE.getMinecraftId());
            playersField.set(packetTeamObject, Collections.singletonList(player.getName()));

            changePacketCollisionType(packetTeamObject);

            Reflector.Packets.sendPacket(player, packetTeamObject);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Sends a packet that disbands the formerly formed team to re-enable collision.
     * <p>
     * If the player hasn't gotten a {@link #sendTeamPacket(Player)} yet, this method does nothing.
     *
     * @param player the player to send it to
     */
    public static synchronized void sendTeamRemovePacket(Player player) {
        if (!teamNameMap.containsKey(player)) {
            return;
        }
        String teamName = teamNameMap.get(player);

        try {
            Object packetTeamObject = packetScoreboardTeamConstructor.newInstance();

            nameField.set(packetTeamObject, teamName);
            modeField.set(packetTeamObject, TeamAction.DISBAND.getMinecraftId());

            Reflector.Packets.sendPacket(player, packetTeamObject);

            teamNameMap.remove(player);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Sets the collision rule to never.
     *
     * @param packetTeamObject the packet to set it on
     * @throws ReflectiveOperationException if an error occurs
     */
    public static void changePacketCollisionType(Object packetTeamObject) throws ReflectiveOperationException {
        collisionRuleField.set(packetTeamObject, "never");
    }

    public static List<Player> getSecurePlayers() {
        return securePlayers;
    }

    /**
     * The different actions a {@code PacketPlayOutScoreboardTeam} packet can represent.
     */
    private enum TeamAction {
        CREATE(0),
        DISBAND(1);

        private int minecraftId;

        TeamAction(int minecraftId) {
            this.minecraftId = minecraftId;
        }

        public int getMinecraftId() {
            return minecraftId;
        }
    }
}
{'fraction_non_alphanumeric': 0.0904405448521291, 'fraction_numerical': 0.003681433304699963, 'mean_word_length': 2.908872901678657, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '1797126', 'n_tokens_mistral': 2188, 'n_tokens_neox': 2123, 'n_words': 534}