FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
minecraft_python_controller/main.py at main · Musaddiq625/minecraft_python_controller · GitHub
Musaddiq625
/
minecraft_python_controller
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
minecraft_python_controller
/
main.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
233 lines (206 loc) · 7.32 KB
Breadcrumbs
minecraft_python_controller
/
main.py
Copy path
File metadata and controls
233 lines (206 loc) · 7.32 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import
sys
import
subprocess
GREEN
=
'
\033
[92m'
RED
=
'
\x1b
[31;20m'
ORANGE
=
"
\033
[38;5;214m"
RESET
=
'
\033
[0m'
def
success_text
(
text
):
return
print
(
f'
{
GREEN
}
{
text
}
{
RESET
}
'
)
def
error_text
(
text
):
return
print
(
f'
{
RED
}
{
text
}
{
RESET
}
'
)
def
info_text
(
text
):
return
print
(
f'
{
ORANGE
}
{
text
}
{
RESET
}
'
)
def
verify_and_import
():
required_packages
=
{
"socket"
:
None
,
"json"
:
None
,
"time"
:
None
,
"threading"
:
None
,
"ctypes"
:
None
,
"sys"
:
None
,
"pynput"
:
"pynput"
,
"psutil"
:
None
,
}
installed_new_package
=
False
for
module
,
package
in
required_packages
.
items
():
try
:
__import__
(
module
)
except
ImportError
:
if
package
:
info_text
(
f"Module '
{
module
}
' is missing. Installing '
{
package
}
'..."
)
subprocess
.
check_call
([
sys
.
executable
,
"-m"
,
"pip"
,
"install"
,
package
])
installed_new_package
=
True
# Mark that we installed something
else
:
error_text
(
f"Module '
{
module
}
' is missing and is a built-in module. Check your Python installation."
)
if
installed_new_package
:
success_text
(
"
\n
Packages were installed. Rerun the script please...
\n
"
)
sys
.
exit
(
0
)
# Run verification before proceeding
verify_and_import
()
# Now, safely import the modules
import
socket
import
json
import
time
import
psutil
import
threading
import
ctypes
from
pynput
.
keyboard
import
Controller
as
KeyboardController
,
Key
from
pynput
.
mouse
import
Button
,
Controller
as
MouseController
keyboard_controller
=
KeyboardController
()
mouse
=
MouseController
()
PROGRAM
=
'Minecraft'
UDP_IP
=
'0.0.0.0'
UDP_PORT
=
65432
GOOGLE_DNS
=
'8.8.8.8'
MOUSE_SENSITIVITY
=
45.2
def
get_local_ip
():
try
:
with
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
as
s
:
s
.
connect
((
GOOGLE_DNS
,
80
))
success_text
(
f'Please enter this IP Address in the Controller App:
\n
{
s
.
getsockname
()[
0
]
}
\n
'
)
return
True
except
Exception
as
e
:
error_text
(
f'Something went wrong!
{
str
(
e
)
}
'
)
return
False
def
is_minecraft_running
():
"""Checks if Minecraft is currently running as an active process."""
minecraft_names
=
[
"minecraft"
,
"javaw.exe"
,
"Minecraft.Windows.exe"
,
"minecraft-launcher.exe"
]
for
proc
in
psutil
.
process_iter
([
'pid'
,
'name'
]):
try
:
process_name
=
proc
.
info
[
'name'
].
lower
()
if
any
(
name
.
lower
()
in
process_name
for
name
in
minecraft_names
):
return
True
except
(
psutil
.
NoSuchProcess
,
psutil
.
AccessDenied
,
psutil
.
ZombieProcess
):
continue
return
False
def
keyboard_press_and_release
(
action
):
keyboard_controller
.
press
(
action
)
time
.
sleep
(
0.05
)
keyboard_controller
.
release
(
action
)
def
mouse_press_and_release
(
action
):
mouse
.
press
(
action
)
time
.
sleep
(
0.05
)
mouse
.
release
(
action
)
def
process_command
(
action
,
command
):
hotbar_mapping
=
{
'hotBar1'
:
'1'
,
'hotBar2'
:
'2'
,
'hotBar3'
:
'3'
,
'hotBar4'
:
'4'
,
'hotBar5'
:
'5'
,
'hotBar6'
:
'6'
,
'hotBar7'
:
'7'
,
'hotBar8'
:
'8'
,
'hotBar9'
:
'9'
,
}
keyboard_mapping
=
{
'left'
:
'a'
,
'right'
:
'd'
,
'up'
:
'w'
,
'down'
:
's'
,
'inventory'
:
'e'
,
'drop'
:
'q'
,
'chat'
:
't'
,
'zoom'
:
'c'
,
'jump'
:
Key
.
space
,
'pause'
:
Key
.
esc
,
'sneak'
:
Key
.
shift
,
'sprint'
:
Key
.
ctrl
,
}
mouse_mapping
=
{
'attack'
:
Button
.
left
,
'use'
:
Button
.
right
,
}
try
:
if
command
==
'start'
:
if
action
in
keyboard_mapping
:
keyboard_controller
.
press
(
keyboard_mapping
[
action
])
elif
action
in
mouse_mapping
:
mouse
.
press
(
mouse_mapping
[
action
])
else
:
error_text
(
f'Unknown action for start:
\'
{
action
}
\'
'
)
elif
command
==
'stop'
:
if
action
in
keyboard_mapping
:
keyboard_controller
.
release
(
keyboard_mapping
[
action
])
elif
action
in
mouse_mapping
:
mouse
.
release
(
mouse_mapping
[
action
])
else
:
error_text
(
f'Unknown action for stop:
\'
{
action
}
\'
'
)
elif
command
==
'press'
:
if
action
in
hotbar_mapping
:
keyboard_press_and_release
(
hotbar_mapping
[
action
])
elif
action
in
keyboard_mapping
:
keyboard_press_and_release
(
keyboard_mapping
[
action
])
elif
action
in
mouse_mapping
:
mouse_press_and_release
(
mouse_mapping
[
action
])
else
:
error_text
(
f'Unknown action for press:
\'
{
action
}
\'
'
)
else
:
error_text
(
f'Unknown command for
\'
{
action
}
\'
:
\'
{
command
}
\'
'
)
except
Exception
as
e
:
error_text
(
f'Error processing command for
\'
{
action
}
\'
with command
\'
{
command
}
\'
:
{
e
}
'
)
def
process_mouse_movement
(
mouse_data
):
user32
=
ctypes
.
windll
.
user32
steps
=
5
delay
=
0.01
dx
=
round
(
mouse_data
.
get
(
'x'
,
0
)
*
MOUSE_SENSITIVITY
)
dy
=
round
(
mouse_data
.
get
(
'y'
,
0
)
*
MOUSE_SENSITIVITY
)
step_x
=
dx
/
steps
step_y
=
dy
/
steps
for
_
in
range
(
steps
):
user32
.
mouse_event
(
0x0001
,
int
(
step_x
),
int
(
step_y
),
0
,
0
)
time
.
sleep
(
delay
)
def
udp_server
():
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
try
:
sock
.
bind
((
UDP_IP
,
UDP_PORT
))
sock
.
settimeout
(
300.0
)
success_text
(
'Server started. Press Ctrl+C to exit.'
)
except
Exception
as
e
:
error_text
(
f'Failed to bind server:
{
e
}
'
)
return
inactive_time
=
0
max_inactive_time
=
5
while
True
:
try
:
if
not
is_minecraft_running
():
info_text
(
f'
{
PROGRAM
}
not running. Exiting server in...
{
max_inactive_time
-
inactive_time
}
'
)
inactive_time
+=
1
time
.
sleep
(
0.5
)
if
inactive_time
>=
max_inactive_time
:
error_text
(
f'
{
PROGRAM
}
inactive for too long. Server shutting down.'
)
break
continue
inactive_time
=
0
data
,
addr
=
sock
.
recvfrom
(
1024
)
message
=
data
.
decode
()
info_text
(
f'Message:
{
message
}
'
)
try
:
message_data
=
json
.
loads
(
message
)
if
'ping'
in
message_data
and
message_data
[
'ping'
]
==
'musaddiq625'
:
pong_response
=
json
.
dumps
({
'pong'
:
'musaddiq625'
})
sock
.
sendto
(
pong_response
.
encode
(),
addr
)
success_text
(
f'Connected to
{
addr
}
'
)
continue
elif
'mouse'
in
message_data
:
process_mouse_movement
(
message_data
[
'mouse'
])
else
:
for
key
,
value
in
message_data
.
items
():
process_command
(
key
,
value
)
except
json
.
JSONDecodeError
:
error_text
(
f'Failed to decode JSON. Raw message:
{
data
}
'
)
except
Exception
as
e
:
error_text
(
f'Error receiving data:
{
e
}
'
)
time
.
sleep
(
0.01
)
sock
.
close
()
sys
.
exit
(
0
)
if
__name__
==
'__main__'
:
try
:
if
get_local_ip
():
server_thread
=
threading
.
Thread
(
target
=
udp_server
,
daemon
=
True
)
server_thread
.
start
()
while
True
:
time
.
sleep
(
1
)
except
KeyboardInterrupt
:
error_text
(
'Server shutting down.'
)
sys
.
exit
(
0
)
Back
|
FazBrowse Home
|
New Git URL