Fix web client crash
This commit is contained in:
@@ -50,9 +50,27 @@ g.food = 20
|
||||
g.sand_origin = None
|
||||
g.reload_lock = threading.RLock()
|
||||
|
||||
def _api_json_default(value):
|
||||
"""Serialize live protocol objects without invoking fragile __repr__ code."""
|
||||
from minecraft.networking.packets import Packet
|
||||
|
||||
if isinstance(value, Packet):
|
||||
fields = {key: item for key, item in vars(value).items()
|
||||
if key != 'context'}
|
||||
return {'__type__': type(value).__name__,
|
||||
'packet_name': getattr(value, 'packet_name', None),
|
||||
'fields': fields}
|
||||
if isinstance(value, (bytes, bytearray)):
|
||||
return value.hex()
|
||||
try:
|
||||
return str(value)
|
||||
except Exception:
|
||||
return object.__repr__(value)
|
||||
|
||||
|
||||
@app.route('/api/global')
|
||||
def api_global():
|
||||
data = json.dumps(g, default=lambda o: str(o), indent=4)
|
||||
data = json.dumps(g, default=_api_json_default, indent=4)
|
||||
|
||||
response = app.response_class(
|
||||
response=data,
|
||||
|
||||
Reference in New Issue
Block a user