Merge pull request #831 from deathbeam/do-not-fail-on-natives

Continue running in case Discord lib load failed
This commit is contained in:
Adam
2018-03-05 09:12:22 -05:00
committed by GitHub

View File

@@ -55,8 +55,7 @@ public class DiscordService implements AutoCloseable
@Inject @Inject
private ScheduledExecutorService executorService; private ScheduledExecutorService executorService;
private final DiscordEventHandlers discordEventHandlers = new DiscordEventHandlers(); private DiscordRPC discordRPC;
private final DiscordRPC discordRPC = DiscordRPC.INSTANCE;
/** /**
* Initializes the Discord service, sets up the event handlers and starts worker thread that will poll discord * Initializes the Discord service, sets up the event handlers and starts worker thread that will poll discord
@@ -66,6 +65,18 @@ public class DiscordService implements AutoCloseable
public void init() public void init()
{ {
log.info("Initializing Discord RPC service."); log.info("Initializing Discord RPC service.");
try
{
discordRPC = DiscordRPC.INSTANCE;
}
catch (UnsatisfiedLinkError e)
{
log.warn("Failed to load Discord library, Discord support will be disabled.");
return;
}
final DiscordEventHandlers discordEventHandlers = new DiscordEventHandlers();
discordEventHandlers.ready = this::ready; discordEventHandlers.ready = this::ready;
discordEventHandlers.disconnected = this::disconnected; discordEventHandlers.disconnected = this::disconnected;
discordEventHandlers.errored = this::errored; discordEventHandlers.errored = this::errored;
@@ -82,9 +93,12 @@ public class DiscordService implements AutoCloseable
*/ */
@Override @Override
public void close() public void close()
{
if (discordRPC != null)
{ {
discordRPC.Discord_Shutdown(); discordRPC.Discord_Shutdown();
} }
}
/** /**
* Updates the currently set presence of the logged in user. * Updates the currently set presence of the logged in user.
@@ -95,6 +109,11 @@ public class DiscordService implements AutoCloseable
*/ */
public void updatePresence(DiscordPresence discordPresence) public void updatePresence(DiscordPresence discordPresence)
{ {
if (discordRPC == null)
{
return;
}
final DiscordRichPresence discordRichPresence = new DiscordRichPresence(); final DiscordRichPresence discordRichPresence = new DiscordRichPresence();
discordRichPresence.state = discordPresence.getState(); discordRichPresence.state = discordPresence.getState();
discordRichPresence.details = discordPresence.getDetails(); discordRichPresence.details = discordPresence.getDetails();
@@ -126,9 +145,12 @@ public class DiscordService implements AutoCloseable
* Clears the currently set presence. * Clears the currently set presence.
*/ */
public void clearPresence() public void clearPresence()
{
if (discordRPC != null)
{ {
discordRPC.Discord_ClearPresence(); discordRPC.Discord_ClearPresence();
} }
}
/** /**
* Responds to the given user with the specified reply type. * Responds to the given user with the specified reply type.
@@ -137,9 +159,12 @@ public class DiscordService implements AutoCloseable
* @param reply The reply type * @param reply The reply type
*/ */
public void respondToRequest(String userId, DiscordReplyType reply) public void respondToRequest(String userId, DiscordReplyType reply)
{
if (discordRPC != null)
{ {
discordRPC.Discord_Respond(userId, reply.ordinal()); discordRPC.Discord_Respond(userId, reply.ordinal());
} }
}
private void ready() private void ready()
{ {