Skip to content

Commit

Permalink
✨ Handle special items and generate the storage file
Browse files Browse the repository at this point in the history
  • Loading branch information
aksiome committed Mar 16, 2024
1 parent 4eb3409 commit 694ba0a
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 133 deletions.
Binary file added assets/command_storage_bs.dat
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

scoreboard objectives add bs.data dummy [{"text":"BS ","color":"dark_gray"},{"text":"Data","color":"aqua"}]

function bs.block:load/lookup_table
function bs.block:load/types_table
function bs.block:load/states_table

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@

# CODE ------------------------------------------------------------------------

$execute store success score #success bs.data run data modify storage bs:out block set from storage bs:const block[{item:"$(item)"}]
execute if score #success bs.data matches 0 run return 0
data modify storage bs:ctx _ set value []
$data modify storage bs:ctx _ append from storage bs:const block[{item:"$(item)"}]
return run data modify storage bs:out block set from storage bs:ctx _[0]
90 changes: 72 additions & 18 deletions scripts/generators/block/block.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,84 @@
import requests
import sys
import os
import math
import nbtlib
from pathlib import Path

from . import consumers
from .data import DataProvider, ITEMS
from .utils import render, write_file

def run(mc_version, world_path):

blocks = []
groups = [{"default":{},"properties":{}}]
response = requests.get(f"https://raw.githubusercontent.com/Ersatz77/mcdata/{mc_version}/processed/reports/blocks/simplified/data.json")
data = DataProvider(mc_version)
module = Path(world_path).joinpath("datapacks/Bookshelf/data/bs.block/")

for index, (type, states) in enumerate(response.json().items()):
if states in groups:
group = groups.index(states)
else:
groups.append(states)
group = len(groups) - 1
print("⚙️ Generating block tags")
write_file(module.joinpath("tags/blocks/has_state.json"), [
'{"values":' + render([block["type"] for block in data.blocks if block['group'] > 0]) + "}"
])
for i in range(math.floor(math.log2(len(data.blocks))) + 1):
write_file(module.joinpath(f"tags/blocks/type/group_{2**i}.json"), [
'{"values":' + render([block["type"] for block in data.blocks if (block["id"] >> i) & 1]) + "}"
])
print("✅ Done!")

blocks.append({ "id": index + 1, "group": group, "type": type })
print("⚙️ Generating types_table.mcfunction")
write_file(module.joinpath("functions/load/types_table.mcfunction"), [
"# This file was automatically generated, do not edit it",
"data modify storage bs:const block set value " + render([{
"id": block["id"],
"group": block["group"],
"type": block["type"],
"item": ITEMS.get(block['type'], block['type']),
} for block in data.blocks])
])
print("✅ Done!")

module_path = os.path.join(world_path, "datapacks/Bookshelf/data/bs.block/")
print("⚙️ Generating states_table.mcfunction")
write_file(module.joinpath("functions/load/states_table.mcfunction"), [
"# This file was automatically generated, do not edit it"
] + [
"data modify storage bs:const block[{group:" + str(group + 1) + "}].iterable_properties set value " + render([{
"name": name,
"options": [{
"index": index,
"value": value,
"state": f"{name}={value},",
"property": {name: value},
} for index, value in enumerate(options)],
} for name, options in states.items()]) for group, states in enumerate(data.groups[1:])
])
print("✅ Done!")

consumers.gen_tags_files(blocks, module_path)
consumers.gen_types_file(blocks, module_path)
consumers.gen_states_file(groups, module_path)
consumers.gen_registry_files(groups, module_path)
print("⚙️ Generating registry functions")
for group, states in enumerate(data.groups[1:]):
write_file(module.joinpath(f"functions/get/registry/{group + 1}.mcfunction"), [
"# This file was automatically generated, do not edit it"
] + [
'execute if block ~ ~ ~ #bs.block:has_state[%name%=%value%] run data modify storage bs:out block.iterable_properties[{name:"%name%"}].options[{value:"%value%"}].selected set value 1b'
.replace("%name%", name)
.replace("%value%", value)
for name, options in states.items() for value in options
])
print("✅ Done!")

print("⚙️ Updating storage file asset")
with nbtlib.load(Path(world_path).joinpath("assets/command_storage_bs.dat")) as storage:
storage['']['data']['contents']['const']['block'] = nbtlib.parse_nbt(render([{
"id": block["id"],
"group": block["group"],
"type": block["type"],
"item": ITEMS.get(block['type'], block['type']),
"iterable_properties": [{
"name": name,
"options": [{
"index": index,
"value": value,
"state": f"{name}={value},",
"property": {name: value},
} for index, value in enumerate(options)],
} for name, options in data.groups[block["group"]].items()]
} for block in data.blocks]))
print("✅ Done!")


if __name__ == "__main__":
Expand Down
4 changes: 0 additions & 4 deletions scripts/generators/block/consumers/__init__.py

This file was deleted.

23 changes: 0 additions & 23 deletions scripts/generators/block/consumers/registry.py

This file was deleted.

40 changes: 0 additions & 40 deletions scripts/generators/block/consumers/states.py

This file was deleted.

18 changes: 0 additions & 18 deletions scripts/generators/block/consumers/tags.py

This file was deleted.

26 changes: 0 additions & 26 deletions scripts/generators/block/consumers/types.py

This file was deleted.

104 changes: 104 additions & 0 deletions scripts/generators/block/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import requests

ITEMS = {
"minecraft:wall_torch": "minecraft:torch",
"minecraft:soul_wall_torch": "minecraft:soul_torch",
"minecraft:redstone_wall_torch": "minecraft:redstone_torch",
"minecraft:oak_wall_sign": "minecraft:oak_sign",
"minecraft:spruce_wall_sign": "minecraft:spruce_sign",
"minecraft:birch_wall_sign": "minecraft:birch_sign",
"minecraft:jungle_wall_sign": "minecraft:jungle_sign",
"minecraft:acacia_wall_sign": "minecraft:acacia_sign",
"minecraft:cherry_wall_sign": "minecraft:cherry_sign",
"minecraft:dark_oak_wall_sign": "minecraft:dark_oak_sign",
"minecraft:mangrove_wall_sign": "minecraft:mangrove_sign",
"minecraft:bamboo_wall_sign": "minecraft:bamboo_sign",
"minecraft:crimson_wall_sign": "minecraft:crimson_sign",
"minecraft:warped_wall_sign": "minecraft:warped_sign",
"minecraft:oak_wall_hanging_sign": "minecraft:oak_hanging_sign",
"minecraft:spruce_wall_hanging_sign": "minecraft:spruce_hanging_sign",
"minecraft:birch_wall_hanging_sign": "minecraft:birch_hanging_sign",
"minecraft:jungle_wall_hanging_sign": "minecraft:jungle_hanging_sign",
"minecraft:acacia_wall_hanging_sign": "minecraft:acacia_hanging_sign",
"minecraft:cherry_wall_hanging_sign": "minecraft:cherry_hanging_sign",
"minecraft:dark_oak_wall_hanging_sign": "minecraft:dark_oak_hanging_sign",
"minecraft:mangrove_wall_hanging_sign": "minecraft:mangrove_hanging_sign",
"minecraft:bamboo_wall_hanging_sign": "minecraft:bamboo_hanging_sign",
"minecraft:crimson_wall_hanging_sign": "minecraft:crimson_hanging_sign",
"minecraft:warped_wall_hanging_sign": "minecraft:warped_hanging_sign",
"minecraft:tube_coral_wall_fan": "minecraft:tube_coral_fan",
"minecraft:brain_coral_wall_fan": "minecraft:brain_coral_fan",
"minecraft:bubble_coral_wall_fan": "minecraft:bubble_coral_fan",
"minecraft:fire_coral_wall_fan": "minecraft:fire_coral_fan",
"minecraft:horn_coral_wall_fan": "minecraft:horn_coral_fan",
"minecraft:dead_tube_coral_wall_fan": "minecraft:dead_tube_coral_fan",
"minecraft:dead_brain_coral_wall_fan": "minecraft:dead_brain_coral_fan",
"minecraft:dead_bubble_coral_wall_fan": "minecraft:dead_bubble_coral_fan",
"minecraft:dead_fire_coral_wall_fan": "minecraft:dead_fire_coral_fan",
"minecraft:dead_horn_coral_wall_fan": "minecraft:dead_horn_coral_fan",
"minecraft:white_wall_banner": "minecraft:white_banner",
"minecraft:orange_wall_banner": "minecraft:orange_banner",
"minecraft:magenta_wall_banner": "minecraft:magenta_banner",
"minecraft:light_blue_wall_banner": "minecraft:light_blue_banner",
"minecraft:yellow_wall_banner": "minecraft:yellow_banner",
"minecraft:lime_wall_banner": "minecraft:lime_banner",
"minecraft:pink_wall_banner": "minecraft:pink_banner",
"minecraft:gray_wall_banner": "minecraft:gray_banner",
"minecraft:light_gray_wall_banner": "minecraft:light_gray_banner",
"minecraft:cyan_wall_banner": "minecraft:cyan_banner",
"minecraft:purple_wall_banner": "minecraft:purple_banner",
"minecraft:blue_wall_banner": "minecraft:blue_banner",
"minecraft:brown_wall_banner": "minecraft:brown_banner",
"minecraft:green_wall_banner": "minecraft:green_banner",
"minecraft:red_wall_banner": "minecraft:red_banner",
"minecraft:black_wall_banner": "minecraft:black_banner",
"minecraft:player_wall_head": "minecraft:player_head",
"minecraft:zombie_wall_head": "minecraft:zombie_head",
"minecraft:creeper_wall_head": "minecraft:creeper_head",
"minecraft:dragon_wall_head": "minecraft:dragon_head",
"minecraft:piglin_wall_head": "minecraft:piglin_head",
"minecraft:skeleton_wall_skull": "minecraft:skeleton_skull",
"minecraft:wither_skeleton_wall_skull": "minecraft:wither_skeleton_skull",
"minecraft:redstone_wire": "minecraft:redstone",
"minecraft:tripwire": "minecraft:string",
"minecraft:water": "minecraft:water_bucket",
"minecraft:lava": "minecraft:lava_bucket",
"minecraft:powder_snow": "minecraft:powder_snow_bucket",
"minecraft:big_dripleaf_stem": "minecraft:big_dripleaf",
"minecraft:wheat": "minecraft:wheat_seeds",
"minecraft:cocoa": "minecraft:cocoa_beans",
"minecraft:melon_stem": "minecraft:pumpkin_seeds",
"minecraft:pumpkin_stem": "minecraft:pumpkin_seeds",
"minecraft:attached_melon_stem": "minecraft:melon_seeds",
"minecraft:attached_pumpkin_stem": "minecraft:melon_seeds",
"minecraft:water_cauldron": "minecraft:cauldron",
"minecraft:lava_cauldron": "minecraft:cauldron",
"minecraft:powder_snow_cauldron": "minecraft:cauldron",
"minecraft:carrots": "minecraft:carrot",
"minecraft:potatoes": "minecraft:potato",
"minecraft:torchflower_crop": "minecraft:torchflower_seeds",
"minecraft:pitcher_crop": "minecraft:pitcher_pod",
"minecraft:beetroots": "minecraft:beetroot_seeds",
"minecraft:sweet_berry_bush": "minecraft:sweet_berries",
"minecraft:cave_vines": "minecraft:glow_berries",
"minecraft:cave_vines_plant": "minecraft:glow_berries",
}

class DataProvider:
blocks = []
groups = [{}]

def __init__(self, mc_version):
response = requests.get(f"https://raw.githubusercontent.com/Ersatz77/mcdata/{mc_version}/processed/reports/blocks/simplified/data.min.json")

for index, (block, block_data) in enumerate(response.json().items()):
states = {}
for name, options in block_data["properties"].items():
while options[0] != block_data["default"][name]:
options.append(options.pop(0))
states[name] = options

if states not in self.groups:
self.groups.append(states)

self.blocks.append({"id": index + 1, "group": self.groups.index(states), "type": block})
19 changes: 19 additions & 0 deletions scripts/generators/block/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def render(value):
"""
Recursively render values like dictionaries and lists as a string.
"""
if isinstance(value, dict):
return "{" + ",".join([key + ":" + render(val) for key, val in value.items()]) + "}"
if isinstance(value, list):
return "[" + ",".join([render(val) for val in value]) + "]"
if isinstance(value, str):
return '"' + value + '"'
return str(value)


def write_file(file, lines):
"""
Write multiple lines to a file.
"""
with open(file, "w") as file:
file.write("\n".join(lines))
1 change: 1 addition & 0 deletions scripts/generators/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
requests
nbtlib==1.12.1

0 comments on commit 694ba0a

Please sign in to comment.