All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.anvilpowered.anvil.api.Environment Maven / Gradle / Ivy

Go to download

A cross-platform database API / ORM / entity framework with useful services for minecraft plugins

The newest version!
/*
 *   Anvil - AnvilPowered
 *   Copyright (C) 2020
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 *
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with this program.  If not, see .
 */

package org.anvilpowered.anvil.api;

import com.google.common.base.Preconditions;
import com.google.common.reflect.TypeToken;
import com.google.inject.Binding;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.TypeLiteral;
import org.anvilpowered.anvil.api.command.CommandNode;
import org.anvilpowered.anvil.api.misc.Named;
import org.anvilpowered.anvil.api.plugin.PluginInfo;
import org.anvilpowered.anvil.api.registry.Registry;
import org.anvilpowered.anvil.api.util.TextService;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.function.Consumer;
import java.util.function.Supplier;

@SuppressWarnings({"UnstableApiUsage", "unchecked"})
public interface Environment extends Named, Comparable {

    static  Binding getBinding(String name, Injector injector) {
        long hash = ((long) name.hashCode()) * ((long) injector.hashCode());
        Binding[] binding = {Anvil.bindingsCache.get(hash)};
        if (binding[0] != null) {
            return (Binding) binding[0];
        }
        injector.getBindings().forEach((k, v) -> {
            if (k.getTypeLiteral().getType().getTypeName().contains(name)) {
                binding[0] = v;
            }
        });
        Binding result = Preconditions.checkNotNull(
            (Binding) binding[0],
            "Could not find binding for service: " + name + " in injector " + injector
        );
        Anvil.bindingsCache.put(hash, result);
        return result;
    }

    static  Key getKey(String name, Injector injector) {
        return Environment.getBinding(name, injector).getKey();
    }

    static  Provider getProvider(String name, Injector injector) {
        return Environment.getBinding(name, injector).getProvider();
    }

    static  T getInstance(String name, Injector injector) {
        return Environment.getProvider(name, injector).get();
    }

    default  Binding getBinding(String name) {
        return getBinding(name, getInjector());
    }

    default  Key getKey(String name) {
        return getKey(name, getInjector());
    }

    default  Provider getProvider(String name) {
        return getProvider(name, getInjector());
    }

    default  T getInstance(String name) {
        return getInstance(name, getInjector());
    }

    void reload();

    Injector getInjector();

    Object getPlugin();

     PluginInfo getPluginInfo();

     TextService getTextService();

    Registry getRegistry();

    interface Builder {

        Builder addModules(Module... modules);

        Builder addModules(Iterable modules);

        Builder addEarlyServices(Key... keys);

        Builder addEarlyServices(Iterable> keys);

        Builder addEarlyServices(Class... classes);

        Builder addEarlyServices(TypeLiteral... typeLiterals);

        Builder addEarlyServices(TypeToken... typeTokens);

         Builder addEarlyServices(Key key, Consumer initializer);

         Builder addEarlyServices(Class clazz, Consumer initializer);

         Builder addEarlyServices(TypeLiteral typeLiteral, Consumer initializer);

         Builder addEarlyServices(TypeToken typeToken, Consumer initializer);

        /**
         * Sets the name for this environment builder.
         *
         * @param name {@link String} Name to set.
         * @return {@code this}
         */
        Builder setName(String name);

        /**
         * Sets the root injector for this environment builder.
         *
         * @param rootInjector {@link Injector} to set. Pass {@code null} to unset.
         * @return {@code this}
         */
        Builder setRootInjector(@Nullable Injector rootInjector);

        /**
         * Sets the logger, currently only necessary on Nukkit.
         *
         * 

* The provided logger is adapted to the {@link org.slf4j.Logger}. * If no logger is provided on Nukkit, Anvil's logger will be used. * (This means logs will be prefixed with "Anvil" instead of your plugin name) *

* *

         *     setLoggerSupplier(MyNukkitPlugin.this::getLogger);
         * 
* * @param logger to set. */ Builder setLoggerSupplier(Supplier logger); /** * This will load your root {@link CommandNode} as * defined by your guice module * * @return {@code this} */ Builder withRootCommand(); /** * Called when the {@link Environment} is loaded. * *

* This {@link Consumer} will be invoked when the {@link Environment} * is first loaded and on subsequent reloads. *

* *

* This method can be called multiple times on one builder. * Preexisting listeners will be used and will not be overridden. *

* * @param listener {@link Consumer} to run when this environment is loaded * @return {@code this} */ Builder whenLoaded(Consumer listener); /** * Called when the {@link Environment} is loaded for the first time. * *

* This {@link Consumer} will only be invoked when the {@link Environment} * is loaded for the first time. *

* *

* This method can be called multiple times on one builder. * Preexisting listeners will be used and will not be overridden. *

* * @param listener {@link Consumer} to run when this environment is ready * @return {@code this} */ Builder whenReady(Consumer listener); /** * Called when the {@link Environment} is reloaded. * *

* This {@link Consumer} will only be invoked when the {@link Environment} * is reloaded, but not when it is first loaded. *

* *

* This method can be called multiple times on one builder. * Preexisting listeners will be used and will not be overridden. *

* * @param listener {@link Consumer} to run when this environment is reloaded * @return {@code this} */ Builder whenReloaded(Consumer listener); /** * Builds an {@link Environment} and registers it. * * @param plugin The owner for this environment */ void register(Object plugin); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy