Advertisement
RexyBadDog

afk_mob_grinder.py

May 6th, 2023
536
0
Never
3
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.72 KB | Gaming | 0 0
  1. # this python script is for afking (Away from Keyboard) auto mob grinder in Minecraft
  2. # dont forget to define the 'hotbar' variable according to your needs
  3. # TODO: add a way to stop the bot instead of CTRL + C, prefebly a key listener at the while loop
  4. # Done: generate updated requirements.txt file
  5.  
  6. import os
  7. import sys
  8. import cv2
  9. import time
  10. import datetime
  11. import pytesseract
  12. import numpy
  13. import win32con
  14. import win32gui
  15. import pynput
  16. import win32ui
  17. from PIL import Image
  18.  
  19. pytesseract.pytesseract.tesseract_cmd = "C:\\Program Files\\Tesseract-OCR\\tesseract.exe"
  20.  
  21. # hotbar dictionary for key switwinch in Minecraft, the values are the number of items in the slot, or a keyword for the item
  22. # the slot 3 to 9 filled with a full stack - 1 becuase we need to reserve inventory space for the exp bottles. without it the inventory
  23. # will be filled with mobs drops and the exp bottles will be dropped on the ground
  24. hotbar = {
  25.     "1": 64,        # food slot
  26.     "2": "Weapon",  # weapon slot
  27.     "3": 63,        # exp potion slot
  28.     "4": 63,        # exp potion slot
  29.     "5": 63,        # exp potion slot
  30.     "6": 63,        # exp potion slot
  31.     "7": 63,        # exp potion slot
  32.     "8": 63,        # exp potion slot
  33.     "9": 63         # exp potion slot
  34. }
  35.  
  36. def windowEnumerationHandler(hwnd, top_windows):
  37.     top_windows.append((hwnd, win32gui.GetWindowText(hwnd)))
  38.  
  39. def switch_to_window(winName):
  40.     results = []
  41.     top_windows = []
  42.     win32gui.EnumWindows(windowEnumerationHandler, top_windows)
  43.     for i in top_windows:
  44.         if winName in i[1].lower():
  45.             print(i)
  46.             win32gui.ShowWindow(i[0],5)
  47.             win32gui.SetForegroundWindow(i[0])
  48.             break
  49.  
  50. def type_a_command(command_string):
  51.     pynput.keyboard.Controller().press(pynput.keyboard.KeyCode.from_char("/"))
  52.     pynput.keyboard.Controller().release(pynput.keyboard.KeyCode.from_char("/"))
  53.     time.sleep(0.2)
  54.     pynput.keyboard.Controller().type(command_string[1:])
  55.     time.sleep(0.2)
  56.     pynput.keyboard.Controller().press(pynput.keyboard.Key.enter)
  57.     pynput.keyboard.Controller().release(pynput.keyboard.Key.enter)
  58.     time.sleep(0.2)
  59.     pynput.mouse.Controller().click(pynput.mouse.Button.left)
  60.     time.sleep(0.2)
  61.  
  62. def fill_exp_potions():
  63.     # this function is for filling the empty bottles from the SlimeFun plugin with exp
  64.     pynput.keyboard.Controller().type('3')
  65.     time.sleep(0.2)
  66.     pynput.mouse.Controller().click(pynput.mouse.Button.right)
  67.     time.sleep(0.2)
  68.     pynput.keyboard.Controller().type('2')
  69.  
  70.  
  71. def main():
  72.     if not switch_to_window("minecraft"):
  73.         print("error: no Minecraft windows found")
  74.     loop_count = 0
  75.     left_right = False
  76.     time.sleep(0.2)
  77.     # it is neccecary to press the ESC key to close the inventory/menu window
  78.     pynput.keyboard.Controller().press(pynput.keyboard.Key.esc)
  79.     pynput.keyboard.Controller().release(pynput.keyboard.Key.esc)
  80.     while True:
  81.         k = cv2.waitKey(1) & 0xFF
  82.         # press 'r' to exit
  83.         #if k == ord('r'):
  84.         if cv2.waitKey(1) == ord("r"):
  85.             break
  86.  
  87.         if left_right:
  88.             pynput.mouse.Controller().click(pynput.mouse.Button.left)
  89.             pynput.mouse.Controller().move(200, 0)
  90.             time.sleep(0.8)
  91.             pynput.mouse.Controller().click(pynput.mouse.Button.left)
  92.             pynput.mouse.Controller().move(200, 0)
  93.             time.sleep(0.8)
  94.             pynput.mouse.Controller().click(pynput.mouse.Button.left)
  95.             pynput.mouse.Controller().move(200, 0)
  96.             time.sleep(0.8)
  97.             pynput.mouse.Controller().click(pynput.mouse.Button.left)
  98.             pynput.mouse.Controller().move(200, 0)
  99.             time.sleep(0.8)
  100.             pynput.mouse.Controller().click(pynput.mouse.Button.left)
  101.             pynput.mouse.Controller().move(200, 0)
  102.             time.sleep(0.8)
  103.             left_right = False
  104.             print("Moved to the Right")
  105.         else:
  106.             pynput.mouse.Controller().click(pynput.mouse.Button.left)
  107.             pynput.mouse.Controller().move(-200, 0)
  108.             time.sleep(0.8)
  109.             pynput.mouse.Controller().click(pynput.mouse.Button.left)
  110.             pynput.mouse.Controller().move(-200, 0)
  111.             time.sleep(0.8)
  112.             pynput.mouse.Controller().click(pynput.mouse.Button.left)
  113.             pynput.mouse.Controller().move(-200, 0)
  114.             time.sleep(0.8)
  115.             pynput.mouse.Controller().click(pynput.mouse.Button.left)
  116.             pynput.mouse.Controller().move(-200, 0)
  117.             time.sleep(0.8)
  118.             pynput.mouse.Controller().click(pynput.mouse.Button.left)
  119.             pynput.mouse.Controller().move(-200, 0)
  120.             time.sleep(0.8)
  121.             left_right = True
  122.             print("Moved to the Left")
  123.         # executing the /balance command to track how much money you get from McMMO Swords/Axe/Unarmed skills
  124.         # type_a_command("/bal")
  125.         fill_exp_potions()
  126.         loop_count += 1
  127.         print("loop_count: {},".format(loop_count))
  128.  
  129.  
  130. if __name__ == "__main__":
  131.     start_time = datetime.datetime.now().time()
  132.     end_time = datetime.datetime.now().time()
  133.     print("starting afk at: {}".format(start_time))
  134.     try:
  135.         main()
  136.     except KeyboardInterrupt:
  137.         # execute the next lines after KeyboardInterrupt: Ctrl+C
  138.         end_time = datetime.datetime.now().time()
  139.         td_start = datetime.timedelta(hours=start_time.hour, minutes=start_time.minute, seconds=start_time.second)
  140.         td_end = datetime.timedelta(hours=end_time.hour, minutes=end_time.minute, seconds=end_time.second)
  141.         print("afk'ed for: {} (hh:mm:ss)".format(td_end - td_start))
  142.         try:
  143.             sys.exit(130)
  144.         except SystemExit:
  145.             os._exit(130)
  146.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement