Skip to content

Commit

Permalink
Add EmbeddedLauncher
Browse files Browse the repository at this point in the history
  • Loading branch information
huanmeng-qwq committed Feb 24, 2024
1 parent 18dcb1f commit 4ae4a0f
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/main/java/snw/kookbc/launcher/EmbeddedLauncher.java
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/
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);
}
}

0 comments on commit 4ae4a0f

Please sign in to comment.