From f769cd92b022dad100eb1f381454df65c2e18bf6 Mon Sep 17 00:00:00 2001 From: ChomeNS Date: Tue, 21 Mar 2023 12:21:12 +0700 Subject: [PATCH] little config update + among us --- .../java/me/chayapak1/chomensbot_mabe/Bot.java | 14 ++++++-------- .../java/me/chayapak1/chomensbot_mabe/Main.java | 12 ++++++------ .../chomensbot_mabe/plugins/ConsolePlugin.java | 6 ++++++ .../chomensbot_mabe/plugins/HashingPlugin.java | 7 ++----- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/Bot.java b/src/main/java/me/chayapak1/chomensbot_mabe/Bot.java index 56d1377..2439194 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/Bot.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/Bot.java @@ -13,7 +13,6 @@ import org.apache.commons.lang3.RandomStringUtils; import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -25,14 +24,12 @@ public class Bot { @Getter private final int port; private final String _username; @Getter private final List allBots; - @Getter private final Map keys; + @Getter private final Configuration config; @Getter private String username; @Getter private Session session; - @Getter private final int reconnectDelay; - @Getter private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); @Getter private final ChatPlugin chat = new ChatPlugin(this); @@ -46,13 +43,12 @@ public class Bot { @Getter private final HashingPlugin hashing = new HashingPlugin(this); @Getter private final MusicPlayerPlugin music = new MusicPlayerPlugin(this); - public Bot (String host, int port, int reconnectDelay, String _username, List allBots, Map keys) { + public Bot (String host, int port, String _username, List allBots, Configuration config) { this.host = host; this.port = port; - this.reconnectDelay = reconnectDelay; this._username = _username; this.allBots = allBots; - this.keys = keys; + this.config = config; reconnect(); } @@ -112,11 +108,13 @@ public class Bot { listener.disconnected(disconnectedEvent); } + final int reconnectDelay = config.reconnectDelay(); + if (reconnectDelay < 0) return; // to disable reconnecting Runnable task = () -> reconnect(); - executor.schedule(task, reconnectDelay(), TimeUnit.MILLISECONDS); + executor.schedule(task, reconnectDelay, TimeUnit.MILLISECONDS); } }); diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/Main.java b/src/main/java/me/chayapak1/chomensbot_mabe/Main.java index 9c2681c..f8b483a 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/Main.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/Main.java @@ -15,7 +15,7 @@ public class Main { final File file = new File("config.yml"); final Constructor constructor = new Constructor(Configuration.class); final Yaml yaml = new Yaml(constructor); - Configuration config; + Configuration _config; if (!file.exists()) { // creates config file from default-config.yml @@ -35,16 +35,16 @@ public class Main { System.out.println("config.yml file not found, so the default one was created"); - config = yaml.load(is); + _config = yaml.load(is); } InputStream opt = new FileInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(opt)); - config = yaml.load(reader); + _config = yaml.load(reader); + + final Configuration config = _config; - final int reconnectDelay = config.reconnectDelay(); - final Map keys = config.keys(); Configuration.Bots[] botsOptions = config.bots(); final List allBots = new ArrayList<>(); @@ -57,7 +57,7 @@ public class Main { final String username = botOption.username(); new Thread(() -> { - final Bot bot = new Bot(host, port, reconnectDelay, username, allBots, keys); + final Bot bot = new Bot(host, port, username, allBots, config); allBots.add(bot); latch.countDown(); diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ConsolePlugin.java b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ConsolePlugin.java index 16c8f94..d3e4902 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ConsolePlugin.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ConsolePlugin.java @@ -9,7 +9,9 @@ import net.kyori.adventure.text.format.NamedTextColor; import org.jline.reader.EndOfFileException; import org.jline.reader.LineReader; import org.jline.reader.LineReaderBuilder; +import org.jline.terminal.TerminalBuilder; +import java.io.IOException; import java.util.Arrays; import java.util.List; @@ -32,6 +34,10 @@ public class ConsolePlugin { bot.logger(new LoggerPlugin(bot)); } + try { + TerminalBuilder.builder().build(); + } catch (IOException ignored) {} + String prompt = "> "; new Thread(() -> { diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/HashingPlugin.java b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/HashingPlugin.java index 8cdcbbb..e66fbf0 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/HashingPlugin.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/HashingPlugin.java @@ -20,11 +20,8 @@ public class HashingPlugin { } public void update () { - // final String ownerHashKey = "b)R��nF�CW���#�\\[�S*8\"t^eia�Z��k����K1�8zȢ�"; - // final String normalHashKey = "�iB_D���k��j8H�{?[/ڭ�f�}Ѣ�^-=�Ț��v]��g>��=c"; - - final String normalHashKey = bot.keys().get("normalKey"); - final String ownerHashKey = bot.keys().get("ownerKey"); + final String normalHashKey = bot.config().keys().get("normalKey"); + final String ownerHashKey = bot.config().keys().get("ownerKey"); final String hashValue = System.currentTimeMillis() / 10_000 + normalHashKey; hash = Hashing.sha256()