From 9bc18f8b8550dad75b271a06ed2de27ba1162b99 Mon Sep 17 00:00:00 2001 From: DaanV2 Date: Thu, 27 Apr 2023 11:15:01 +0200 Subject: [PATCH] Redid block --- src/Lib/Project/BehaviorPack/Block/Block.ts | 64 +----------------- .../Project/BehaviorPack/Block/BlockState.ts | 65 +++++++++++++++++++ src/Lib/Project/BehaviorPack/Block/index.ts | 1 + 3 files changed, 67 insertions(+), 63 deletions(-) create mode 100644 src/Lib/Project/BehaviorPack/Block/BlockState.ts diff --git a/src/Lib/Project/BehaviorPack/Block/Block.ts b/src/Lib/Project/BehaviorPack/Block/Block.ts index 5ee7b7b6..0f655364 100644 --- a/src/Lib/Project/BehaviorPack/Block/Block.ts +++ b/src/Lib/Project/BehaviorPack/Block/Block.ts @@ -1,6 +1,7 @@ import { Types } from "bc-minecraft-bedrock-types"; import { Molang } from "bc-minecraft-molang"; import { MolangCarrier } from "../../../Types"; +import { BlockState } from "./BlockState"; /** */ export interface Block extends Types.BaseObject, MolangCarrier { @@ -9,66 +10,3 @@ export interface Block extends Types.BaseObject, MolangCarrier /** */ states: BlockState[]; } - -/** */ -export interface BlockState { - /** */ - name: string; - /** */ - type: "byte" | "int" | "string"; - /** */ - values: string[]; -} - -/** */ -export namespace BlockState { - /** - * - * @param value - * @returns - */ - export function is(value: any): value is BlockState { - if (value && typeof value.name === "string" && typeof value.type === "string") { - if (value.type === "byte" || value.type === "int" || value.type === "string") { - if (Array.isArray(value.values)) return true; - } - } - - return false; - } - - /** - * - * @param name - * @param values - * @returns - */ - export function create(name: string, values: string[] | number[] | boolean[]): BlockState | undefined { - const out: BlockState = { - name: name, - type: "string", - values: [], - }; - - const f = typeof values[0]; - - switch (f) { - case "boolean": - out.type = "byte"; - out.values = values.map((x) => (x === true ? "1" : "0")); - break; - - case "string": - out.type = "string"; - out.values = values.map((x) => x.toString()); - break; - - case "number": - out.type = "int"; - out.values = values.map((x) => x.toString()); - break; - } - - return out; - } -} diff --git a/src/Lib/Project/BehaviorPack/Block/BlockState.ts b/src/Lib/Project/BehaviorPack/Block/BlockState.ts new file mode 100644 index 00000000..111ad77b --- /dev/null +++ b/src/Lib/Project/BehaviorPack/Block/BlockState.ts @@ -0,0 +1,65 @@ +/**The block state description*/ +interface BaseBlockState { + /**The name of the block state*/ + name: string; + /**The type of the block state*/ + type: T; + /**The possible values of the block state*/ + values: U[]; +} + +export type BlockStateInt = BaseBlockState<"int", number>; +export type BlockStateBool = BaseBlockState<"bool", boolean>; +export type BlockStateString = BaseBlockState<"string", string>; +export type BlockState = BlockStateInt | BlockStateBool | BlockStateString; + +/** */ +export namespace BlockState { + /** + * + * @param value + * @returns + */ + export function is(value: any): value is BlockState { + if (value && typeof value.name === "string" && typeof value.type === "string") { + if (typeof value.type === "string") { + if (Array.isArray(value.values)) return true; + } + } + + return false; + } + + /** + * + * @param name + * @param values + * @returns + */ + export function create(name: string, values: string[] | number[] | boolean[]): BlockState | undefined { + const f = typeof values[0]; + + switch (f) { + case "boolean": + return { + name: name, + type: "bool", + values: values as boolean[], + }; + + default: + case "string": + return { + name: name, + type: "string", + values: values as string[], + }; + case "number": + return { + name: name, + type: "int", + values: values as number[], + }; + } + } +} diff --git a/src/Lib/Project/BehaviorPack/Block/index.ts b/src/Lib/Project/BehaviorPack/Block/index.ts index b208db44..0a1cdd92 100644 --- a/src/Lib/Project/BehaviorPack/Block/index.ts +++ b/src/Lib/Project/BehaviorPack/Block/index.ts @@ -1,4 +1,5 @@ /* Auto generated */ export * from "./Block"; +export * from "./BlockState"; export * from "./Process"; \ No newline at end of file