Skip to content

Commit

Permalink
feat: adding source item for components in the context object (#316)
Browse files Browse the repository at this point in the history
* feat: adding source item for components in the context object

* Refining the context
  • Loading branch information
DaanV2 authored Feb 8, 2025
1 parent a592ec3 commit f5d4a35
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const component_dependents_any: DependedMap = {
* @param diagnoser The diagnoser to report to*/
export function behaviorpack_block_components_dependencies(
block: Internal.BehaviorPack.Block,
context: Context,
context: Context<Internal.BehaviorPack.Block>,
diagnoser: DiagnosticsBuilder
): void {
components_dependencies("block", context, diagnoser, component_dependents_all, component_dependents_any);
Expand Down
25 changes: 14 additions & 11 deletions src/Lib/Diagnostics/BehaviorPack/Block/components/diagnose.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ComponentBehavior } from 'bc-minecraft-bedrock-types/lib/minecraft/components';
import { ComponentBehavior } from "bc-minecraft-bedrock-types/lib/minecraft/components";
import { DiagnosticSeverity, DocumentDiagnosticsBuilder } from "../../../../Types";
import { Context } from "../../../../utility/components";
import { component_error, ComponentCheck, components_check } from "../../../../utility/components/checks";
import { check_geo_and_rules } from "../../../ResourcePack/BlockCulling";
import { resourcepack_has_model } from "../../../ResourcePack/Model/diagnose";
import { behaviorpack_loot_table_diagnose } from "../../Loot Table";
import { behaviorpack_check_blockid } from "../diagnose";
import { Internal } from "bc-minecraft-bedrock-project";

/**
*
Expand All @@ -15,13 +16,13 @@ import { behaviorpack_check_blockid } from "../diagnose";
*/
export function behaviorpack_diagnose_block_components(
container: ComponentBehavior,
context: Context,
context: Context<Internal.BehaviorPack.Block>,
diagnoser: DocumentDiagnosticsBuilder
): void {
components_check(container, context, diagnoser, component_test);
}

const component_test: Record<string, ComponentCheck> = {
const component_test: Record<string, ComponentCheck<Internal.BehaviorPack.Block>> = {
"minecraft:destroy_time": deprecated_component("minecraft:destructible_by_mining"),
"minecraft:block_light_emission": deprecated_component("minecraft:light_emission"),
"minecraft:block_light_absorption": deprecated_component("minecraft:light_dampening"),
Expand Down Expand Up @@ -68,8 +69,8 @@ const component_test: Record<string, ComponentCheck> = {
"minecraft:placement_filter": (name, component, context, diagnoser) => {
for (const condition of component.conditions) {
condition.block_filter?.forEach((block: string | { name: string }) => {
if (typeof block == 'object' && 'name' in block) behaviorpack_check_blockid(block.name, diagnoser)
else if (typeof block == 'string') behaviorpack_check_blockid(block, diagnoser);
if (typeof block == "object" && "name" in block) behaviorpack_check_blockid(block.name, diagnoser);
else if (typeof block == "string") behaviorpack_check_blockid(block, diagnoser);
});
}
},
Expand All @@ -87,15 +88,17 @@ const component_test: Record<string, ComponentCheck> = {
if (typeof component === "string") behaviorpack_loot_table_diagnose(component, diagnoser);
},
"minecraft:material_instances": (name, component, context, diagnoser) => {
Object.keys(component).forEach(value => {
Object.keys(component).forEach((value) => {
const textureId = component[value].texture;
if (!diagnoser.context.getCache().resourcePacks.terrainTextures.find(val => val.id == textureId))
diagnoser.add(textureId,
if (!diagnoser.context.getCache().resourcePacks.terrainTextures.find((val) => val.id == textureId))
diagnoser.add(
textureId,
`Texture reference "${textureId}" was not defined in terrain_texture.json`,
DiagnosticSeverity.error,
'behaviorpack.block.components.texture_not_found')
})
}
"behaviorpack.block.components.texture_not_found"
);
});
},
};

function deprecated_component(replacement?: string) {
Expand Down
17 changes: 9 additions & 8 deletions src/Lib/Diagnostics/BehaviorPack/Block/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export function Diagnose(diagnoser: DocumentDiagnosticsBuilder): void {
if (!Internal.BehaviorPack.Block.is(block)) return;

//check components
const context: Context = {
const context: Context<Internal.BehaviorPack.Block> = {
source: block,
components: getUsedComponents(block["minecraft:block"]),
};
block["minecraft:block"]?.permutations?.forEach((p) => {
Expand All @@ -33,11 +34,11 @@ export function Diagnose(diagnoser: DocumentDiagnosticsBuilder): void {

behaviorpack_block_components_dependencies(block, context, diagnoser);

if (block["minecraft:block"]["events"]) diagnoser.add(
`events`,
`Block events have been deprecated in favour of \`minecraft:custom_components\`.`,
DiagnosticSeverity.error,
"behaviorpack.block.deprecated"
);

if (block["minecraft:block"]["events"])
diagnoser.add(
`events`,
`Block events have been deprecated in favour of \`minecraft:custom_components\`.`,
DiagnosticSeverity.error,
"behaviorpack.block.deprecated"
);
}
4 changes: 2 additions & 2 deletions src/Lib/Diagnostics/BehaviorPack/Entity/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { behaviorpack_diagnose_entity_components } from "./components/diagnose";

export function behaviorpack_entity_components_check(
entity: Internal.BehaviorPack.Entity,
context: Context,
context: Context<Internal.BehaviorPack.Entity>,
diagnoser: DocumentDiagnosticsBuilder
) {
const desc = entity["minecraft:entity"];
Expand All @@ -25,7 +25,7 @@ export function behaviorpack_entity_components_check(

function behaviorpack_entity_componentscontainer_check(
container: ComponentContainer | undefined | null,
context: Context,
context: Context<Internal.BehaviorPack.Entity>,
diagnoser: DocumentDiagnosticsBuilder
) {
if (container === null || typeof container !== "object") return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const component_dependents_any: DependedMap = {
* @param diagnoser The diagnoser to report to*/
export function behaviorpack_entity_components_dependencies(
entity: Internal.BehaviorPack.Entity,
context: Context,
context: Context<Internal.BehaviorPack.Entity>,
diagnoser: DiagnosticsBuilder
): void {
const components = context.components;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ComponentCheck, components_check } from "../../../../utility/components
import { behaviorpack_entity_components_filters } from './filters';
import { check_loot_table } from "./loot";
import { check_trade_table } from "./trade";
import { Internal } from 'bc-minecraft-bedrock-project';

/**
*
Expand All @@ -14,15 +15,15 @@ import { check_trade_table } from "./trade";
*/
export function behaviorpack_diagnose_entity_components(
container: ComponentBehavior,
context: Context,
context: Context<Internal.BehaviorPack.Entity>,
diagnoser: DocumentDiagnosticsBuilder
): void {
components_check(container, context, diagnoser, component_test);

behaviorpack_entity_components_filters(container, diagnoser);
}

const component_test: Record<string, ComponentCheck> = {
const component_test: Record<string, ComponentCheck<Internal.BehaviorPack.Entity>> = {
"minecraft:economy_trade_table": check_trade_table,
"minecraft:equipment": check_loot_table,
"minecraft:loot": check_loot_table,
Expand Down
12 changes: 8 additions & 4 deletions src/Lib/Diagnostics/BehaviorPack/Entity/components/loot.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { behaviorpack_loot_table_diagnose } from "../../Loot Table/diagnose";
import { DiagnosticsBuilder } from "../../../../Types";
import { Context } from '../../../../utility/components';

import { Context } from "../../../../utility/components";
import { behaviorpack_loot_table_diagnose } from "../../Loot Table/diagnose";

export function check_loot_table(name: string, component: any, context: Context, diagnoser: DiagnosticsBuilder): void {
export function check_loot_table<T>(
_name: string,
component: any,
_context: Context<T>,
diagnoser: DiagnosticsBuilder
): void {
if (component === undefined) return;
if (typeof component.table !== "string") return;
const table = component.table;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DiagnosticsBuilder } from "../../../../Types";
import { behaviorpack_trading_diagnose } from "../../Trading/diagnose";
import { Context } from '../../../../utility/components';

export function check_trade_table(name: string, component: any, context: Context, diagnoser: DiagnosticsBuilder): void {
export function check_trade_table<T>(_name: string, component: any, _context: Context<T>, diagnoser: DiagnosticsBuilder): void {
if (component === undefined) return;
if (typeof component.table !== "string") return;
const table = component.table;
Expand Down
3 changes: 2 additions & 1 deletion src/Lib/Diagnostics/BehaviorPack/Entity/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export function Diagnose(diagnoser: DocumentDiagnosticsBuilder): void {
//No resource-pack check, entities can exist without their rp side

//check components
const context: Context = {
const context: Context<Internal.BehaviorPack.Entity> = {
source: entity,
components: getUsedComponents(entity["minecraft:entity"]),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const component_dependents_any: DependedMap = {
* @param diagnoser The diagnoser to report to*/
export function behaviorpack_item_components_dependencies(
item: Internal.BehaviorPack.Item,
context: Context,
context: Context<Internal.BehaviorPack.Item>,
diagnoser: DiagnosticsBuilder
): void {
components_dependencies("item", context, diagnoser, component_dependents_all, component_dependents_any);
Expand Down
7 changes: 4 additions & 3 deletions src/Lib/Diagnostics/BehaviorPack/Item/components/diagnose.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Internal } from "bc-minecraft-bedrock-project";
import { ComponentBehavior } from "bc-minecraft-bedrock-types/lib/minecraft/components";
import { DiagnosticSeverity, DocumentDiagnosticsBuilder } from "../../../../Types";
import { Context } from "../../../../utility/components";
import { component_error, ComponentCheck, components_check } from "../../../../utility/components/checks";
import { minecraft_get_item } from "../../../Minecraft/Items";
import { behaviorpack_check_blockid } from "../../Block";
import { behaviorpack_entityid_diagnose } from "../../Entity";
import { behaviorpack_item_diagnose } from "../diagnose";
import { minecraft_get_item } from "../../../Minecraft/Items";

/**
*
Expand All @@ -15,13 +16,13 @@ import { minecraft_get_item } from "../../../Minecraft/Items";
*/
export function behaviorpack_diagnose_item_components(
container: ComponentBehavior,
context: Context,
context: Context<Internal.BehaviorPack.Item>,
diagnoser: DocumentDiagnosticsBuilder
): void {
components_check(container, context, diagnoser, component_test);
}

const component_test: Record<string, ComponentCheck> = {
const component_test: Record<string, ComponentCheck<Internal.BehaviorPack.Item>> = {
"minecraft:armor": deprecated_component("minecraft:wearable"),
"minecraft:chargeable": deprecated_component("minecraft:custom_components"),
"minecraft:creative_category": deprecated_component("description.menu_category"),
Expand Down
3 changes: 2 additions & 1 deletion src/Lib/Diagnostics/BehaviorPack/Item/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export function Diagnose(diagnoser: DocumentDiagnosticsBuilder): void {
if (!Internal.BehaviorPack.Item.is(item)) return;

//Check components
const context: Context = {
const context: Context<Internal.BehaviorPack.Item> = {
source: item,
components: getUsedComponents(item["minecraft:item"]),
};

Expand Down
29 changes: 17 additions & 12 deletions src/Lib/utility/components/checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@ import { ComponentBehavior, ComponentContainer } from "bc-minecraft-bedrock-type
import { DocumentDiagnosticsBuilder, DiagnosticSeverity } from "../../Types";
import { Context } from "./components";

export type ComponentCheck = (name: string, component: any, context: Context, diagnoser: DocumentDiagnosticsBuilder) => void;

export function component_error(message: string, code: string | number): ComponentCheck {
return (name, component, context, diagnoser) => {
export type ComponentCheck<T> = (
name: string,
component: any,
context: Context<T>,
diagnoser: DocumentDiagnosticsBuilder
) => void;

export function component_error<T>(message: string, code: string | number): ComponentCheck<T> {
return (name, _component, _context, diagnoser) => {
diagnoser.add(name, message, DiagnosticSeverity.error, code);
};
}

export function component_warning(message: string, code: string | number): ComponentCheck {
return (name, component, context, diagnoser) => {
export function component_warning<T>(message: string, code: string | number): ComponentCheck<T> {
return (name, _component, _context, diagnoser) => {
diagnoser.add(name, message, DiagnosticSeverity.warning, code);
};
}

export function components_check(
export function components_check<T>(
data: ComponentBehavior | undefined,
context: Context,
context: Context<T>,
diagnoser: DocumentDiagnosticsBuilder,
component_test: Record<string, ComponentCheck>
component_test: Record<string, ComponentCheck<T>>
): void {
if (data === undefined) return;

Expand All @@ -33,11 +38,11 @@ export function components_check(
}
}

function compContainerCheck(
function compContainerCheck<T>(
container: ComponentContainer | undefined,
context: Context,
context: Context<T>,
diagnoser: DocumentDiagnosticsBuilder,
component_test: Record<string, ComponentCheck>
component_test: Record<string, ComponentCheck<T>>
): void {
if (container === undefined) return;

Expand Down
12 changes: 6 additions & 6 deletions src/Lib/utility/components/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ export type DependedMap = Record<string, Depended[]>;
/**
*
*/
export interface Context {
/**
*
*/
export interface Context<T> {
/** The object / item that defined or carries the components */
source: Readonly<T>;
/** The components used by the source collected from multiple sources */
components: string[];
}

/**Checks if components dependencies are present, a component might need others to be present
* @param entity The entity to check
* @param entity The needed context
* @param diagnoser The diagnoser to report to*/
export function components_dependencies(
export function components_dependencies<T>(
owner: string,
context: Context,
context: Context<T>,
diagnoser: DiagnosticsBuilder,
component_dependents_all: DependedMap,
component_dependents_any: DependedMap
Expand Down

0 comments on commit f5d4a35

Please sign in to comment.