From 384584e5685c7bc61cecd76f464a18ffe8876eeb Mon Sep 17 00:00:00 2001 From: Roujel Williams Date: Sat, 23 Nov 2024 11:24:19 -0500 Subject: [PATCH] Improved gradient command --- src/server/commands/generation/gradient.ts | 125 +++++++++++++-------- texts/en_US.po | 6 + 2 files changed, 85 insertions(+), 46 deletions(-) diff --git a/src/server/commands/generation/gradient.ts b/src/server/commands/generation/gradient.ts index ff9c08461..6eced385d 100644 --- a/src/server/commands/generation/gradient.ts +++ b/src/server/commands/generation/gradient.ts @@ -1,8 +1,7 @@ -import { RawText, regionSize } from "@notbeer-api"; +import { axis, RawText, regionSize } from "@notbeer-api"; import { registerCommand } from "../register_commands.js"; import { assertCuboidSelection } from "@modules/assert.js"; import { Pattern } from "@modules/pattern.js"; -import { Vector3 } from "@minecraft/server"; const registerInformation = { name: "gradient", @@ -10,62 +9,96 @@ const registerInformation = { description: "commands.wedit:gradient.description", usage: [ { - flag: "s", + subName: "create", + args: [ + { + flag: "s", + }, + { + flag: "f", + name: "fade", + type: "float", + range: [0, 1] as [number, number], + }, + { + name: "id", + type: "string", + }, + { + name: "patterns", + type: "Pattern...", + default: [new Pattern("stone")], + }, + ], }, { - flag: "f", - name: "fade", - type: "float", - range: [0, 1] as [number, number], + subName: "delete", + args: [ + { + name: "id", + type: "string", + }, + ], }, { - name: "id", - type: "string", - }, - { - name: "patterns", - type: "Pattern...", - default: [new Pattern("stone")], + subName: "list", }, ], }; +function truncateStringFromMiddle(str: string, length: number) { + if (str.length <= length) return str; + const halfLength = Math.floor((length - 3) / 2); + const start = str.slice(0, halfLength); + const end = str.slice(str.length - halfLength); + return start + "..." + end; +} + registerCommand(registerInformation, function (session, builder, args) { - const patterns = []; - if (args.has("s")) { - assertCuboidSelection(session); - const [min, max] = session.selection.getRange(); - const size = regionSize(min, max); - const dim = builder.dimension; - type axis = "x" | "y" | "z"; - let s: axis, t: axis, u: axis; - if (size.x > size.y && size.x > size.z) { - s = "y"; - t = "z"; - u = "x"; - } else if (size.z > size.x && size.z > size.y) { - s = "x"; - t = "y"; - u = "z"; - } else { - s = "x"; - t = "z"; - u = "y"; - } + if (args.has("create")) { + const patterns = []; + if (args.has("s")) { + assertCuboidSelection(session); + const [min, max] = session.selection.getRange(); + const size = regionSize(min, max); + const dim = builder.dimension; + let s: axis, t: axis, u: axis; + if (size.x > size.y && size.x > size.z) (s = "y"), (t = "z"), (u = "x"); + else if (size.z > size.x && size.z > size.y) (s = "x"), (t = "y"), (u = "z"); + else (s = "x"), (t = "z"), (u = "y"); - for (let i = min[u]; i <= max[u]; i++) { - const pattern = new Pattern(); - for (let j = min[s]; j <= max[s]; j++) { - for (let k = min[t]; k <= max[t]; k++) { - pattern.addBlock(dim.getBlock({ [s]: j, [t]: k, [u]: i } as unknown as Vector3).permutation); + for (let i = min[u]; i <= max[u]; i++) { + const pattern = new Pattern(); + for (let j = min[s]; j <= max[s]; j++) { + for (let k = min[t]; k <= max[t]; k++) { + pattern.addBlock(dim.getBlock({ [s]: j, [t]: k, [u]: i }).permutation); + } } + patterns.push(pattern); } - patterns.push(pattern); + } else { + patterns.push(...args.get("patterns")); } - } else { - patterns.push(...args.get("patterns")); - } - session.createGradient(args.get("id"), args.get("f-fade") ?? 1.0, patterns); - return RawText.translate("commands.wedit:gradient.create").with(args.get("id")); + session.createGradient(args.get("id"), args.get("f-fade") ?? 1.0, patterns); + return RawText.translate("commands.wedit:gradient.create").with(args.get("id")); + } else if (args.has("delete")) { + if (session.getGradientNames().includes(args.get("id"))) { + session.deleteGradient(args.get("id")); + return RawText.translate("commands.wedit:gradient.delete").with(args.get("id")); + } else { + throw RawText.translate("commands.wedit:gradient.noExist").with(args.get("id")); + } + } else if (args.has("list")) { + const gradients = session.getGradientNames(); + if (gradients.length) + return gradients + .map((id) => { + const patterns = session.getGradient(id).patterns; + const patternStr = patterns.map((p) => `"${p.toJSON()}"`).join(", "); + return `- ${id} (${truncateStringFromMiddle(patternStr, 100)})`; + }) + .join("\n"); + else "commands.wedit:gradient.none"; + } }); diff --git a/texts/en_US.po b/texts/en_US.po index 6aa67c1d3..dab1f6735 100644 --- a/texts/en_US.po +++ b/texts/en_US.po @@ -541,8 +541,14 @@ msgid "commands.wedit:gmask.set" msgstr "Global mask set." msgid "commands.wedit:gradient.create" msgstr "Gradient %s created." +msgid "commands.wedit:gradient.delete" +msgstr "Gradient %s deleted." msgid "commands.wedit:gradient.description" msgstr "Create a gradient to use in patterns." +msgid "commands.wedit:gradient.noExist" +msgstr "Gradient %s does not exist." +msgid "commands.wedit:gradient.none" +msgstr "There are no gradients to list" msgid "commands.wedit:green.description" msgstr "Turn nearby dirt into grass" msgid "commands.wedit:hcyl.description"