refactor: rename functions and change imports in utils

This commit is contained in:
trafficlunar 2025-01-31 14:16:47 +00:00
parent 59d69152dc
commit b9e1307ace
7 changed files with 17 additions and 17 deletions

View file

@ -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);

View file

@ -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() {
<XIcon />
</Button>
<span className="mx-2 text-[0.85rem]">Confirm selection?</span>
<Button variant="ghost" className="w-8 h-8" onClick={() => confirmSelection(blocks, layerBlocks, setBlocks, setLayerBlocks)}>
<Button variant="ghost" className="w-8 h-8" onClick={() => selection.confirm(blocks, layerBlocks, setBlocks, setLayerBlocks)}>
<CheckIcon />
</Button>
</div>

View file

@ -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;

View file

@ -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);
});

View file

@ -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

View file

@ -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<React.SetStateAction<Block[]>>,

View file

@ -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;