MC/tools/upgrade-datapack-26.py
2026-11-27 23:43:05 +00:00

132 lines
6.3 KiB
Python

"""Convert the deployed Dune biome packs to Minecraft 26.2/26.3 without touching chunks.
Uses the 26.3 native biome attribute format. Writes a separate deterministic ZIP;
the caller must stage/load it before replacing a production datapack.
"""
import argparse
import copy
import json
from pathlib import Path
import zipfile
def color(value):
return f'#{value:06x}' if isinstance(value, int) else value
def convert_biome(original, target='26.3'):
if target not in ('26.2', '26.3'):
raise ValueError('Unsupported target version')
biome = copy.deepcopy(original)
attributes = biome.setdefault('attributes', {})
effects = biome.setdefault('effects', {})
for field in ('fog_color', 'sky_color', 'water_fog_color'):
if field in effects:
attributes['minecraft:visual/' + field] = color(effects.pop(field))
for field in ('water_color', 'grass_color', 'foliage_color', 'dry_foliage_color'):
if field in effects:
effects[field] = color(effects[field])
ambient = {}
for old, new in [('mood_sound', 'mood'), ('additions_sound', 'additions'), ('ambient_sound', 'loop')]:
if old in effects:
ambient[new] = effects.pop(old)
if ambient:
attributes['minecraft:audio/ambient_sounds'] = ambient
if 'music_volume' in effects:
attributes['minecraft:audio/music_volume'] = effects.pop('music_volume')
if 'music' in effects:
music = effects.pop('music')
if not music:
attributes['minecraft:audio/music_volume'] = 0.0
else:
if isinstance(music, list):
if len(music) != 1:
raise ValueError('Multiple weighted tracks require an explicit native music policy')
music = music[0].get('data', music[0])
music.pop('replace_current_music', None)
attributes['minecraft:audio/background_music'] = {'default': music}
if 'particle' in effects:
particle = effects.pop('particle')
particles = [
{'particle': particle['options'], 'probability': particle['probability']}
]
attributes['minecraft:visual/ambient_particles'] = particles if target == '26.2' else {'argument': particles, 'modifier': 'append'}
if target == '26.3' and 'creature_spawn_probability' in biome:
attributes['minecraft:gameplay/creature_world_gen_spawn_probability'] = biome.pop('creature_spawn_probability')
if target == '26.3' and 'spawners' in biome:
categories = {}
for category, entries in biome.pop('spawners').items():
categories[category] = []
for entry in entries:
low, high = entry['minCount'], entry['maxCount']
if not 1 <= low <= high:
raise ValueError('Invalid legacy spawn count')
categories[category].append({
'type': entry['type'], 'weight': entry['weight'],
'count': low if low == high else {
'type': 'minecraft:uniform', 'min_inclusive': low, 'max_inclusive': high
}
})
attributes['minecraft:gameplay/natural_mob_spawns'] = {
'argument': {'spawn_costs': biome.pop('spawn_costs', {}), 'spawns_by_category': categories},
'modifier': 'overlay'
}
return biome
def convert_feature(original, target):
feature = copy.deepcopy(original)
if target == '26.2' and feature.get('type') == 'minecraft:dripstone_cluster':
feature['type'] = 'minecraft:speleothem_cluster'
config = feature['config']
for old, new in [
('chance_of_dripstone_column_at_max_distance_from_center', 'chance_of_speleothem_at_max_distance_from_center'),
('max_distance_from_edge_affecting_chance_of_dripstone_column', 'max_distance_from_edge_affecting_chance_of_speleothem'),
('dripstone_block_layer_thickness', 'speleothem_block_layer_thickness'),
]:
config[new] = config.pop(old)
config.update(base_block={'Name': 'minecraft:dripstone_block'},
pointed_block={'Name': 'minecraft:pointed_dripstone', 'Properties': {
'thickness': 'tip', 'vertical_direction': 'up', 'waterlogged': 'false'}},
replaceable_blocks='#minecraft:dripstone_replaceable_blocks')
return feature
def convert_pack(source, destination, target='26.3'):
if target not in ('26.2', '26.3'):
raise ValueError('Unsupported target version')
source, destination = Path(source), Path(destination)
if source.resolve() == destination.resolve() or destination.exists():
raise ValueError('Use a new destination; source packs must remain intact')
changed = []
with zipfile.ZipFile(source) as src, zipfile.ZipFile(destination, 'w', zipfile.ZIP_DEFLATED) as dst:
for name in src.namelist():
data = src.read(name)
if name == 'pack.mcmeta':
meta = json.loads(data)
for key in ('pack_format', 'supported_formats'):
meta['pack'].pop(key, None)
version = [107, 1] if target == '26.2' else [121, 0]
meta['pack'].update(min_format=version, max_format=version)
data = (json.dumps(meta, indent=2) + '\n').encode()
elif '/worldgen/biome/' in name and '/tags/' not in name and name.endswith('.json'):
data = (json.dumps(convert_biome(json.loads(data), target), indent=2) + '\n').encode()
changed.append(name)
elif '/worldgen/configured_feature/' in name and name.endswith('.json'):
original = json.loads(data)
converted = convert_feature(original, target)
if converted != original:
data = (json.dumps(converted, indent=2) + '\n').encode()
info = zipfile.ZipInfo(name, date_time=(2026, 1, 1, 0, 0, 0))
info.compress_type = zipfile.ZIP_DEFLATED
dst.writestr(info, data)
return changed
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('source')
parser.add_argument('destination')
parser.add_argument('--target', choices=['26.2', '26.3'], default='26.3')
args = parser.parse_args()
print(json.dumps({'convertedBiomes': convert_pack(args.source, args.destination, args.target)}))