Fix more generics, change the way modules are registered

This commit is contained in:
Vankka 2022-01-20 00:04:07 +02:00
parent 0f5d36b407
commit a89ab7441e
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
11 changed files with 29 additions and 87 deletions

View File

@ -33,7 +33,7 @@ import com.discordsrv.bukkit.scheduler.BukkitScheduler;
import com.discordsrv.common.config.manager.ConnectionConfigManager;
import com.discordsrv.common.config.manager.MainConfigManager;
import com.discordsrv.common.logging.Logger;
import com.discordsrv.common.module.ModuleInitializationFunction;
import com.discordsrv.common.messageforwarding.game.MinecraftToDiscordChatModule;
import com.discordsrv.common.server.ServerDiscordSRV;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.Server;
@ -140,15 +140,8 @@ public class BukkitDiscordSRV extends ServerDiscordSRV<BukkitConfig, BukkitConne
server().getPluginManager().registerEvents(new BukkitDeathListener(this), plugin());
server().getPluginManager().registerEvents(new BukkitStatusMessageListener(this), plugin());
for (ModuleFunction function : new ModuleFunction[]{
VaultIntegration::new
}) {
try {
registerModule(function.initialize(this));
} catch (Throwable ignored) {}
}
registerModule(VaultIntegration::new);
registerModule(MinecraftToDiscordChatModule::new);
}
private interface ModuleFunction extends ModuleInitializationFunction<BukkitDiscordSRV> {}
}

View File

@ -21,7 +21,6 @@ package com.discordsrv.proxy;
import com.discordsrv.common.AbstractDiscordSRV;
import com.discordsrv.common.config.connection.ConnectionConfig;
import com.discordsrv.common.config.main.MainConfig;
import com.discordsrv.common.module.ModuleInitializationFunction;
import com.discordsrv.proxy.modules.ServerSwitchMessageModule;
public abstract class ProxyDiscordSRV<C extends MainConfig, CC extends ConnectionConfig> extends AbstractDiscordSRV<C, CC> {
@ -30,12 +29,6 @@ public abstract class ProxyDiscordSRV<C extends MainConfig, CC extends Connectio
protected void enable() throws Throwable {
super.enable();
for (ModuleInitializationFunction function : new ModuleInitializationFunction[]{
ServerSwitchMessageModule::new
}) {
try {
registerModule(function.initialize(this));
} catch (Throwable ignored) {}
}
registerModule(ServerSwitchMessageModule::new);
}
}

View File

@ -21,7 +21,6 @@ package com.discordsrv.common.server;
import com.discordsrv.common.AbstractDiscordSRV;
import com.discordsrv.common.config.connection.ConnectionConfig;
import com.discordsrv.common.config.main.MainConfig;
import com.discordsrv.common.module.ModuleInitializationFunction;
import com.discordsrv.common.server.modules.DeathMessageModule;
import com.discordsrv.common.server.player.ServerPlayerProvider;
import com.discordsrv.common.server.scheduler.ServerScheduler;
@ -42,16 +41,8 @@ public abstract class ServerDiscordSRV<C extends MainConfig, CC extends Connecti
protected void enable() throws Throwable {
super.enable();
for (ModuleFunction function : new ModuleFunction[]{
DeathMessageModule::new
}) {
try {
registerModule(function.initialize(this));
} catch (Throwable ignored) {}
registerModule(DeathMessageModule::new);
}
}
private interface ModuleFunction extends ModuleInitializationFunction<ServerDiscordSRV<?, ?>> {}
public final CompletableFuture<Void> invokeServerStarted() {
return invokeLifecycle(this::serverStarted, "Failed to enable", true);

View File

@ -22,7 +22,6 @@ import com.discordsrv.api.discord.connection.DiscordConnectionDetails;
import com.discordsrv.api.event.bus.EventBus;
import com.discordsrv.api.event.events.lifecycle.DiscordSRVReloadEvent;
import com.discordsrv.api.event.events.lifecycle.DiscordSRVShuttingDownEvent;
import com.discordsrv.common.discord.api.DiscordAPIEventModule;
import com.discordsrv.common.api.util.ApiInstanceUtil;
import com.discordsrv.common.channel.ChannelConfigHelper;
import com.discordsrv.common.channel.ChannelUpdaterModule;
@ -32,23 +31,23 @@ import com.discordsrv.common.config.connection.ConnectionConfig;
import com.discordsrv.common.config.main.MainConfig;
import com.discordsrv.common.config.manager.ConnectionConfigManager;
import com.discordsrv.common.config.manager.MainConfigManager;
import com.discordsrv.common.discord.api.DiscordAPIEventModule;
import com.discordsrv.common.discord.api.DiscordAPIImpl;
import com.discordsrv.common.discord.connection.DiscordConnectionManager;
import com.discordsrv.common.discord.connection.jda.JDAConnectionManager;
import com.discordsrv.common.discord.details.DiscordConnectionDetailsImpl;
import com.discordsrv.common.event.bus.EventBusImpl;
import com.discordsrv.common.function.CheckedFunction;
import com.discordsrv.common.function.CheckedRunnable;
import com.discordsrv.common.integration.LuckPermsIntegration;
import com.discordsrv.common.logging.impl.DiscordSRVLogger;
import com.discordsrv.common.logging.Logger;
import com.discordsrv.common.logging.adapter.DependencyLoggerAdapter;
import com.discordsrv.common.logging.impl.DependencyLoggingHandler;
import com.discordsrv.common.logging.impl.DiscordSRVLogger;
import com.discordsrv.common.messageforwarding.discord.DiscordChatMessageModule;
import com.discordsrv.common.messageforwarding.discord.DiscordMessageMirroringModule;
import com.discordsrv.common.messageforwarding.game.JoinMessageModule;
import com.discordsrv.common.messageforwarding.game.LeaveMessageModule;
import com.discordsrv.common.messageforwarding.game.MinecraftToDiscordChatModule;
import com.discordsrv.common.module.ModuleInitializationFunction;
import com.discordsrv.common.module.ModuleManager;
import com.discordsrv.common.module.type.AbstractModule;
import com.discordsrv.common.module.type.Module;
@ -187,6 +186,13 @@ public abstract class AbstractDiscordSRV<C extends MainConfig, CC extends Connec
moduleManager.register(module);
}
@SuppressWarnings("unchecked")
protected <T extends DiscordSRV> void registerModule(CheckedFunction<T, AbstractModule<?>> function) {
try {
registerModule(function.apply((T) this));
} catch (Throwable ignored) {}
}
@Override
public void unregisterModule(AbstractModule<?> module) {
moduleManager.unregister(module);
@ -302,28 +308,16 @@ public abstract class AbstractDiscordSRV<C extends MainConfig, CC extends Connec
// Register modules
moduleManager = new ModuleManager(this);
for (ModuleFunction function : new ModuleFunction[]{
ChannelUpdaterModule::new,
GlobalChannelLookupModule::new,
DiscordAPIEventModule::new,
LuckPermsIntegration::new,
DiscordChatMessageModule::new,
DiscordMessageMirroringModule::new,
JoinMessageModule::new,
LeaveMessageModule::new,
MinecraftToDiscordChatModule::new,
}) {
try {
registerModule(function.initialize(this));
} catch (Throwable ignored) {}
registerModule(ChannelUpdaterModule::new);
registerModule(GlobalChannelLookupModule::new);
registerModule(DiscordAPIEventModule::new);
registerModule(LuckPermsIntegration::new);
registerModule(DiscordChatMessageModule::new);
registerModule(DiscordMessageMirroringModule::new);
registerModule(JoinMessageModule::new);
registerModule(LeaveMessageModule::new);
}
}
private interface ModuleFunction extends ModuleInitializationFunction<DiscordSRV> {}
@OverridingMethodsMustInvokeSuper
protected void disable() {

View File

@ -34,7 +34,7 @@ import java.util.Set;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class ChannelUpdaterModule extends AbstractModule {
public class ChannelUpdaterModule extends AbstractModule<DiscordSRV> {
private final Set<ScheduledFuture<?>> futures = new LinkedHashSet<>();
private boolean firstReload = true;

View File

@ -24,7 +24,7 @@ import com.discordsrv.api.event.events.channel.GameChannelLookupEvent;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.module.type.AbstractModule;
public class GlobalChannelLookupModule extends AbstractModule {
public class GlobalChannelLookupModule extends AbstractModule<DiscordSRV> {
private final DefaultGlobalChannel defaultGlobalChannel;

View File

@ -30,7 +30,7 @@ import net.dv8tion.jda.api.events.message.MessageDeleteEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.message.MessageUpdateEvent;
public class DiscordAPIEventModule extends AbstractModule {
public class DiscordAPIEventModule extends AbstractModule<DiscordSRV> {
public DiscordAPIEventModule(DiscordSRV discordSRV) {
super(discordSRV);

View File

@ -41,7 +41,7 @@ import net.kyori.adventure.text.Component;
import java.util.Map;
public class DiscordChatMessageModule extends AbstractModule {
public class DiscordChatMessageModule extends AbstractModule<DiscordSRV> {
public DiscordChatMessageModule(DiscordSRV discordSRV) {
super(discordSRV);

View File

@ -44,7 +44,7 @@ import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
public class DiscordMessageMirroringModule extends AbstractModule {
public class DiscordMessageMirroringModule extends AbstractModule<DiscordSRV> {
private final Cache<MessageReference, Set<MessageReference>> mapping;

View File

@ -48,7 +48,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CopyOnWriteArrayList;
public abstract class AbstractGameMessageModule<T> extends AbstractModule {
public abstract class AbstractGameMessageModule<T> extends AbstractModule<DiscordSRV> {
public AbstractGameMessageModule(DiscordSRV discordSRV) {
super(discordSRV);

View File

@ -1,29 +0,0 @@
/*
* This file is part of DiscordSRV, licensed under the GPLv3 License
* Copyright (c) 2016-2022 Austin "Scarsz" Shapiro, Henri "Vankka" Schubin and DiscordSRV contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.discordsrv.common.module;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.module.type.AbstractModule;
@FunctionalInterface
public interface ModuleInitializationFunction<DT extends DiscordSRV> {
AbstractModule<DT> initialize(DT discordSRV) throws Throwable;
}