Read nearby 26.3 sign text
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import collections
|
||||
import json
|
||||
import re
|
||||
import time
|
||||
import random
|
||||
@@ -18,6 +19,63 @@ class World:
|
||||
def block_at(self, x, y, z):
|
||||
return self.g.chunks.get_block_at(x, y, z)
|
||||
|
||||
@staticmethod
|
||||
def _sign_component_text(value):
|
||||
if value is None:
|
||||
return ''
|
||||
if isinstance(value, str):
|
||||
stripped = value.strip()
|
||||
if stripped.startswith(('{', '[')):
|
||||
try:
|
||||
parsed = json.loads(stripped)
|
||||
except ValueError:
|
||||
return value
|
||||
if parsed != value:
|
||||
return World._sign_component_text(parsed)
|
||||
return value
|
||||
if isinstance(value, (list, tuple)):
|
||||
return ''.join(World._sign_component_text(part) for part in value)
|
||||
if isinstance(value, dict):
|
||||
text = value.get('text', '')
|
||||
if not text and value.get('translate'):
|
||||
text = value['translate']
|
||||
if isinstance(text, (dict, list)):
|
||||
text = World._sign_component_text(text)
|
||||
return (str(text) +
|
||||
World._sign_component_text(value.get('with', [])) +
|
||||
World._sign_component_text(value.get('extra', [])))
|
||||
return str(value)
|
||||
|
||||
def find_signs(self, center, distance=16):
|
||||
"""Return nearby loaded signs as position/side/four-lines records."""
|
||||
result = []
|
||||
for position, entity in self.g.chunks.block_entities.items():
|
||||
if utils.phyp(center, position) > distance:
|
||||
continue
|
||||
data = entity.get('data') or {}
|
||||
for side in ('front', 'back'):
|
||||
text_data = data.get(side + '_text')
|
||||
if not isinstance(text_data, dict):
|
||||
continue
|
||||
messages = text_data.get('messages', [])
|
||||
lines = tuple(self._sign_component_text(line)
|
||||
for line in messages[:4])
|
||||
lines += ('',) * (4 - len(lines))
|
||||
if any(line.strip() for line in lines):
|
||||
result.append({
|
||||
'position': position, 'side': side, 'lines': lines,
|
||||
})
|
||||
if not any(key in data for key in ('front_text', 'back_text')):
|
||||
old_lines = tuple(self._sign_component_text(data.get(
|
||||
'Text{}'.format(index), '')) for index in range(1, 5))
|
||||
if any(line.strip() for line in old_lines):
|
||||
result.append({
|
||||
'position': position, 'side': 'front',
|
||||
'lines': old_lines,
|
||||
})
|
||||
result.sort(key=lambda sign: utils.phyp(center, sign['position']))
|
||||
return result
|
||||
|
||||
def check_air_column(self, pos, distance):
|
||||
for i in range(distance):
|
||||
check = utils.padd(pos, (0, i, 0))
|
||||
|
||||
Reference in New Issue
Block a user