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

me.hsgamer.unihologram.spigot.common.provider.SpigotHologramProvider Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
package me.hsgamer.unihologram.spigot.common.provider;

import me.hsgamer.unihologram.common.api.Hologram;
import me.hsgamer.unihologram.common.api.HologramProvider;
import me.hsgamer.unihologram.common.hologram.NoneHologram;
import me.hsgamer.unihologram.common.provider.LocalHologramProvider;
import org.bukkit.Location;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Optional;

/**
 * A hologram provider for Spigot.
 * It will use the best provider available.
 */
public class SpigotHologramProvider implements HologramProvider {
    private final HologramProvider provider;

    /**
     * Create a new hologram provider
     *
     * @param plugin the plugin
     */
    public SpigotHologramProvider(Plugin plugin) {
        this.provider = getDefaultProviderOrNone(plugin);
    }

    /**
     * Create a new hologram provider
     */
    public SpigotHologramProvider() {
        this(JavaPlugin.getProvidingPlugin(SpigotHologramProvider.class));
    }

    private static boolean checkClassDependAvailable(Class clazz) {
        try {
            Method method = clazz.getDeclaredMethod("isAvailable");
            if (method.getReturnType().equals(boolean.class) || method.getReturnType().equals(Boolean.class)) {
                return false;
            }
            return (boolean) method.invoke(null);
        } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
            return true;
        }
    }

    private static Optional> getProvider(Plugin plugin, String className) {
        Class clazz;
        try {
            clazz = Class.forName(className);
            if (!HologramProvider.class.isAssignableFrom(clazz)) {
                return Optional.empty();
            }
        } catch (ClassNotFoundException e) {
            return Optional.empty();
        }

        if (!checkClassDependAvailable(clazz)) {
            return Optional.empty();
        }

        Constructor constructor;
        boolean hasPluginConstructor;
        try {
            constructor = clazz.getConstructor(Plugin.class);
            hasPluginConstructor = true;
        } catch (NoSuchMethodException e) {
            try {
                constructor = clazz.getConstructor();
                hasPluginConstructor = false;
            } catch (NoSuchMethodException ex) {
                return Optional.empty();
            }
        }

        HologramProvider provider;
        try {
            if (hasPluginConstructor) {
                //noinspection unchecked
                provider = (HologramProvider) constructor.newInstance(plugin);
            } else {
                //noinspection unchecked
                provider = (HologramProvider) constructor.newInstance();
            }
        } catch (ReflectiveOperationException e) {
            return Optional.empty();
        }

        return Optional.of(provider);
    }

    private static HologramProvider getDefaultProviderOrNone(Plugin plugin) {
        final String[] classNames = {
                "me.hsgamer.unihologram.spigot.decentholograms.provider.DHHologramProvider",
                "me.hsgamer.unihologram.spigot.holographicdisplays.provider.HDHologramProvider",
                "me.hsgamer.unihologram.spigot.cmi.provider.CMIHologramProvider",
                "me.hsgamer.unihologram.spigot.vanilla.provider.VanillaHologramProvider"
        };

        for (String className : classNames) {
            Optional> optional = getProvider(plugin, className);
            if (optional.isPresent()) {
                return optional.get();
            }
        }

        return new LocalHologramProvider() {
            @Override
            protected @NotNull Hologram newHologram(@NotNull String name, @NotNull Location location) {
                return new NoneHologram<>(name, location);
            }
        };
    }

    @Override
    public @NotNull Hologram createHologram(@NotNull String name, @NotNull Location location) {
        return provider.createHologram(name, location);
    }

    @Override
    public Optional> getHologram(@NotNull String name) {
        return provider.getHologram(name);
    }

    @Override
    public Collection> getAllHolograms() {
        return provider.getAllHolograms();
    }

    @Override
    public boolean isLocal() {
        return provider.isLocal();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy