Create an index of interesting blocks

master
Tanner Collin 4 years ago
parent 6b2df0c27e
commit b723143299
  1. 9
      blocks.py
  2. 3
      game.py
  3. 18
      protocol/managers.py
  4. 8
      protocol/packets.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]

@ -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)

@ -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):

@ -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

Loading…
Cancel
Save