me.lucko.helper.Services Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of helper Show documentation
Show all versions of helper Show documentation
A utility to reduce boilerplate code in Bukkit plugins.
/*
* This file is part of helper, licensed under the MIT License.
*
* Copyright (c) lucko (Luck)
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.helper;
import me.lucko.helper.internal.LoaderUtils;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.ServicesManager;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.Nonnull;
/**
* Utility class for interacting with the Bukkit {@link ServicesManager}.
*/
public final class Services {
/**
* Loads a service instance, throwing a {@link IllegalStateException} if no registration is
* present.
*
* @param clazz the service class
* @param the service class type
* @return the service instance
*/
@Nonnull
public static T load(@Nonnull Class clazz) {
Objects.requireNonNull(clazz, "clazz");
return get(clazz).orElseThrow(() -> new IllegalStateException("No registration present for service '" + clazz.getName() + "'"));
}
/**
* Loads a service instance
*
* @param clazz the service class
* @param the service class type
* @return the service instance, as an optional
*/
@Nonnull
public static Optional get(@Nonnull Class clazz) {
Objects.requireNonNull(clazz, "clazz");
RegisteredServiceProvider registration = Bukkit.getServicesManager().getRegistration(clazz);
if (registration == null) {
return Optional.empty();
}
return Optional.ofNullable(registration.getProvider());
}
/**
* Provides a service.
*
* @param clazz the service class
* @param instance the service instance
* @param plugin the plugin to register the service to
* @param priority the priority to register the service instance at
* @param the service class type
* @return the same service instance
*/
@Nonnull
public static T provide(@Nonnull Class clazz, @Nonnull T instance, @Nonnull Plugin plugin, @Nonnull ServicePriority priority) {
Objects.requireNonNull(clazz, "clazz");
Objects.requireNonNull(instance, "instance");
Objects.requireNonNull(plugin, "plugin");
Objects.requireNonNull(priority, "priority");
Bukkit.getServicesManager().register(clazz, instance, plugin, priority);
return instance;
}
/**
* Provides a service.
*
* @param clazz the service class
* @param instance the service instance
* @param priority the priority to register the service instance at
* @param the service class type
* @return the same service instance
*/
@Nonnull
public static T provide(@Nonnull Class clazz, @Nonnull T instance, @Nonnull ServicePriority priority) {
return provide(clazz, instance, LoaderUtils.getPlugin(), priority);
}
/**
* Provides a service.
*
* @param clazz the service class
* @param instance the service instance
* @param the service class type
* @return the same service instance
*/
@Nonnull
public static T provide(@Nonnull Class clazz, @Nonnull T instance) {
return provide(clazz, instance, ServicePriority.Normal);
}
private Services() {
throw new UnsupportedOperationException("This class cannot be instantiated");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy