Add commands for dropping and dumping inventory

This commit is contained in:
Tanner Collin 2020-09-17 14:54:41 -06:00
parent c33e1e04b8
commit ca45925da1
3 changed files with 63 additions and 10 deletions

3
bot.py
View File

@ -153,6 +153,9 @@ def init(global_state):
g.breaking = None
g.break_time = 0
g.dumping = None
g.dump_lock = False
g.job = jobs.JobStates(g)
def bot(global_state):

58
game.py
View File

@ -285,13 +285,33 @@ class Game:
reply = 'ok'
if command == 'inv':
inv_list = []
for i in self.g.inv.values():
if i.present:
print(items.ITEM_NAMES[i.item_id], 'x', i.item_count)
inv_list.append('{}:{} x {}'.format(items.ITEM_NAMES[i.item_id], str(i.item_id), i.item_count))
reply = ', '.join(inv_list)
if command == 'drop':
self.drop_stack()
if command == '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.has_item([item]):
self.g.dumping = item
reply = 'ok'
else:
reply = 'not found'
if reply:
print(reply)
self.g.chat.send(reply)
@ -304,6 +324,9 @@ class Game:
if packet.window_id == 0:
self.g.inv[packet.slot] = packet.slot_data
if not packet.slot_data.present:
self.g.dump_lock = False
def break_block(self, location):
bid = self.g.chunks.get_block_at(*location)
if bid != 0:
@ -325,7 +348,6 @@ class Game:
self.g.chunks.set_block_at(*self.g.breaking, 0)
self.g.breaking = None
def handle_break_animation(self, packet):
print(packet)
@ -366,9 +388,41 @@ class Game:
else:
self.pick(slot)
def has_item(self, items):
# select the first match from items of inv
for slot, item in self.g.inv.items():
if item.item_id in items:
return True
else: #for
return False
def select_item(self, items):
# select the first match from items of inv
for slot, item in self.g.inv.items():
if item.item_id in items:
self.g.game.choose_slot(slot)
return True
else: #for
return False
def drop_stack(self):
packet = PlayerDiggingPacket()
packet.status = 3
packet.location = utils.pint(self.g.pos)
packet.face = 1
self.g.connection.write_packet(packet)
def tick(self):
if self.g.breaking:
self.animate()
if time.time() >= self.g.break_time - 2*utils.TICK:
self.break_finish()
if self.g.dumping and not self.g.dump_lock:
if self.select_item([self.g.dumping]):
self.drop_stack()
self.g.dump_lock = True
else:
self.g.dumping = None

12
jobs.py
View File

@ -310,14 +310,10 @@ class SleepWithBedStates:
self.state = self.select_bed
def select_bed(self):
for slot, item in self.g.inv.items():
if item.item_id in items.BED_IDS:
print('Found bed in slot', slot)
self.g.look_at = utils.padd(self.area, path.BLOCK_BELOW)
self.g.game.choose_slot(slot)
self.state = self.place_bed
break
else: # for
if self.game.select_item(items.BED_IDS):
self.g.look_at = utils.padd(self.area, path.BLOCK_BELOW)
self.state = self.place_bed
else:
self.g.chat.send('I need a bed')
self.state = self.cleanup