Skip to content

Commit

Permalink
feat(world): components now contain a static property "types"
Browse files Browse the repository at this point in the history
  • Loading branch information
PMK744 committed Jul 22, 2024
1 parent d7f8707 commit 89e887b
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/world/src/components/block/block-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class BlockComponent extends Component {
*/
public static readonly components = new Map<string, typeof BlockComponent>();

/**
* The block type identifiers to bind the component to.
*/
public static readonly types: Array<BlockIdentifier> = [];

/**
* The block the component is binded to.
*/
Expand Down
5 changes: 4 additions & 1 deletion packages/world/src/components/block/crafting-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { BlockNBTComponent } from "./nbt";
import type { CraftingTableComponentNBT } from "../../types";
import type { Block } from "../../block";

/**
* @deprecated
*/
class BlockCraftingTableComponent extends BlockNBTComponent {
public static readonly identifier = "minecraft:crafting_table";

Expand Down Expand Up @@ -52,7 +55,7 @@ class BlockCraftingTableComponent extends BlockNBTComponent {
);

// Create a new inventory component for the block.
new BlockInventoryComponent(block, container);
new BlockInventoryComponent(block).container = container;
}

public static serialize(): CompoundTag<CraftingTableComponentNBT> {
Expand Down
5 changes: 5 additions & 0 deletions packages/world/src/components/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class Component {
*/
public static readonly identifier: string;

/**
* The types to bind the component to.
*/
public static readonly types: Array<unknown>;

/**
* The identifier of the component.
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/world/src/components/entity/entity-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class EntityComponent extends Component {
*/
public static readonly components = new Map<string, typeof EntityComponent>();

/**
* The entity type identifiers to bind the component to.
*/
public static readonly types: Array<EntityIdentifier> = [];

/**
* The entity the component is binded to.
*/
Expand Down
40 changes: 40 additions & 0 deletions packages/world/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* eslint-disable import/order */
export * from "./component";
export * from "./entity";
export * from "./player";
export * from "./item";
export * from "./block";

import { ItemType } from "@serenityjs/item";
//
// Register all valid entity components.
//
Expand All @@ -12,6 +14,18 @@ import * as EntityComponents from "./entity";
for (const key in EntityComponents) {
const value = EntityComponents[key as keyof typeof EntityComponents];

// Iterate over the entity types.
for (const identifier of value.types) {
// Get the entity type.
const type = EntityType.get(identifier);

// Check if the entity type exists.
if (!type) continue;

// Register the component to the entity type.
value.register(type);
}

// Register the component to the collective registry.
EntityComponent.components.set(
value.identifier,
Expand All @@ -27,6 +41,18 @@ import * as ItemComponents from "./item";
for (const key in ItemComponents) {
const value = ItemComponents[key as keyof typeof ItemComponents];

// Iterate over the item types.
for (const identifier of value.types) {
// Get the item type.
const type = ItemType.get(identifier);

// Check if the item type exists.
if (!type) continue;

// Register the component to the item type.
value.register(type);
}

// Register the component to the collective registry.
ItemComponent.components.set(value.identifier, value as typeof ItemComponent);
}
Expand All @@ -36,9 +62,23 @@ for (const key in ItemComponents) {
//
import { BlockComponent } from "./block";
import * as BlockComponents from "./block";
import { BlockType } from "@serenityjs/block";
import { EntityType } from "@serenityjs/entity";
for (const key in BlockComponents) {
const value = BlockComponents[key as keyof typeof BlockComponents];

// Iterate over the block types.
for (const identifier of value.types) {
// Get the block type.
const type = BlockType.get(identifier);

// Check if the block type exists.
if (!type) continue;

// Register the component to the block type.
value.register(type);
}

// Register the component to the collective registry.
BlockComponent.components.set(
value.identifier,
Expand Down
5 changes: 5 additions & 0 deletions packages/world/src/components/item/item-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class ItemComponent<T extends keyof Items> extends Component {
*/
public static readonly components = new Map<string, typeof ItemComponent>();

/**
* The item the component is binded to.
*/
public static readonly types: Array<ItemIdentifier> = [];

/**
* The item the component is binded to.
*/
Expand Down

0 comments on commit 89e887b

Please sign in to comment.