diff --git a/src/components/canvas/Canvas.tsx b/src/components/canvas/Canvas.tsx index 4f397d5..c81b9a1 100644 --- a/src/components/canvas/Canvas.tsx +++ b/src/components/canvas/Canvas.tsx @@ -13,7 +13,7 @@ import { ToolContext } from "@/context/Tool"; import { useTextures } from "@/hooks/useTextures"; import { useBlockData } from "@/hooks/useBlockData"; -import { confirmSelection, isInSelection } from "@/utils/selection"; +import * as selection from "@/utils/selection"; import * as clipboard from "@/utils/clipboard"; import Blocks from "./Blocks"; @@ -116,7 +116,7 @@ function Canvas() { const updated = blocks.filter((block) => { const withinRadius = block.x >= radiusPosition.x && block.x < radiusPosition.x + radius && block.y >= radiusPosition.y && block.y < radiusPosition.y + radius; - return !withinRadius || !isInSelection(selectionCoords, block.x, block.y); + return !withinRadius || !selection.isIn(selectionCoords, block.x, block.y); }); setBlocks(updated); @@ -142,7 +142,7 @@ function Canvas() { setBlocks((prev) => prev.filter((b) => { - const isSelected = isInSelection(selectionCoords, b.x, b.y); + const isSelected = selection.isIn(selectionCoords, b.x, b.y); // Add blocks in the selection coords to the selection layer if (isSelected) result.push(b); @@ -197,7 +197,7 @@ function Canvas() { const tileY = radiusPosition.y + y; // Only add blocks within the selection - if (isInSelection(selectionCoords, tileX, tileY)) { + if (selection.isIn(selectionCoords, tileX, tileY)) { radiusBlocks.push({ name: selectedBlock, x: tileX, @@ -414,7 +414,7 @@ function Canvas() { setSelectionLayerBlocks([]); break; case "Enter": - confirmSelection(blocks, selectionLayerBlocks, setBlocks, setSelectionLayerBlocks); + selection.confirm(blocks, selectionLayerBlocks, setBlocks, setSelectionLayerBlocks); break; case " ": // Space setDragging(true); diff --git a/src/components/canvas/SelectionBar.tsx b/src/components/canvas/SelectionBar.tsx index b421f57..5a60c1c 100644 --- a/src/components/canvas/SelectionBar.tsx +++ b/src/components/canvas/SelectionBar.tsx @@ -4,7 +4,7 @@ import { CheckIcon, XIcon } from "lucide-react"; import { CanvasContext } from "@/context/Canvas"; import { SelectionContext } from "@/context/Selection"; -import { confirmSelection } from "@/utils/selection"; +import * as selection from "@/utils/selection"; import { Button } from "@/components/ui/button"; @@ -29,7 +29,7 @@ function SelectionBar() { Confirm selection? - diff --git a/src/components/dialogs/OpenSchematic.tsx b/src/components/dialogs/OpenSchematic.tsx index a27fe03..53b91a3 100644 --- a/src/components/dialogs/OpenSchematic.tsx +++ b/src/components/dialogs/OpenSchematic.tsx @@ -7,7 +7,7 @@ import * as nbt from "nbtify"; import { CanvasContext } from "@/context/Canvas"; import { LoadingContext } from "@/context/Loading"; -import { decodeVarint } from "@/utils/varint"; +import * as varint from "@/utils/varint"; import { DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; @@ -158,7 +158,7 @@ function OpenSchematic({ close }: DialogProps) { for (let y = spongeData.Height; y > 0; y--) { for (let x = 0; x < spongeData.Width; x++) { // Decode varint to get the palette value - const { value: paletteValue, bytesRead } = decodeVarint(spongeData.Blocks.Data, offset); + const { value: paletteValue, bytesRead } = varint.decode(spongeData.Blocks.Data, offset); const paletteBlock = Object.keys(spongeData.Blocks.Palette).find((key) => spongeData.Blocks.Palette[key] == paletteValue); offset += bytesRead; diff --git a/src/components/dialogs/SaveSchem.tsx b/src/components/dialogs/SaveSchem.tsx index 6c7de57..5f00e4e 100644 --- a/src/components/dialogs/SaveSchem.tsx +++ b/src/components/dialogs/SaveSchem.tsx @@ -4,7 +4,7 @@ import * as nbt from "nbtify"; import { CanvasContext } from "@/context/Canvas"; import { LoadingContext } from "@/context/Loading"; -import { encodeVarint } from "@/utils/varint"; +import * as varint from "@/utils/varint"; import { DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; @@ -120,7 +120,7 @@ function SaveSchem({ close, registerSubmit, dialogKeyHandler }: DialogProps) { const blockId = blockPalette[`minecraft:${blockName}${properties}`]; // Parse blockId to number then encode as varint - const id = encodeVarint(parseInt(blockId.toString())); + const id = varint.encode(parseInt(blockId.toString())); // Push to separate array to make array buffer ids.push(...id); }); diff --git a/src/components/tool-settings/Replace.tsx b/src/components/tool-settings/Replace.tsx index 1b25001..4ed48fb 100644 --- a/src/components/tool-settings/Replace.tsx +++ b/src/components/tool-settings/Replace.tsx @@ -10,7 +10,7 @@ import { Label } from "@/components/ui/label"; import { Button } from "@/components/ui/button"; import { useTextures } from "@/hooks/useTextures"; -import { isInSelection } from "@/utils/selection"; +import * as selection from "@/utils/selection"; function Replace() { const { version, setBlocks } = useContext(CanvasContext); @@ -36,7 +36,7 @@ function Replace() { setBlocks((prev) => prev .map((block) => { - if (isInSelection(selectionCoords, block.x, block.y)) { + if (selection.isIn(selectionCoords, block.x, block.y)) { if (block.name === block1) { // If block2 is air, return null // If not, change the block name diff --git a/src/utils/selection.ts b/src/utils/selection.ts index f99732e..38029a6 100644 --- a/src/utils/selection.ts +++ b/src/utils/selection.ts @@ -1,12 +1,12 @@ // Check if a block is within the selection -export function isInSelection(selection: CoordinateArray, x: number, y: number): boolean { +export function isIn(selection: CoordinateArray, x: number, y: number): boolean { if (selection.length !== 0) { return selection.some(([x2, y2]) => x2 === x && y2 === y); } return true; } -export function confirmSelection( +export function confirm( blocks: Block[], layerBlocks: Block[], setBlocks: React.Dispatch>, diff --git a/src/utils/varint.ts b/src/utils/varint.ts index 1491572..a4007e9 100644 --- a/src/utils/varint.ts +++ b/src/utils/varint.ts @@ -1,4 +1,4 @@ -export function encodeVarint(number: number): Uint8Array { +export function encode(number: number): Uint8Array { const result = []; while (number >= 0x80) { // Take 7 bits and set the MSB @@ -13,7 +13,7 @@ export function encodeVarint(number: number): Uint8Array { return new Uint8Array(result); } -export function decodeVarint(buffer: Uint8Array, offset: number): { value: number; bytesRead: number } { +export function decode(buffer: Uint8Array, offset: number): { value: number; bytesRead: number } { let value = 0; let position = 0; let byte: number;