From 42f128b77935c07fb72b57faa91195f9221e4896 Mon Sep 17 00:00:00 2001 From: Moonlit Jolteon Date: Thu, 21 Nov 2024 19:46:31 -0500 Subject: [PATCH] INITIAL COMMIT --- .gitignore | 1 + main.py | 89 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 3 files changed, 91 insertions(+) create mode 100644 .gitignore create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f5e96db --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +venv \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..9fcacd4 --- /dev/null +++ b/main.py @@ -0,0 +1,89 @@ +from javascript import require, On, off +mineflayer = require('mineflayer') +pathfinder = require('mineflayer-pathfinder') + +RANGE_GOAL = 3 +SERVER_IP = '127.0.0.1' +SERVER_PORT = 25565 +BOT_ARGS = { + 'host': SERVER_IP, + 'port': SERVER_PORT + } + + +class MCBot: + def __init__(self, bot_name, bot_args = BOT_ARGS): + self.bot_args = { + 'host': bot_args['host'], + 'port': bot_args['port'], + 'username': bot_name + } + self.reconnect = True + self.bot_name = bot_name + self.start_bot() + + def start_bot(self): + self.bot = mineflayer.createBot(self.bot_args) + self.bot.loadPlugin(pathfinder.pathfinder) + print(f"Started mineflayer but with the name {self.bot_name}") + self.start_listeners() + + def start_listeners(self): + @On(self.bot, 'login') + def handle_login(this): + bot_socket = self.bot._client.socket + print( + f"Logged in to {bot_socket.server if bot_socket.server else bot_socket._host }" + ) + + @On(self.bot, 'spawn') + def handle_spawn(this): + print("I spawned 👋") + + @On(self.bot, 'chat') + def handle_chat(this, sender, message, *args): + print(f"{self.bot_name} got a message: {sender}> {message}") + if sender and (sender != self.bot_name) and (message.startswith(f"{self.bot_name}: ") or (message.startswith(f"Bots: "))): + command = " ".join(message.split(' ')[1:]) + if 'come' in command: + player = self.bot.players[sender] + print("Target", player) + target = player.entity + if not target: + self.bot.chat("I don't see you !") + return + + pos = target.position + movements = pathfinder.Movements(self.bot) + movements.canDig = False + self.bot.pathfinder.setMovements(movements) + self.bot.pathfinder.setGoal(pathfinder.goals.GoalNear(pos.x, pos.y, pos.z, RANGE_GOAL)) + + if 'quit' in command: + self.reconnect = False + self.bot.quit() + + @On(self.bot, "kicked") + def handle_kicked(this, reason, loggedIn): + if loggedIn: + print(f"Kicked from server: {reason}") + else: + print(f"Kicked whilst trying to connect: {reason}") + + @On(self.bot, "end") + def handle_end(this, reason): + print(f"Disconnected: {reason}") + + # Turn off event listeners + off(self.bot, "login", handle_login) + off(self.bot, "kicked", handle_kicked) + off(self.bot, "spawn", handle_spawn) + off(self.bot, "chat", handle_chat) + if self.reconnect: + print("Bot restarting...") + print(self.bot_name) + self.start_bot() + off(self.bot, "end", handle_end) + +for i in range(5): + bot = MCBot(f"bot{i}") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..719f455 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +javascript==1!1.2.1