Generate property-aware block stubs
This commit is contained in:
@@ -21,6 +21,7 @@ def main() -> None:
|
||||
with zipfile.ZipFile(jar) as archive:
|
||||
names = archive.namelist()
|
||||
block_names = sorted(stem(n) for n in names if n.startswith(f"assets/{MODID}/blockstates/") and n.endswith(".json"))
|
||||
block_kinds = {name: block_kind(archive, name) for name in block_names}
|
||||
item_model_names = sorted(stem(n) for n in names if n.startswith(f"assets/{MODID}/models/item/") and n.endswith(".json"))
|
||||
standalone_items = sorted(name for name in item_model_names if name not in set(block_names))
|
||||
|
||||
@@ -32,6 +33,7 @@ def main() -> None:
|
||||
"source_sha1": "528d81630a23fb4004e3abdd99b16bd225cd1e92",
|
||||
"modid": MODID,
|
||||
"blocks_from_blockstates": block_names,
|
||||
"block_kinds": block_kinds,
|
||||
"standalone_items_from_item_models": standalone_items,
|
||||
"counts": {
|
||||
"blockstates": len(block_names),
|
||||
@@ -45,13 +47,39 @@ def main() -> None:
|
||||
},
|
||||
}
|
||||
write_json(INVENTORY_DIR / "original-inventory.json", inventory)
|
||||
write_generated_registries(block_names, standalone_items)
|
||||
write_generated_registries(block_names, block_kinds, standalone_items)
|
||||
|
||||
|
||||
def stem(path: str) -> str:
|
||||
return Path(path).name.removesuffix(".json")
|
||||
|
||||
|
||||
def block_kind(archive: zipfile.ZipFile, name: str) -> str:
|
||||
with archive.open(f"assets/{MODID}/blockstates/{name}.json") as handle:
|
||||
data = json.loads(handle.read().decode("utf-8"))
|
||||
keys: set[str] = set()
|
||||
for variant in data.get("variants", {}):
|
||||
if not variant:
|
||||
continue
|
||||
for part in variant.split(","):
|
||||
keys.add(part.split("=")[0])
|
||||
if {"facing", "half", "hinge", "open"}.issubset(keys):
|
||||
return "door"
|
||||
if {"facing", "half", "open"}.issubset(keys):
|
||||
return "trapdoor"
|
||||
if {"facing", "half", "shape"}.issubset(keys):
|
||||
return "stairs"
|
||||
if "type" in keys:
|
||||
return "slab"
|
||||
if {"face", "facing"}.issubset(keys):
|
||||
return "face_attached"
|
||||
if "axis" in keys:
|
||||
return "axis"
|
||||
if "facing" in keys:
|
||||
return "horizontal"
|
||||
return "plain"
|
||||
|
||||
|
||||
def extract_resources(archive: zipfile.ZipFile) -> None:
|
||||
for info in archive.infolist():
|
||||
if info.is_dir():
|
||||
@@ -79,7 +107,7 @@ def migrate_data_path(path: str) -> str:
|
||||
return path
|
||||
|
||||
|
||||
def write_generated_registries(blocks: list[str], items: list[str]) -> None:
|
||||
def write_generated_registries(blocks: list[str], block_kinds: dict[str, str], items: list[str]) -> None:
|
||||
package_dir = GENERATED_JAVA / "net" / "mcreator" / "crustychunks" / "init"
|
||||
package_dir.mkdir(parents=True, exist_ok=True)
|
||||
used: set[str] = set()
|
||||
@@ -87,6 +115,7 @@ def write_generated_registries(blocks: list[str], items: list[str]) -> None:
|
||||
"package net.mcreator.crustychunks.init;",
|
||||
"",
|
||||
"import net.mcreator.crustychunks.CrustyChunksMod;",
|
||||
"import net.minecraft.core.Direction;",
|
||||
"import net.minecraft.core.registries.Registries;",
|
||||
"import net.minecraft.network.chat.Component;",
|
||||
"import net.minecraft.world.item.BlockItem;",
|
||||
@@ -94,8 +123,21 @@ def write_generated_registries(blocks: list[str], items: list[str]) -> None:
|
||||
"import net.minecraft.world.item.Item;",
|
||||
"import net.minecraft.world.item.ItemStack;",
|
||||
"import net.minecraft.world.item.Items;",
|
||||
"import net.minecraft.world.item.context.BlockPlaceContext;",
|
||||
"import net.minecraft.world.level.block.Block;",
|
||||
"import net.minecraft.world.level.block.Blocks;",
|
||||
"import net.minecraft.world.level.block.DoorBlock;",
|
||||
"import net.minecraft.world.level.block.FaceAttachedHorizontalDirectionalBlock;",
|
||||
"import net.minecraft.world.level.block.HorizontalDirectionalBlock;",
|
||||
"import net.minecraft.world.level.block.RotatedPillarBlock;",
|
||||
"import net.minecraft.world.level.block.SlabBlock;",
|
||||
"import net.minecraft.world.level.block.StairBlock;",
|
||||
"import net.minecraft.world.level.block.TrapDoorBlock;",
|
||||
"import net.minecraft.world.level.block.state.BlockState;",
|
||||
"import net.minecraft.world.level.block.state.BlockBehaviour;",
|
||||
"import net.minecraft.world.level.block.state.StateDefinition;",
|
||||
"import net.minecraft.world.level.block.state.properties.AttachFace;",
|
||||
"import net.minecraft.world.level.block.state.properties.BlockSetType;",
|
||||
"import net.neoforged.bus.api.IEventBus;",
|
||||
"import net.neoforged.neoforge.registries.DeferredHolder;",
|
||||
"import net.neoforged.neoforge.registries.DeferredRegister;",
|
||||
@@ -109,7 +151,8 @@ def write_generated_registries(blocks: list[str], items: list[str]) -> None:
|
||||
|
||||
for name in blocks:
|
||||
ident = safe_java_identifier(name, used)
|
||||
lines.append(f' public static final DeferredHolder<Block, Block> {ident} = BLOCKS.register("{name}", () -> new Block(BlockBehaviour.Properties.of().strength(2.0F, 6.0F)));')
|
||||
kind = block_kinds.get(name, "plain")
|
||||
lines.append(f' public static final DeferredHolder<Block, Block> {ident} = BLOCKS.register("{name}", () -> createBlock("{kind}"));')
|
||||
lines.append(f' public static final DeferredHolder<Item, BlockItem> {ident}_ITEM = ITEMS.register("{name}", () -> new BlockItem({ident}.get(), new Item.Properties()));')
|
||||
|
||||
for name in items:
|
||||
@@ -132,6 +175,49 @@ def write_generated_registries(blocks: list[str], items: list[str]) -> None:
|
||||
" ITEMS.register(modBus);",
|
||||
" CREATIVE_TABS.register(modBus);",
|
||||
" }",
|
||||
"",
|
||||
" private static Block createBlock(String kind) {",
|
||||
" BlockBehaviour.Properties properties = BlockBehaviour.Properties.of().strength(2.0F, 6.0F);",
|
||||
" return switch (kind) {",
|
||||
' case "axis" -> new RotatedPillarBlock(properties);',
|
||||
' case "door" -> new DoorBlock(BlockSetType.IRON, properties);',
|
||||
' case "face_attached" -> new GeneratedFaceAttachedHorizontalBlock(properties);',
|
||||
' case "horizontal" -> new GeneratedHorizontalBlock(properties);',
|
||||
' case "slab" -> new SlabBlock(properties);',
|
||||
' case "stairs" -> new StairBlock(Blocks.STONE.defaultBlockState(), properties);',
|
||||
' case "trapdoor" -> new TrapDoorBlock(BlockSetType.IRON, properties);',
|
||||
" default -> new Block(properties);",
|
||||
" };",
|
||||
" }",
|
||||
"",
|
||||
" private static final class GeneratedHorizontalBlock extends HorizontalDirectionalBlock {",
|
||||
" private GeneratedHorizontalBlock(BlockBehaviour.Properties properties) {",
|
||||
" super(properties);",
|
||||
" registerDefaultState(stateDefinition.any().setValue(FACING, Direction.NORTH));",
|
||||
" }",
|
||||
"",
|
||||
" @Override",
|
||||
" public BlockState getStateForPlacement(BlockPlaceContext context) {",
|
||||
" return defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite());",
|
||||
" }",
|
||||
"",
|
||||
" @Override",
|
||||
" protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {",
|
||||
" builder.add(FACING);",
|
||||
" }",
|
||||
" }",
|
||||
"",
|
||||
" private static final class GeneratedFaceAttachedHorizontalBlock extends FaceAttachedHorizontalDirectionalBlock {",
|
||||
" private GeneratedFaceAttachedHorizontalBlock(BlockBehaviour.Properties properties) {",
|
||||
" super(properties);",
|
||||
" registerDefaultState(stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(FACE, AttachFace.FLOOR));",
|
||||
" }",
|
||||
"",
|
||||
" @Override",
|
||||
" protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {",
|
||||
" builder.add(FACING, FACE);",
|
||||
" }",
|
||||
" }",
|
||||
"}",
|
||||
"",
|
||||
])
|
||||
|
||||
Reference in New Issue
Block a user