Compare commits

..

2 Commits

Author SHA1 Message Date
b8cbd9a5f0 Upgrade pyCraft 2020-09-17 18:56:52 -06:00
ca45925da1 Add commands for dropping and dumping inventory 2020-09-17 14:54:41 -06:00
5 changed files with 72 additions and 12 deletions

View File

@ -20,6 +20,7 @@ SAND = 66
SINGLE_SNOW = 3921 SINGLE_SNOW = 3921
SOUL_TORCH = 4008 SOUL_TORCH = 4008
TEST_BLOCK = (616, 78, 496)
AVOID = [ AVOID = [

5
bot.py
View File

@ -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):
@ -195,6 +198,8 @@ def bot(global_state):
init(g) init(g)
print('Initialized.') print('Initialized.')
print(blocks.mcd.windows)
while g.running: while g.running:
tick(g) tick(g)

64
game.py
View File

@ -7,6 +7,7 @@ from itertools import count
from panda3d.core import LPoint3f from panda3d.core import LPoint3f
from minecraft.networking.packets import Packet, clientbound, serverbound from minecraft.networking.packets import Packet, clientbound, serverbound
from minecraft.networking.types import BlockFace
from protocol.packets import TimeUpdatePacket, SetSlotPacket, PlayerDiggingPacket, BlockBreakAnimationPacket, AcknowledgePlayerDiggingPacket, HeldItemChangePacket, PickItemPacket from protocol.packets import TimeUpdatePacket, SetSlotPacket, PlayerDiggingPacket, BlockBreakAnimationPacket, AcknowledgePlayerDiggingPacket, HeldItemChangePacket, PickItemPacket
@ -261,7 +262,7 @@ class Game:
raise raise
if command == 'break': if command == 'break':
self.break_block((616, 78, 496)) self.break_block(blocks.TEST_BLOCK)
reply = 'ok' reply = 'ok'
if command == 'gather' and data: if command == 'gather' and data:
@ -285,13 +286,36 @@ 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 command == 'open':
self.place_block(blocks.TEST_BLOCK, BlockFace.TOP)
if reply: if reply:
print(reply) print(reply)
self.g.chat.send(reply) self.g.chat.send(reply)
@ -304,6 +328,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 +352,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 +392,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

12
jobs.py
View File

@ -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: self.g.look_at = utils.padd(self.area, path.BLOCK_BELOW)
print('Found bed in slot', slot) self.state = self.place_bed
self.g.look_at = utils.padd(self.area, path.BLOCK_BELOW) else:
self.g.game.choose_slot(slot)
self.state = self.place_bed
break
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

View File

@ -14,7 +14,7 @@ minecraft-data==2.67.0
panda3d==1.10.6.post2 panda3d==1.10.6.post2
pathtools==0.1.2 pathtools==0.1.2
pycparser==2.20 pycparser==2.20
pyCraft @ git+https://github.com/ammaraskar/pyCraft.git@cf93923acc2dcfbc076379b43842228d77aea188 pyCraft @ git+https://github.com/ammaraskar/pyCraft.git@903c20f9e2c8751fc28af46fc0dc3f7d3ec14a82
PyNBT==3.0.0 PyNBT==3.0.0
requests==2.24.0 requests==2.24.0
six==1.15.0 six==1.15.0