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.DiscordGatewayIntent;
import com.discordsrv.api.discord.connection.details.DiscordMemberCachePolicy; import com.discordsrv.api.discord.connection.details.DiscordMemberCachePolicy;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.function.Consumer;
public interface Module { 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() { default void enable() {}
reload();
}
/** /**
* Called by DiscordSRV to disable this module. * Called by DiscordSRV to disable this module.
@ -105,18 +102,8 @@ public interface Module {
default void disable() {} 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. * Called by DiscordSRV to reload this module. This is called when the module is enabled as well.
* Use {@link #reloadNoResult()} if you don't wish to provide any result. * @param resultConsumer a consumer to supply results to, if any apply
* @return the result(s) that occurred during this reload, if any. May be {@code null}.
*/ */
@Nullable default void reload(Consumer<DiscordSRVApi.ReloadResult> resultConsumer) {}
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() {}
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -219,10 +219,12 @@ public class ModuleManager {
} }
try { try {
Set<DiscordSRVApi.ReloadResult> results = abstractModule.reload(); abstractModule.reload(result -> {
if (results != null) { if (result == null) {
reloadResults.addAll(results); throw new NullPointerException("null result supplied to resultConsumer");
} }
reloadResults.add(result);
});
} catch (Throwable t) { } catch (Throwable t) {
discordSRV.logger().error("Failed to reload " + module.getClass().getSimpleName(), 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 org.jetbrains.annotations.NotNull;
import java.util.Collection; import java.util.Collection;
import java.util.Set; import java.util.function.Consumer;
public class ModuleDelegate extends AbstractModule<DiscordSRV> { public class ModuleDelegate extends AbstractModule<DiscordSRV> {
@ -75,8 +75,8 @@ public class ModuleDelegate extends AbstractModule<DiscordSRV> {
} }
@Override @Override
public Set<DiscordSRVApi.ReloadResult> reload() { public void reload(Consumer<DiscordSRVApi.ReloadResult> resultConsumer) {
return module.reload(); module.reload(resultConsumer);
} }
@Override @Override