mc-assets 0.2.27 → 0.2.29

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.
@@ -14,7 +14,7 @@ export declare class ItemsRenderer {
14
14
  type: string;
15
15
  /** @deprecated */
16
16
  path: string;
17
- };
17
+ } | undefined;
18
18
  tryGetFullBlock(blockName: string, properties?: Record<string, string | boolean>): {
19
19
  top: {
20
20
  slice: [number, number, number, number];
@@ -35,7 +35,7 @@ export declare class ItemsRenderer {
35
35
  path: string;
36
36
  };
37
37
  } | undefined;
38
- getItemTexture(itemName: string, properties?: Record<string, string | boolean>): {
38
+ getItemTexture(itemNameOrModel: string, properties?: Record<string, string | boolean>, exactItemResolve?: boolean): {
39
39
  slice: [number, number, number, number];
40
40
  type: string;
41
41
  /** @deprecated */
@@ -19,6 +19,8 @@ export class ItemsRenderer {
19
19
  const type = texture.includes('items/') ? 'items' : (texture.includes('block/') || texture.includes('blocks/')) ? 'blocks' : 'items';
20
20
  const atlasParser = type === 'blocks' ? this.blocksAtlasParser : this.itemsAtlasParser;
21
21
  const textureInfo = atlasParser.getTextureInfo(texture.replace('block/', '').replace('blocks/', '').replace('item/', '').replace('items/', ''), this.version);
22
+ if (!textureInfo)
23
+ return;
22
24
  const atlas = atlasParser.atlas[textureInfo.imageType];
23
25
  return {
24
26
  slice: [
@@ -51,32 +53,43 @@ export class ItemsRenderer {
51
53
  const rightTexture = elem.faces.north?.texture ?? elem.faces.right?.texture ?? elem.faces.side?.texture;
52
54
  if (!topTexture || !leftTexture || !rightTexture)
53
55
  return;
56
+ const topTextureResolved = this.resolveTexture(topTexture);
57
+ if (!topTextureResolved)
58
+ throw new Error(`Missing texture for ${blockName} top texture`);
59
+ const leftTextureResolved = this.resolveTexture(leftTexture);
60
+ if (!leftTextureResolved)
61
+ throw new Error(`Missing texture for ${blockName} left texture`);
62
+ const rightTextureResolved = this.resolveTexture(rightTexture);
63
+ if (!rightTextureResolved)
64
+ throw new Error(`Missing texture for ${blockName} right texture`);
54
65
  return {
55
- top: this.resolveTexture(topTexture),
56
- left: this.resolveTexture(leftTexture),
57
- right: this.resolveTexture(rightTexture),
66
+ top: topTextureResolved,
67
+ left: leftTextureResolved,
68
+ right: rightTextureResolved,
58
69
  };
59
70
  }
60
- getItemTexture(itemName, properties = {}) {
71
+ getItemTexture(itemNameOrModel, properties = {}, exactItemResolve = false) {
72
+ itemNameOrModel = itemNameOrModel.replace(/^minecraft:/, '');
61
73
  let model;
62
- if (itemName.includes('/')) {
63
- model = this.modelsStore.get(this.version, itemName);
74
+ if (itemNameOrModel.includes('/') || exactItemResolve) {
75
+ model = this.modelsStore.get(this.version, itemNameOrModel);
64
76
  }
65
77
  else {
66
- model = this.modelsStore.get(this.version, `item/${itemName}`);
67
- if (!model || model.parent?.includes('block/') || this.modelsStore.get(this.version, `block/${itemName}`)) {
68
- return this.tryGetFullBlock(itemName, properties);
78
+ model = this.modelsStore.get(this.version, `item/${itemNameOrModel}`);
79
+ if (!model || model.parent?.includes('block/')) {
80
+ return this.tryGetFullBlock(itemNameOrModel, properties);
69
81
  }
70
82
  }
71
83
  if (!model)
72
84
  return;
73
- const texture = itemName.includes('block/') ?
85
+ const texture = itemNameOrModel.includes('block/') ?
74
86
  // first defined block texture
75
87
  Object.values(model.textures ?? {})[0] :
76
88
  model.textures?.layer0; // classic item texture
77
89
  if (!texture)
78
90
  return;
79
- return this.resolveTexture(texture);
91
+ return (texture.startsWith('invsprite_') ? this.resolveTexture(texture.replace('invsprite_', '')) : undefined)
92
+ ?? this.resolveTexture(texture);
80
93
  // const {resolvedModel} = this.assetsParser.getResolvedModelByModelName('item/' + itemName, itemName) ?? {}
81
94
  // resolvedModel?.textures['layer0']
82
95
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,98 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { ItemsRenderer } from './itemsRenderer';
3
+ import { AtlasParser } from './atlasParser';
4
+ import fs from 'fs';
5
+ // Load test data
6
+ const blockstatesModels = JSON.parse(fs.readFileSync('./dist/blockStatesModels.json', 'utf8'));
7
+ const itemsAtlases = JSON.parse(fs.readFileSync('./dist/itemsAtlases.json', 'utf8'));
8
+ const blocksAtlases = JSON.parse(fs.readFileSync('./dist/blocksAtlases.json', 'utf8'));
9
+ describe('ItemsRenderer', () => {
10
+ const itemsAtlasParser = new AtlasParser(itemsAtlases, '');
11
+ const blocksAtlasParser = new AtlasParser(blocksAtlases, '');
12
+ const renderer = new ItemsRenderer('latest', blockstatesModels, itemsAtlasParser, blocksAtlasParser);
13
+ describe('getItemTexture', () => {
14
+ it('items texture', () => {
15
+ expect(renderer.getItemTexture('item_frame')).toMatchInlineSnapshot(`
16
+ {
17
+ "path": "items",
18
+ "slice": [
19
+ 720,
20
+ 128,
21
+ 16,
22
+ 16,
23
+ ],
24
+ "type": "items",
25
+ }
26
+ `);
27
+ });
28
+ it('full blocks texture', () => {
29
+ expect(renderer.getItemTexture('stone')).toMatchInlineSnapshot(`
30
+ {
31
+ "left": {
32
+ "path": "blocks",
33
+ "slice": [
34
+ 816,
35
+ 64,
36
+ 16,
37
+ 16,
38
+ ],
39
+ "type": "blocks",
40
+ },
41
+ "right": {
42
+ "path": "blocks",
43
+ "slice": [
44
+ 816,
45
+ 64,
46
+ 16,
47
+ 16,
48
+ ],
49
+ "type": "blocks",
50
+ },
51
+ "top": {
52
+ "path": "blocks",
53
+ "slice": [
54
+ 816,
55
+ 64,
56
+ 16,
57
+ 16,
58
+ ],
59
+ "type": "blocks",
60
+ },
61
+ }
62
+ `);
63
+ });
64
+ it('invsprite textures', () => {
65
+ expect(renderer.getItemTexture('chest')).toMatchInlineSnapshot(`
66
+ {
67
+ "path": "items",
68
+ "slice": [
69
+ 400,
70
+ 0,
71
+ 16,
72
+ 16,
73
+ ],
74
+ "type": "items",
75
+ }
76
+ `);
77
+ });
78
+ it('not implemented logic', () => {
79
+ expect(renderer.getItemTexture('cut_copper_slab')).toMatchInlineSnapshot(`undefined`);
80
+ });
81
+ });
82
+ describe('resolveTexture', () => {
83
+ it('should resolve item textures correctly', () => {
84
+ expect(renderer.resolveTexture('items/item_frame')).toMatchInlineSnapshot(`
85
+ {
86
+ "path": "items",
87
+ "slice": [
88
+ 720,
89
+ 128,
90
+ 16,
91
+ 16,
92
+ ],
93
+ "type": "items",
94
+ }
95
+ `);
96
+ });
97
+ });
98
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mc-assets",
3
- "version": "0.2.27",
3
+ "version": "0.2.29",
4
4
  "author": "Vitaly Turovsky <vital2580@icloud.com>",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -15,7 +15,7 @@
15
15
  "@xmcl/core": "^2.13.0",
16
16
  "@xmcl/installer": "^5.4.0",
17
17
  "@zardoy/tsconfig": "^1.5.1",
18
- "canvas": "^2.11.2",
18
+ "canvas": "^3.1.0",
19
19
  "filesize": "^10.1.4",
20
20
  "imagemin": "^9.0.0",
21
21
  "imagemin-optipng": "^8.0.0",
@@ -60,6 +60,7 @@
60
60
  "regen-blockentities": "rm -f dist/blockStatesModels.json && tsx src/blockEntities.ts && tsx src/newAssetsBuilder.ts && tsx src/genBlocks.ts",
61
61
  "build-consumer": "tsc -p tsconfig.consumer.json",
62
62
  "watch-dist": "tsc -p tsconfig.consumer.json -w",
63
- "build-web": "pnpm --dir web-demo build"
63
+ "build-web": "pnpm --dir web-demo build",
64
+ "test": "vitest"
64
65
  }
65
66
  }