Fix exception while running vanilla commands for chat command relay (#4552)

Fixes #4545
This commit is contained in:
Josh Roy 2021-10-11 15:32:41 -04:00 committed by GitHub
parent f3aea5e6ec
commit cb7d9baf7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import net.essentialsx.discord.JDADiscordService;
import net.essentialsx.discord.util.DiscordCommandSender;
import net.essentialsx.discord.util.DiscordUtil;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandException;
import org.jetbrains.annotations.NotNull;
public class DiscordCommandDispatcher extends ListenerAdapter {
@ -23,9 +24,21 @@ public class DiscordCommandDispatcher extends ListenerAdapter {
return;
}
Bukkit.getScheduler().runTask(jda.getPlugin(), () ->
final String command = event.getMessage().getContentRaw();
Bukkit.getScheduler().runTask(jda.getPlugin(), () -> {
try {
Bukkit.dispatchCommand(new DiscordCommandSender(jda, Bukkit.getConsoleSender(), message ->
event.getMessage().reply(message).queue()).getSender(), event.getMessage().getContentRaw()));
event.getMessage().reply(message).queue()).getSender(), command);
} catch (CommandException e) {
// Check if this is a vanilla command, in which case we have to use a vanilla command sender :(
if (e.getMessage().contains("a vanilla command listener") || (e.getCause() != null && e.getCause().getMessage().contains("a vanilla command listener"))) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
return;
}
// Something unrelated, should error out here
throw e;
}
});
}
}