Skip to content

Commit

Permalink
Redid block
Browse files Browse the repository at this point in the history
  • Loading branch information
DaanV2 committed Apr 27, 2023
1 parent 34650aa commit 9bc18f8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 63 deletions.
64 changes: 1 addition & 63 deletions src/Lib/Project/BehaviorPack/Block/Block.ts
Original file line number Diff line number Diff line change
@@ -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<Molang.MolangSet> {
Expand All @@ -9,66 +10,3 @@ export interface Block extends Types.BaseObject, MolangCarrier<Molang.MolangSet>
/** */
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;
}
}
65 changes: 65 additions & 0 deletions src/Lib/Project/BehaviorPack/Block/BlockState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**The block state description*/
interface BaseBlockState<T extends string, U> {
/**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[],
};
}
}
}
1 change: 1 addition & 0 deletions src/Lib/Project/BehaviorPack/Block/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Auto generated */

export * from "./Block";
export * from "./BlockState";
export * from "./Process";

0 comments on commit 9bc18f8

Please sign in to comment.