Skip to content

Commit 8044e7b

Browse files
committed
Improve plugin behaviour when startup fails or the plugin hasn't been configured yet
1 parent a9b3ff4 commit 8044e7b

File tree

14 files changed

+95
-57
lines changed

14 files changed

+95
-57
lines changed

api/src/main/java/com/discordsrv/api/DiscordSRVApi.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,14 @@ enum Status {
208208
CONNECTED,
209209

210210
/**
211-
* DiscordSRV failed to load its configuration.
211+
* DiscordSRV has not been configured.
212212
* @see #isError()
213213
* @see #isStartupError()
214214
*/
215-
FAILED_TO_LOAD_CONFIG(true),
215+
NOT_CONFIGURED(true),
216216

217217
/**
218-
* DiscordSRV failed to start, unless the configuration failed to load, in that case the status will be {@link #FAILED_TO_LOAD_CONFIG}.
218+
* DiscordSRV failed to start.
219219
* @see #isError()
220220
* @see #isStartupError()
221221
*/
@@ -260,7 +260,7 @@ public boolean isShutdown() {
260260
}
261261

262262
public boolean isStartupError() {
263-
return this == FAILED_TO_START || this == FAILED_TO_LOAD_CONFIG;
263+
return this == FAILED_TO_START || this == NOT_CONFIGURED;
264264
}
265265

266266
public boolean isReady() {

bukkit/src/main/java/com/discordsrv/bukkit/command/game/handler/BukkitBasicCommandHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class BukkitBasicCommandHandler extends BasicCommandHandler implements IC
4141
protected final Logger logger;
4242

4343
public BukkitBasicCommandHandler(BukkitDiscordSRV discordSRV) {
44+
super(discordSRV);
4445
this.discordSRV = discordSRV;
4546
this.logger = new NamedLogger(discordSRV, "COMMAND_HANDLER");
4647
}

bukkit/src/main/java/com/discordsrv/bukkit/command/game/handler/CommodoreHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public CommodoreHandler(BukkitDiscordSRV discordSRV) {
4040
protected void registerPluginCommand(Command command, GameCommand gameCommand) {
4141
super.registerPluginCommand(command, gameCommand);
4242

43-
LiteralCommandNode<?> commandNode = BrigadierUtil.convertToBrigadier(gameCommand, null);
43+
LiteralCommandNode<?> commandNode = BrigadierUtil.convertToBrigadier(discordSRV, gameCommand, null);
4444
commodore.register(command, commandNode);
4545
}
4646
}

common/src/main/java/com/discordsrv/common/AbstractDiscordSRV.java

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -756,51 +756,44 @@ protected List<ReloadResult> reload(Set<ReloadFlag> flags, boolean initial) thro
756756
boolean configUpgrade = flags.contains(ReloadFlag.CONFIG_UPGRADE);
757757

758758
if (flags.contains(ReloadFlag.CONFIG) || configUpgrade) {
759-
try {
760-
Path backupPath = null;
761-
if (configUpgrade) {
762-
backupPath = generateBackupPath();
763-
}
759+
Path backupPath = null;
760+
if (configUpgrade) {
761+
backupPath = generateBackupPath();
762+
}
764763

765-
AtomicBoolean anyMissingOptions = new AtomicBoolean(false);
766-
connectionConfigManager().reload(configUpgrade, anyMissingOptions, backupPath);
767-
configManager().reload(configUpgrade, anyMissingOptions, backupPath);
768-
messagesConfigManager().reload(configUpgrade, anyMissingOptions, backupPath);
764+
AtomicBoolean anyMissingOptions = new AtomicBoolean(false);
765+
connectionConfigManager().reload(configUpgrade, anyMissingOptions, backupPath);
766+
configManager().reload(configUpgrade, anyMissingOptions, backupPath);
767+
messagesConfigManager().reload(configUpgrade, anyMissingOptions, backupPath);
769768

770-
if (anyMissingOptions.get()) {
771-
if (config().automaticConfigurationUpgrade) {
772-
logger().info("Some configuration options are missing, attempting to upgrade configuration...");
769+
if (anyMissingOptions.get()) {
770+
if (config().automaticConfigurationUpgrade) {
771+
logger().info("Some configuration options are missing, attempting to upgrade configuration...");
773772

774-
if (backupPath == null) {
775-
backupPath = generateBackupPath();
776-
}
777-
AtomicBoolean stillMissingOptions = new AtomicBoolean(false);
773+
if (backupPath == null) {
774+
backupPath = generateBackupPath();
775+
}
776+
AtomicBoolean stillMissingOptions = new AtomicBoolean(false);
778777

779-
connectionConfigManager().reload(true, stillMissingOptions, backupPath);
780-
configManager().reload(true, stillMissingOptions, backupPath);
781-
messagesConfigManager().reload(true, stillMissingOptions, backupPath);
778+
connectionConfigManager().reload(true, stillMissingOptions, backupPath);
779+
configManager().reload(true, stillMissingOptions, backupPath);
780+
messagesConfigManager().reload(true, stillMissingOptions, backupPath);
782781

783-
if (stillMissingOptions.get()) {
784-
logger().warning("Attempted to upgrade configuration automatically, but some options are still missing.");
785-
} else {
786-
logger().info("Configuration successfully upgraded");
787-
}
788-
} else if (configUpgrade) {
789-
logger().warning("Attempted to upgrade configuration by reload command, but some options are still missing.");
782+
if (stillMissingOptions.get()) {
783+
logger().warning("Attempted to upgrade configuration automatically, but some options are still missing.");
790784
} else {
791-
logger().info("Use \"/discordsrv reload config_upgrade\" to write the latest configuration");
792-
logger().info("Or set \"automatic-configuration-upgrade\" to true in the config to automatically upgrade the configuration on startup or reload");
785+
logger().info("Configuration successfully upgraded");
793786
}
787+
} else if (configUpgrade) {
788+
logger().warning("Attempted to upgrade configuration by reload command, but some options are still missing.");
789+
} else {
790+
logger().info("Use \"/discordsrv reload config_upgrade\" to write the latest configuration");
791+
logger().info("Or set \"automatic-configuration-upgrade\" to true in the config to automatically upgrade the configuration on startup or reload");
794792
}
795-
796-
channelConfig().reload();
797-
createHttpClient();
798-
} catch (Throwable t) {
799-
if (initial) {
800-
setStatus(Status.FAILED_TO_LOAD_CONFIG);
801-
}
802-
throw t;
803793
}
794+
795+
channelConfig().reload();
796+
createHttpClient();
804797
}
805798

806799
List<ReloadResult> results = new ArrayList<>();
@@ -819,6 +812,7 @@ protected List<ReloadResult> reload(Set<ReloadFlag> flags, boolean initial) thro
819812
logger().info("");
820813
}
821814
discordConnectionManager.invalidToken(true);
815+
setStatus(Status.NOT_CONFIGURED);
822816
results.add(ReloadResult.DEFAULT_BOT_TOKEN);
823817
return results;
824818
}

common/src/main/java/com/discordsrv/common/command/game/GameCommandModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public GameCommandModule(DiscordSRV discordSRV) {
4242

4343
@Override
4444
public boolean canEnableBeforeReady() {
45-
// Can enable after JDA starts attempting to connect
45+
// Can enable after JDA starts attempting to connect or if startup fails
4646
return discordSRV.config() != null && discordSRV.status() != DiscordSRVApi.Status.INITIALIZED;
4747
}
4848

common/src/main/java/com/discordsrv/common/command/game/abstraction/handler/BasicCommandHandler.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818

1919
package com.discordsrv.common.command.game.abstraction.handler;
2020

21+
import com.discordsrv.common.DiscordSRV;
2122
import com.discordsrv.common.command.game.abstraction.command.GameCommand;
2223
import com.discordsrv.common.command.game.abstraction.command.GameCommandArguments;
2324
import com.discordsrv.common.command.game.abstraction.sender.ICommandSender;
25+
import com.discordsrv.common.util.CommandUtil;
2426
import net.kyori.adventure.text.Component;
2527
import net.kyori.adventure.text.format.NamedTextColor;
2628
import net.kyori.adventure.text.format.TextDecoration;
@@ -32,8 +34,13 @@
3234

3335
public class BasicCommandHandler implements ICommandHandler {
3436

37+
private final DiscordSRV discordSRV;
3538
private final Map<String, GameCommand> commands = new HashMap<>();
3639

40+
public BasicCommandHandler(DiscordSRV discordSRV) {
41+
this.discordSRV = discordSRV;
42+
}
43+
3744
@Override
3845
public void registerCommand(GameCommand command) {
3946
commands.put(command.getLabel(), command);
@@ -144,6 +151,8 @@ private <T> T useCommand(
144151
}
145152

146153
public void execute(ICommandSender sender, String command, List<String> arguments) {
154+
CommandUtil.basicStatusCheck(discordSRV, sender);
155+
147156
useCommand(
148157
command,
149158
arguments,

common/src/main/java/com/discordsrv/common/command/game/abstraction/handler/util/BrigadierUtil.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818

1919
package com.discordsrv.common.command.game.abstraction.handler.util;
2020

21+
import com.discordsrv.common.DiscordSRV;
2122
import com.discordsrv.common.command.game.abstraction.command.GameCommand;
2223
import com.discordsrv.common.command.game.abstraction.command.GameCommandArguments;
2324
import com.discordsrv.common.command.game.abstraction.command.GameCommandExecutor;
2425
import com.discordsrv.common.command.game.abstraction.command.GameCommandSuggester;
2526
import com.discordsrv.common.command.game.abstraction.sender.ICommandSender;
2627
import com.discordsrv.common.permission.game.Permission;
28+
import com.discordsrv.common.util.CommandUtil;
2729
import com.mojang.brigadier.Command;
2830
import com.mojang.brigadier.arguments.*;
2931
import com.mojang.brigadier.builder.ArgumentBuilder;
@@ -48,12 +50,12 @@ public final class BrigadierUtil {
4850

4951
private BrigadierUtil() {}
5052

51-
public static <S> LiteralCommandNode<S> convertToBrigadier(GameCommand command, Function<S, ICommandSender> commandSenderMapper) {
52-
return (LiteralCommandNode<S>) convert(command, commandSenderMapper);
53+
public static <S> LiteralCommandNode<S> convertToBrigadier(DiscordSRV discordSRV, GameCommand command, Function<S, ICommandSender> commandSenderMapper) {
54+
return (LiteralCommandNode<S>) convert(discordSRV, command, commandSenderMapper);
5355
}
5456

5557
@SuppressWarnings("unchecked")
56-
private static <S> CommandNode<S> convert(GameCommand commandBuilder, Function<S, ICommandSender> commandSenderMapper) {
58+
private static <S> CommandNode<S> convert(DiscordSRV discordSRV, GameCommand commandBuilder, Function<S, ICommandSender> commandSenderMapper) {
5759
CommandNode<S> alreadyConverted = (CommandNode<S>) CACHE.get(commandBuilder);
5860
if (alreadyConverted != null) {
5961
return alreadyConverted;
@@ -74,12 +76,12 @@ private static <S> CommandNode<S> convert(GameCommand commandBuilder, Function<S
7476
}
7577

7678
for (GameCommand child : commandBuilder.getChildren()) {
77-
argumentBuilder.then(convert(child, commandSenderMapper));
79+
argumentBuilder.then(convert(discordSRV, child, commandSenderMapper));
7880
}
7981
if (redirection != null) {
8082
CommandNode<S> redirectNode = (CommandNode<S>) CACHE.get(redirection);
8183
if (redirectNode == null) {
82-
redirectNode = convert(redirection, commandSenderMapper);
84+
redirectNode = convert(discordSRV, redirection, commandSenderMapper);
8385
}
8486
argumentBuilder.redirect(redirectNode);
8587
}
@@ -92,8 +94,11 @@ private static <S> CommandNode<S> convert(GameCommand commandBuilder, Function<S
9294
}
9395
if (executor != null) {
9496
argumentBuilder.executes(context -> {
97+
ICommandSender commandSender = commandSenderMapper.apply(context.getSource());
98+
CommandUtil.basicStatusCheck(discordSRV, commandSender);
99+
95100
executor.execute(
96-
commandSenderMapper.apply(context.getSource()),
101+
commandSender,
97102
getArgumentMapper(context),
98103
label
99104
);

common/src/main/java/com/discordsrv/common/core/module/ModuleManager.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private <T> Set<T> getModuleDetails(Function<Module, Collection<T>> detailFuncti
6666
Set<T> details = new HashSet<>();
6767
for (Module module : modules) {
6868
try {
69-
if (!module.isEnabled()) {
69+
if (getAbstract(module).isCurrentlyDisabled()) {
7070
continue;
7171
}
7272

@@ -91,11 +91,11 @@ public Set<DiscordCacheFlag> requiredCacheFlags() {
9191

9292
@SuppressWarnings("unchecked")
9393
public <T extends Module> T getModule(Class<T> moduleType) {
94-
return (T) moduleLookupTable.computeIfAbsent(moduleType.getName(), key -> {
94+
T resolvedModule = (T) moduleLookupTable.computeIfAbsent(moduleType.getName(), key -> {
9595
Module bestCandidate = null;
9696
int bestCandidatePriority = Integer.MIN_VALUE;
9797
for (Module module : modules) {
98-
if (!module.isEnabled()) {
98+
if (getAbstract(module).isCurrentlyDisabled()) {
9999
continue;
100100
}
101101
int priority;
@@ -106,6 +106,11 @@ public <T extends Module> T getModule(Class<T> moduleType) {
106106
}
107107
return bestCandidate;
108108
});
109+
110+
if (getAbstract(resolvedModule).isCurrentlyDisabled()) {
111+
return null;
112+
}
113+
return resolvedModule;
109114
}
110115

111116
private AbstractModule<?> getAbstract(Module module) {
@@ -301,7 +306,7 @@ public void onDebugGenerate(DebugGenerateEvent event) {
301306
builder.append("Enabled modules:");
302307
List<Module> disabled = new ArrayList<>();
303308
for (Module module : modules) {
304-
if (!getAbstract(module).isEnabled()) {
309+
if (getAbstract(module).isCurrentlyDisabled()) {
305310
disabled.add(module);
306311
continue;
307312
}

common/src/main/java/com/discordsrv/common/core/module/type/AbstractModule.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ protected final boolean checkCancellation(Cancellable event) {
7171

7272
// Internal
7373

74+
public boolean isCurrentlyDisabled() {
75+
return !isCurrentlyEnabled;
76+
}
77+
7478
public final boolean enableModule() {
7579
if (isCurrentlyEnabled) {
7680
return false;

common/src/main/java/com/discordsrv/common/discord/connection/jda/JDAConnectionManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ private void checkDefaultFailureCallback() {
143143
@Subscribe
144144
public void onStatusChange(StatusChangeEvent event) {
145145
DiscordSRV.Status currentStatus = discordSRV.status();
146-
if (currentStatus.isShutdown() || currentStatus.isStartupError()) {
147-
// Don't change the status if it's shutdown or failed to start
146+
if (currentStatus.isShutdown()) {
147+
// Don't change the status if it's shutdown
148148
return;
149149
}
150150

common/src/main/java/com/discordsrv/common/feature/PresenceUpdaterModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public PresenceUpdaterModule(DiscordSRV discordSRV) {
4848

4949
@Override
5050
public boolean canEnableBeforeReady() {
51-
return true;
51+
return discordSRV.config() != null;
5252
}
5353

5454
public void serverStarted() {

common/src/main/java/com/discordsrv/common/util/CommandUtil.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package com.discordsrv.common.util;
2020

21+
import com.discordsrv.api.DiscordSRVApi;
2122
import com.discordsrv.common.DiscordSRV;
2223
import com.discordsrv.common.abstraction.player.IPlayer;
2324
import com.discordsrv.common.command.combined.abstraction.CommandExecution;
@@ -27,9 +28,12 @@
2728
import com.discordsrv.common.config.messages.MessagesConfig;
2829
import com.discordsrv.common.core.logging.Logger;
2930
import com.discordsrv.common.permission.game.Permission;
31+
import com.discordsrv.common.permission.game.Permissions;
3032
import net.dv8tion.jda.api.JDA;
3133
import net.dv8tion.jda.api.entities.User;
3234
import net.dv8tion.jda.api.utils.MiscUtil;
35+
import net.kyori.adventure.text.Component;
36+
import net.kyori.adventure.text.format.NamedTextColor;
3337
import org.jetbrains.annotations.Nullable;
3438

3539
import java.util.List;
@@ -40,6 +44,22 @@ public final class CommandUtil {
4044

4145
private CommandUtil() {}
4246

47+
public static void basicStatusCheck(DiscordSRV discordSRV, ICommandSender sender) {
48+
if (discordSRV.status().isError() && sender.hasPermission(Permissions.COMMAND_DEBUG)) {
49+
if (discordSRV.status() == DiscordSRVApi.Status.NOT_CONFIGURED) {
50+
sender.sendMessage(
51+
Component.text("DiscordSRV has not been fully configured yet, please check your server log for more details")
52+
.color(NamedTextColor.RED)
53+
);
54+
} else {
55+
sender.sendMessage(
56+
Component.text("DiscordSRV did not start correctly, please check your server log for more details")
57+
.color(NamedTextColor.RED)
58+
);
59+
}
60+
}
61+
}
62+
4363
public static CompletableFuture<UUID> lookupPlayer(
4464
DiscordSRV discordSRV,
4565
Logger logger,

fabric/src/main/java/com/discordsrv/fabric/command/game/handler/FabricCommandHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private ICommandSender getSender(CommandSource source) {
4848

4949
@Override
5050
public void registerCommand(GameCommand command) {
51-
LiteralCommandNode<ServerCommandSource> node = BrigadierUtil.convertToBrigadier(command, this::getSender);
51+
LiteralCommandNode<ServerCommandSource> node = BrigadierUtil.convertToBrigadier(discordSRV, command, this::getSender);
5252
discordSRV.getServer().getCommandManager().getDispatcher().getRoot().addChild(node);
5353
}
5454
}

velocity/src/main/java/com/discordsrv/velocity/command/game/handler/VelocityCommandHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private ICommandSender getSender(CommandSource source) {
5050

5151
@Override
5252
public void registerCommand(GameCommand command) {
53-
LiteralCommandNode<CommandSource> node = BrigadierUtil.convertToBrigadier(command, this::getSender);
53+
LiteralCommandNode<CommandSource> node = BrigadierUtil.convertToBrigadier(discordSRV, command, this::getSender);
5454
discordSRV.proxy().getCommandManager().register(
5555
discordSRV.proxy().getCommandManager().getCommandMeta(command.getLabel()),
5656
new BrigadierCommand(node)

0 commit comments

Comments
 (0)