forked from unimatrix/web
55 lines
3.2 KiB
Python
55 lines
3.2 KiB
Python
"""Versioned kit cosmetics; only bounded literal text and vanilla item metadata."""
|
|
import copy
|
|
import json
|
|
import re
|
|
from pathlib import Path
|
|
|
|
CONTRACT = json.loads(Path(__file__).with_name('kit-equipment-contract.json').read_text())
|
|
|
|
|
|
def exact(value, fields):
|
|
return isinstance(value, dict) and set(value) == set(fields)
|
|
|
|
|
|
def text(value, maximum):
|
|
return isinstance(value, str) and len(value.encode('utf-16-le')) // 2 <= maximum and not re.search(r'[\x00-\x1f\x7f<>§]', value)
|
|
|
|
|
|
def validate_lore(value):
|
|
if not isinstance(value, list) or len(value) > 6:
|
|
raise ValueError('Use at most six item lore lines')
|
|
result = []
|
|
for line in value:
|
|
if (not exact(line, ['text', 'color', 'bold', 'italic']) or not text(line['text'], 120)
|
|
or not isinstance(line['color'], str) or not re.fullmatch(r'#[a-f0-9]{6}', line['color'], re.I)
|
|
or type(line['bold']) is not bool or type(line['italic']) is not bool):
|
|
raise ValueError('Invalid literal item lore')
|
|
result.append({**line, 'color': line['color'].upper()})
|
|
return result
|
|
|
|
|
|
def validate_equipment(value):
|
|
if not exact(value, ['version', 'armor', 'banner']) or type(value['version']) is not int or value['version'] != 3 or not exact(value['armor'], CONTRACT['slots']):
|
|
raise ValueError('Complete version 3 kit required')
|
|
armor = {}
|
|
for slot in CONTRACT['slots']:
|
|
piece = value['armor'][slot]
|
|
if (not exact(piece, ['material', 'name', 'lore', 'trim']) or piece['material'] != 'netherite'
|
|
or not text(piece['name'], 64) or not exact(piece['trim'], ['pattern', 'material'])
|
|
or piece['trim']['pattern'] not in CONTRACT['trimPatterns'] or piece['trim']['material'] not in CONTRACT['trimMaterials']):
|
|
raise ValueError('Invalid armour piece or trim')
|
|
armor[slot] = {**piece, 'name': piece['name'].strip(), 'lore': validate_lore(piece['lore']), 'trim': dict(piece['trim'])}
|
|
banner = value['banner']
|
|
if (not exact(banner, ['enabled', 'name', 'base', 'patterns', 'lore']) or type(banner['enabled']) is not bool
|
|
or not text(banner['name'], 64) or banner['base'] not in CONTRACT['bannerColors']
|
|
or not isinstance(banner['patterns'], list) or len(banner['patterns']) > 6):
|
|
raise ValueError('Invalid banner')
|
|
for layer in banner['patterns']:
|
|
if not exact(layer, ['pattern', 'color']) or layer['pattern'] not in CONTRACT['bannerPatterns'] or layer['color'] not in CONTRACT['bannerColors']:
|
|
raise ValueError('Invalid banner layer')
|
|
return {'version': 3, 'armor': armor, 'banner': {**banner, 'name': banner['name'].strip(), 'patterns': copy.deepcopy(banner['patterns']), 'lore': validate_lore(banner['lore'])}}
|
|
|
|
|
|
def default_equipment(legacy):
|
|
lore = [{'text': line, 'color': '#AAAAAA', 'bold': False, 'italic': False} for line in legacy.get('lore', [])[:6]]
|
|
return validate_equipment({'version': 3, 'armor': {slot: {'material': 'netherite', 'name': legacy.get('itemNames', {}).get(slot, ''), 'lore': copy.deepcopy(lore), 'trim': {'pattern': 'dune', 'material': 'gold'}} for slot in CONTRACT['slots']}, 'banner': {'enabled': False, 'name': '', 'base': 'black', 'patterns': [], 'lore': []}})
|

Staff