diff --git a/chomens_bot.iml b/chomens_bot.iml
new file mode 100644
index 0000000..c462275
--- /dev/null
+++ b/chomens_bot.iml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+ ADVENTURE
+
+
+
+
+
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index cb9b8c1..d89d938 100644
--- a/pom.xml
+++ b/pom.xml
@@ -84,6 +84,12 @@
cowjar-extra
1.1.0
+
+
+ org.yaml
+ snakeyaml
+ 1.29
+
diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/Bot.java b/src/main/java/me/chayapak1/chomensbot_mabe/Bot.java
index f3107d7..28160f8 100644
--- a/src/main/java/me/chayapak1/chomensbot_mabe/Bot.java
+++ b/src/main/java/me/chayapak1/chomensbot_mabe/Bot.java
@@ -7,11 +7,15 @@ import com.github.steveice10.packetlib.event.session.*;
import com.github.steveice10.packetlib.packet.Packet;
import com.github.steveice10.packetlib.tcp.TcpClientSession;
import lombok.Getter;
+import lombok.Setter;
import me.chayapak1.chomensbot_mabe.plugins.*;
import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
public class Bot {
private final ArrayListlisteners = new ArrayList<>();
@@ -19,25 +23,35 @@ public class Bot {
@Getter private final String host;
@Getter private final int port;
@Getter private final String username;
+ @Getter private final List allBots;
- @Getter private final Session session;
+ @Getter private Session session;
+
+ @Getter private final int reconnectDelay;
@Getter private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
@Getter private final ChatPlugin chat = new ChatPlugin(this);
- @Getter private final LoggerPlugin logger = new LoggerPlugin(this);
+ @Getter @Setter private LoggerPlugin logger; // in ConsolePlugin
@Getter private final SelfCarePlugin selfCare = new SelfCarePlugin(this);
- @Getter private final ConsolePlugin console = new ConsolePlugin(this);
+ @Getter @Setter private ConsolePlugin console;
@Getter private final PositionPlugin position = new PositionPlugin(this);
@Getter private final CorePlugin core = new CorePlugin(this);
@Getter private final CommandHandlerPlugin commandHandler = new CommandHandlerPlugin();
@Getter private final ChatCommandHandlerPlugin chatCommandHandler = new ChatCommandHandlerPlugin(this);
@Getter private final HashingPlugin hashing = new HashingPlugin(this);
- public Bot (String host, int port, String username) {
+ public Bot (String host, int port, int reconnectDelay, String username, List allBots) {
this.host = host;
this.port = port;
+ this.reconnectDelay = reconnectDelay;
this.username = username;
+ this.allBots = allBots;
+
+ reconnect();
+ }
+
+ public void reconnect () {
Session session = new TcpClientSession(host, port, new MinecraftProtocol(username), null);
this.session = session;
@@ -46,12 +60,12 @@ public class Bot {
@Override
public void packetReceived(Session session, Packet packet) {
- for (SessionListener listener : listeners) {
- if (packet instanceof ClientboundLoginPacket) {
- listener.connected(new ConnectedEvent(session));
- }
- listener.packetReceived(session, packet);
- }
+ for (SessionListener listener : listeners) {
+ if (packet instanceof ClientboundLoginPacket) {
+ listener.connected(new ConnectedEvent(session));
+ }
+ listener.packetReceived(session, packet);
+ }
}
@Override
@@ -87,6 +101,12 @@ public class Bot {
for (SessionListener listener : listeners) {
listener.disconnected(disconnectedEvent);
}
+
+ if (reconnectDelay < 0) return; // to disable reconnecting
+
+ Runnable task = () -> reconnect();
+
+ 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 09d7ae5..59c2fa5 100644
--- a/src/main/java/me/chayapak1/chomensbot_mabe/Main.java
+++ b/src/main/java/me/chayapak1/chomensbot_mabe/Main.java
@@ -1,11 +1,69 @@
package me.chayapak1.chomensbot_mabe;
-public class Main {
- public static void main(String[] args) {
- final String host = args[0];
- final int port = Integer.parseInt(args[1]);
- final String username = args[2];
+import me.chayapak1.chomensbot_mabe.plugins.ConsolePlugin;
+import org.yaml.snakeyaml.Yaml;
- new Bot(host, port, username);
+import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+
+public class Main {
+ public static void main(String[] args) throws IOException, InterruptedException {
+ final File file = new File("config.yml");
+ final Yaml yaml = new Yaml();
+ Map>> config;
+
+ if (!file.exists()) {
+ // creates config file from default-config.yml
+ InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("default-config.yml");
+ BufferedReader reader = new BufferedReader(new InputStreamReader(is));
+ StringBuilder stringBuilder = new StringBuilder();
+ while (reader.ready()) {
+ char character = (char) reader.read();
+ stringBuilder.append(character);
+ }
+ String defaultConfig = stringBuilder.toString();
+
+ // writes it
+ BufferedWriter configWriter = new BufferedWriter(new FileWriter(file));
+ configWriter.write(defaultConfig);
+ configWriter.close();
+
+ System.out.println("config.yml file not found, so the default one was created");
+
+ config = yaml.load(is);
+ }
+
+ InputStream opt = new FileInputStream(file);
+ BufferedReader reader = new BufferedReader(new InputStreamReader(opt));
+
+ config = yaml.load(reader);
+
+ final Object reconnectDelayObject = config.get("reconnectDelay");
+ final int reconnectDelay = (int) reconnectDelayObject;
+
+ List