Track name, dimension, add addressable commands

This commit is contained in:
Tanner Collin 2020-12-13 05:31:09 +00:00
parent f2cb61c4bc
commit d86c21fead
3 changed files with 148 additions and 103 deletions

195
game.py
View File

@ -309,6 +309,7 @@ class Game:
self.g = global_state self.g = global_state
register = self.g.connection.register_packet_listener register = self.g.connection.register_packet_listener
register(self.handle_login_success, clientbound.login.LoginSuccessPacket)
register(self.handle_block_change, clientbound.play.BlockChangePacket) register(self.handle_block_change, clientbound.play.BlockChangePacket)
register(self.handle_join_game, clientbound.play.JoinGamePacket) register(self.handle_join_game, clientbound.play.JoinGamePacket)
register(self.handle_position_and_look, clientbound.play.PlayerPositionAndLookPacket) register(self.handle_position_and_look, clientbound.play.PlayerPositionAndLookPacket)
@ -325,17 +326,23 @@ class Game:
register(self.handle_entity_position_rotation, EntityPositionRotationPacket) register(self.handle_entity_position_rotation, EntityPositionRotationPacket)
register(self.handle_destroy_entities, DestroyEntitiesPacket) register(self.handle_destroy_entities, DestroyEntitiesPacket)
register(self.handle_spawn_player, SpawnPlayerPacket) register(self.handle_spawn_player, SpawnPlayerPacket)
register(self.handle_respawn, clientbound.play.RespawnPacket)
#register(self.handle_entity_velocity, clientbound.play.EntityVelocityPacket) #register(self.handle_entity_velocity, clientbound.play.EntityVelocityPacket)
#register(self.handle_packet, Packet, early=True) #register(self.handle_packet, Packet, early=True)
self.g.chat.set_handler(self.handle_chat) self.g.chat.set_handler(self.handle_chat)
def handle_login_success(self, packet):
print(packet)
self.g.name = packet.Username
def handle_join_game(self, packet): def handle_join_game(self, packet):
print('Connected.') print('Connected.')
print(packet) print(packet)
self.g.info = packet self.g.info = packet
self.g.eid = packet.entity_id self.g.eid = packet.entity_id
self.g.dimension = packet.world_name.replace('minecraft:', '')
def handle_block_change(self, packet): def handle_block_change(self, packet):
if packet.block_state_id == blocks.SOUL_TORCH: if packet.block_state_id == blocks.SOUL_TORCH:
@ -390,10 +397,11 @@ class Game:
source, text = message source, text = message
reply = None reply = None
private = False private = False
for_me = False
authed = False
if source == 'SYSTEM': if source == 'SYSTEM':
self.g.command_lock = False self.g.command_lock = False
return
if text == 'You are now AFK.': if text == 'You are now AFK.':
self.g.afk = True self.g.afk = True
@ -410,9 +418,17 @@ class Game:
else: else:
return return
if sender == 'tanner6':
authed = True
if text.startswith('zzz'): if text.startswith('zzz'):
text = '!zzz' text = '!zzz'
bot_num = self.g.name[-1]
if text.startswith(bot_num):
text = text[1:]
for_me = True
if text.startswith('! '): if text.startswith('! '):
text = text[2:] text = text[2:]
elif text.startswith('!'): elif text.startswith('!'):
@ -428,22 +444,24 @@ class Game:
data = None data = None
try: try:
################# Public commands ########################
if command == 'ping': if command == 'ping':
reply = 'pong' reply = 'pong'
if command == 'echo' and data: if command == 'echo' and data:
reply = data reply = data
if command == 'respawn':
packet = serverbound.play.ClientStatusPacket()
packet.action_id = serverbound.play.ClientStatusPacket.RESPAWN
self.g.connection.write_packet(packet)
reply = 'ok'
if command == 'pos': if command == 'pos':
reply = str(utils.pint(self.g.pos))[1:-1] reply = str(utils.pint(self.g.pos))[1:-1]
if command == 'afk': if command == 'afk':
if not self.g.afk:
reply = '/afk'
if command == 'unafk':
if self.g.afk:
reply = '/afk' reply = '/afk'
if command == 'error': if command == 'error':
@ -454,43 +472,6 @@ class Game:
self.break_block(blocks.TEST_BLOCK) self.break_block(blocks.TEST_BLOCK)
reply = 'ok' reply = 'ok'
if command == 'gather' and data:
if data == 'wood':
self.g.job.state = self.g.job.gather_wood
reply = 'ok'
elif data == 'sand':
self.g.job.state = self.g.job.gather_sand
reply = 'ok'
if reply:
for i in self.g.inv.values():
print(i.item_id)
if i.item_id in items.BED_IDS:
break
else:
reply += ', I need a bed'
if command == 'farm' and data:
if data == 'wood':
self.g.job.state = self.g.job.farm_wood
reply = 'ok'
elif data == 'sand':
self.g.job.state = self.g.job.farm_sand
reply = 'ok'
if reply:
for i in self.g.inv.values():
if i.item_id in items.BED_IDS:
break
else:
reply += ', I need a bed'
if command == 'stop':
self.g.job.stop()
self.g.path = []
self.g.look_at = None
reply = 'ok'
if command == 'inv': if command == 'inv':
inv_list = [] inv_list = []
for i in self.g.inv.values(): for i in self.g.inv.values():
@ -500,27 +481,9 @@ class Game:
result = '\n'.join(inv_list) result = '\n'.join(inv_list)
print(result or 'Empty') print(result or 'Empty')
if command == 'drop':
self.drop_stack()
if command == 'time': if command == 'time':
reply = str(self.g.time) reply = str(self.g.time)
if command == 'select' and data:
item = int(data)
if self.select_item([item]):
reply = 'ok'
else:
reply = 'not found'
if command == 'dump' and data:
item = int(data)
if self.count_items([item]):
self.g.dumping = item
reply = 'ok'
else:
reply = 'not found'
if command == 'count' and data: if command == 'count' and data:
item = int(data) item = int(data)
reply = str(self.count_items([item])) reply = str(self.count_items([item]))
@ -549,12 +512,6 @@ class Game:
if command == 'loaded': if command == 'loaded':
reply = str(self.g.chunks.get_loaded_area()) reply = str(self.g.chunks.get_loaded_area())
if command == 'gapple':
self.g.job.state = self.g.job.find_gapple
if data:
self.g.job.find_gapple_states.count = int(data)
reply = 'ok'
if command == 'objects': if command == 'objects':
if data == 'clear': if data == 'clear':
self.g.objects = {} self.g.objects = {}
@ -594,12 +551,6 @@ class Game:
print(str(t.entity_id) + ':', t, mobs.MOB_NAMES[t.type]) print(str(t.entity_id) + ':', t, mobs.MOB_NAMES[t.type])
reply = str(len(threats)) + ' threats' reply = str(len(threats)) + ' threats'
if command == 'cache':
self.g.job.state = self.g.job.cache_items
self.g.job.cache_items_states.minimum = 0
self.g.job.cache_items_states.silent = True
reply = 'ok'
if command == 'spiral' and data: if command == 'spiral' and data:
for i in range(int(data)): for i in range(int(data)):
print(utils.spiral(i)) print(utils.spiral(i))
@ -609,17 +560,91 @@ class Game:
reply = str(result) reply = str(result)
if command == 'zzz': if command == 'zzz':
if not self.g.afk: if not self.g.afk and self.g.dimension == 'overworld':
if self.g.path: if self.g.path:
travel_time = int(len(self.g.path) * 0.4) + 2 travel_time = int(len(self.g.path) * 0.4) + 2
reply = 'give me ' + str(travel_time) + ' secs, moving' reply = 'gimme ' + str(travel_time) + ' secs, moving'
self.g.queue_afk = True self.g.queue_afk = True
else: else:
reply = '/afk' reply = '/afk'
if command == 'print' and sender == 'tanner6':
data = data.replace('^', '.') ################# Specific commands ##########################
reply = str(eval(data)) if for_me:
pass
if command == 'respawn':
packet = serverbound.play.ClientStatusPacket()
packet.action_id = serverbound.play.ClientStatusPacket.RESPAWN
self.g.connection.write_packet(packet)
reply = 'ok'
if command == 'gather' and data:
if data == 'wood':
self.g.job.state = self.g.job.gather_wood
reply = 'ok'
elif data == 'sand':
self.g.job.state = self.g.job.gather_sand
reply = 'ok'
if reply:
for i in self.g.inv.values():
print(i.item_id)
if i.item_id in items.BED_IDS:
break
else:
reply += ', I need a bed'
if command == 'farm' and data:
if data == 'wood':
self.g.job.state = self.g.job.farm_wood
reply = 'ok'
elif data == 'sand':
self.g.job.state = self.g.job.farm_sand
reply = 'ok'
if reply:
for i in self.g.inv.values():
if i.item_id in items.BED_IDS:
break
else:
reply += ', I need a bed'
if command == 'stop':
self.g.job.stop()
self.g.path = []
self.g.look_at = None
reply = 'ok'
if command == 'drop':
self.drop_stack()
if command == 'select' and data:
item = int(data)
if self.select_item([item]):
reply = 'ok'
else:
reply = 'not found'
if command == 'dump' and data:
item = int(data)
if self.count_items([item]):
self.g.dumping = item
reply = 'ok'
else:
reply = 'not found'
if command == 'gapple':
self.g.job.state = self.g.job.find_gapple
if data:
self.g.job.find_gapple_states.count = int(data)
reply = 'ok'
if command == 'cache':
self.g.job.state = self.g.job.cache_items
self.g.job.cache_items_states.minimum = 0
self.g.job.cache_items_states.silent = True
reply = 'ok'
if command == 'fill': if command == 'fill':
try: try:
@ -648,6 +673,14 @@ class Game:
reply = 'filling ' + str(utils.pvolume(coord1, coord2)) + ' with ' + blocks.BLOCKS[block] reply = 'filling ' + str(utils.pvolume(coord1, coord2)) + ' with ' + blocks.BLOCKS[block]
################# Authorized commands ##########################
if authed:
if command == 'print':
data = data.replace('^', '.')
reply = str(eval(data))
except BaseException as e: except BaseException as e:
import traceback import traceback
print(traceback.format_exc()) print(traceback.format_exc())
@ -932,6 +965,10 @@ class Game:
packet.jump_boost = 0 packet.jump_boost = 0
self.g.connection.write_packet(packet) self.g.connection.write_packet(packet)
def handle_respawn(self, packet):
print(packet)
self.g.dimension = packet.world_name.replace('minecraft:', '')
def tick(self): def tick(self):
if self.g.breaking: if self.g.breaking:

12
jobs.py
View File

@ -480,11 +480,17 @@ class SleepWithBedStates:
return None return None
def init(self): def init(self):
if self.g.time >= 12000: if self.g.time < 12000:
self.state = self.select_bed
else:
print('Aborting sleep, not night') print('Aborting sleep, not night')
self.state = self.cleanup self.state = self.cleanup
return
if self.g.dimension != 'overworld':
print('Aborting sleep, not in overworld')
self.state = self.cleanup
return
self.state = self.select_bed
def select_bed(self): def select_bed(self):
if self.g.game.select_item(items.BED_IDS): if self.g.game.select_item(items.BED_IDS):

View File

@ -16,8 +16,10 @@ import bot
global_state = Munch() global_state = Munch()
g = global_state g = global_state
g.connection = False g.connection = False
g.name = None
g.mcdata = False g.mcdata = False
g.pos = False g.pos = False
g.dimension = None
g.inv = {} g.inv = {}
g.objects = {} g.objects = {}
g.mobs = {} g.mobs = {}