Advertisement
LunaeStellsr

Minecraft Auto

Feb 20th, 2020
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.42 KB | None | 0 0
  1. require 'win32/api'
  2. require 'win32con'
  3. require 'win32con/keymap'
  4.  
  5. include Win32CON
  6.  
  7. $MC_Hwnd = 0
  8.  
  9. SendMessage = Win32::API.new('SendMessage', 'LLLL', 'L', 'user32')
  10. PostMessage = Win32::API.new('SendMessage', 'LLLL', 'L', 'user32')
  11. GetWindowThreadProcessId = Win32::API.new('GetWindowThreadProcessId', 'LL', 'L', 'user32')
  12. GetAsyncKeyState = Win32::API.new('GetAsyncKeyState', 'L', 'L', 'user32')
  13.  
  14. EnumWindows     = Win32::API.new('EnumWindows', 'KL', 'L', 'user32')
  15. GetWindowText   = Win32::API.new('GetWindowText', 'LPI', 'I', 'user32')
  16. EnumWindowsProc = Win32::API::Callback.new('LP', 'I'){ |handle, param|
  17.   buf = "\0" * 200
  18.   GetWindowText.call(handle, buf, 200);
  19.   buf.encode!('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
  20.   text = buf.strip
  21.   flag_found = false
  22.   if text.downcase.match(/minecraft(.+)(\d+)/i)
  23.     $MC_Hwnd = handle
  24.     puts text
  25.     flag_found = true
  26.   end
  27.   !flag_found # Proc return whether enum continues
  28. }
  29.  
  30. FPS = (1 / 120)
  31.  
  32. $flag_running = true
  33. $flag_pressed = false
  34. $flag_working = false
  35.  
  36. module Input
  37.   @keystate = Array.new(0xff){0}
  38.   module_function
  39.   def update
  40.     0xff.times do |i|
  41.       @keystate[i] = (GetAsyncKeyState.call(i) & 0x8000) > 0 ? @keystate[i] + 1 : 0
  42.     end
  43.   end
  44.  
  45.   def trigger?(kcode)
  46.     @keystate[kcode] == 1
  47.   end
  48.  
  49.   def keystate; @keystate; end
  50.  
  51.   def mouse_rdown
  52.     SendMessage.call($MC_Hwnd, WM_RBUTTONDOWN, 0, 0)
  53.     $flag_pressed = true
  54.   end
  55.  
  56.   def mouse_rup
  57.     SendMessage.call($MC_Hwnd, WM_RBUTTONUP, 0, 0)
  58.     $flag_pressed = false
  59.   end
  60.  
  61.   def key_down(kid)
  62.     PostMessage.call($MC_Hwnd, WM_KEYDOWN, kid, 0xC0000000)
  63.   end
  64.  
  65.   def key_up(kid)
  66.     PostMessage.call($MC_Hwnd, WM_KEYUP, kid, 0)
  67.   end
  68. end
  69.  
  70. def main_update
  71.   Input.update
  72.   if Input.trigger?(Keymap[:vk_f8])
  73.     # $flag_pressed ? Input.mouse_rup : Input.mouse_rdown
  74.     $flag_working ^= true
  75.   end
  76.  
  77.   if $flag_working
  78.     Input.key_down(Keymap[:vk_control])
  79.     sleep(0.03)
  80.     Input.key_up(Keymap[:vk_control])
  81.   end
  82.   $flag_running = false if Input.trigger?(Keymap[:vk_f9])
  83. end
  84.  
  85. def notfound_exit
  86. end
  87.  
  88. def start
  89.   EnumWindows.call(EnumWindowsProc, 0)
  90.   notfound_exit if ($MC_Hwnd || 0) == 0
  91.   puts "Press F8 to start/pause, F9 to stop program"
  92.   while $flag_running
  93.     sleep(FPS)
  94.     main_update
  95.   end
  96. end
  97.  
  98. begin
  99.   start
  100. rescue SystemExit, Interrupt
  101.   exit
  102. ensure
  103.   puts "Bye!"
  104.   Input.mouse_rup if $flag_pressed
  105. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement