Change integration registering to a safer method

This commit is contained in:
Vankka 2022-03-28 17:13:11 +03:00
parent 9124c18d7a
commit 1f6cefebac
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
2 changed files with 28 additions and 4 deletions

View File

@ -25,7 +25,6 @@ import com.discordsrv.bukkit.config.main.BukkitConfig;
import com.discordsrv.bukkit.config.manager.BukkitConfigManager;
import com.discordsrv.bukkit.config.manager.BukkitConnectionConfigManager;
import com.discordsrv.bukkit.console.BukkitConsole;
import com.discordsrv.bukkit.integration.VaultIntegration;
import com.discordsrv.bukkit.listener.BukkitChatListener;
import com.discordsrv.bukkit.listener.BukkitConnectionListener;
import com.discordsrv.bukkit.listener.BukkitDeathListener;
@ -188,9 +187,11 @@ public class BukkitDiscordSRV extends ServerDiscordSRV<BukkitConfig, BukkitConne
server().getPluginManager().registerEvents(new BukkitStatusMessageListener(this), plugin());
// Modules
registerModule(VaultIntegration::new);
registerModule(MinecraftToDiscordChatModule::new);
// Integrations
registerIntegration("com.discordsrv.bukkit.integration.VaultIntegration");
super.enable();
}

View File

@ -69,13 +69,16 @@ import com.discordsrv.common.storage.Storage;
import com.discordsrv.common.storage.StorageType;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.dv8tion.jda.api.JDA;
import okhttp3.*;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.annotation.OverridingMethodsMustInvokeSuper;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.util.Locale;
import java.util.Optional;
@ -306,10 +309,27 @@ public abstract class AbstractDiscordSRV<C extends MainConfig, CC extends Connec
}
@SuppressWarnings("unchecked")
protected <T extends DiscordSRV> void registerModule(CheckedFunction<T, AbstractModule<?>> function) {
protected final <T extends DiscordSRV> void registerModule(CheckedFunction<T, AbstractModule<?>> function) {
moduleManager.registerModule((T) this, function);
}
/**
* @param className a class which has a constructor with {@link DiscordSRV} (or implementation specific) as the only parameter.
*/
protected final void registerIntegration(
@Language(value = "JAVA", prefix = "class X{static{Class.forName(\"", suffix = "\");}}") String className
) {
Object module;
try {
Class<?> clazz = Class.forName(className);
Constructor<?> constructor = clazz.getConstructor(getClass());
module = constructor.newInstance(this);
} catch (Throwable ignored) {
return;
}
moduleManager.registerModule(this, d -> (AbstractModule<?>) module);
}
@Override
public void unregisterModule(AbstractModule<?> module) {
moduleManager.unregister(module);
@ -440,6 +460,9 @@ public abstract class AbstractDiscordSRV<C extends MainConfig, CC extends Connec
registerModule(LeaveMessageModule::new);
registerModule(DiscordInviteModule::new);
// Integrations
registerIntegration("com.discordsrv.common.integration.LuckPermsIntegration");
// Initial load
try {
invokeReload(ReloadFlag.ALL, true).get();