|
| 1 | +import { assertSelection } from "@modules/assert.js"; |
| 2 | +import { Pattern } from "@modules/pattern.js"; |
| 3 | +import { CommandInfo, RawText, regionBounds } from "@notbeer-api"; |
| 4 | +import { registerCommand } from "../register_commands.js"; |
| 5 | +import { Jobs } from "@modules/jobs.js"; |
| 6 | +import { balloonPath, plotCurve } from "./paths_func.js"; |
| 7 | +import config from "config.js"; |
| 8 | + |
| 9 | +const registerInformation: CommandInfo = { |
| 10 | + name: "curve", |
| 11 | + permission: "worldedit.region.curve", |
| 12 | + description: "commands.wedit:curve.description", |
| 13 | + usage: [ |
| 14 | + { name: "pattern", type: "Pattern" }, |
| 15 | + { name: "thickness", type: "int", range: [0, config.maxBrushRadius], default: 0 }, |
| 16 | + ], |
| 17 | +}; |
| 18 | + |
| 19 | +registerCommand(registerInformation, function* (session, builder, args) { |
| 20 | + assertSelection(session); |
| 21 | + if (session.selection.mode != "convex") throw "commands.wedit:curve.invalidType"; |
| 22 | + if (args.get("_using_item") && session.globalPattern.empty()) throw "worldEdit.selectionFill.noPattern"; |
| 23 | + const thickness = <number>args.get("thickness"); |
| 24 | + |
| 25 | + const points = session.selection.points; |
| 26 | + const [start, end] = session.selection.getRange(); |
| 27 | + |
| 28 | + const dim = builder.dimension; |
| 29 | + const pattern = (<Pattern>(args.get("_using_item") ? session.globalPattern : args.get("pattern"))).withContext(session, [start, end]); |
| 30 | + const mask = session.globalMask.withContext(session); |
| 31 | + let count: number; |
| 32 | + |
| 33 | + yield* Jobs.run(session, 1, function* () { |
| 34 | + const history = session.history; |
| 35 | + const record = history.record(); |
| 36 | + try { |
| 37 | + const blocks = yield* balloonPath(plotCurve(points), thickness); |
| 38 | + const [start, end] = regionBounds(blocks); |
| 39 | + |
| 40 | + yield* history.trackRegion(record, start, end); |
| 41 | + count = 0; |
| 42 | + for (const point of blocks) { |
| 43 | + const block = dim.getBlock(point) ?? (yield* Jobs.loadBlock(point)); |
| 44 | + if (mask.matchesBlock(block) && pattern.setBlock(block)) count++; |
| 45 | + yield; |
| 46 | + } |
| 47 | + |
| 48 | + history.trackSelection(record); |
| 49 | + yield* history.commit(record); |
| 50 | + } catch (e) { |
| 51 | + history.cancel(record); |
| 52 | + throw e; |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + return RawText.translate("commands.blocks.wedit:changed").with(`${count}`); |
| 57 | +}); |
0 commit comments