mca-json 1.0.4 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -8
- package/dist/bin/mca-find-signs.d.ts +3 -0
- package/dist/bin/mca-find-signs.d.ts.map +1 -0
- package/dist/bin/mca-find-signs.js +148 -0
- package/dist/bin/mca-find-signs.js.map +1 -0
- package/dist/bin/mca-remove-chunks.d.ts +3 -0
- package/dist/bin/mca-remove-chunks.d.ts.map +1 -0
- package/dist/bin/mca-remove-chunks.js +144 -0
- package/dist/bin/mca-remove-chunks.js.map +1 -0
- package/dist/bin/nbt-get-player-location.js +18 -9
- package/dist/bin/nbt-get-player-location.js.map +1 -1
- package/dist/lib/anvil.d.ts.map +1 -1
- package/dist/lib/anvil.js +4 -3
- package/dist/lib/anvil.js.map +1 -1
- package/dist/lib/bit-data.js +2 -2
- package/dist/lib/bit-data.js.map +1 -1
- package/dist/lib/chunk.d.ts.map +1 -1
- package/dist/lib/chunk.js +12 -7
- package/dist/lib/chunk.js.map +1 -1
- package/dist/nbt/snbt-parse.d.ts.map +1 -1
- package/dist/nbt/snbt-parse.js +7 -4
- package/dist/nbt/snbt-parse.js.map +1 -1
- package/dist/nbt/snbt-to-nbt.js +14 -4
- package/dist/nbt/snbt-to-nbt.js.map +1 -1
- package/package.json +2 -2
- package/src/bin/mca-find-signs.ts +214 -0
- package/src/bin/mca-remove-chunks.ts +183 -0
- package/src/bin/nbt-get-player-location.ts +19 -11
- package/src/lib/anvil.ts +5 -3
- package/src/lib/bit-data.ts +2 -2
- package/src/lib/chunk.ts +15 -7
- package/src/nbt/snbt-parse.ts +7 -4
- package/src/nbt/snbt-to-nbt.ts +63 -20
- package/dist/bin/mca-find-chunks-with-signs.d.ts +0 -3
- package/dist/bin/mca-find-chunks-with-signs.d.ts.map +0 -1
- package/dist/bin/mca-find-chunks-with-signs.js +0 -79
- package/dist/bin/mca-find-chunks-with-signs.js.map +0 -1
- package/dist/bin/mca-trim-chunks-without-signs.d.ts +0 -3
- package/dist/bin/mca-trim-chunks-without-signs.d.ts.map +0 -1
- package/dist/bin/mca-trim-chunks-without-signs.js +0 -117
- package/dist/bin/mca-trim-chunks-without-signs.js.map +0 -1
- package/src/bin/mca-find-chunks-with-signs.ts +0 -109
- package/src/bin/mca-trim-chunks-without-signs.ts +0 -146
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import debug from 'debug';
|
|
3
|
-
import neodoc from 'neodoc';
|
|
4
|
-
import { Anvil } from '../lib/anvil';
|
|
5
|
-
import { Sign } from '../block/sign';
|
|
6
|
-
import { readFile, writeFile } from 'node:fs/promises';
|
|
7
|
-
const debugLog = debug('mca-trim-chunks-without-signs');
|
|
8
|
-
const args = neodoc.run(`Usage: mca-trim-chunks-without-signs [OPTIONS] MCA_FILE...
|
|
9
|
-
|
|
10
|
-
Removes chunks from MCA files that do not contain a matching sign block. Signs
|
|
11
|
-
need to have at least one letter or number on them to differentiate them from
|
|
12
|
-
naturally generated signs.
|
|
13
|
-
|
|
14
|
-
This does not change the file size. Similar to deleting a file on a hard drive,
|
|
15
|
-
the space is marked as free but not overwritten.
|
|
16
|
-
|
|
17
|
-
Options:
|
|
18
|
-
-h, --help Show this message.
|
|
19
|
-
-n, --dry-run Do not write changes to the file.
|
|
20
|
-
--preserve CHUNK_COORDINATES...
|
|
21
|
-
Do not remove chunks with these coordinates. Coordinates
|
|
22
|
-
are in the format "x,z". May be specified multiple times.
|
|
23
|
-
-v, --verbose Show more information during processing.
|
|
24
|
-
`, {
|
|
25
|
-
argv: [...process.argv].slice(2),
|
|
26
|
-
laxPlacement: true,
|
|
27
|
-
});
|
|
28
|
-
if (!args.MCA_FILE?.length) {
|
|
29
|
-
console.error('No files specified.');
|
|
30
|
-
process.exit(1);
|
|
31
|
-
}
|
|
32
|
-
if (!args['--preserve']) {
|
|
33
|
-
args['--preserve'] = [];
|
|
34
|
-
}
|
|
35
|
-
if (args['--preserve'].length) {
|
|
36
|
-
console.log(`Preserving coordinates: ${args['--preserve'].join(' ')}`);
|
|
37
|
-
}
|
|
38
|
-
for (const file of args.MCA_FILE) {
|
|
39
|
-
try {
|
|
40
|
-
await processFile(file);
|
|
41
|
-
}
|
|
42
|
-
catch (e) {
|
|
43
|
-
console.error(`Error processing file ${file}: ${e}`);
|
|
44
|
-
process.exit(1);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
async function processFile(filename) {
|
|
48
|
-
debugLog(`Reading file: ${filename}`);
|
|
49
|
-
if (args['--verbose']) {
|
|
50
|
-
console.log(`Reading file: ${filename}`);
|
|
51
|
-
}
|
|
52
|
-
const contents = await readFile(filename);
|
|
53
|
-
const anvil = Anvil.fromBuffer(contents.buffer);
|
|
54
|
-
const chunks = anvil.getAllChunks();
|
|
55
|
-
let changes = 0;
|
|
56
|
-
const preservedCoordinates = new Set(args['--preserve']);
|
|
57
|
-
for (const chunk of chunks) {
|
|
58
|
-
const chunkKey = chunk.chunkKey() || '';
|
|
59
|
-
if (preservedCoordinates.has(chunkKey)) {
|
|
60
|
-
debugLog(`Preserving chunk (via command-line): ${chunkKey}`);
|
|
61
|
-
if (args['--verbose']) {
|
|
62
|
-
console.log(`Preserving chunk (via command-line): ${chunkKey}`);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
else if (processChunk(chunk)) {
|
|
66
|
-
debugLog(`Preserving chunk (sign detected): ${chunkKey}`);
|
|
67
|
-
if (args['--verbose']) {
|
|
68
|
-
console.log(`Preserving chunk (sign detected): ${chunkKey}`);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
debugLog(`Removing chunk: ${chunkKey}`);
|
|
73
|
-
anvil.deleteChunk(chunk);
|
|
74
|
-
changes += 1;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
if (args['--verbose']) {
|
|
78
|
-
console.log(`Chunks removed: ${changes}/${chunks.length}`);
|
|
79
|
-
}
|
|
80
|
-
if (changes) {
|
|
81
|
-
if (args['--dry-run']) {
|
|
82
|
-
console.log(`DRY RUN: Would write changes to file: ${filename}`);
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
console.log(`Writing changes to file: ${filename}`);
|
|
86
|
-
const modifiedBuffer = Buffer.from(anvil.buffer());
|
|
87
|
-
await writeFile(filename, modifiedBuffer);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
function processChunk(chunk) {
|
|
92
|
-
const blockNames = chunk.uniqueBlockNames();
|
|
93
|
-
for (const blockName of blockNames) {
|
|
94
|
-
if (blockName.match(/^minecraft:.+_sign$/)) {
|
|
95
|
-
debugLog(`Found sign: ${blockName}`);
|
|
96
|
-
for (const coords of chunk.findBlocksByName(blockName)) {
|
|
97
|
-
const block = chunk.getBlock(coords);
|
|
98
|
-
if (!(block instanceof Sign)) {
|
|
99
|
-
throw new Error(`Block is supposed to be a sign: ${block.name}`);
|
|
100
|
-
}
|
|
101
|
-
const front = block.frontText();
|
|
102
|
-
const back = block.backText();
|
|
103
|
-
debugLog(`Front: ${JSON.stringify(front)}`);
|
|
104
|
-
debugLog(`Back: ${JSON.stringify(back)}`);
|
|
105
|
-
if ([...front, ...(back || [])].join('').match(/[a-z0-9]/i)) {
|
|
106
|
-
debugLog(`Found a user-created sign. Preserving chunk.`);
|
|
107
|
-
if (args['--verbose']) {
|
|
108
|
-
console.log(`Found a user-created sign: ${JSON.stringify(front)} and ${JSON.stringify(back)}`);
|
|
109
|
-
}
|
|
110
|
-
return true;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
return false;
|
|
116
|
-
}
|
|
117
|
-
//# sourceMappingURL=mca-trim-chunks-without-signs.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mca-trim-chunks-without-signs.js","sourceRoot":"","sources":["../../src/bin/mca-trim-chunks-without-signs.ts"],"names":[],"mapp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AAK,GAAG,KAAK,CAAC,SAAS,EAAE,CAAC;gBAChC,MAAM,IAAI,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAC;gBAC9B,QAAQ,CAAC,UAAU,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;gBAC5C,QAAQ,CAAC,SAAS,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;gBAE1C,IAAI,CAAC,GAAG,KAAK,EAAE,GAAG,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,WAAW,CAAC,EAAE,CAAC;oBAC1D,QAAQ,CAAC,8CAA8C,CAAC,CAAC;oBAEzD,IAAI,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC;wBACpB,OAAO,CAAC,GAAG,CAAC,8BAA8B,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,QAAQ,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;oBACnG,CAAC;oBAED,OAAO,IAAI,CAAC;gBAChB,CAAC;YACL,CAAC;QACL,CAAC;IACL,CAAC;IAED,OAAO,KAAK,CAAC;AACjB,CAAC","sourcesContent":["#!/usr/bin/env node\n\nimport debug from 'debug';\nimport neodoc from 'neodoc';\nimport { Anvil } from '../lib/anvil';\nimport { Chunk } from '../lib/chunk';\nimport { Sign } from '../block/sign';\nimport { readFile, writeFile } from 'node:fs/promises';\n\nconst debugLog = debug('mca-trim-chunks-without-signs');\nconst args: {\n '--dry-run'?: boolean;\n '--preserve'?: string[];\n '--verbose'?: boolean;\n 'MCA_FILE'?: string[];\n} = neodoc.run(\n`Usage: mca-trim-chunks-without-signs [OPTIONS] MCA_FILE...\n\nRemoves chunks from MCA files that do not contain a matching sign block. Signs\nneed to have at least one letter or number on them to differentiate them from\nnaturally generated signs.\n\nThis does not change the file size. Similar to deleting a file on a hard drive,\nthe space is marked as free but not overwritten.\n\nOptions:\n -h, --help Show this message.\n -n, --dry-run Do not write changes to the file.\n --preserve CHUNK_COORDINATES...\n Do not remove chunks with these coordinates. Coordinates\n are in the format \"x,z\". May be specified multiple times.\n -v, --verbose Show more information during processing.\n`,\n {\n argv: [...process.argv].slice(2),\n laxPlacement: true,\n }\n);\n\nif (!args.MCA_FILE?.length) {\n console.error('No files specified.');\n process.exit(1);\n}\n\nif (!args['--preserve']) {\n args['--preserve'] = [];\n}\n\nif (args['--preserve'].length) {\n console.log(`Preserving coordinates: ${args['--preserve'].join(' ')}`);\n}\n\nfor (const file of args.MCA_FILE) {\n try {\n await processFile(file);\n } catch (e) {\n console.error(`Error processing file ${file}: ${e}`);\n\n process.exit(1);\n }\n}\n\nasync function processFile(filename: string) {\n debugLog(`Reading file: ${filename}`);\n\n if (args['--verbose']) {\n console.log(`Reading file: ${filename}`);\n }\n\n const contents = await readFile(filename);\n const anvil = Anvil.fromBuffer(contents.buffer);\n const chunks = anvil.getAllChunks();\n let changes = 0;\n const preservedCoordinates = new Set(args['--preserve']);\n\n for (const chunk of chunks) {\n const chunkKey = chunk.chunkKey() || '';\n\n if (preservedCoordinates.has(chunkKey)) {\n debugLog(`Preserving chunk (via command-line): ${chunkKey}`);\n\n if (args['--verbose']) {\n console.log(`Preserving chunk (via command-line): ${chunkKey}`);\n }\n } else if (processChunk(chunk)) {\n debugLog(`Preserving chunk (sign detected): ${chunkKey}`);\n\n if (args['--verbose']) {\n console.log(`Preserving chunk (sign detected): ${chunkKey}`);\n }\n } else {\n debugLog(`Removing chunk: ${chunkKey}`);\n anvil.deleteChunk(chunk);\n changes += 1;\n }\n }\n\n if (args['--verbose']) {\n console.log(`Chunks removed: ${changes}/${chunks.length}`);\n }\n\n if (changes) {\n if (args['--dry-run']) {\n console.log(`DRY RUN: Would write changes to file: ${filename}`);\n } else {\n console.log(`Writing changes to file: ${filename}`);\n const modifiedBuffer = Buffer.from(anvil.buffer());\n await writeFile(filename, modifiedBuffer);\n }\n }\n}\n\nfunction processChunk(chunk: Chunk): boolean {\n const blockNames = chunk.uniqueBlockNames();\n\n for (const blockName of blockNames) {\n if (blockName.match(/^minecraft:.+_sign$/)) {\n debugLog(`Found sign: ${blockName}`);\n\n for (const coords of chunk.findBlocksByName(blockName)) {\n const block = chunk.getBlock(coords);\n\n if (!(block instanceof Sign)) {\n throw new Error(`Block is supposed to be a sign: ${block.name}`);\n }\n\n const front = block.frontText();\n const back = block.backText();\n debugLog(`Front: ${JSON.stringify(front)}`);\n debugLog(`Back: ${JSON.stringify(back)}`);\n\n if ([...front, ...(back || [])].join('').match(/[a-z0-9]/i)) {\n debugLog(`Found a user-created sign. Preserving chunk.`);\n\n if (args['--verbose']) {\n console.log(`Found a user-created sign: ${JSON.stringify(front)} and ${JSON.stringify(back)}`);\n }\n\n return true;\n }\n }\n }\n }\n\n return false;\n}\n"]}
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import debug from 'debug';
|
|
4
|
-
import neodoc from 'neodoc';
|
|
5
|
-
import { Anvil } from '../lib/anvil';
|
|
6
|
-
import { Chunk } from '../lib/chunk';
|
|
7
|
-
import { Sign } from '../block/sign';
|
|
8
|
-
import { readFile } from 'node:fs/promises';
|
|
9
|
-
|
|
10
|
-
const debugLog = debug('mca-find-chunks-with-signs');
|
|
11
|
-
const args: {
|
|
12
|
-
'--verbose'?: boolean;
|
|
13
|
-
MCA_FILE?: string[];
|
|
14
|
-
} = neodoc.run(
|
|
15
|
-
`Usage: mca-trim-chunks-without-signs [OPTIONS] MCA_FILE...
|
|
16
|
-
|
|
17
|
-
Displays chunk X and Z coordinates, separated by a tab, for all chunks in MCA
|
|
18
|
-
files that contain a sign block. Signs need to have at least one letter or
|
|
19
|
-
number on them to differentiate them from naturally generated signs.
|
|
20
|
-
|
|
21
|
-
Options:
|
|
22
|
-
-h, --help Show this message.
|
|
23
|
-
-v, --verbose Show more information during processing.
|
|
24
|
-
`,
|
|
25
|
-
{
|
|
26
|
-
argv: [...process.argv].slice(2),
|
|
27
|
-
laxPlacement: true,
|
|
28
|
-
}
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
if (!args.MCA_FILE?.length) {
|
|
32
|
-
console.error('No files specified.');
|
|
33
|
-
process.exit(1);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
for (const file of args.MCA_FILE) {
|
|
37
|
-
try {
|
|
38
|
-
await processFile(file);
|
|
39
|
-
} catch (e) {
|
|
40
|
-
console.error(`Error processing file ${file}: ${e}`);
|
|
41
|
-
|
|
42
|
-
process.exit(1);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async function processFile(filename: string) {
|
|
47
|
-
debugLog(`Reading file: ${filename}`);
|
|
48
|
-
|
|
49
|
-
if (args['--verbose']) {
|
|
50
|
-
console.log(`Reading file: ${filename}`);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const contents = await readFile(filename);
|
|
54
|
-
const anvil = Anvil.fromBuffer(contents.buffer);
|
|
55
|
-
const chunks = anvil.getAllChunks();
|
|
56
|
-
|
|
57
|
-
for (const chunk of chunks) {
|
|
58
|
-
const chunkKey = chunk.chunkKey() || '';
|
|
59
|
-
|
|
60
|
-
if (processChunk(chunk)) {
|
|
61
|
-
const coords = chunk.chunkCoordinates();
|
|
62
|
-
|
|
63
|
-
if (!coords) {
|
|
64
|
-
throw new Error(`Chunk ${chunkKey} has no coordinates.`);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
console.log(`${coords[0]}\t${coords[1]}`);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function processChunk(chunk: Chunk): boolean {
|
|
73
|
-
const blockNames = chunk.uniqueBlockNames();
|
|
74
|
-
|
|
75
|
-
for (const blockName of blockNames) {
|
|
76
|
-
if (blockName.match(/^minecraft:.+_sign$/)) {
|
|
77
|
-
debugLog(`Found sign: ${blockName}`);
|
|
78
|
-
|
|
79
|
-
for (const coords of chunk.findBlocksByName(blockName)) {
|
|
80
|
-
const block = chunk.getBlock(coords);
|
|
81
|
-
|
|
82
|
-
if (!(block instanceof Sign)) {
|
|
83
|
-
throw new Error(
|
|
84
|
-
`Block is supposed to be a sign: ${block.name}`
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const front = block.frontText();
|
|
89
|
-
const back = block.backText();
|
|
90
|
-
debugLog(`Front: ${JSON.stringify(front)}`);
|
|
91
|
-
debugLog(`Back: ${JSON.stringify(back)}`);
|
|
92
|
-
|
|
93
|
-
if ([...front, ...(back || [])].join('').match(/[a-z0-9]/i)) {
|
|
94
|
-
debugLog(`Found a user-created sign. Preserving chunk.`);
|
|
95
|
-
|
|
96
|
-
if (args['--verbose']) {
|
|
97
|
-
console.log(
|
|
98
|
-
`Found a user-created sign: ${JSON.stringify(front)} and ${JSON.stringify(back)}`
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return true;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import debug from 'debug';
|
|
4
|
-
import neodoc from 'neodoc';
|
|
5
|
-
import { Anvil } from '../lib/anvil';
|
|
6
|
-
import { Chunk } from '../lib/chunk';
|
|
7
|
-
import { Sign } from '../block/sign';
|
|
8
|
-
import { readFile, writeFile } from 'node:fs/promises';
|
|
9
|
-
|
|
10
|
-
const debugLog = debug('mca-trim-chunks-without-signs');
|
|
11
|
-
const args: {
|
|
12
|
-
'--dry-run'?: boolean;
|
|
13
|
-
'--preserve'?: string[];
|
|
14
|
-
'--verbose'?: boolean;
|
|
15
|
-
'MCA_FILE'?: string[];
|
|
16
|
-
} = neodoc.run(
|
|
17
|
-
`Usage: mca-trim-chunks-without-signs [OPTIONS] MCA_FILE...
|
|
18
|
-
|
|
19
|
-
Removes chunks from MCA files that do not contain a matching sign block. Signs
|
|
20
|
-
need to have at least one letter or number on them to differentiate them from
|
|
21
|
-
naturally generated signs.
|
|
22
|
-
|
|
23
|
-
This does not change the file size. Similar to deleting a file on a hard drive,
|
|
24
|
-
the space is marked as free but not overwritten.
|
|
25
|
-
|
|
26
|
-
Options:
|
|
27
|
-
-h, --help Show this message.
|
|
28
|
-
-n, --dry-run Do not write changes to the file.
|
|
29
|
-
--preserve CHUNK_COORDINATES...
|
|
30
|
-
Do not remove chunks with these coordinates. Coordinates
|
|
31
|
-
are in the format "x,z". May be specified multiple times.
|
|
32
|
-
-v, --verbose Show more information during processing.
|
|
33
|
-
`,
|
|
34
|
-
{
|
|
35
|
-
argv: [...process.argv].slice(2),
|
|
36
|
-
laxPlacement: true,
|
|
37
|
-
}
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
if (!args.MCA_FILE?.length) {
|
|
41
|
-
console.error('No files specified.');
|
|
42
|
-
process.exit(1);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (!args['--preserve']) {
|
|
46
|
-
args['--preserve'] = [];
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (args['--preserve'].length) {
|
|
50
|
-
console.log(`Preserving coordinates: ${args['--preserve'].join(' ')}`);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
for (const file of args.MCA_FILE) {
|
|
54
|
-
try {
|
|
55
|
-
await processFile(file);
|
|
56
|
-
} catch (e) {
|
|
57
|
-
console.error(`Error processing file ${file}: ${e}`);
|
|
58
|
-
|
|
59
|
-
process.exit(1);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
async function processFile(filename: string) {
|
|
64
|
-
debugLog(`Reading file: ${filename}`);
|
|
65
|
-
|
|
66
|
-
if (args['--verbose']) {
|
|
67
|
-
console.log(`Reading file: ${filename}`);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const contents = await readFile(filename);
|
|
71
|
-
const anvil = Anvil.fromBuffer(contents.buffer);
|
|
72
|
-
const chunks = anvil.getAllChunks();
|
|
73
|
-
let changes = 0;
|
|
74
|
-
const preservedCoordinates = new Set(args['--preserve']);
|
|
75
|
-
|
|
76
|
-
for (const chunk of chunks) {
|
|
77
|
-
const chunkKey = chunk.chunkKey() || '';
|
|
78
|
-
|
|
79
|
-
if (preservedCoordinates.has(chunkKey)) {
|
|
80
|
-
debugLog(`Preserving chunk (via command-line): ${chunkKey}`);
|
|
81
|
-
|
|
82
|
-
if (args['--verbose']) {
|
|
83
|
-
console.log(`Preserving chunk (via command-line): ${chunkKey}`);
|
|
84
|
-
}
|
|
85
|
-
} else if (processChunk(chunk)) {
|
|
86
|
-
debugLog(`Preserving chunk (sign detected): ${chunkKey}`);
|
|
87
|
-
|
|
88
|
-
if (args['--verbose']) {
|
|
89
|
-
console.log(`Preserving chunk (sign detected): ${chunkKey}`);
|
|
90
|
-
}
|
|
91
|
-
} else {
|
|
92
|
-
debugLog(`Removing chunk: ${chunkKey}`);
|
|
93
|
-
anvil.deleteChunk(chunk);
|
|
94
|
-
changes += 1;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (args['--verbose']) {
|
|
99
|
-
console.log(`Chunks removed: ${changes}/${chunks.length}`);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (changes) {
|
|
103
|
-
if (args['--dry-run']) {
|
|
104
|
-
console.log(`DRY RUN: Would write changes to file: ${filename}`);
|
|
105
|
-
} else {
|
|
106
|
-
console.log(`Writing changes to file: ${filename}`);
|
|
107
|
-
const modifiedBuffer = Buffer.from(anvil.buffer());
|
|
108
|
-
await writeFile(filename, modifiedBuffer);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function processChunk(chunk: Chunk): boolean {
|
|
114
|
-
const blockNames = chunk.uniqueBlockNames();
|
|
115
|
-
|
|
116
|
-
for (const blockName of blockNames) {
|
|
117
|
-
if (blockName.match(/^minecraft:.+_sign$/)) {
|
|
118
|
-
debugLog(`Found sign: ${blockName}`);
|
|
119
|
-
|
|
120
|
-
for (const coords of chunk.findBlocksByName(blockName)) {
|
|
121
|
-
const block = chunk.getBlock(coords);
|
|
122
|
-
|
|
123
|
-
if (!(block instanceof Sign)) {
|
|
124
|
-
throw new Error(`Block is supposed to be a sign: ${block.name}`);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const front = block.frontText();
|
|
128
|
-
const back = block.backText();
|
|
129
|
-
debugLog(`Front: ${JSON.stringify(front)}`);
|
|
130
|
-
debugLog(`Back: ${JSON.stringify(back)}`);
|
|
131
|
-
|
|
132
|
-
if ([...front, ...(back || [])].join('').match(/[a-z0-9]/i)) {
|
|
133
|
-
debugLog(`Found a user-created sign. Preserving chunk.`);
|
|
134
|
-
|
|
135
|
-
if (args['--verbose']) {
|
|
136
|
-
console.log(`Found a user-created sign: ${JSON.stringify(front)} and ${JSON.stringify(back)}`);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
return true;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
return false;
|
|
146
|
-
}
|