Files
.github
src
build.gradle
manifest.kod
README.md
settings.gradle
Code
14 1,052
src/main/java/com/voidsmpbot/VoidSMPBotPlugin.java
package com.voidsmpbot;
import com.voidsmpbot.database.Database;
import com.voidsmpbot.discord.DiscordBotService;
import com.voidsmpbot.link.LinkService;
import com.voidsmpbot.status.StatusService;
import com.voidsmpbot.web.MinecraftChatListener;
import com.voidsmpbot.web.MinecraftCommands;
import java.nio.file.Path;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
public final class VoidSMPBotPlugin extends JavaPlugin {
private ExecutorService databaseExecutor;
private ExecutorService workerExecutor;
private ExecutorService startupExecutor;
private Database database;
private LinkService linkService;
private DiscordBotService discordBotService;
@Override
public void onEnable() {
saveDefaultConfig();
databaseExecutor = Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, "VoidSMPBot-SQLite"));
workerExecutor = Executors.newFixedThreadPool(2, runnable -> new Thread(runnable, "VoidSMPBot-Worker"));
startupExecutor = Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, "VoidSMPBot-Startup"));
String databaseFile = getConfig().getString("storage.database-file", "linked-accounts.sqlite");
Path databasePath = getDataFolder().toPath().resolve(databaseFile);
database = new Database(databasePath);
long expirationSeconds = getConfig().getLong("account-link.code-expiration-seconds", 300L);
linkService = new LinkService(database, databaseExecutor, expirationSeconds);
StatusService statusService = new StatusService(this, workerExecutor);
discordBotService = new DiscordBotService(this, linkService, statusService);
Bukkit.getPluginManager().registerEvents(new MinecraftChatListener(discordBotService), this);
getCommand("linkaccount").setExecutor(new MinecraftCommands(this, linkService));
getCommand("dc").setExecutor(new MinecraftCommands(this, linkService));
CompletableFuture.runAsync(() -> {
try {
database.initialize();
} catch (Exception exception) {
throw new CompletionException(exception);
}
}, databaseExecutor).thenRunAsync(() -> {
try {
discordBotService.start();
} catch (InterruptedException exception) {
Thread.currentThread().interrupt();
throw new CompletionException(exception);
}
}, startupExecutor).exceptionally(error -> {
Throwable cause = error instanceof CompletionException && error.getCause() != null
? error.getCause() : error;
getLogger().log(Level.SEVERE, "VoidSMPBot could not finish starting. Disabling the plugin.", cause);
if (isEnabled()) {
Bukkit.getScheduler().runTask(this, () -> Bukkit.getPluginManager().disablePlugin(this));
}
return null;
});
}
@Override
public void onDisable() {
if (discordBotService != null) {
discordBotService.shutdown();
}
if (startupExecutor != null) {
startupExecutor.shutdownNow();
}
if (workerExecutor != null) {
workerExecutor.shutdownNow();
}
if (databaseExecutor != null) {
databaseExecutor.execute(() -> {
try {
database.close();
} catch (Exception exception) {
getLogger().log(Level.WARNING, "Could not close the account database cleanly.", exception);
}
});
databaseExecutor.shutdown();
}
}
}