2020-09-16 06:02:36 +00:00
|
|
|
import re
|
|
|
|
import time
|
|
|
|
import importlib
|
2020-10-14 02:26:50 +00:00
|
|
|
import random
|
2020-09-16 20:09:14 +00:00
|
|
|
from math import hypot
|
2020-09-17 01:12:01 +00:00
|
|
|
from itertools import count
|
2020-10-14 02:26:50 +00:00
|
|
|
from munch import Munch
|
2020-10-15 07:37:47 +00:00
|
|
|
from copy import copy
|
2020-09-16 06:02:36 +00:00
|
|
|
|
|
|
|
from panda3d.core import LPoint3f
|
|
|
|
|
|
|
|
from minecraft.networking.packets import Packet, clientbound, serverbound
|
2020-09-18 00:56:52 +00:00
|
|
|
from minecraft.networking.types import BlockFace
|
2020-09-16 06:02:36 +00:00
|
|
|
|
2020-09-23 21:36:18 +00:00
|
|
|
from protocol.packets import (
|
2020-10-15 07:37:47 +00:00
|
|
|
SetSlotPacket, PlayerDiggingPacket,
|
2020-09-23 21:36:18 +00:00
|
|
|
BlockBreakAnimationPacket, AcknowledgePlayerDiggingPacket,
|
|
|
|
HeldItemChangePacket, PickItemPacket, OpenWindowPacket,
|
|
|
|
ClickWindowPacket, CloseWindowPacket, ServerWindowConfirmationPacket,
|
|
|
|
ClientWindowConfirmationPacket, EntityMetadataPacket,
|
2020-10-15 07:37:47 +00:00
|
|
|
SpawnLivingEntityPacket, EntityPositionRotationPacket, DestroyEntitiesPacket,
|
2020-09-23 21:36:18 +00:00
|
|
|
)
|
|
|
|
|
2020-09-21 05:41:55 +00:00
|
|
|
from protocol.types import Slot
|
2020-09-16 06:02:36 +00:00
|
|
|
|
|
|
|
import utils
|
|
|
|
importlib.reload(utils)
|
|
|
|
import path
|
|
|
|
importlib.reload(path)
|
2020-09-16 06:49:15 +00:00
|
|
|
import blocks
|
|
|
|
importlib.reload(blocks)
|
2020-09-17 02:11:42 +00:00
|
|
|
import items
|
|
|
|
importlib.reload(items)
|
2020-09-21 01:08:23 +00:00
|
|
|
import data
|
|
|
|
importlib.reload(data)
|
2020-09-16 06:02:36 +00:00
|
|
|
|
2020-09-16 20:09:14 +00:00
|
|
|
class MCWorld:
|
|
|
|
def __init__(self, global_state):
|
|
|
|
self.g = global_state
|
|
|
|
|
|
|
|
def block_at(self, x, y, z):
|
|
|
|
return self.g.chunks.get_block_at(x, y, z)
|
|
|
|
|
2020-10-16 08:56:11 +00:00
|
|
|
def find_blocks_3d(self, center, block_ids, distance=0, y_limit=0):
|
|
|
|
for offset in utils.search_3d(distance, y_limit):
|
|
|
|
check = utils.padd(center, offset)
|
|
|
|
if self.block_at(*check) in block_ids:
|
|
|
|
yield check
|
|
|
|
|
2020-09-16 20:09:14 +00:00
|
|
|
def find_blocks(self, center, distance, block_ids, limit=0):
|
|
|
|
# search in a spiral from center to all blocks with ID
|
|
|
|
result = []
|
|
|
|
for n in count():
|
|
|
|
offset = utils.spiral(n)
|
|
|
|
check = utils.padd(center, offset)
|
|
|
|
if self.block_at(*check) in block_ids:
|
|
|
|
if hypot(*offset) < distance:
|
|
|
|
result.append(check)
|
|
|
|
if limit and len(result) == limit:
|
|
|
|
return result
|
|
|
|
if offset[0] > distance:
|
|
|
|
return result
|
|
|
|
|
|
|
|
def find_trees(self, center, distance):
|
2020-10-19 21:49:14 +00:00
|
|
|
found_trees = []
|
|
|
|
for log in self.find_blocks_3d(center, blocks.LOG_IDS, distance, 15):
|
2020-09-16 20:09:14 +00:00
|
|
|
# crawl to the bottom log
|
|
|
|
while self.block_at(*utils.padd(log, path.BLOCK_BELOW)) in blocks.LOG_IDS:
|
|
|
|
log = utils.padd(log, path.BLOCK_BELOW)
|
|
|
|
|
|
|
|
# make sure we are on the ground
|
|
|
|
if self.block_at(*utils.padd(log, path.BLOCK_BELOW)) in blocks.NON_SOLID_IDS:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# crawl to the top log to count
|
|
|
|
log_count = 1
|
|
|
|
while self.block_at(*utils.padd(log, path.BLOCK_ABOVE)) in blocks.LOG_IDS:
|
|
|
|
log = utils.padd(log, path.BLOCK_ABOVE)
|
|
|
|
log_count += 1
|
|
|
|
|
|
|
|
# make sure it's a good tree
|
2020-10-19 21:49:14 +00:00
|
|
|
if self.block_at(*utils.padd(log, path.BLOCK_ABOVE)) not in blocks.LEAF_IDS or log_count < 3:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# crawl back to the bottom log
|
|
|
|
while self.block_at(*utils.padd(log, path.BLOCK_BELOW)) in blocks.LOG_IDS:
|
|
|
|
log = utils.padd(log, path.BLOCK_BELOW)
|
2020-09-16 20:09:14 +00:00
|
|
|
|
2020-10-19 21:49:14 +00:00
|
|
|
if log in found_trees:
|
|
|
|
continue
|
|
|
|
found_trees.append(log)
|
|
|
|
|
|
|
|
yield log
|
2020-09-16 20:09:14 +00:00
|
|
|
|
|
|
|
def find_tree_openings(self, tree):
|
|
|
|
# returns coords in a cardinal direction where we can stand by tree
|
2020-09-17 01:12:01 +00:00
|
|
|
maze_solver = path.Pathfinder(self.g.chunks)
|
2020-09-16 20:09:14 +00:00
|
|
|
result = []
|
|
|
|
|
|
|
|
# TODO: make sure only non-solid and leaves between
|
2020-10-14 02:26:50 +00:00
|
|
|
# make sure traversable too and non-avoid
|
2020-09-16 20:09:14 +00:00
|
|
|
|
|
|
|
for distance in range(5):
|
2020-09-17 01:12:01 +00:00
|
|
|
for direction in path.CHECK_DIRECTIONS:
|
|
|
|
offset = utils.pmul(direction, distance+1)
|
2020-09-16 20:09:14 +00:00
|
|
|
if maze_solver.check_traverse(tree, offset):
|
|
|
|
result.append(utils.padd(tree, offset))
|
|
|
|
return result
|
|
|
|
|
|
|
|
def path_to_place(self, start, place):
|
2020-09-17 01:12:01 +00:00
|
|
|
maze_solver = path.Pathfinder(self.g.chunks)
|
2020-09-16 20:09:14 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
s = maze_solver.astar(start, place)
|
|
|
|
return list(s) if s else None
|
2020-09-17 06:01:10 +00:00
|
|
|
except path.AStarTimeout:
|
2020-09-16 20:09:14 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
def find_bed_areas(self, center, distance):
|
2020-10-15 07:37:47 +00:00
|
|
|
bed_clearance = 25 # 5x5 area
|
|
|
|
clear_distance = 3
|
2020-09-16 20:09:14 +00:00
|
|
|
|
2020-10-16 08:56:11 +00:00
|
|
|
for a in self.find_blocks_3d(center, [0], distance, 10):
|
2020-10-15 07:37:47 +00:00
|
|
|
# check for air around the area
|
|
|
|
if len(self.find_blocks(a, clear_distance, [0], bed_clearance)) < bed_clearance:
|
2020-09-16 20:09:14 +00:00
|
|
|
continue
|
|
|
|
|
2020-10-15 07:37:47 +00:00
|
|
|
# check for ground around the area
|
|
|
|
if len(self.find_blocks(utils.padd(a, path.BLOCK_BELOW), clear_distance, blocks.NON_SOLID_IDS, bed_clearance)):
|
2020-09-16 20:09:14 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
# check for air above the area
|
2020-10-15 07:37:47 +00:00
|
|
|
if len(self.find_blocks(utils.padd(a, path.BLOCK_ABOVE), clear_distance, [0], bed_clearance)) < bed_clearance:
|
2020-09-16 20:09:14 +00:00
|
|
|
continue
|
|
|
|
|
2020-10-16 08:56:11 +00:00
|
|
|
yield a
|
2020-09-16 20:09:14 +00:00
|
|
|
|
2020-09-21 01:08:23 +00:00
|
|
|
def find_cache_areas(self, center, distance):
|
|
|
|
return self.find_bed_areas(center, distance)
|
|
|
|
|
2020-09-16 20:09:14 +00:00
|
|
|
def sand_adjacent_safe(self, sand):
|
2020-09-17 01:12:01 +00:00
|
|
|
for direction in path.CHECK_DIRECTIONS:
|
2020-09-16 20:09:14 +00:00
|
|
|
if self.block_at(*utils.padd(sand, direction)) in blocks.AVOID_IDS:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def find_sand(self, center, distance, origin):
|
|
|
|
sand = []
|
|
|
|
for i in range(10):
|
2020-09-17 02:11:42 +00:00
|
|
|
check = utils.padd(center, utils.alternate(i, 1))
|
2020-09-17 06:01:10 +00:00
|
|
|
sand.extend(self.find_blocks(check, distance, [blocks.SAND], 20))
|
2020-09-16 20:09:14 +00:00
|
|
|
|
|
|
|
safe_sand = []
|
|
|
|
for s in sand:
|
|
|
|
# make sure it has solid below
|
|
|
|
if self.block_at(*utils.padd(s, path.BLOCK_BELOW)) in blocks.NON_SOLID_IDS:
|
|
|
|
continue
|
|
|
|
# make sure it has solid two below - prevent hanging sand
|
|
|
|
if self.block_at(*utils.padd(s, path.BLOCK_BELOW2)) in blocks.NON_SOLID_IDS:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# and walkable air above
|
|
|
|
if self.block_at(*utils.padd(s, path.BLOCK_ABOVE)) not in blocks.NON_SOLID_IDS:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not self.sand_adjacent_safe(s):
|
|
|
|
continue
|
|
|
|
|
|
|
|
safe_sand.append(s)
|
|
|
|
|
|
|
|
safe_sand.sort(key=lambda x: utils.phyp_bias(center, x, origin))
|
|
|
|
return safe_sand
|
|
|
|
|
|
|
|
def find_bed_openings(self, area):
|
|
|
|
# returns coords in a cardinal direction where we can stand by bed
|
|
|
|
result = []
|
|
|
|
|
2020-09-17 01:12:01 +00:00
|
|
|
for direction in path.CHECK_DIRECTIONS:
|
2020-09-16 20:09:14 +00:00
|
|
|
result.append(utils.padd(area, direction))
|
|
|
|
return result
|
|
|
|
|
2020-09-21 01:08:23 +00:00
|
|
|
def find_cache_openings(self, area):
|
|
|
|
return self.find_bed_openings(area)
|
|
|
|
|
2020-10-15 07:37:47 +00:00
|
|
|
def find_objects(self, object_ids):
|
|
|
|
result = []
|
|
|
|
for eid, obj in copy(self.g.objects).items():
|
|
|
|
if obj.get('item_id', None) in object_ids:
|
|
|
|
result.append(obj)
|
|
|
|
return result
|
|
|
|
|
2020-10-18 05:35:43 +00:00
|
|
|
def find_leaves(self, center, distance):
|
|
|
|
for a in self.find_blocks_3d(center, blocks.LEAF_IDS, distance, 10):
|
|
|
|
yield a
|
|
|
|
|
2020-09-16 20:09:14 +00:00
|
|
|
|
2020-09-16 06:02:36 +00:00
|
|
|
class Game:
|
|
|
|
def __init__(self, global_state):
|
|
|
|
self.g = global_state
|
|
|
|
|
|
|
|
register = self.g.connection.register_packet_listener
|
|
|
|
register(self.handle_block_change, clientbound.play.BlockChangePacket)
|
|
|
|
register(self.handle_join_game, clientbound.play.JoinGamePacket)
|
|
|
|
register(self.handle_position_and_look, clientbound.play.PlayerPositionAndLookPacket)
|
2020-10-15 07:37:47 +00:00
|
|
|
register(self.handle_time_update, clientbound.play.TimeUpdatePacket)
|
2020-09-16 06:02:36 +00:00
|
|
|
register(self.handle_set_slot, SetSlotPacket)
|
|
|
|
register(self.handle_break_animation, BlockBreakAnimationPacket)
|
|
|
|
register(self.handle_break_ack, AcknowledgePlayerDiggingPacket)
|
2020-09-21 01:08:23 +00:00
|
|
|
register(self.handle_window, OpenWindowPacket)
|
2020-09-21 05:41:55 +00:00
|
|
|
register(self.handle_window_confirmation, ClientWindowConfirmationPacket)
|
2020-09-23 06:00:28 +00:00
|
|
|
register(self.handle_spawn_object, clientbound.play.SpawnObjectPacket)
|
|
|
|
register(self.handle_entity_metadata, EntityMetadataPacket)
|
2020-09-23 21:36:18 +00:00
|
|
|
register(self.handle_spawn_living, SpawnLivingEntityPacket)
|
2020-10-15 07:37:47 +00:00
|
|
|
register(self.handle_entity_position, clientbound.play.EntityPositionDeltaPacket)
|
2020-09-23 21:36:18 +00:00
|
|
|
register(self.handle_entity_position_rotation, EntityPositionRotationPacket)
|
2020-10-14 02:26:50 +00:00
|
|
|
register(self.handle_destroy_entities, DestroyEntitiesPacket)
|
2020-10-15 07:37:47 +00:00
|
|
|
#register(self.handle_entity_velocity, clientbound.play.EntityVelocityPacket)
|
2020-09-23 06:00:28 +00:00
|
|
|
|
|
|
|
#register(self.handle_packet, Packet, early=True)
|
2020-09-16 06:02:36 +00:00
|
|
|
|
|
|
|
self.g.chat.set_handler(self.handle_chat)
|
|
|
|
|
|
|
|
def handle_join_game(self, packet):
|
|
|
|
print('Connected.')
|
|
|
|
print(packet)
|
|
|
|
self.g.info = packet
|
|
|
|
self.g.eid = packet.entity_id
|
|
|
|
|
|
|
|
def handle_block_change(self, packet):
|
2020-09-16 06:49:15 +00:00
|
|
|
if packet.block_state_id == blocks.SOUL_TORCH:
|
2020-09-16 06:02:36 +00:00
|
|
|
try:
|
2020-09-16 20:16:10 +00:00
|
|
|
self.g.goal = LPoint3f(x=packet.location[0], y=packet.location[1], z=packet.location[2])
|
|
|
|
print('new waypoint:', self.g.goal)
|
2020-09-16 06:02:36 +00:00
|
|
|
|
|
|
|
start = time.time()
|
2020-09-17 01:12:01 +00:00
|
|
|
solution = path.Pathfinder(self.g.chunks).astar(utils.pint(self.g.pos), utils.pint(self.g.goal))
|
2020-09-16 06:02:36 +00:00
|
|
|
if solution:
|
|
|
|
solution = list(solution)
|
2020-10-14 02:26:50 +00:00
|
|
|
self.g.path = solution
|
|
|
|
self.g.job.state = self.g.job.stop
|
2020-09-16 06:02:36 +00:00
|
|
|
print(len(solution))
|
|
|
|
print(solution)
|
|
|
|
print(round(time.time() - start, 3), 'seconds')
|
|
|
|
else:
|
|
|
|
print('No path found')
|
|
|
|
#say(connection, 'No path found')
|
|
|
|
|
2020-09-16 20:16:10 +00:00
|
|
|
#g.y_v = 10.0
|
|
|
|
#g.y_a = -36.0
|
2020-09-16 06:02:36 +00:00
|
|
|
except BaseException as e:
|
|
|
|
import traceback
|
|
|
|
print(traceback.format_exc())
|
|
|
|
|
|
|
|
def handle_position_and_look(self, packet):
|
|
|
|
print(packet)
|
|
|
|
p = LPoint3f(x=packet.x, y=packet.y, z=packet.z)
|
|
|
|
self.g.pos = p
|
|
|
|
|
2020-09-25 21:51:36 +00:00
|
|
|
confirm_packet = serverbound.play.TeleportConfirmPacket()
|
|
|
|
confirm_packet.teleport_id = packet.teleport_id
|
|
|
|
self.g.connection.write_packet(confirm_packet)
|
|
|
|
|
2020-10-19 21:49:14 +00:00
|
|
|
self.g.correction_count += 1
|
|
|
|
|
|
|
|
if self.g.get('path', None) and self.g.correction_count > 5:
|
|
|
|
self.g.correction_count = 0
|
|
|
|
dest = self.g.path[-1]
|
|
|
|
w = self.g.world
|
|
|
|
p = utils.pint(self.g.pos)
|
|
|
|
new_path = w.path_to_place(p, dest)
|
|
|
|
|
|
|
|
if new_path:
|
|
|
|
self.g.path = new_path
|
2020-09-25 21:51:36 +00:00
|
|
|
|
2020-09-16 06:02:36 +00:00
|
|
|
def handle_chat(self, message):
|
|
|
|
source, text = message
|
|
|
|
reply = None
|
2020-10-16 08:56:11 +00:00
|
|
|
private = False
|
2020-09-16 06:02:36 +00:00
|
|
|
|
2020-09-25 06:03:22 +00:00
|
|
|
if source == 'SYSTEM':
|
|
|
|
self.g.command_lock = False
|
|
|
|
return
|
|
|
|
|
2020-10-16 08:56:11 +00:00
|
|
|
match1 = re.match(r'<(\w+)> (.*)', text)
|
|
|
|
match2 = re.match(r'\[(\w+) -> me] (.*)', text)
|
|
|
|
if match1:
|
|
|
|
sender, text = match1.groups()
|
|
|
|
elif match2:
|
|
|
|
sender, text = match2.groups()
|
|
|
|
private = True
|
2020-09-16 06:02:36 +00:00
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
if text.startswith('! '):
|
|
|
|
text = text[2:]
|
|
|
|
elif text.startswith('!'):
|
|
|
|
text = text[1:]
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
if ' ' in text:
|
|
|
|
command = text.split(' ', 1)[0]
|
|
|
|
data = text.split(' ', 1)[1]
|
|
|
|
else:
|
|
|
|
command = text
|
2020-09-25 06:03:22 +00:00
|
|
|
data = None
|
2020-09-16 06:02:36 +00:00
|
|
|
|
|
|
|
if command == 'ping':
|
|
|
|
reply = 'pong'
|
|
|
|
|
|
|
|
if command == 'echo' and 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':
|
|
|
|
reply = str(utils.pint(self.g.pos))[1:-1]
|
|
|
|
|
|
|
|
if command == 'afk':
|
|
|
|
reply = '/afk'
|
|
|
|
|
|
|
|
if command == 'error':
|
|
|
|
reply = 'ok'
|
|
|
|
raise
|
|
|
|
|
|
|
|
if command == 'break':
|
2020-09-18 00:56:52 +00:00
|
|
|
self.break_block(blocks.TEST_BLOCK)
|
2020-09-16 06:02:36 +00:00
|
|
|
reply = 'ok'
|
|
|
|
|
2020-09-17 01:12:01 +00:00
|
|
|
if command == 'gather' and data:
|
|
|
|
if data == 'wood':
|
2020-10-13 18:54:24 +00:00
|
|
|
self.g.job.state = self.g.job.gather_wood
|
2020-09-17 01:12:01 +00:00
|
|
|
reply = 'ok'
|
2020-09-17 02:31:46 +00:00
|
|
|
elif data == 'sand':
|
|
|
|
self.g.job.state = self.g.job.gather_sand
|
|
|
|
reply = 'ok'
|
2020-09-17 01:12:01 +00:00
|
|
|
|
2020-09-17 02:11:42 +00:00
|
|
|
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'
|
|
|
|
|
2020-10-14 02:26:50 +00:00
|
|
|
if command == 'farm' and data:
|
|
|
|
if data == 'wood':
|
|
|
|
self.g.job.state = self.g.job.farm_wood
|
|
|
|
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'
|
|
|
|
|
2020-09-17 02:31:46 +00:00
|
|
|
if command == 'stop':
|
|
|
|
self.g.job.state = self.g.job.stop
|
|
|
|
reply = 'ok'
|
|
|
|
|
2020-09-17 02:11:42 +00:00
|
|
|
if command == 'inv':
|
2020-09-17 20:54:41 +00:00
|
|
|
inv_list = []
|
2020-09-17 02:11:42 +00:00
|
|
|
for i in self.g.inv.values():
|
|
|
|
if i.present:
|
2020-09-17 20:54:41 +00:00
|
|
|
inv_list.append('{}:{} x {}'.format(items.ITEM_NAMES[i.item_id], str(i.item_id), i.item_count))
|
2020-10-17 18:41:41 +00:00
|
|
|
inv_list.sort()
|
2020-10-16 08:56:11 +00:00
|
|
|
result = '\n'.join(inv_list)
|
|
|
|
print(result or 'Empty')
|
2020-09-21 05:41:55 +00:00
|
|
|
|
2020-09-17 20:54:41 +00:00
|
|
|
if command == 'drop':
|
|
|
|
self.drop_stack()
|
2020-09-17 02:11:42 +00:00
|
|
|
|
|
|
|
if command == 'time':
|
|
|
|
reply = str(self.g.time)
|
|
|
|
|
2020-09-17 20:54:41 +00:00
|
|
|
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)
|
2020-10-18 05:35:43 +00:00
|
|
|
if self.count_items([item]):
|
2020-09-17 20:54:41 +00:00
|
|
|
self.g.dumping = item
|
|
|
|
reply = 'ok'
|
|
|
|
else:
|
|
|
|
reply = 'not found'
|
|
|
|
|
2020-10-18 05:35:43 +00:00
|
|
|
if command == 'count' and data:
|
|
|
|
item = int(data)
|
|
|
|
reply = str(self.count_items([item]))
|
|
|
|
|
2020-09-18 00:56:52 +00:00
|
|
|
if command == 'open':
|
2020-09-21 01:08:23 +00:00
|
|
|
self.open_container(blocks.TEST_BLOCK)
|
|
|
|
|
|
|
|
if command == 'close':
|
|
|
|
if self.g.window:
|
|
|
|
self.close_window()
|
|
|
|
else:
|
|
|
|
reply = 'nothing open'
|
|
|
|
|
|
|
|
if command == 'click' and data:
|
|
|
|
if self.g.window:
|
2020-09-21 05:41:55 +00:00
|
|
|
slot, button, mode = [int(x) for x in data.split(' ')]
|
|
|
|
try:
|
|
|
|
item = self.g.window.contents[slot]
|
|
|
|
except KeyError:
|
|
|
|
item = Slot(present=False, item_id=None, item_count=None, nbt=None)
|
2020-09-21 01:08:23 +00:00
|
|
|
print(item)
|
2020-09-21 05:41:55 +00:00
|
|
|
self.click_window(slot, button, mode, item)
|
2020-09-21 01:08:23 +00:00
|
|
|
else:
|
|
|
|
reply = 'nothing open'
|
2020-09-18 00:56:52 +00:00
|
|
|
|
2020-09-24 23:46:00 +00:00
|
|
|
if command == 'loaded':
|
|
|
|
reply = str(self.g.chunks.get_loaded_area())
|
|
|
|
|
2020-09-25 06:03:22 +00:00
|
|
|
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'
|
|
|
|
|
2020-10-14 02:26:50 +00:00
|
|
|
if command == 'objects':
|
|
|
|
for k, v in self.g.objects.items():
|
|
|
|
if data and v.item_id != int(data): continue
|
|
|
|
print(str(k) + ':', v)
|
|
|
|
|
2020-10-16 08:56:11 +00:00
|
|
|
if command == 'cache':
|
|
|
|
self.g.job.state = self.g.job.cache_items
|
|
|
|
reply = 'ok'
|
|
|
|
|
2020-09-16 06:02:36 +00:00
|
|
|
if reply:
|
|
|
|
print(reply)
|
2020-10-16 08:56:11 +00:00
|
|
|
if private and not reply.startswith('/'):
|
|
|
|
self.g.chat.send('/m ' + sender + ' ' + reply)
|
|
|
|
else:
|
|
|
|
self.g.chat.send(reply)
|
2020-09-16 06:02:36 +00:00
|
|
|
|
|
|
|
def handle_time_update(self, packet):
|
2020-09-16 20:16:10 +00:00
|
|
|
self.g.time = packet.time_of_day % 24000
|
2020-09-16 06:02:36 +00:00
|
|
|
|
|
|
|
def handle_set_slot(self, packet):
|
2020-09-21 01:08:23 +00:00
|
|
|
g = self.g
|
2020-09-16 06:02:36 +00:00
|
|
|
print(packet)
|
|
|
|
if packet.window_id == 0:
|
2020-09-21 01:08:23 +00:00
|
|
|
g.inv[packet.slot] = packet.slot_data
|
|
|
|
elif g.window:
|
|
|
|
g.window.contents[packet.slot] = packet.slot_data
|
2020-09-16 06:02:36 +00:00
|
|
|
|
2020-09-21 05:41:55 +00:00
|
|
|
if packet.window_id >= 0 and not packet.slot_data.present:
|
|
|
|
print('unlocking item lock')
|
2020-09-21 01:08:23 +00:00
|
|
|
g.item_lock = False
|
2020-09-17 20:54:41 +00:00
|
|
|
|
2020-09-16 06:02:36 +00:00
|
|
|
def break_block(self, location):
|
2020-09-16 06:49:15 +00:00
|
|
|
bid = self.g.chunks.get_block_at(*location)
|
|
|
|
if bid != 0:
|
2020-09-16 06:02:36 +00:00
|
|
|
packet = PlayerDiggingPacket()
|
|
|
|
packet.status = 0
|
|
|
|
packet.location = location
|
|
|
|
packet.face = 1
|
|
|
|
self.g.connection.write_packet(packet)
|
|
|
|
|
2020-09-16 20:16:10 +00:00
|
|
|
self.g.breaking = location
|
|
|
|
self.g.break_time = time.time() + utils.break_time(bid)
|
2020-09-16 06:49:15 +00:00
|
|
|
|
|
|
|
def break_finish(self):
|
|
|
|
packet = PlayerDiggingPacket()
|
|
|
|
packet.status = 2
|
2020-09-16 20:16:10 +00:00
|
|
|
packet.location = self.g.breaking
|
2020-09-16 06:49:15 +00:00
|
|
|
packet.face = 1
|
|
|
|
self.g.connection.write_packet(packet)
|
2020-09-17 01:12:01 +00:00
|
|
|
self.g.chunks.set_block_at(*self.g.breaking, 0)
|
2020-09-16 20:16:10 +00:00
|
|
|
self.g.breaking = None
|
2020-09-16 06:49:15 +00:00
|
|
|
|
|
|
|
def handle_break_animation(self, packet):
|
|
|
|
print(packet)
|
|
|
|
|
|
|
|
def handle_break_ack(self, packet):
|
2020-09-17 01:12:01 +00:00
|
|
|
#print(packet)
|
|
|
|
return
|
2020-09-16 06:02:36 +00:00
|
|
|
|
|
|
|
def animate(self):
|
|
|
|
packet = serverbound.play.AnimationPacket()
|
|
|
|
packet.hand = packet.HAND_MAIN
|
|
|
|
self.g.connection.write_packet(packet)
|
|
|
|
|
2020-09-16 20:09:14 +00:00
|
|
|
def place_block(self, location, face):
|
|
|
|
packet = serverbound.play.PlayerBlockPlacementPacket()
|
|
|
|
packet.hand = 0
|
2020-09-17 02:11:42 +00:00
|
|
|
packet.location = location
|
2020-09-16 20:09:14 +00:00
|
|
|
packet.face = face
|
|
|
|
packet.x = 0.5
|
|
|
|
packet.y = 0.5
|
|
|
|
packet.z = 0.5
|
|
|
|
packet.inside_block = False
|
|
|
|
self.g.connection.write_packet(packet)
|
|
|
|
|
2020-09-17 02:11:42 +00:00
|
|
|
def pick(self, slot):
|
|
|
|
packet = PickItemPacket()
|
|
|
|
packet.slot_to_use = slot
|
|
|
|
self.g.connection.write_packet(packet)
|
|
|
|
|
|
|
|
def hold(self, slot):
|
|
|
|
packet = HeldItemChangePacket()
|
|
|
|
packet.slot = slot
|
|
|
|
self.g.connection.write_packet(packet)
|
|
|
|
|
|
|
|
def choose_slot(self, slot):
|
|
|
|
if slot >= 36:
|
|
|
|
slot -= 36
|
|
|
|
self.hold(slot)
|
|
|
|
else:
|
|
|
|
self.pick(slot)
|
|
|
|
|
2020-10-18 05:35:43 +00:00
|
|
|
def count_items(self, items):
|
|
|
|
# count how many items are in inv
|
|
|
|
count = 0
|
2020-09-17 20:54:41 +00:00
|
|
|
for slot, item in self.g.inv.items():
|
|
|
|
if item.item_id in items:
|
2020-10-18 05:35:43 +00:00
|
|
|
count += item.item_count
|
|
|
|
return count
|
2020-09-17 20:54:41 +00:00
|
|
|
|
|
|
|
def select_item(self, items):
|
|
|
|
# select the first match from items of inv
|
2020-10-17 18:41:41 +00:00
|
|
|
# uses smallest stack of that match
|
|
|
|
inv_items = list(self.g.inv.items())
|
|
|
|
inv_items.sort(key=lambda x: x[1].item_count or 0)
|
|
|
|
for slot, item in inv_items:
|
2020-09-17 20:54:41 +00:00
|
|
|
if item.item_id in items:
|
|
|
|
self.g.game.choose_slot(slot)
|
|
|
|
return True
|
|
|
|
else: #for
|
|
|
|
return False
|
|
|
|
|
2020-10-14 02:26:50 +00:00
|
|
|
def select_random_item(self, items):
|
|
|
|
# select a random match from items of inv
|
|
|
|
# this is random per item type
|
|
|
|
# example: 5 stacks wood, 1 stack glass
|
|
|
|
# -> still 50/50 chance between them
|
|
|
|
|
|
|
|
matches = set()
|
|
|
|
for slot, item in self.g.inv.items():
|
|
|
|
if item.item_id in items:
|
|
|
|
matches.add(item.item_id)
|
|
|
|
|
|
|
|
if matches:
|
|
|
|
return self.select_item([random.choice(list(matches))])
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-09-17 20:54:41 +00:00
|
|
|
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)
|
|
|
|
|
2020-09-21 01:08:23 +00:00
|
|
|
def open_container(self, location):
|
|
|
|
bid = self.g.chunks.get_block_at(*location)
|
|
|
|
# TODO: check if block is a chest??
|
|
|
|
self.place_block(location, BlockFace.TOP)
|
|
|
|
|
|
|
|
def handle_window(self, packet):
|
|
|
|
print(packet)
|
2020-10-14 02:26:50 +00:00
|
|
|
self.g.window = Munch(data=packet, contents=dict(), count=0)
|
2020-09-21 05:41:55 +00:00
|
|
|
|
|
|
|
def click_window(self, slot, button, mode, item):
|
|
|
|
w = self.g.window
|
2020-09-21 01:08:23 +00:00
|
|
|
|
|
|
|
packet = ClickWindowPacket()
|
2020-09-21 05:41:55 +00:00
|
|
|
packet.window_id = w.data.window_id
|
2020-09-21 01:08:23 +00:00
|
|
|
packet.slot = slot
|
|
|
|
packet.button = button
|
2020-09-21 05:41:55 +00:00
|
|
|
packet.action_number = w.count
|
2020-09-21 01:08:23 +00:00
|
|
|
packet.mode = mode
|
|
|
|
packet.clicked_item = item
|
|
|
|
self.g.connection.write_packet(packet)
|
2020-09-21 05:41:55 +00:00
|
|
|
print('<--', packet)
|
|
|
|
|
|
|
|
w.count += 1
|
2020-09-21 01:08:23 +00:00
|
|
|
|
|
|
|
def close_window(self):
|
|
|
|
packet = CloseWindowPacket()
|
|
|
|
packet.window_id = self.g.window.data.window_id
|
|
|
|
self.g.connection.write_packet(packet)
|
|
|
|
self.g.window = None
|
|
|
|
|
2020-09-21 05:41:55 +00:00
|
|
|
def handle_window_confirmation(self, packet):
|
|
|
|
print(packet)
|
|
|
|
packet2 = ServerWindowConfirmationPacket()
|
|
|
|
packet2.window_id = packet.window_id
|
|
|
|
packet2.action_number = packet.action_number
|
|
|
|
packet2.accepted = packet.accepted
|
|
|
|
self.g.connection.write_packet(packet2)
|
2020-09-21 01:08:23 +00:00
|
|
|
|
2020-09-23 06:00:28 +00:00
|
|
|
def handle_spawn_object(self, packet):
|
2020-10-14 02:26:50 +00:00
|
|
|
#return
|
2020-09-24 22:02:15 +00:00
|
|
|
if packet.type_id != 37: return
|
2020-10-16 08:56:11 +00:00
|
|
|
#print(packet)
|
2020-10-14 02:26:50 +00:00
|
|
|
self.g.objects[packet.entity_id] = Munch(
|
2020-10-15 07:37:47 +00:00
|
|
|
entity_id=packet.entity_id,
|
2020-10-14 02:26:50 +00:00
|
|
|
x=packet.x,
|
|
|
|
y=packet.y,
|
|
|
|
z=packet.z,
|
|
|
|
velocity_x=packet.velocity_x,
|
|
|
|
velocity_y=packet.velocity_y,
|
|
|
|
velocity_z=packet.velocity_z,
|
|
|
|
)
|
|
|
|
|
|
|
|
def check_gapple(self, packet):
|
2020-09-25 06:03:22 +00:00
|
|
|
if not self.g.job:
|
|
|
|
return
|
|
|
|
|
2020-10-14 02:26:50 +00:00
|
|
|
current_gapple_chest = self.g.job.find_gapple_states.current_chest
|
|
|
|
if current_gapple_chest:
|
|
|
|
for entry in packet.metadata:
|
|
|
|
if entry.type != 6:
|
|
|
|
continue
|
2020-09-25 06:03:22 +00:00
|
|
|
|
2020-10-14 02:26:50 +00:00
|
|
|
if entry.value.item_id in items.GAPPLE_ID:
|
|
|
|
self.g.chat.send('gapple found: ' + str(current_gapple_chest)[1:-1])
|
|
|
|
print('gapple found:', str(current_gapple_chest)[1:-1])
|
2020-09-25 06:03:22 +00:00
|
|
|
|
2020-10-14 02:26:50 +00:00
|
|
|
def handle_entity_metadata(self, packet):
|
|
|
|
if not packet.metadata:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.check_gapple(packet)
|
2020-09-25 06:03:22 +00:00
|
|
|
|
2020-10-14 02:26:50 +00:00
|
|
|
obj = self.g.objects.get(packet.entity_id, None)
|
|
|
|
if obj:
|
|
|
|
for entry in packet.metadata:
|
|
|
|
if entry.type != 6:
|
|
|
|
continue
|
|
|
|
obj.item_id = entry.value.item_id
|
|
|
|
obj.item_count = entry.value.item_count
|
2020-09-23 21:36:18 +00:00
|
|
|
|
|
|
|
def handle_spawn_living(self, packet):
|
2020-09-24 22:02:15 +00:00
|
|
|
return
|
2020-09-23 06:00:28 +00:00
|
|
|
print(packet)
|
|
|
|
|
2020-09-23 21:36:18 +00:00
|
|
|
def handle_entity_position(self, packet):
|
2020-10-14 02:26:50 +00:00
|
|
|
obj = self.g.objects.get(packet.entity_id, None)
|
|
|
|
if obj:
|
|
|
|
pass
|
|
|
|
#obj.x += packet.delta_x
|
|
|
|
#obj.y += packet.delta_y
|
|
|
|
#obj.z += packet.delta_z
|
2020-09-23 21:36:18 +00:00
|
|
|
|
|
|
|
def handle_entity_position_rotation(self, packet):
|
2020-10-14 02:26:50 +00:00
|
|
|
obj = self.g.objects.get(packet.entity_id, None)
|
|
|
|
if obj:
|
|
|
|
print('object rotation found:', packet)
|
|
|
|
raise
|
|
|
|
|
|
|
|
def handle_entity_velocity(self, packet):
|
|
|
|
obj = self.g.objects.get(packet.entity_id, None)
|
|
|
|
if obj:
|
|
|
|
print(packet)
|
|
|
|
#obj.velocity_x = packet.velocity_x
|
|
|
|
#obj.velocity_y = packet.velocity_y
|
|
|
|
#obj.velocity_z = packet.velocity_z
|
|
|
|
|
|
|
|
def handle_destroy_entities(self, packet):
|
|
|
|
for eid in packet.entity_ids:
|
|
|
|
if eid in self.g.objects:
|
|
|
|
del self.g.objects[eid]
|
2020-09-23 06:00:28 +00:00
|
|
|
|
2020-09-16 06:02:36 +00:00
|
|
|
def tick(self):
|
2020-09-16 20:16:10 +00:00
|
|
|
if self.g.breaking:
|
2020-09-16 06:02:36 +00:00
|
|
|
self.animate()
|
2020-09-16 06:49:15 +00:00
|
|
|
|
2020-10-18 05:35:43 +00:00
|
|
|
if time.time() >= self.g.break_time: #- 2*utils.TICK:
|
2020-09-16 06:49:15 +00:00
|
|
|
self.break_finish()
|
2020-09-17 20:54:41 +00:00
|
|
|
|
2020-09-21 01:08:23 +00:00
|
|
|
if self.g.dumping and not self.g.item_lock:
|
2020-09-17 20:54:41 +00:00
|
|
|
if self.select_item([self.g.dumping]):
|
|
|
|
self.drop_stack()
|
2020-09-21 01:08:23 +00:00
|
|
|
self.g.item_lock = True
|
2020-09-17 20:54:41 +00:00
|
|
|
else:
|
|
|
|
self.g.dumping = None
|
|
|
|
|
2020-10-19 21:49:14 +00:00
|
|
|
if not len(self.g.path):
|
|
|
|
self.g.correction_count = 0
|
|
|
|
|