Add commands for dropping and dumping inventory
This commit is contained in:
		
							
								
								
									
										3
									
								
								bot.py
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								bot.py
									
									
									
									
									
								
							@@ -153,6 +153,9 @@ def init(global_state):
 | 
				
			|||||||
    g.breaking = None
 | 
					    g.breaking = None
 | 
				
			||||||
    g.break_time = 0
 | 
					    g.break_time = 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    g.dumping = None
 | 
				
			||||||
 | 
					    g.dump_lock = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    g.job = jobs.JobStates(g)
 | 
					    g.job = jobs.JobStates(g)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def bot(global_state):
 | 
					def bot(global_state):
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										58
									
								
								game.py
									
									
									
									
									
								
							
							
						
						
									
										58
									
								
								game.py
									
									
									
									
									
								
							@@ -285,13 +285,33 @@ class Game:
 | 
				
			|||||||
            reply = 'ok'
 | 
					            reply = 'ok'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if command == 'inv':
 | 
					        if command == 'inv':
 | 
				
			||||||
 | 
					            inv_list = []
 | 
				
			||||||
            for i in self.g.inv.values():
 | 
					            for i in self.g.inv.values():
 | 
				
			||||||
                if i.present:
 | 
					                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':
 | 
					        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.has_item([item]):
 | 
				
			||||||
 | 
					                self.g.dumping = item
 | 
				
			||||||
 | 
					                reply = 'ok'
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                reply = 'not found'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if reply:
 | 
					        if reply:
 | 
				
			||||||
            print(reply)
 | 
					            print(reply)
 | 
				
			||||||
            self.g.chat.send(reply)
 | 
					            self.g.chat.send(reply)
 | 
				
			||||||
@@ -304,6 +324,9 @@ class Game:
 | 
				
			|||||||
        if packet.window_id == 0:
 | 
					        if packet.window_id == 0:
 | 
				
			||||||
            self.g.inv[packet.slot] = packet.slot_data
 | 
					            self.g.inv[packet.slot] = packet.slot_data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if not packet.slot_data.present:
 | 
				
			||||||
 | 
					                self.g.dump_lock = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def break_block(self, location):
 | 
					    def break_block(self, location):
 | 
				
			||||||
        bid = self.g.chunks.get_block_at(*location)
 | 
					        bid = self.g.chunks.get_block_at(*location)
 | 
				
			||||||
        if bid != 0:
 | 
					        if bid != 0:
 | 
				
			||||||
@@ -325,7 +348,6 @@ class Game:
 | 
				
			|||||||
        self.g.chunks.set_block_at(*self.g.breaking, 0)
 | 
					        self.g.chunks.set_block_at(*self.g.breaking, 0)
 | 
				
			||||||
        self.g.breaking = None
 | 
					        self.g.breaking = None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
    def handle_break_animation(self, packet):
 | 
					    def handle_break_animation(self, packet):
 | 
				
			||||||
        print(packet)
 | 
					        print(packet)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -366,9 +388,41 @@ class Game:
 | 
				
			|||||||
        else:
 | 
					        else:
 | 
				
			||||||
            self.pick(slot)
 | 
					            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):
 | 
					    def tick(self):
 | 
				
			||||||
        if self.g.breaking:
 | 
					        if self.g.breaking:
 | 
				
			||||||
            self.animate()
 | 
					            self.animate()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if time.time() >= self.g.break_time - 2*utils.TICK:
 | 
					            if time.time() >= self.g.break_time - 2*utils.TICK:
 | 
				
			||||||
                self.break_finish()
 | 
					                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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										8
									
								
								jobs.py
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								jobs.py
									
									
									
									
									
								
							@@ -310,14 +310,10 @@ class SleepWithBedStates:
 | 
				
			|||||||
            self.state = self.select_bed
 | 
					            self.state = self.select_bed
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def select_bed(self):
 | 
					    def select_bed(self):
 | 
				
			||||||
        for slot, item in self.g.inv.items():
 | 
					        if self.game.select_item(items.BED_IDS):
 | 
				
			||||||
            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.look_at = utils.padd(self.area, path.BLOCK_BELOW)
 | 
				
			||||||
                self.g.game.choose_slot(slot)
 | 
					 | 
				
			||||||
            self.state = self.place_bed
 | 
					            self.state = self.place_bed
 | 
				
			||||||
                break
 | 
					        else:
 | 
				
			||||||
        else: # for
 | 
					 | 
				
			||||||
            self.g.chat.send('I need a bed')
 | 
					            self.g.chat.send('I need a bed')
 | 
				
			||||||
            self.state = self.cleanup
 | 
					            self.state = self.cleanup
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user