Improve Module reload methods

This commit is contained in:
Vankka 2023-09-04 22:48:37 +03:00
parent ca2d4f0df4
commit 6c5064b081
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
9 changed files with 36 additions and 35 deletions

View File

@ -28,11 +28,10 @@ import com.discordsrv.api.discord.connection.details.DiscordCacheFlag;
import com.discordsrv.api.discord.connection.details.DiscordGatewayIntent;
import com.discordsrv.api.discord.connection.details.DiscordMemberCachePolicy;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.function.Consumer;
public interface Module {
@ -93,11 +92,9 @@ public interface Module {
}
/**
* Called by DiscordSRV to enable this module. Calls {@link #reload()} if not implemented.
* Called by DiscordSRV to enable this module.
*/
default void enable() {
reload();
}
default void enable() {}
/**
* Called by DiscordSRV to disable this module.
@ -105,18 +102,8 @@ public interface Module {
default void disable() {}
/**
* Called by DiscordSRV to reload this module. This is called to enable the module as well unless {@link #enable()} is overridden and does not call super.
* Use {@link #reloadNoResult()} if you don't wish to provide any result.
* @return the result(s) that occurred during this reload, if any. May be {@code null}.
* Called by DiscordSRV to reload this module. This is called when the module is enabled as well.
* @param resultConsumer a consumer to supply results to, if any apply
*/
@Nullable
default Set<DiscordSRVApi.ReloadResult> reload() {
reloadNoResult();
return null;
}
/**
* An alternative to {@link #reload()}, which returns {@code void} instead of results. This method will <b>not</b> be called if {@link #reload()} is overridden!
*/
default void reloadNoResult() {}
default void reload(Consumer<DiscordSRVApi.ReloadResult> resultConsumer) {}
}

View File

@ -18,6 +18,7 @@
package com.discordsrv.common.channel;
import com.discordsrv.api.DiscordSRVApi;
import com.discordsrv.api.discord.connection.jda.errorresponse.ErrorCallbackContext;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.config.main.TimedUpdaterConfig;
@ -35,6 +36,7 @@ import java.util.List;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
public class TimedUpdaterModule extends AbstractModule<DiscordSRV> {
@ -64,7 +66,7 @@ public class TimedUpdaterModule extends AbstractModule<DiscordSRV> {
}
@Override
public void reloadNoResult() {
public void reload(Consumer<DiscordSRVApi.ReloadResult> resultConsumer) {
futures.forEach(future -> future.cancel(false));
futures.clear();

View File

@ -18,6 +18,7 @@
package com.discordsrv.common.command.game;
import com.discordsrv.api.DiscordSRVApi;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.command.game.abstraction.GameCommand;
import com.discordsrv.common.command.game.commands.DiscordSRVGameCommand;
@ -28,6 +29,7 @@ import com.discordsrv.common.module.type.AbstractModule;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;
public class GameCommandModule extends AbstractModule<DiscordSRV> {
@ -47,7 +49,7 @@ public class GameCommandModule extends AbstractModule<DiscordSRV> {
}
@Override
public void reloadNoResult() {
public void reload(Consumer<DiscordSRVApi.ReloadResult> resultConsumer) {
GameCommandConfig config = discordSRV.config().gameCommand;
if (config == null) {
return;

View File

@ -18,6 +18,7 @@
package com.discordsrv.common.groupsync;
import com.discordsrv.api.DiscordSRVApi;
import com.discordsrv.api.discord.entity.guild.DiscordRole;
import com.discordsrv.api.discord.events.member.role.DiscordMemberRoleAddEvent;
import com.discordsrv.api.discord.events.member.role.DiscordMemberRoleRemoveEvent;
@ -46,6 +47,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
public class GroupSyncModule extends AbstractModule<DiscordSRV> {
@ -84,7 +86,7 @@ public class GroupSyncModule extends AbstractModule<DiscordSRV> {
}
@Override
public void reloadNoResult() {
public void reload(Consumer<DiscordSRVApi.ReloadResult> resultConsumer) {
synchronized (pairs) {
pairs.values().forEach(future -> {
if (future != null) {

View File

@ -18,6 +18,7 @@
package com.discordsrv.common.invite;
import com.discordsrv.api.DiscordSRVApi;
import com.discordsrv.api.discord.connection.details.DiscordGatewayIntent;
import com.discordsrv.api.discord.connection.jda.errorresponse.ErrorCallbackContext;
import com.discordsrv.api.event.bus.Subscribe;
@ -39,6 +40,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
public class DiscordInviteModule extends AbstractModule<DiscordSRV> {
@ -63,17 +65,17 @@ public class DiscordInviteModule extends AbstractModule<DiscordSRV> {
@Subscribe
public void onGuildInviteDelete(GuildInviteDeleteEvent event) {
if (invite.equals(event.getUrl())) {
reload();
reload(__ -> {});
}
}
@Subscribe
public void onGuildUpdateVanityCode(GuildUpdateVanityCodeEvent event) {
reload();
reload(__ -> {});
}
@Override
public void reloadNoResult() {
public void reload(Consumer<DiscordSRVApi.ReloadResult> resultConsumer) {
JDA jda = discordSRV.jda();
if (jda == null) {
return;

View File

@ -18,6 +18,7 @@
package com.discordsrv.common.linking.requirelinking;
import com.discordsrv.api.DiscordSRVApi;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.config.main.linking.RequiredLinkingConfig;
import com.discordsrv.common.linking.impl.MinecraftAuthenticationLinker;
@ -36,6 +37,7 @@ import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Consumer;
public abstract class RequiredLinkingModule<T extends DiscordSRV> extends AbstractModule<T> {
@ -75,7 +77,7 @@ public abstract class RequiredLinkingModule<T extends DiscordSRV> extends Abstra
}
@Override
public void reloadNoResult() {
public void reload(Consumer<DiscordSRVApi.ReloadResult> resultConsumer) {
List<Requirement<?>> requirements = new ArrayList<>();
requirements.add(new DiscordRoleRequirement(discordSRV));

View File

@ -18,6 +18,7 @@
package com.discordsrv.common.linking.requirelinking;
import com.discordsrv.api.DiscordSRVApi;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.component.util.ComponentUtil;
import com.discordsrv.common.config.main.linking.RequirementsConfig;
@ -31,6 +32,7 @@ import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
public abstract class ServerRequireLinkingModule<T extends DiscordSRV> extends RequiredLinkingModule<T> {
@ -44,8 +46,8 @@ public abstract class ServerRequireLinkingModule<T extends DiscordSRV> extends R
public abstract ServerRequiredLinkingConfig config();
@Override
public void reloadNoResult() {
super.reloadNoResult();
public void reload(Consumer<DiscordSRVApi.ReloadResult> resultConsumer) {
super.reload(resultConsumer);
synchronized (compiledRequirements) {
compiledRequirements.clear();

View File

@ -219,10 +219,12 @@ public class ModuleManager {
}
try {
Set<DiscordSRVApi.ReloadResult> results = abstractModule.reload();
if (results != null) {
reloadResults.addAll(results);
abstractModule.reload(result -> {
if (result == null) {
throw new NullPointerException("null result supplied to resultConsumer");
}
reloadResults.add(result);
});
} catch (Throwable t) {
discordSRV.logger().error("Failed to reload " + module.getClass().getSimpleName(), t);
}

View File

@ -28,7 +28,7 @@ import com.discordsrv.common.logging.NamedLogger;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Set;
import java.util.function.Consumer;
public class ModuleDelegate extends AbstractModule<DiscordSRV> {
@ -75,8 +75,8 @@ public class ModuleDelegate extends AbstractModule<DiscordSRV> {
}
@Override
public Set<DiscordSRVApi.ReloadResult> reload() {
return module.reload();
public void reload(Consumer<DiscordSRVApi.ReloadResult> resultConsumer) {
module.reload(resultConsumer);
}
@Override