-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,904 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import * as data from "../../bedrock-samples/metadata/vanilladata_modules/mojang-blocks.json"; | ||
import { BlockProperty } from "../../src/block-properties/interface"; | ||
import { Builder } from "../base/builder"; | ||
|
||
export function generateFromBlocks(builder: Builder) { | ||
blockProperties(builder); | ||
} | ||
|
||
function blockProperties(builder: Builder) { | ||
data.block_properties.forEach((blockProperty) => { | ||
const p = convertBlockProperty(blockProperty); | ||
|
||
builder.block_properties[blockProperty.name] = p; | ||
}); | ||
} | ||
|
||
function convertBlockProperty( | ||
property: (typeof data.block_properties)[0] | ||
): BlockProperty { | ||
switch (property.type) { | ||
case "bool": | ||
return { | ||
name: property.name, | ||
type: "bool", | ||
values: property.values.map((v) => v.value as boolean), | ||
}; | ||
|
||
case "int": | ||
return { | ||
name: property.name, | ||
type: "int", | ||
values: property.values.map((v) => v.value as number), | ||
}; | ||
|
||
case "string": | ||
return { | ||
name: property.name, | ||
type: "string", | ||
values: property.values.map((v) => v.value as string), | ||
}; | ||
|
||
default: | ||
throw new Error(`Unknown block property type: ${property.type}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { Builder } from "./base/builder"; | ||
import { FileBuilder } from "./base/file-builder"; | ||
|
||
export function saveBlocks(builder: Builder) { | ||
saveAllBlocksProperties(builder); | ||
saveAllBlocks(builder); | ||
} | ||
|
||
function saveAllBlocks(builder: Builder) { | ||
const blocksFile = new FileBuilder("src/blocks/blocks.auto.ts"); | ||
blocksFile | ||
.appendLine(`import { Block } from "./interface";`) | ||
.appendLineBreak() | ||
.appendLine(`export const Blocks: Record<string, Block> = {`); | ||
|
||
for (const blockName in builder.blocks) { | ||
const data = builder.blocks[blockName]; | ||
|
||
blocksFile.appendLine( | ||
` "${blockName}": ${JSON.stringify(data, null, 2)},` | ||
); | ||
} | ||
|
||
blocksFile.appendLine(`};`); | ||
|
||
//Namespace | ||
|
||
blocksFile.save(); | ||
} | ||
|
||
function saveAllBlocksProperties(builder: Builder) { | ||
const propertiesFile = new FileBuilder( | ||
"src/block-properties/block-properties.auto.ts" | ||
); | ||
propertiesFile | ||
.appendLine(`import { BlockProperty } from "./interface"; | ||
export type BlockPropertyNames = keyof typeof data; | ||
/** The namespace that access the block properties */ | ||
export namespace BlockProperties { | ||
/** | ||
* Check if the given property name exists | ||
* @param name The name of the property | ||
* @returns true if the property exists | ||
*/ | ||
export function has(name: BlockPropertyNames): boolean { | ||
return name in data; | ||
} | ||
/** | ||
* Get the property by name | ||
* @param name The name of the property | ||
* @returns The property or undefined if not found | ||
*/ | ||
export function get(name: BlockPropertyNames): BlockProperty | undefined { | ||
return (data as Record<string, BlockProperty>)[name]; | ||
} | ||
/** | ||
* Get all properties | ||
* @returns All properties | ||
*/ | ||
export function properties(): Record<BlockPropertyNames, BlockProperty> { | ||
return data as Record<BlockPropertyNames, BlockProperty>; | ||
} | ||
}`) | ||
.appendLineBreak() | ||
.appendLine("const data = {"); | ||
|
||
for (const propertyName in builder.block_properties) { | ||
const data = builder.block_properties[propertyName]; | ||
|
||
propertiesFile.appendLine( | ||
` "${propertyName}": ${JSON.stringify(data, null, 2)},` | ||
); | ||
} | ||
|
||
propertiesFile.appendLine(`};`); | ||
|
||
propertiesFile.save(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.