Skip to content

Commit 2fe6eaa

Browse files
committed
add datapack change methods
1 parent 2b35be5 commit 2fe6eaa

File tree

2 files changed

+72
-11
lines changed

2 files changed

+72
-11
lines changed

src/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import JSZip from "jszip";
22
import type { Datapack } from "./datapack";
3-
import { DatapackModifierInstance } from "./datapack_changes";
3+
import { DatapackModifierInstance, type DatapackChangeMethod } from "./datapack_changes";
44

55
type TextWidget = {
66
type: "title" | "heading" | "text";
@@ -388,7 +388,7 @@ async function dostuff(datapack: Datapack, method: Method) {
388388
JSON.parse(content);
389389

390390
DatapackModifierInstance.queueChange(
391-
datapack, file_name, accessor.value_path, final_value
391+
datapack, file_name, accessor.value_path, final_value, accessor.method as DatapackChangeMethod
392392
);
393393
});
394394

src/datapack_changes.ts

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,25 @@ interface DatapackChange {
44
datapack: Datapack;
55
file_path: string;
66
value_path: string;
7-
new_value: string | number | boolean;
7+
value: DatapackChangeValue;
8+
application_method: DatapackChangeMethod;
89
}
910

11+
export type DatapackChangeValue = string | number | boolean;
12+
13+
export type DatapackChangeMethod =
14+
"multiply"
15+
| "divide"
16+
| "add"
17+
| "subtract"
18+
| "set"
19+
| "multiply_int"
20+
| "divide_int"
21+
| "add_int"
22+
| "subtract_int"
23+
| "remove"
24+
| "pop";
25+
1026
export class DatapackModifier {
1127
private static instance: DatapackModifier;
1228
private changeQueue: Array<DatapackChange>;
@@ -19,14 +35,20 @@ export class DatapackModifier {
1935
this.changeQueue = [];
2036
}
2137

22-
public queueChange(datapack: Datapack, file_path: string, value_path: string, value: number | string | boolean) {
23-
const change: DatapackChange = {
24-
datapack: datapack,
25-
file_path: file_path,
26-
value_path: value_path,
27-
new_value: value
28-
};
29-
this.changeQueue.push(change);
38+
public queueChange(datapack: Datapack, file_path: string, value_path: string, value: DatapackChangeValue, method: DatapackChangeMethod) {
39+
if (valueMatchesMethod(value, method)) {
40+
const change: DatapackChange = {
41+
datapack: datapack,
42+
file_path: file_path,
43+
value_path: value_path,
44+
value: value,
45+
application_method: method
46+
};
47+
this.changeQueue.push(change);
48+
}
49+
else {
50+
console.warn(`Datapack change wasn't queued - value ${value} (type <${typeof value}>) doesn't match application method "${method}!"`);
51+
}
3052
}
3153

3254
public async applyChanges() {
@@ -42,3 +64,42 @@ export class DatapackModifier {
4264
}
4365

4466
export const DatapackModifierInstance = DatapackModifier.Instance;
67+
68+
69+
const string_methods: ReadonlyArray<DatapackChangeMethod> = [
70+
"add",
71+
"pop",
72+
"remove",
73+
"set"
74+
];
75+
76+
const number_methods: ReadonlyArray<DatapackChangeMethod> = [
77+
"multiply",
78+
"divide",
79+
"add",
80+
"subtract",
81+
"set",
82+
"multiply_int",
83+
"divide_int",
84+
"add_int",
85+
"subtract_int",
86+
"remove",
87+
"pop"
88+
];
89+
90+
const boolean_methods: ReadonlyArray<DatapackChangeMethod> = [
91+
"set"
92+
];
93+
94+
function valueMatchesMethod(value: DatapackChangeValue, method: DatapackChangeMethod) {
95+
if (typeof value === "string" && !string_methods.includes(method)) {
96+
return false;
97+
}
98+
else if (typeof value === "number" && !number_methods.includes(method)) {
99+
return false;
100+
}
101+
else if (typeof value === "boolean" && !boolean_methods.includes(method)) {
102+
return false;
103+
}
104+
return true;
105+
}

0 commit comments

Comments
 (0)