<commit_before>package net.glowstone;

import com.destroystokyo.paper.event.profile.ProfileWhitelistVerifyEvent;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import lombok.Getter;
import lombok.Setter;
import net.glowstone.entity.GlowPlayer;
import net.glowstone.i18n.ConsoleMessages;
import net.glowstone.i18n.GlowstoneMessages;
import net.glowstone.i18n.GlowstoneMessages.Kick;
import net.glowstone.scheduler.GlowScheduler;
import org.bukkit.BanList;
import org.bukkit.BanList.Type;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerLoginEvent.Result;
import org.bukkit.event.player.PlayerPreLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.scheduler.BukkitScheduler;

/**
 * Central class for the calling of events.
 */
public class EventFactory {

    /**
     * The instance of this class. Setter should only be called in tests when mocking.
     */
    @Getter
    @Setter
    private static EventFactory instance = new EventFactory();

    private EventFactory() {
    }

    /**
     * Calls an event through the plugin manager.
     *
     * @param event The event to throw.
     * @param <T> The type of the event.
     * @return the called event
     */
    public <T extends Event> T callEvent(T event) {
        Server server = ServerProvider.getServer();
        if (event.isAsynchronous()) {
            server.getPluginManager().callEvent(event);
            return event;
        } else {
            FutureTask<T> task = new FutureTask<>(
                () -> server.getPluginManager().callEvent(event), event);
            BukkitScheduler scheduler = server.getScheduler();
            ((GlowScheduler) scheduler).scheduleInTickExecution(task);
            try {
                return task.get();
            } catch (InterruptedException e) {
                ConsoleMessages.Warn.Event.INTERRUPTED.log(e,
                        event.getClass().getSimpleName());
                return event;
            } catch (CancellationException e) {
                ConsoleMessages.Warn.Event.SHUTDOWN.log(event.getClass().getSimpleName());
                return event;
            } catch (ExecutionException e) {
                throw new RuntimeException(e); // No checked exceptions declared for callEvent
            }
        }
    }<commit_msg>Optimization: don't fire a task if it has no handlers Will limit the impact of #994.<commit_after>package net.glowstone;

import com.destroystokyo.paper.event.profile.ProfileWhitelistVerifyEvent;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import lombok.Getter;
import lombok.Setter;
import net.glowstone.entity.GlowPlayer;
import net.glowstone.i18n.ConsoleMessages;
import net.glowstone.i18n.GlowstoneMessages;
import net.glowstone.i18n.GlowstoneMessages.Kick;
import net.glowstone.scheduler.GlowScheduler;
import org.bukkit.BanList;
import org.bukkit.BanList.Type;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerLoginEvent.Result;
import org.bukkit.event.player.PlayerPreLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.scheduler.BukkitScheduler;

/**
 * Central class for the calling of events.
 */
public class EventFactory {

    /**
     * The instance of this class. Setter should only be called in tests when mocking.
     */
    @Getter
    @Setter
    private static EventFactory instance = new EventFactory();

    private EventFactory() {
    }

    /**
     * Calls an event through the plugin manager.
     *
     * @param event The event to throw.
     * @param <T> The type of the event.
     * @return the called event
     */
    public <T extends Event> T callEvent(T event) {
        if (event.getHandlers().getRegisteredListeners().length == 0) {
            return event;
        }
        Server server = ServerProvider.getServer();
        if (event.isAsynchronous()) {
            server.getPluginManager().callEvent(event);
            return event;
        } else {
            FutureTask<T> task = new FutureTask<>(
                () -> server.getPluginManager().callEvent(event), event);
            BukkitScheduler scheduler = server.getScheduler();
            ((GlowScheduler) scheduler).scheduleInTickExecution(task);
            try {
                return task.get();
            } catch (InterruptedException e) {
                ConsoleMessages.Warn.Event.INTERRUPTED.log(e,
                        event.getClass().getSimpleName());
                return event;
            } catch (CancellationException e) {
                ConsoleMessages.Warn.Event.SHUTDOWN.log(event.getClass().getSimpleName());
                return event;
            } catch (ExecutionException e) {
                throw new RuntimeException(e); // No checked exceptions declared for callEvent
            }
        }
    }
{'fraction_non_alphanumeric': 0.09968282736746716, 'fraction_numerical': 0.0024165533907264765, 'mean_word_length': 3.212468193384224, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '3008853', 'n_tokens_mistral': 1988, 'n_tokens_neox': 1905, 'n_words': 406}