mca-json 1.0.5 → 1.1.1

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.
Files changed (37) hide show
  1. package/README.md +41 -8
  2. package/dist/bin/mca-find-signs.d.ts +3 -0
  3. package/dist/bin/mca-find-signs.d.ts.map +1 -0
  4. package/dist/bin/mca-find-signs.js +148 -0
  5. package/dist/bin/mca-find-signs.js.map +1 -0
  6. package/dist/bin/mca-remove-chunks.d.ts +3 -0
  7. package/dist/bin/mca-remove-chunks.d.ts.map +1 -0
  8. package/dist/bin/mca-remove-chunks.js +144 -0
  9. package/dist/bin/mca-remove-chunks.js.map +1 -0
  10. package/dist/bin/nbt-get-player-location.js +18 -9
  11. package/dist/bin/nbt-get-player-location.js.map +1 -1
  12. package/dist/lib/anvil.d.ts.map +1 -1
  13. package/dist/lib/anvil.js +4 -3
  14. package/dist/lib/anvil.js.map +1 -1
  15. package/dist/lib/bit-data.js +2 -2
  16. package/dist/lib/bit-data.js.map +1 -1
  17. package/dist/lib/chunk.d.ts +3 -1
  18. package/dist/lib/chunk.d.ts.map +1 -1
  19. package/dist/lib/chunk.js +22 -15
  20. package/dist/lib/chunk.js.map +1 -1
  21. package/package.json +2 -2
  22. package/src/bin/mca-find-signs.ts +214 -0
  23. package/src/bin/mca-remove-chunks.ts +183 -0
  24. package/src/bin/nbt-get-player-location.ts +19 -11
  25. package/src/lib/anvil.ts +5 -3
  26. package/src/lib/bit-data.ts +2 -2
  27. package/src/lib/chunk.ts +27 -17
  28. package/dist/bin/mca-find-chunks-with-signs.d.ts +0 -3
  29. package/dist/bin/mca-find-chunks-with-signs.d.ts.map +0 -1
  30. package/dist/bin/mca-find-chunks-with-signs.js +0 -79
  31. package/dist/bin/mca-find-chunks-with-signs.js.map +0 -1
  32. package/dist/bin/mca-trim-chunks-without-signs.d.ts +0 -3
  33. package/dist/bin/mca-trim-chunks-without-signs.d.ts.map +0 -1
  34. package/dist/bin/mca-trim-chunks-without-signs.js +0 -117
  35. package/dist/bin/mca-trim-chunks-without-signs.js.map +0 -1
  36. package/src/bin/mca-find-chunks-with-signs.ts +0 -109
  37. package/src/bin/mca-trim-chunks-without-signs.ts +0 -146
package/src/lib/chunk.ts CHANGED
@@ -10,8 +10,10 @@ import { Block, BlockInstance } from '../block/block';
10
10
  import { BlockData } from './block-data';
11
11
  import { Coords2d, Coords3d } from '../types/coords';
12
12
  import { NbtBase } from '../nbt/nbt-base';
13
+ import { NbtByte } from '../nbt/nbt-byte';
13
14
  import { NbtCompound } from '../nbt/nbt-compound';
14
15
  import { NbtInt } from '../nbt/nbt-int';
16
+ import { NbtIntArray } from '../nbt/nbt-int-array';
15
17
  import { NbtList } from '../nbt/nbt-list';
16
18
  import { NbtLong } from '../nbt/nbt-long';
17
19
  import { NbtLongArray } from '../nbt/nbt-long-array';
@@ -75,21 +77,28 @@ export class Chunk {
75
77
  */
76
78
  chunkCoordinates(): Coords2d | undefined {
77
79
  const x =
78
- // 1.18+
80
+ // 1.18+ region chunks
79
81
  this.rootNbt.findChild<NbtInt>('xPos') ||
80
- // up to 1.17
82
+ // up to 1.17 region chunks
81
83
  this.rootNbt.findChild<NbtInt>('Level/xPos');
82
84
  const z =
83
- // 1.18+
85
+ // 1.18+ region chunks
84
86
  this.rootNbt.findChild<NbtInt>('zPos') ||
85
- // up to 1.17
87
+ // up to 1.17 region chunks
86
88
  this.rootNbt.findChild<NbtInt>('Level/zPos');
87
89
 
88
- if (typeof x?.data !== 'number' || typeof z?.data !== 'number') {
89
- return;
90
+ if (typeof x?.data === 'number' && typeof z?.data === 'number') {
91
+ return [x.data, z.data];
92
+ }
93
+
94
+ // Check for entity chunks
95
+ const position = this.rootNbt.findChild<NbtIntArray>('Position');
96
+
97
+ if (position && position.data.length === 2) {
98
+ return [position.data[0], position.data[1]];
90
99
  }
91
100
 
92
- return [x.data, z.data];
101
+ return;
93
102
  }
94
103
 
95
104
  /**
@@ -141,7 +150,7 @@ export class Chunk {
141
150
 
142
151
  const index = blockData.chunkCoordinatesToIndex([
143
152
  xChunk,
144
- coords[1] % 16,
153
+ ((coords[1] % 16) + 16) % 16, // Handle negative Y coordinates
145
154
  zChunk,
146
155
  ]);
147
156
  const name = blockData.getBlockByIndex(index);
@@ -369,21 +378,22 @@ export class Chunk {
369
378
  }
370
379
 
371
380
  /**
372
- * Returns the Y coordinate of a section, which goes from 0 to 256. If you want the world coordinate, pass true to get -128 to 4031.
381
+ * Returns the Y coordinate of a section, which goes from -4 to 19. If you
382
+ * want the world coordinate, pass true to get -64 to 304. The maximum supported
383
+ * range is -128 to 127 (chunk coordinates) or -2048 to 2032 (world coordinates).
373
384
  */
374
385
  private sectionY(section: NbtCompound, useWorldCoordinate = false): number {
375
- const ySection = section.findChild<NbtInt>('Y')?.data || 0;
386
+ let ySection = section.findChild<NbtByte>('Y')?.data || 0;
376
387
 
377
- if (!useWorldCoordinate) {
378
- return ySection;
388
+ // If the high bit is on, it's negative because this byte is a signed short.
389
+ if (ySection >= 128) {
390
+ ySection -= 256;
379
391
  }
380
392
 
381
- let yWorld = ySection * 16;
382
-
383
- if (yWorld >= 4032) {
384
- yWorld -= 4096;
393
+ if (!useWorldCoordinate) {
394
+ return ySection;
385
395
  }
386
396
 
387
- return yWorld;
397
+ return ySection * 16;
388
398
  }
389
399
  }
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env node
2
- export {};
3
- //# sourceMappingURL=mca-find-chunks-with-signs.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mca-find-chunks-with-signs.d.ts","sourceRoot":"","sources":["../../src/bin/mca-find-chunks-with-signs.ts"],"names":[],"mappings":""}
@@ -1,79 +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 } from 'node:fs/promises';
7
- const debugLog = debug('mca-find-chunks-with-signs');
8
- const args = neodoc.run(`Usage: mca-trim-chunks-without-signs [OPTIONS] MCA_FILE...
9
-
10
- Displays chunk X and Z coordinates, separated by a tab, for all chunks in MCA
11
- files that contain a sign block. Signs need to have at least one letter or
12
- number on them to differentiate them from naturally generated signs.
13
-
14
- Options:
15
- -h, --help Show this message.
16
- -v, --verbose Show more information during processing.
17
- `, {
18
- argv: [...process.argv].slice(2),
19
- laxPlacement: true,
20
- });
21
- if (!args.MCA_FILE?.length) {
22
- console.error('No files specified.');
23
- process.exit(1);
24
- }
25
- for (const file of args.MCA_FILE) {
26
- try {
27
- await processFile(file);
28
- }
29
- catch (e) {
30
- console.error(`Error processing file ${file}: ${e}`);
31
- process.exit(1);
32
- }
33
- }
34
- async function processFile(filename) {
35
- debugLog(`Reading file: ${filename}`);
36
- if (args['--verbose']) {
37
- console.log(`Reading file: ${filename}`);
38
- }
39
- const contents = await readFile(filename);
40
- const anvil = Anvil.fromBuffer(contents.buffer);
41
- const chunks = anvil.getAllChunks();
42
- for (const chunk of chunks) {
43
- const chunkKey = chunk.chunkKey() || '';
44
- if (processChunk(chunk)) {
45
- const coords = chunk.chunkCoordinates();
46
- if (!coords) {
47
- throw new Error(`Chunk ${chunkKey} has no coordinates.`);
48
- }
49
- console.log(`${coords[0]}\t${coords[1]}`);
50
- }
51
- }
52
- }
53
- function processChunk(chunk) {
54
- const blockNames = chunk.uniqueBlockNames();
55
- for (const blockName of blockNames) {
56
- if (blockName.match(/^minecraft:.+_sign$/)) {
57
- debugLog(`Found sign: ${blockName}`);
58
- for (const coords of chunk.findBlocksByName(blockName)) {
59
- const block = chunk.getBlock(coords);
60
- if (!(block instanceof Sign)) {
61
- throw new Error(`Block is supposed to be a sign: ${block.name}`);
62
- }
63
- const front = block.frontText();
64
- const back = block.backText();
65
- debugLog(`Front: ${JSON.stringify(front)}`);
66
- debugLog(`Back: ${JSON.stringify(back)}`);
67
- if ([...front, ...(back || [])].join('').match(/[a-z0-9]/i)) {
68
- debugLog(`Found a user-created sign. Preserving chunk.`);
69
- if (args['--verbose']) {
70
- console.log(`Found a user-created sign: ${JSON.stringify(front)} and ${JSON.stringify(back)}`);
71
- }
72
- return true;
73
- }
74
- }
75
- }
76
- }
77
- return false;
78
- }
79
- //# sourceMappingURL=mca-find-chunks-with-signs.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mca-find-chunks-with-signs.js","sourceRoot":"","sources":["../../src/bin/mca-find-chunks-with-signs.ts"],"names":[],"mappings":";AAEA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAErC,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AACrC,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C,MAAM,QAAQ,GAAG,KAAK,CAAC,4BAA4B,CAAC,CAAC;AACrD,MAAM,IAAI,GAGN,MAAM,CAAC,GAAG,CACV;;;;;;;;;CASH,EACG;IACI,IAAI,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;IAChC,YAAY,EAAE,IAAI;CACrB,CACJ,CAAC;AAEF,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,CAAC;IACzB,OAAO,CAAC,KAAK,CAAC,qBAAqB,CAAC,CAAC;IACrC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACpB,CAAC;AAED,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;IAC/B,IAAI,CAAC;QACD,MAAM,WAAW,CAAC,IAAI,CAAC,CAAC;IAC5B,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACT,OAAO,CAAC,KAAK,CAAC,yBAAyB,IAAI,KAAK,CAAC,EAAE,CAAC,CAAC;QAErD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACpB,CAAC;AACL,CAAC;AAED,KAAK,UAAU,WAAW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C,QAAQ,CAAC,MAAM,CAAC,CAAC;gBAErC,IAAI,CAAC,CAAC,KAAK,YAAY,IAAI,CAAC,EAAE,CAAC;oBAC3B,MAAM,IAAI,KAAK,CACX,mCAAmC,KAAK,CAAC,IAAI,EAAE,CAClD,CAAC;gBACN,CAAC;gBAED,MAAM,KAAK,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,CACP,8BAA8B,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,QAAQ,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CACpF,CAAC;oBACN,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 } from 'node:fs/promises';\n\nconst debugLog = debug('mca-find-chunks-with-signs');\nconst args: {\n '--verbose'?: boolean;\n MCA_FILE?: string[];\n} = neodoc.run(\n `Usage: mca-trim-chunks-without-signs [OPTIONS] MCA_FILE...\n\nDisplays chunk X and Z coordinates, separated by a tab, for all chunks in MCA\nfiles that contain a sign block. Signs need to have at least one letter or\nnumber on them to differentiate them from naturally generated signs.\n\nOptions:\n -h, --help Show this message.\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\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\n for (const chunk of chunks) {\n const chunkKey = chunk.chunkKey() || '';\n\n if (processChunk(chunk)) {\n const coords = chunk.chunkCoordinates();\n\n if (!coords) {\n throw new Error(`Chunk ${chunkKey} has no coordinates.`);\n }\n\n console.log(`${coords[0]}\\t${coords[1]}`);\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(\n `Block is supposed to be a sign: ${block.name}`\n );\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(\n `Found a user-created sign: ${JSON.stringify(front)} and ${JSON.stringify(back)}`\n );\n }\n\n return true;\n }\n }\n }\n }\n\n return false;\n}\n"]}
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env node
2
- export {};
3
- //# sourceMappingURL=mca-trim-chunks-without-signs.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mca-trim-chunks-without-signs.d.ts","sourceRoot":"","sources":["../../src/bin/mca-trim-chunks-without-signs.ts"],"names":[],"mappings":""}
@@ -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":[],"mappings":";AAEA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAErC,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AACrC,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAEvD,MAAM,QAAQ,GAAG,KAAK,CAAC,+BAA+B,CAAC,CAAC;AACxD,MAAM,IAAI,GAKN,MAAM,CAAC,GAAG,CACd;;;;;;;;;;;;;;;;CAgBC,EACG;IACI,IAAI,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;IAChC,YAAY,EAAE,IAAI;CACrB,CACJ,CAAC;AAEF,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,CAAC;IACzB,OAAO,CAAC,KAAK,CAAC,qBAAqB,CAAC,CAAC;IACrC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACpB,CAAC;AAED,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE,CAAC;IACtB,IAAI,CAAC,YAAY,CAAC,GAAG,EAAE,CAAC;AAC5B,CAAC;AAED,IAAI,IAAI,CAAC,YAAY,CAAC,CAAC,MAAM,EAAE,CAAC;IAC5B,OAAO,CAAC,GAAG,CAAC,2BAA2B,IAAI,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;AAC3E,CAAC;AAED,KAAK,MAAM,IAAI,IAAI,IAA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D,MAAM,KAAK,GAAG,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;gBAErC,IAAI,CAAC,CAAC,KAAK,YAAY,IAAI,CAAC,EAAE,CAAC;oBAC3B,MAAM,IAAI,KAAK,CAAC,mCAAmC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;gBACrE,CAAC;gBAED,MAAM,KAAK,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
- }