Skip to content

Commit

Permalink
feat(entity): added CustomEntityType
Browse files Browse the repository at this point in the history
  • Loading branch information
PMK744 committed Sep 21, 2024
1 parent 51b1502 commit 13eca55
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 8 deletions.
3 changes: 0 additions & 3 deletions packages/block/src/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ class CustomBlockType extends BlockType {

// Construct the NBT tag.
this.nbt = new CompoundTag("", {});

// Register the block type.
BlockType.types.set(identifier, this);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions packages/entity/src/custom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { EntityType } from "./type";

import type { EntityIdentifier } from "./enums";

class CustomEntityType extends EntityType {
/**
* Create a new custom entity type.
* @param identifier The identifier of the custom entity type.
* @param components The default components of the custom entity type.
*/
public constructor(identifier: string, components?: Array<string>) {
super(identifier as EntityIdentifier, components);
}
}

export { CustomEntityType };
1 change: 1 addition & 0 deletions packages/entity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import "./registry";

export * from "./enums";
export * from "./type";
export * from "./custom";
10 changes: 10 additions & 0 deletions packages/entity/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ class EntityType {
*/
public static readonly types = new Map<string, EntityType>();

/**
* The network identifier counter for entity types.
*/
public static network = 1;

/**
* The identifier of the entity type.
*/
public readonly identifier: EntityIdentifier;

/**
* The network id of the entity type.
*/
public readonly network: number = EntityType.network++;

/**
* The default components of the entity type.
*/
Expand Down
3 changes: 0 additions & 3 deletions packages/item/src/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ class CustomItemType extends ItemType {
this.category = category ?? ItemCategory.None;
this.group = group ?? null;

// Register the item type.
ItemType.types.set(identifier, this);

// If the custom item type has a creative category, register it as a creative item.
if (this.category !== ItemCategory.None) {
// Register the creative item.
Expand Down
26 changes: 24 additions & 2 deletions packages/serenity/src/handlers/resource-pack-client-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import {
DisconnectReason,
type BlockProperties,
ResourceIdVersions,
CraftingDataPacket
CraftingDataPacket,
AvailableActorIdentifiersPacket
} from "@serenityjs/protocol";
import { BIOME_DEFINITION_LIST, CRAFTING_DATA } from "@serenityjs/data";
import { CreativeItem, CustomItemType, ItemType } from "@serenityjs/item";
import { PlayerStatus } from "@serenityjs/world";
import { CompoundTag, Tag } from "@serenityjs/nbt";

import { ResourcePack } from "../resource-packs/resource-pack-manager";

Expand Down Expand Up @@ -476,11 +478,31 @@ class ResourcePackClientResponse extends SerenityHandler {
};
});

// Map the entities to a nbt tag
const entities = world.entities.getAllTypes().map((type) => {
const tag = new CompoundTag();
tag.createStringTag("bid", "");
tag.createByteTag("hasspawnegg", 1);
tag.createStringTag("id", type.identifier);
tag.createIntTag("rid", type.network);
tag.createByteTag("summonable", 1);

return tag;
});

// Create root compound tag
const root = new CompoundTag();
root.createListTag("idlist", Tag.Compound, entities);

// Create the actors packet
const actors = new AvailableActorIdentifiersPacket();
actors.data = root;

const status = new PlayStatusPacket();
status.status = PlayStatus.PlayerSpawn;

// Send the spawn sequence
session.send(packet, biomes, content, status, crafting);
session.send(packet, biomes, content, status, crafting, actors);

// Add the player to the connecting map
this.serenity.connecting.set(player.session, [
Expand Down
31 changes: 31 additions & 0 deletions packages/world/src/entity/palette.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { type EntityIdentifier, EntityType } from "@serenityjs/entity";
import { CustomEntityType } from "@serenityjs/entity";

import {
type EntityComponent,
ENTITY_COMPONENTS,
type PlayerComponent,
PLAYER_COMPONENTS
} from "../components";
import { EntityEnum } from "../commands";

class EntityPalette {
/**
Expand Down Expand Up @@ -50,6 +52,16 @@ class EntityPalette {
return [...this.types.values()];
}

/**
* Gets all custom entity types from the palette.
* @returns All custom entity types from the palette.
*/
public getAllCustomTypes(): Array<CustomEntityType> {
return [...this.types.values()].filter(
(type) => type instanceof CustomEntityType
) as Array<CustomEntityType>;
}

/**
* Gets an entity type from the palette.
* @param identifier The entity identifier to get.
Expand All @@ -59,6 +71,25 @@ class EntityPalette {
return this.types.get(identifier) as EntityType;
}

/**
* Register an entity type to the palette.
* @param type The entity type to register.
* @returns True if the entity type was registered, false otherwise.
*/
public registerType(type: EntityType): boolean {
// Check if the entity type is already registered.
if (this.types.has(type.identifier)) return false;

// Register the entity type.
this.types.set(type.identifier, type);

// Register the entity type in the entity enum.
EntityEnum.options.push(type.identifier);

// Return true if the entity type was registered.
return true;
}

/**
* Get the registry for the given entity identifier.
* @param identifier The entity identifier to get the registry for.
Expand Down

0 comments on commit 13eca55

Please sign in to comment.