From b720584388c9e25e1ef2fe78cb665014f6f1c37b Mon Sep 17 00:00:00 2001 From: Niklas Widmann Date: Mon, 26 Aug 2024 19:19:23 +0200 Subject: [PATCH] fix nested blocks added using *add* not working --- src/emit/content/blockDefinition.ts | 6 ++--- src/emit/content/innerBlockDefinition.ts | 12 +++++----- src/schema/content/blockDefinition.ts | 2 +- test/content.test.ts | 30 +++++++++++++++++++++++- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/emit/content/blockDefinition.ts b/src/emit/content/blockDefinition.ts index e4df5a2..2663ddd 100644 --- a/src/emit/content/blockDefinition.ts +++ b/src/emit/content/blockDefinition.ts @@ -1,11 +1,11 @@ +import { Acceptor } from '@pssbletrngle/pack-resolver' import { Id, IdInput } from '../../common/id.js' -import CustomEmitter from '../custom.js' import { BlockDefinition, BlockProperties } from '../../schema/content/blockDefinition.js' -import { ModelRules } from '../assets/models.js' import { BlockstateRules } from '../assets/blockstates.js' +import { ModelRules } from '../assets/models.js' +import CustomEmitter from '../custom.js' import { LootRules } from '../data/loot.js' import { ClearableEmitter } from '../index.js' -import { Acceptor } from '@pssbletrngle/pack-resolver' export type BlockDefinitionOptions = Readonly<{ blockstate?: boolean diff --git a/src/emit/content/innerBlockDefinition.ts b/src/emit/content/innerBlockDefinition.ts index e4082ae..c1448ee 100644 --- a/src/emit/content/innerBlockDefinition.ts +++ b/src/emit/content/innerBlockDefinition.ts @@ -1,15 +1,15 @@ -import { AbstractBlockDefinitionRules, BlockDefinitionRules } from './blockDefinition.js' +import { IdInput } from '../../common/id.js' +import { BlockDefinition } from '../../schema/content/blockDefinition.js' +import { BlockstateRules } from '../assets/blockstates.js' import { ModelRules } from '../assets/models.js' import { LootRules } from '../data/loot.js' -import { BlockstateRules } from '../assets/blockstates.js' -import { BlockDefinition } from '../../schema/content/blockDefinition.js' -import { IdInput } from '../../common/id.js' +import { AbstractBlockDefinitionRules, BlockDefinitionRules } from './blockDefinition.js' /* eslint-disable @typescript-eslint/no-explicit-any */ class InnerBlockDefinitionRules extends AbstractBlockDefinitionRules { - add(id: IdInput, definition: T): T { - return definition + add(id: IdInput | T, definition?: T): T { + return definition ?? (id as T) } } diff --git a/src/schema/content/blockDefinition.ts b/src/schema/content/blockDefinition.ts index c3f20e0..819d03d 100644 --- a/src/schema/content/blockDefinition.ts +++ b/src/schema/content/blockDefinition.ts @@ -1,5 +1,5 @@ export type BlockProperties = Readonly<{ - material: string + material?: string strength?: number }> diff --git a/test/content.test.ts b/test/content.test.ts index 184d9a1..131dae5 100644 --- a/test/content.test.ts +++ b/test/content.test.ts @@ -1,5 +1,5 @@ -import setupLoader from './shared/loaderSetup.js' import createTestAcceptor from './mock/TestAcceptor.js' +import setupLoader from './shared/loaderSetup.js' const { loader } = setupLoader({ load: false }) @@ -68,4 +68,32 @@ describe('integration with content packs mod', () => { }, }) }) + + it('uses custom definition types for nested block', async () => { + const acceptor = createTestAcceptor() + + loader.content.blocks.basic('example:sapphire_block', { material: 'stone', type: 'example_block' }) + loader.content.items.basic('example:sapphire', { type: 'example' }) + loader.content.items.blockItem('example:sapphire_ore', { + type: 'example_block_item', + block: blocks => + blocks.add({ + type: 'example_block', + properties: { + material: 'ice', + }, + }), + }) + + await loader.emit(acceptor) + + expect(acceptor.jsonAt('content/example/block/sapphire_block.json')).toMatchObject({ type: 'example_block' }) + expect(acceptor.jsonAt('content/example/item/sapphire.json')).toMatchObject({ type: 'example' }) + expect(acceptor.jsonAt('content/example/item/sapphire_ore.json')).toMatchObject({ + type: 'example_block_item', + block: { + type: 'example_block', + }, + }) + }) })