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> botsOptions = config.get("bots"); + + final List allBots = new ArrayList<>(); + + final CountDownLatch latch = new CountDownLatch(botsOptions.size()); + + for (Map botOption : botsOptions) { + final String host = (String) botOption.get("host"); + final int port = (int) botOption.get("port"); + final String username = (String) botOption.get("username"); + + new Thread(() -> { + final Bot bot = new Bot(host, port, reconnectDelay, username, allBots); + allBots.add(bot); + + latch.countDown(); + }).start(); + } + + latch.await(); + new ConsolePlugin(allBots); } } \ No newline at end of file diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/commands/HelpCommand.java b/src/main/java/me/chayapak1/chomensbot_mabe/commands/HelpCommand.java index df53d3b..5430e98 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/commands/HelpCommand.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/commands/HelpCommand.java @@ -3,7 +3,6 @@ package me.chayapak1.chomensbot_mabe.commands; import me.chayapak1.chomensbot_mabe.Bot; import me.chayapak1.chomensbot_mabe.command.Command; import me.chayapak1.chomensbot_mabe.command.CommandContext; -import me.chayapak1.chomensbot_mabe.plugins.CommandHandlerPlugin; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.JoinConfiguration; import net.kyori.adventure.text.format.NamedTextColor; @@ -38,7 +37,10 @@ public class HelpCommand implements Command { return 0; } + private Bot bot; + public Component execute(CommandContext context, String[] args, String[] fullArgs) { + this.bot = context.bot(); if (args.length == 0) { sendCommandList(context); return Component.text("success"); @@ -72,9 +74,11 @@ public class HelpCommand implements Command { public List getCommandListByTrustLevel (int trustLevel) { final List list = new ArrayList<>(); - for (Command command : CommandHandlerPlugin.commands()) { + for (Command command : bot.commandHandler().commands()) { final String name = command.name(); + bot.logger().log(name); + if (command.trustLevel() != trustLevel) continue; list.add(Component.text(name).color(getColorByTrustLevel(trustLevel))); } @@ -98,7 +102,7 @@ public class HelpCommand implements Command { final String commandName = args[0]; - for (Command command : CommandHandlerPlugin.commands()) { + for (Command command : bot.commandHandler().commands()) { if (!command.name().equals(commandName)) continue; final List usages = new ArrayList<>(); diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ChatCommandHandlerPlugin.java b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ChatCommandHandlerPlugin.java index fc4d173..5d7df5c 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ChatCommandHandlerPlugin.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ChatCommandHandlerPlugin.java @@ -33,7 +33,7 @@ public class ChatCommandHandlerPlugin extends ChatPlugin.ChatListener { final PlayerCommandContext context = new PlayerCommandContext(bot, displayName); - final Component output = CommandHandlerPlugin.executeCommand(commandString, context, bot.hashing().hash(), bot.hashing().ownerHash()); + final Component output = bot.commandHandler().executeCommand(commandString, context, bot.hashing().hash(), bot.hashing().ownerHash()); final String textOutput = ((TextComponent) output).content(); if (!textOutput.equals("success")) { diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/CommandHandlerPlugin.java b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/CommandHandlerPlugin.java index 25ff9c9..f823a1b 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/CommandHandlerPlugin.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/CommandHandlerPlugin.java @@ -10,10 +10,12 @@ import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.format.NamedTextColor; import org.apache.commons.lang3.exception.ExceptionUtils; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; public class CommandHandlerPlugin { - @Getter private static final List commands = new ArrayList<>(); + @Getter private final List commands = new ArrayList<>(); public CommandHandlerPlugin () { registerCommand(new CommandBlockCommand()); @@ -31,7 +33,7 @@ public class CommandHandlerPlugin { commands.add(command); } - public static Component executeCommand (String input, CommandContext context, String hash, String ownerHash) { + public Component executeCommand (String input, CommandContext context, String hash, String ownerHash) { final String[] splitInput = input.split("\\s+"); final String commandName = splitInput[0]; 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 b37024c..aab2489 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ConsolePlugin.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ConsolePlugin.java @@ -10,17 +10,28 @@ import org.jline.reader.EndOfFileException; import org.jline.reader.LineReader; import org.jline.reader.LineReaderBuilder; +import java.util.Arrays; +import java.util.List; + public class ConsolePlugin { - private final Bot bot; + private final List allBots; @Getter public final LineReader reader; + @Getter private String consoleServer = "all"; + @Getter private final String prefix = "."; + @Getter private final String consoleServerPrefix = "/"; - public ConsolePlugin (Bot bot) { - this.bot = bot; - + public ConsolePlugin (List allBots) { + this.allBots = allBots; this.reader = LineReaderBuilder.builder().build(); + + for (Bot bot : allBots) { + bot.console(this); + bot.logger(new LoggerPlugin(bot)); + } + String prompt = "> "; new Thread(() -> { @@ -42,26 +53,50 @@ public class ConsolePlugin { public void handleLine (String line) { if (line == null) return; - if (line.startsWith(prefix)) { - final ConsoleCommandContext context = new ConsoleCommandContext(bot); + if (line.startsWith(consoleServerPrefix)) { + final String substringLine = line.substring(consoleServerPrefix.length()); + final String[] splitInput = substringLine.split("\\s+"); + final String commandName = splitInput[0]; + final String[] args = Arrays.copyOfRange(splitInput, 1, splitInput.length); - final Component output = CommandHandlerPlugin.executeCommand(line.substring(prefix.length()), context, "h", "o"); - final String textOutput = ((TextComponent) output).content(); - - if (!textOutput.equals("success")) { - context.sendOutput(output); + if (commandName.equals("csvr") || commandName.equals("consoleserver")) { + for (Bot bot : allBots) { + if (args.length == 0) { + bot.logger().log("No server specified"); + return; + } + consoleServer = args[0]; + bot.logger().log("Set the console server to " + consoleServer); + } } return; } - bot.chat().tellraw( - Component.translatable( - "[%s] %s › %s", - Component.text(bot.username() + " Console").color(NamedTextColor.GRAY), - Component.text("chayapak").color(NamedTextColor.GREEN), - Component.text(line).color(NamedTextColor.GRAY) - ).color(NamedTextColor.DARK_GRAY) - ); + for (Bot bot : allBots) { + if (!bot.host().equals(consoleServer) && !consoleServer.equals("all")) continue; + + if (line.startsWith(prefix)) { + final ConsoleCommandContext context = new ConsoleCommandContext(bot); + + final Component output = bot.commandHandler().executeCommand(line.substring(prefix.length()), context, "h", "o"); + final String textOutput = ((TextComponent) output).content(); + + if (!textOutput.equals("success")) { + context.sendOutput(output); + } + + continue; + } + + bot.chat().tellraw( + Component.translatable( + "[%s] %s › %s", + Component.text(bot.username() + " Console").color(NamedTextColor.GRAY), + Component.text("chayapak").color(NamedTextColor.GREEN), + Component.text(line).color(NamedTextColor.GRAY) + ).color(NamedTextColor.DARK_GRAY) + ); + } } } diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/LoggerPlugin.java b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/LoggerPlugin.java index b63f432..9a70e36 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/LoggerPlugin.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/LoggerPlugin.java @@ -1,5 +1,8 @@ package me.chayapak1.chomensbot_mabe.plugins; +import com.github.steveice10.packetlib.event.session.ConnectedEvent; +import com.github.steveice10.packetlib.event.session.DisconnectedEvent; +import com.github.steveice10.packetlib.event.session.SessionAdapter; import me.chayapak1.chomensbot_mabe.Bot; import net.kyori.adventure.text.Component; @@ -8,11 +11,30 @@ public class LoggerPlugin extends ChatPlugin.ChatListener { public LoggerPlugin(Bot bot) { this.bot = bot; + + bot.addListener(new SessionAdapter() { + @Override + public void connected (ConnectedEvent event) { + log("Successfully connected to: " + bot.host() + ":" + bot.port()); + } + + @Override + public void disconnected (DisconnectedEvent event) { + log("Disconnected from " + bot.host() + ":" + bot.port() + ", reason: " + event.getReason()); + } + }); + bot.chat().addListener(this); } public void log (String message) { - bot.console().reader().printAbove(message); + bot.console().reader().printAbove( + String.format( + "[%s] %s", + bot.host() + ":" + bot.port(), + message + ) + ); } @Override diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/util/ElementUtilities.java b/src/main/java/me/chayapak1/chomensbot_mabe/util/ElementUtilities.java index 03ccd5e..aabd949 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/util/ElementUtilities.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/util/ElementUtilities.java @@ -7,7 +7,13 @@ import java.util.List; public class ElementUtilities { public static Command findCommand(List commands, String searchTerm) { for (Command command : commands) { - if (command.name().equals(searchTerm) || command.alias().contains(searchTerm)) { + if ( + ( + command.name().equals(searchTerm.toLowerCase()) || + command.alias().contains(searchTerm.toLowerCase()) + ) && + !searchTerm.equals("") // ig yup + ) { return command; } } diff --git a/src/main/resources/default-config.yml b/src/main/resources/default-config.yml new file mode 100644 index 0000000..9025aa5 --- /dev/null +++ b/src/main/resources/default-config.yml @@ -0,0 +1,5 @@ +reconnectDelay: 7000 +bots: + - host: 'localhost' + port: 25565 + username: 'ChomeNS_Bot' \ No newline at end of file