Skip to content

Commit

Permalink
docs: Update exports / Add main doc blocks (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonjgardner authored Jul 15, 2024
1 parent f016a1a commit 8b55bcb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export { default as img2schematic } from "./src/schematic/mod.ts";
export { default as img2mcstructure } from "./src/mcstructure/mod.ts";
export { default as img2nbt } from "./src/nbt/mod.ts";
export { default as vox2mcstructure, vox2gif } from "./src/vox/mod.ts";
export { default as img2mcaddon } from "./src/mcaddon/mod.ts"
export { default as img2mcfunction } from "./src/mcfunction/mod.ts";
15 changes: 13 additions & 2 deletions src/mcfunction/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ import { getNearestColor } from "../_lib.ts";
import decode from "../_decode.ts";
import createPalette from "../_palette.ts";

export default async function createFunction(
blocks: PaletteSource,
/**
* Convert an image to a series of `setblock` commands.
* @param imgSrc Source image
* @param blocks Block palette database
* @param offset Coordinate offset to apply to the `setblock` function
* @returns mcfunction lines
*/
export default async function img2mcfunction(
imgSrc: string,
blocks: PaletteSource,
offset: [number, number, number] = [0, 0, 0],
) {
const frames = await decode(imgSrc);
Expand All @@ -20,6 +27,10 @@ export default async function createFunction(
for (const [x, y, c] of img.iterateWithColors()) {
const [r, g, b, a] = imagescript.Image.colorToRGBA(c);

if (a < 128) {
continue;
}

const nearest = getNearestColor([r, g, b], createPalette(blocks));
lines.push(
`setblock ${x + offset[0]} ${Math.abs(img.height - y + offset[1])} ${
Expand Down
7 changes: 7 additions & 0 deletions src/mcstructure/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ export async function createMcStructure(
});
}

/**
* Convert an image to a Minecraft Bedrock structure file.
* @param imgSrc Image source
* @param db Block palette
* @param axis Axis on which to stack frames
* @returns .mcstructure data
*/
export default async function img2mcstructure(
imgSrc: string,
db: IBlock[] = [],
Expand Down
13 changes: 13 additions & 0 deletions src/nbt/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ export interface IPaletteEntry {

export type StructurePalette = IPaletteEntry[];

/**
* NBT format
*/
export interface INbtBlock {
/**
* Block position (X, Y, Z)
*/
pos: [number, number, number];
state: number;
}
Expand Down Expand Up @@ -151,6 +157,13 @@ export async function createNbtStructure(
});
}

/**
* Create a NBT structure from an image.
* @param imgSrc Image source
* @param db Block palette
* @param axis Axis orientation
* @returns NBT structure data
*/
export default async function img2nbt(
imgSrc: string,
db: IBlock[] = [],
Expand Down
12 changes: 11 additions & 1 deletion src/schematic/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Axis, IBlock } from "../types.ts";
import * as nbt from "nbtify";
import * as imagescript from "imagescript";
import { DEFAULT_BLOCK, MASK_BLOCK } from "../_constants.ts";
import { compareStates, getNearestColor } from "../_lib.ts";
import { getNearestColor } from "../_lib.ts";
import decode from "../_decode.ts";

export type PaletteBlock = string;
Expand All @@ -14,6 +14,9 @@ export interface ISchemaBlock {
state: number;
}

/**
* Schematic NBT format
*/
export interface ISchematicTag {
x: number;
y: number;
Expand Down Expand Up @@ -140,6 +143,13 @@ export async function createSchematic(
});
}

/**
* Convert an image to a Minecraft schematic file.
* @param imgSrc Source image to convert
* @param db Block palette to use
* @param axis Axis on which to stack frames
* @returns NBT schematic data
*/
export default async function img2schematic(
imgSrc: string,
db: IBlock[] = [],
Expand Down

0 comments on commit 8b55bcb

Please sign in to comment.