Skip to content

Commit

Permalink
Added ;revolve command
Browse files Browse the repository at this point in the history
  • Loading branch information
SIsilicon committed Nov 3, 2024
1 parent 40c8c70 commit 24b5319
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/server/commands/command_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import "./region/set.js";
import "./region/replace.js";
import "./region/move.js";
import "./region/stack.js";
import "./region/revolve.js";
import "./region/rotate.js";
import "./region/flip.js";
import "./region/wall.js";
Expand Down
110 changes: 110 additions & 0 deletions src/server/commands/region/revolve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { assertCuboidSelection } from "@modules/assert.js";
import { Jobs } from "@modules/jobs.js";
import { RawText, regionBounds, regionOffset, Vector } from "@notbeer-api";
import { registerCommand } from "../register_commands.js";
import { copy } from "../clipboard/copy.js";
import { RegionBuffer } from "@modules/region_buffer.js";
import { Cardinal } from "@modules/directions.js";

const registerInformation = {
name: "revolve",
permission: "worldedit.region.revolve",
description: "commands.wedit:revolve.description",
usage: [
{
flag: "a",
},
{
flag: "s",
},
{
name: "count",
type: "int",
range: [2, null] as [number, null],
},
{
name: "start",
type: "float",
default: 0,
},
{
name: "end",
type: "float",
default: 360,
},
{
name: "heightDiff",
type: "int",
default: 0,
},
{
flag: "d",
name: "direction",
type: "Direction",
},
{
flag: "m",
name: "mask",
type: "Mask",
},
],
};

registerCommand(registerInformation, function* (session, builder, args) {
assertCuboidSelection(session);
const amount = args.get("count");
const dir = (<Cardinal>args.get("d-direction"))?.getDirection(builder) ?? Vector.UP;
const [start, end] = session.selection.getRange();
const heightDiff = args.get("heightDiff");
const [startRotation, endRotation] = [args.get("start"), args.get("end")];
const origin = Vector.from(builder.location).floor().add(0.5);
const offset = start.sub(origin);
const dim = builder.dimension;

// [load position, rotation angles]
const loads: [Vector, Vector][] = [];
const points: Vector[] = [];
const [originStart, originEnd] = regionOffset(start, end, offset.mul(-1));
for (let i = 0; i <= amount; i++) {
const t = i / amount;
const rotation = dir.mul((1 - t) * startRotation + t * endRotation);
const height = dir.mul(Math.round(t * heightDiff));
const [loadStart, loadEnd] = RegionBuffer.createBounds(originStart, originEnd, { offset: offset.add(height), rotation });
loads.push([origin.add(height), rotation]);
points.push(loadStart, loadEnd);
}
const revolveRegion = regionBounds(points);

let count = 0;
const history = session.getHistory();
const record = history.record();
const tempRevolve = session.createRegion(true);

yield* Jobs.run(session, loads.length + 1, function* () {
try {
yield* copy(session, args, tempRevolve);
yield history.addUndoStructure(record, ...revolveRegion, "any");
for (const [loadPosition, rotation] of loads) {
yield Jobs.nextStep("Pasting blocks...");
yield* tempRevolve.load(loadPosition, dim, { rotation, offset });
count += tempRevolve.getBlockCount();
}
yield history.addRedoStructure(record, ...revolveRegion, "any");

if (args.has("s")) {
history.recordSelection(record, session);
session.selection.set(0, points[points.length - 2]);
session.selection.set(1, points[points.length - 1]);
history.recordSelection(record, session);
}

history.commit(record);
} catch (e) {
history.cancel(record);
throw e;
} finally {
session.deleteRegion(tempRevolve);
}
});
return RawText.translate("commands.wedit:revolve.explain").with(count);
});
4 changes: 4 additions & 0 deletions texts/en_US.po
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,10 @@ msgid "commands.wedit:removenear.description"
msgstr "Remove nearby blocks"
msgid "commands.wedit:replacenear.description"
msgstr "Replace nearby blocks with other blocks"
msgid "commands.wedit:revolve.description"
msgstr "Make copies of the selection revolving around the player"
msgid "commands.wedit:revolve.explain"
msgstr "Created %s blocks from revolving."
msgid "commands.wedit:rotate.description"
msgstr "Rotate the selection"
msgid "commands.wedit:rotate.explain"
Expand Down

0 comments on commit 24b5319

Please sign in to comment.