Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sisilicon
GitHub Repository: sisilicon/worldedit-be
Path: blob/master/src/editor/modules/history.ts
1784 views
1
import { History, setHistoryClass } from "@modules/history";
2
import { EditorModule } from "./base";
3
import { Player, Vector3 } from "@minecraft/server";
4
import { VectorSet, Thread, getCurrentThread } from "@notbeer-api";
5
import { IPlayerUISession, TransactionManager } from "@minecraft/server-editor";
6
7
const transactionManagers = new WeakMap<Player, TransactionManager>();
8
9
class EditorHistory extends History {
10
private activeThread?: Thread;
11
12
record() {
13
if (!this.transactionManager.openTransaction("WorldEdit operation")) this.assertNotRecording();
14
this.activeThread = getCurrentThread();
15
return 0;
16
}
17
18
*commit(): Generator<any, void> {
19
yield;
20
try {
21
this.transactionManager.commitOpenTransaction();
22
} catch {
23
/* pass */
24
}
25
this.activeThread = undefined;
26
return;
27
}
28
29
cancel() {
30
this.transactionManager.discardOpenTransaction();
31
this.activeThread = undefined;
32
}
33
34
*trackRegion(_: number, start: Vector3 | Vector3[] | VectorSet, end?: Vector3): Generator<any, void> {
35
yield;
36
if ("x" in start) this.transactionManager.trackBlockChangeArea(start, end);
37
else this.transactionManager.trackBlockChangeList(Array.from(start));
38
return;
39
}
40
41
trackSelection(): void {
42
console.error("Selection tracking is not implemented yet.");
43
}
44
45
*undo(): Generator<any, boolean> {
46
yield;
47
if (!this.transactionManager.undoSize()) return true;
48
this.transactionManager.undo();
49
return false;
50
}
51
52
*redo(): Generator<any, boolean> {
53
yield;
54
if (!this.transactionManager.redoSize()) return true;
55
this.transactionManager.redo();
56
return false;
57
}
58
59
clear() {
60
console.error("History clear is not implemented.");
61
}
62
63
isRecording(): boolean {
64
const madeTransaction = this.transactionManager.openTransaction("Testing WorldEdit history recording");
65
if (madeTransaction) this.transactionManager.discardOpenTransaction();
66
return !madeTransaction;
67
}
68
69
getActivePointsInThread(thread: Thread): number[] {
70
return thread === this.activeThread ? [0] : [];
71
}
72
73
private get transactionManager(): TransactionManager {
74
return transactionManagers.get(this.player)!;
75
}
76
}
77
setHistoryClass(EditorHistory);
78
79
export class HistoryModule extends EditorModule {
80
constructor(session: IPlayerUISession) {
81
super(session);
82
const transactionManager = this.session.extensionContext.transactionManager;
83
transactionManagers.set(this.player, transactionManager);
84
transactionManager.discardOpenTransaction();
85
}
86
87
teardown() {
88
transactionManagers.delete(this.player);
89
}
90
}
91
92