Skip to content

Commit

Permalink
rotate and flip work on clipboard instead of selection
Browse files Browse the repository at this point in the history
  • Loading branch information
SIsilicon committed Jun 30, 2024
1 parent e6c86f1 commit e0d2db0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 45 deletions.
2 changes: 0 additions & 2 deletions src/server/commands/misc/kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ registerCommand(registerInformation, function (session, builder) {
"wedit:rotate_ccw_button",
"wedit:mask_picker",
"wedit:draw_line",
"wedit:selection_wall",
"wedit:selection_outline",
"wedit:spawn_glass",
];

Expand Down
96 changes: 53 additions & 43 deletions src/server/tools/button_tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { Tools } from "./tool_manager.js";
import { PlayerUtil } from "@modules/player_util.js";
import { Selection } from "@modules/selection.js";

interface PreviewPaste {
outlines: Map<PlayerSession, Selection>;
tick: typeof previewPaste;
}

abstract class CommandButton extends Tool {
abstract readonly command: string | string[];

Expand All @@ -31,41 +36,17 @@ class CopyTool extends CommandButton {
}
Tools.register(CopyTool, "copy", "wedit:copy_button");

class PasteTool extends CommandButton {
class PasteTool extends CommandButton implements PreviewPaste {
command = ["paste", "-s"];
permission = "worldedit.clipboard.paste";

outlines = new Map<PlayerSession, Selection>();

use = function (self: CommandButton, player: Player, session: PlayerSession) {
Server.command.callCommand(player, self.command[0], self.command.slice(1) as string[]);
};

tick = function* (self: PasteTool, player: Player, session: PlayerSession, tick: number): Generator<void> {
if (!session.clipboard || !session.drawOutlines) {
return;
}

if (!self.outlines.has(session)) {
const selection = new Selection(player);
selection.mode = "cuboid";
self.outlines.set(session, selection);
}
const rotation = session.clipboardTransform.rotation;
const flip = session.clipboardTransform.flip;
const bounds = regionTransformedBounds(Vector.ZERO.floor(), session.clipboard.getSize().offset(-1, -1, -1), Vector.ZERO, rotation, flip);
const size = Vector.from(regionSize(bounds[0], bounds[1]));

const loc = PlayerUtil.getBlockLocation(player);
const pasteStart = Vector.add(loc, session.clipboardTransform.relative).sub(size.mul(0.5).sub(1));
const pasteEnd = pasteStart.add(Vector.sub(size, Vector.ONE)).floor();

const selection = self.outlines.get(session);
selection.set(0, pasteStart.floor());
selection.set(1, pasteEnd);
selection.draw();
yield;
};
outlines = new Map();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
tick = <any>previewPaste;
}
Tools.register(PasteTool, "paste", "wedit:paste_button");

Expand All @@ -81,42 +62,48 @@ class RedoTool extends CommandButton {
}
Tools.register(RedoTool, "redo", "wedit:redo_button");

class RotateCWTool extends Tool {
class RotateCWTool extends Tool implements PreviewPaste {
permission = "worldedit.region.rotate";

use = function (self: Tool, player: Player, session: PlayerSession) {
const args = ["90", "-sw"];
if (player.isSneaking) {
args.push("-o");
}
const args = ["90"];
if (player.isSneaking) args.push("-o");
Server.command.callCommand(player, "rotate", args);
};

outlines = new Map();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
tick = <any>previewPaste;
}
Tools.register(RotateCWTool, "rotate_cw", "wedit:rotate_cw_button");

class RotateCCWTool extends Tool {
class RotateCCWTool extends Tool implements PreviewPaste {
permission = "worldedit.region.rotate";

use = function (self: Tool, player: Player, session: PlayerSession) {
const args = ["-90", "-sw"];
if (player.isSneaking) {
args.push("-o");
}
const args = ["-90"];
if (player.isSneaking) args.push("-o");
Server.command.callCommand(player, "rotate", args);
};

outlines = new Map();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
tick = <any>previewPaste;
}
Tools.register(RotateCCWTool, "rotate_ccw", "wedit:rotate_ccw_button");

class FlipTool extends Tool {
class FlipTool extends Tool implements PreviewPaste {
permission = "worldedit.region.flip";

use = function (self: Tool, player: Player, session: PlayerSession) {
const args = ["-sw"];
if (player.isSneaking) {
args.push("-o");
}
const args = [];
if (player.isSneaking) args.push("-o");
Server.command.callCommand(player, "flip", args);
};

outlines = new Map();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
tick = <any>previewPaste;
}
Tools.register(FlipTool, "flip", "wedit:flip_button");

Expand All @@ -133,3 +120,26 @@ class ConfigTool extends Tool {
};
}
Tools.register(ConfigTool, "config", "wedit:config_button");

function* previewPaste(self: PreviewPaste, player: Player, session: PlayerSession): Generator<void> {
if (!session.clipboard || !session.drawOutlines) return;

if (!self.outlines.has(session)) {
const selection = new Selection(player);
self.outlines.set(session, selection);
}
const rotation = session.clipboardTransform.rotation;
const flip = session.clipboardTransform.flip;
const bounds = regionTransformedBounds(Vector.ZERO.floor(), session.clipboard.getSize().offset(-1, -1, -1), Vector.ZERO, rotation, flip);
const size = Vector.from(regionSize(bounds[0], bounds[1]));

const loc = PlayerUtil.getBlockLocation(player);
const pasteStart = Vector.add(loc, session.clipboardTransform.relative).sub(size.mul(0.5).sub(1));
const pasteEnd = pasteStart.add(Vector.sub(size, Vector.ONE)).floor();

const selection = self.outlines.get(session)!;
selection.set(0, pasteStart.floor());
selection.set(1, pasteEnd);
selection.draw();
yield;
}

0 comments on commit e0d2db0

Please sign in to comment.