diff --git a/blocks.py b/blocks.py index 8016bbc..6eaa4ba 100644 --- a/blocks.py +++ b/blocks.py @@ -214,6 +214,10 @@ LEAVES = [ 'dark_oak_leaves', ] +INDEXED = [ + 'chest', +] + NON_SOLID_IDS = set([SINGLE_SNOW]) for block_name in NON_SOLID: @@ -235,6 +239,11 @@ for block_name in LEAVES: for state in JSON_BLOCKS['minecraft:' + block_name]['states']: LEAF_IDS.add(state['id']) +INDEXED_IDS = set() +for block_name in INDEXED: + for state in JSON_BLOCKS['minecraft:' + block_name]['states']: + INDEXED_IDS.add(state['id']) + def get(bid): name = BLOCKS[bid] diff --git a/game.py b/game.py index 162d60f..581c351 100644 --- a/game.py +++ b/game.py @@ -367,6 +367,9 @@ class Game: else: reply = 'nothing open' + if command == 'loaded': + reply = str(self.g.chunks.get_loaded_area()) + if reply: print(reply) self.g.chat.send(reply) diff --git a/protocol/managers.py b/protocol/managers.py index 05da829..0224326 100644 --- a/protocol/managers.py +++ b/protocol/managers.py @@ -40,6 +40,7 @@ class ChunksManager: self.data = data_manager self.chunks = {} self.biomes = {} + self.index = {} def handle_block(self, block_packet): self.set_block_at(block_packet.location.x, block_packet.location.y, block_packet.location.z, block_packet.block_state_id) @@ -53,7 +54,22 @@ class ChunksManager: def handle_chunk(self, chunk_packet): for i in chunk_packet.chunks: - self.chunks[(chunk_packet.x, i, chunk_packet.z)] = chunk_packet.chunks[i] + chunk = chunk_packet.chunks[i] + self.chunks[(chunk_packet.x, i, chunk_packet.z)] = chunk + + if chunk.sub_index: + dx = chunk.x * 16 + dy = chunk.y * 16 + dz = chunk.z * 16 + + for item_id, locations in chunk.sub_index.items(): + if item_id not in self.index: + self.index[item_id] = [] + + for l in locations: + coords = (dx + l%16, dy + l//256, dz + l%256//16) + self.index[item_id].append(coords) + self.biomes[(chunk_packet.x, None, chunk_packet.z)] = chunk_packet.biomes # FIXME def register(self, connection): diff --git a/protocol/packets.py b/protocol/packets.py index 4bba2ca..5175c33 100644 --- a/protocol/packets.py +++ b/protocol/packets.py @@ -10,6 +10,8 @@ from minecraft.networking.types import ( from protocol.types import Nbt, Slot, Entry +import blocks + class ChunkDataPacket(Packet): id = 0x20 @@ -75,6 +77,7 @@ class Chunk: self.z = z self.empty = empty self.entities = [] + self.sub_index = {} def __repr__(self): return 'Chunk(%r, %r, %r)' % (self.x, self.y, self.z) @@ -111,6 +114,11 @@ class Chunk: n = self.palette[n] self.blocks.append(n) + if n in blocks.INDEXED_IDS: + if n not in self.sub_index: + self.sub_index[n] = [] + self.sub_index[n].append(i) + def write_fields(self, packet_buffer): pass # TODO