Search in sources :

Example 1 with Timing

use of com.voxelgameslib.voxelgameslib.internal.timings.Timing in project VoxelGamesLibv2 by VoxelGamesLib.

the class VoxelGamesLib method onEnable.

@Override
public void onEnable() {
    try {
        // logging first, only changes prefixes anyways
        loggingHandler = new LoggingHandler();
        loggingHandler.setErrorHandler(errorHandler);
        loggingHandler.enable();
        // enable by enabling external stuff. they don't require any VGL stuff
        // timings
        timingManager = TimingManager.of(this);
        // commands
        commandManager = new BukkitCommandManager(this);
        commandManager.setDefaultExceptionHandler((scope, registeredCommand, sender, args, t) -> {
            errorHandler.handle(sender, args, t);
            return false;
        });
        // living on the edge!
        commandManager.enableUnstableAPI("help");
        // task chain
        taskChainFactory = BukkitTaskChainFactory.create(this);
        taskChainFactory.setDefaultErrorHandler((e, t) -> {
            log.severe("Task " + t.hashCode() + " generated an exception:");
            e.printStackTrace();
        });
        // chat menu api
        ChatMenuAPI.init(this);
        // menu builder (excuse the hack, but we want to shade you)
        MenuBuilderPlugin menuBuilderPluginFake = new ObjenesisStd().getInstantiatorOf(MenuBuilderPlugin.class).newInstance();
        menuBuilderPluginFake.inventoryListener = new InventoryListener(menuBuilderPluginFake);
        MenuBuilderPlugin.instance = menuBuilderPluginFake;
        Bukkit.getPluginManager().registerEvents(menuBuilderPluginFake.inventoryListener, this);
        // guice
        VoxelGamesLibModule module = new VoxelGamesLibModule(this, loggingHandler, timingManager, commandManager, getVersion(), getDataFolder(), ModuleHandler.getOfferedModules(), errorHandler);
        injector = module.createInjector();
        injector.injectMembers(this);
        // startup handler
        startupHandler.registerService("onEnable");
        Bukkit.getPluginManager().registerEvents(injector.getInstance(StartupListener.class), this);
        // then enable all VGL stuff
        try (final Timing timing = new Timing("EnableAllHandlers")) {
            eventHandler.enable();
            configHandler.enable();
            persistenceHandler.enable();
            langHandler.enable();
            tickHandler.enable();
            chatHandler.enable();
            userHandler.enable();
            roleHandler.enable();
            mapHandler.enable();
            worldHandler.enable();
            teamHandler.enable();
            eloHandler.enable();
            matchmakingHandler.enable();
            signHandler.enable();
            metricHandler.enable();
            pointHandler.enable();
            kitHandler.enable();
            commandHandler.enable();
            textureHandler.enable();
            statsHandler.enable();
            gameHandler.enable();
        }
        registerListeners();
    } catch (Exception ex) {
        errorHandler.handle(ex, Severity.ERROR, true);
        startupHandler.interrupt();
        return;
    }
    // register commands
    registerCommandContexts();
    registerCommandReplacements();
    registerCommands();
    registerCommandCompletions();
    moduleHandler.enable();
    gameHandler.startDefaultGame();
    getServer().getPluginManager().callEvent(new VoxelGamesLibEnableEvent());
    testStuff.test();
    startupHandler.unregisterService("onEnable");
    enabledCleanly = true;
}
Also used : MenuBuilderPlugin(org.inventivetalent.menubuilder.MenuBuilderPlugin) LoggingHandler(com.voxelgameslib.voxelgameslib.internal.log.LoggingHandler) BukkitCommandManager(co.aikar.commands.BukkitCommandManager) ObjenesisStd(org.objenesis.ObjenesisStd) InventoryListener(org.inventivetalent.menubuilder.inventory.InventoryListener) VoxelGamesLibEnableEvent(com.voxelgameslib.voxelgameslib.api.event.events.VoxelGamesLibEnableEvent) Timing(com.voxelgameslib.voxelgameslib.internal.timings.Timing) StartupListener(com.voxelgameslib.voxelgameslib.internal.startup.StartupListener)

Example 2 with Timing

use of com.voxelgameslib.voxelgameslib.internal.timings.Timing in project VoxelGamesLibv2 by VoxelGamesLib.

the class MetricHandler method enable.

@Override
public void enable() {
    try (Timing timing = new Timing("MetricsEnable")) {
        metrics = new Metrics(voxelGamesLib);
        // TODO add custom charts, like user/gamemode, installed gamesmodes, user/lang, installed langs etc
        // gamemodes multiline TODO enable this on the bstats page once its implemented....
        metrics.addCustomChart(new Metrics.MultiLineChart("gamemodes", () -> {
            Map<String, Integer> valueMap = new HashMap<>();
            gameHandler.getGameModes().forEach((gm) -> valueMap.put(gm.getName(), 1));
            return valueMap;
        }));
    } catch (Throwable ex) {
        log.warning("Metrics failed to enabled. This is not a critical problem. You can ignore it.");
    }
}
Also used : Inject(javax.inject.Inject) Timing(com.voxelgameslib.voxelgameslib.internal.timings.Timing) VoxelGamesLib(com.voxelgameslib.voxelgameslib.VoxelGamesLib) Map(java.util.Map) Metrics(org.bstats.bukkit.Metrics) HashMap(java.util.HashMap) GameHandler(com.voxelgameslib.voxelgameslib.api.game.GameHandler) Handler(com.voxelgameslib.voxelgameslib.internal.handler.Handler) Logger(java.util.logging.Logger) Singleton(javax.inject.Singleton) Metrics(org.bstats.bukkit.Metrics) Timing(com.voxelgameslib.voxelgameslib.internal.timings.Timing) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with Timing

use of com.voxelgameslib.voxelgameslib.internal.timings.Timing in project VoxelGamesLibv2 by VoxelGamesLib.

the class ModuleHandler method findModules.

private void findModules() {
    try (final Timing timing = new Timing("RegisterModules")) {
        isAcceptingOffers = false;
        for (Class<? extends Module> clazz : offeredModules.keySet()) {
            ModuleInfo info = clazz.getAnnotation(ModuleInfo.class);
            if (info == null) {
                log.warning("Class " + clazz.getSimpleName() + " has no module info!");
                continue;
            }
            log.info("Loading module " + info.name() + " v" + info.version() + " by " + Arrays.toString(info.authors()));
            if (Module.class.isAssignableFrom(clazz)) {
                // noinspection SuspiciousMethodCalls
                Module module = offeredModules.get(clazz);
                injector.injectMembers(module);
                this.modules.add(module);
            } else {
                log.warning("Class " + clazz.getSimpleName() + " has the ModuleInfo annotation but does not implement Module!");
            }
        }
        offeredModules.clear();
        log.info("Loaded " + this.modules.size() + " modules!");
    }
}
Also used : Timing(com.voxelgameslib.voxelgameslib.internal.timings.Timing)

Example 4 with Timing

use of com.voxelgameslib.voxelgameslib.internal.timings.Timing in project VoxelGamesLibv2 by VoxelGamesLib.

the class StatsHandler method enable.

@Override
public void enable() {
    registerTrackable(StatType.JOIN_COUNT);
    Bukkit.getScheduler().runTaskTimer(vgl, () -> statTypes.stream().filter(Stat::shouldTick).forEach(Stat::tickOneMinute), 20 * 60, 20 * 60);
    try (final Timing timing = new Timing("RegisterStatTypes")) {
        // noinspection unchecked
        scanner.getSubclasses(Stat.class.getName()).loadClasses().forEach(clazz -> registerStatType((Class<? extends Stat>) clazz));
    }
    log.info("Registered " + statTypes.size() + " StatsTypes");
    Bukkit.getScheduler().runTaskTimer(vgl, () -> userHandler.getUsers().forEach(user -> {
        if (user.getUserData().getStats().values().stream().anyMatch(StatInstance::isDirty)) {
            log.finer("Persisting stats for " + user.getRawDisplayName());
            persistenceHandler.getProvider().saveUser(user.getUserData());
        }
    }), 60 * 20, 60 * 20);
}
Also used : ClassGraph(io.github.classgraph.ClassGraph) Arrays(java.util.Arrays) Timing(com.voxelgameslib.voxelgameslib.internal.timings.Timing) LoadingCache(com.google.common.cache.LoadingCache) Handler(com.voxelgameslib.voxelgameslib.internal.handler.Handler) Singleton(javax.inject.Singleton) ArrayList(java.util.ArrayList) Pair(com.voxelgameslib.voxelgameslib.util.Pair) Inject(javax.inject.Inject) UserHandler(com.voxelgameslib.voxelgameslib.components.user.UserHandler) VoxelGamesLib(com.voxelgameslib.voxelgameslib.VoxelGamesLib) ScanResult(io.github.classgraph.ScanResult) Severity(com.bugsnag.Severity) Bukkit(org.bukkit.Bukkit) UUID(java.util.UUID) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) ErrorHandler(com.voxelgameslib.voxelgameslib.internal.error.ErrorHandler) CacheLoader(com.google.common.cache.CacheLoader) Injector(com.google.inject.Injector) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Component(net.kyori.text.Component) PersistenceHandler(com.voxelgameslib.voxelgameslib.internal.persistence.PersistenceHandler) Optional(java.util.Optional) Named(com.google.inject.name.Named) CacheBuilder(com.google.common.cache.CacheBuilder) Timing(com.voxelgameslib.voxelgameslib.internal.timings.Timing)

Example 5 with Timing

use of com.voxelgameslib.voxelgameslib.internal.timings.Timing in project VoxelGamesLibv2 by VoxelGamesLib.

the class HibernatePersistenceProvider method enable.

@Override
public void enable() {
    boolean shouldCreateTable = config.persistence.initialTableCreation;
    if (shouldCreateTable) {
        config.persistence.initialTableCreation = false;
        configHandler.saveGlobalConfig();
    }
    startupHandler.registerService("Hibernate");
    Thread thread = new Thread(() -> {
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder().applySetting("hibernate.connection.username", config.persistence.user).applySetting("hibernate.connection.password", config.persistence.pass).applySetting("hibernate.connection.driver_class", config.persistence.driver).applySetting("hibernate.connection.url", config.persistence.url + "?useSSL=false").applySetting("hibernate.dialect", config.persistence.dialect).applySetting("hibernate.hbm2ddl.auto", shouldCreateTable ? "create" : "update").applySetting("hibernate.show_sql", config.persistence.showSQL + "").applySetting("hibernate.enable_lazy_load_no_trans", true).applySetting("hibernate.connection.autocommit", true).applySetting("hibernate.connection.pool_size", config.persistence.pool_size + "").build();
        MetadataSources sources = new MetadataSources(registry);
        try (final Timing timing = new Timing("Init converters")) {
            scanner.getClassesImplementing(VGLConverter.class.getName()).loadClasses().forEach((annotatedClass) -> {
                try {
                    ((VGLConverter<?, ?>) annotatedClass.newInstance()).init();
                } catch (InstantiationException | IllegalAccessException e) {
                    log.warning("Error while initializing converter " + annotatedClass.getSimpleName());
                    e.printStackTrace();
                }
            });
        }
        try (final Timing timing = new Timing("RegisterDBEntities")) {
            scanner.getClassesWithAnnotation(Entity.class.getName()).loadClasses().forEach((annotatedClass) -> {
                if (!annotatedClass.getName().contains("ebean"))
                    sources.addAnnotatedClass(annotatedClass);
            });
        }
        try {
            Metadata metadata = sources.buildMetadata();
            sessionFactory = metadata.buildSessionFactory();
            log.info("Build HibernationSessionFactory with " + sources.getAnnotatedClasses().size() + " entities.");
        } catch (Exception e) {
            StandardServiceRegistryBuilder.destroy(registry);
            e.printStackTrace();
        }
        cBuilder = sessionFactory.getCriteriaBuilder();
        startupHandler.unregisterService("Hibernate");
    });
    thread.setName("Hibernate Startup");
    thread.start();
}
Also used : Entity(javax.persistence.Entity) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) VGLConverter(com.voxelgameslib.voxelgameslib.internal.persistence.converter.VGLConverter) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) JDBCConnectionException(org.hibernate.exception.JDBCConnectionException) Timing(com.voxelgameslib.voxelgameslib.internal.timings.Timing) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry)

Aggregations

Timing (com.voxelgameslib.voxelgameslib.internal.timings.Timing)7 Named (com.google.inject.name.Named)2 VoxelGamesLib (com.voxelgameslib.voxelgameslib.VoxelGamesLib)2 Handler (com.voxelgameslib.voxelgameslib.internal.handler.Handler)2 ClassGraph (io.github.classgraph.ClassGraph)2 Logger (java.util.logging.Logger)2 Inject (javax.inject.Inject)2 Singleton (javax.inject.Singleton)2 BukkitCommandManager (co.aikar.commands.BukkitCommandManager)1 Severity (com.bugsnag.Severity)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 CacheLoader (com.google.common.cache.CacheLoader)1 LoadingCache (com.google.common.cache.LoadingCache)1 Injector (com.google.inject.Injector)1 Provides (com.google.inject.Provides)1 VoxelGamesLibDisableEvent (com.voxelgameslib.voxelgameslib.api.event.events.VoxelGamesLibDisableEvent)1 VoxelGamesLibEnableEvent (com.voxelgameslib.voxelgameslib.api.event.events.VoxelGamesLibEnableEvent)1 GameHandler (com.voxelgameslib.voxelgameslib.api.game.GameHandler)1 UserHandler (com.voxelgameslib.voxelgameslib.components.user.UserHandler)1 ErrorHandler (com.voxelgameslib.voxelgameslib.internal.error.ErrorHandler)1