diff --git a/src/main/java/snw/kookbc/launcher/EmbeddedLauncher.java b/src/main/java/snw/kookbc/launcher/EmbeddedLauncher.java new file mode 100644 index 00000000..b9b784ed --- /dev/null +++ b/src/main/java/snw/kookbc/launcher/EmbeddedLauncher.java @@ -0,0 +1,89 @@ +/* + * KookBC -- The Kook Bot Client & JKook API standard implementation for Java. + * Copyright (C) 2022 - 2023 KookBC contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package snw.kookbc.launcher; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; +import snw.jkook.JKook; +import snw.jkook.config.file.YamlConfiguration; +import snw.kookbc.impl.CoreImpl; +import snw.kookbc.impl.KBCClient; +import snw.kookbc.impl.command.CommandManagerImpl; + +import java.io.File; + +public class EmbeddedLauncher extends Launcher { + private String token; + private final YamlConfiguration config; + private final Logger logger; + private final File pluginsFolder; + + private KBCClient client; + private CoreImpl core; + + public EmbeddedLauncher(@NotNull File configFile) { + this(YamlConfiguration.loadConfiguration(configFile), null, null, null); + } + + public EmbeddedLauncher(@NotNull YamlConfiguration config, @Nullable String token, @Nullable Logger logger, @Nullable File pluginsFolder) { + super(); + this.config = config; + this.token = token; + this.logger = logger; + this.pluginsFolder = pluginsFolder; + } + + public KBCClient getClient() { + if (client != null) { + return client; + } + build(); + return client; + } + + public CoreImpl getCore() { + if (core != null) { + return core; + } + build(); + return core; + } + + protected void build() { + if (logger != null) { + core = new CoreImpl(logger); + } else { + core = new CoreImpl(); + } + JKook.setCore(core); + + if (token == null) { + String configToken = config.getString("token"); + if (configToken != null && !configToken.isEmpty()) { + if (token == null || token.isEmpty()) { + token = configToken; + } + } else { + throw new IllegalArgumentException("Token is not set"); + } + } + client = new KBCClient(core, config, pluginsFolder, token, CommandManagerImpl::new, + null, null, null, null, null, null); + } +}