-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.py
76 lines (64 loc) · 2.3 KB
/
utils.py
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
import threading
import time
import os
import logging
from scipy.spatial.distance import euclidean
#import numpy as np
#from peon import BlockTypes
log = logging.getLogger(__name__)
def start_afk_thread(bot):
def do_afk_thread(bot, pid):
i = 0
while True:
if time.time() - bot.last_keepalive > 20:
print 'Server disconnected. Exiting'
os.kill(pid, 0)
break
bot.send(bot.proto.PlayServerboundHeldItemChange.id,
slot=i
)
i = (i + 1) % 8
time.sleep(55)
pid = os.getppid()
thread = threading.Thread(target=do_afk_thread, name='afk', args=(bot, pid))
thread.daemon = True
thread.start()
return thread
def start_shear_thread(bot):
def do_shear_thread(bot, pid):
#shears_id = BlockTypes.get_id('Shears')
while True:
for entity in bot.player.iter_entities_in_range('Sheep'):
is_sheared = entity.metadata.get(16, (0, 0))[1] >= 16
is_child = entity.metadata.get(12, (0, 0))[1] < 0
#holding_shears = bot.player.held_item.item_id == shears_id
if not is_child and not is_sheared:
log.debug("Entity metadata: %s", str(entity.metadata))
log.info("Sending UseEntity for eid: [%d]", entity.eid)
bot.send(bot.proto.PlayServerboundUseEntity.id,
target=entity.eid,
type=0
)
time.sleep(1)
time.sleep(5)
pid = os.getppid()
thread = threading.Thread(target=do_shear_thread, name='afk', args=(bot, pid))
thread.daemon = True
thread.start()
return thread
def start_chat_interface(bot):
message = ''
while message not in ['exit', 'quit']:
message = raw_input('> ').strip()
if message not in ['exit', 'quit']:
bot.send(bot.proto.PlayServerboundChatMessage.id,
chat=message
)
def start_cmd_interface(bot, handlers):
cmd = ''
while cmd not in ['exit', 'quit']:
args = raw_input('> ').strip().split()
if not args:
continue
if args[0] in handlers:
handlers[args[0]](bot, *args[1:])