Skip to content

Commit

Permalink
feat(world): added EntityVariantComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
PMK744 committed Jul 17, 2024
1 parent 8bda783 commit 1c1af6b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
5 changes: 1 addition & 4 deletions packages/world/src/components/entity/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
export * from "./data";

// Concrete components
// export * from "./has-gravity";
// export * from "./breathing";
export * from "./nametag";
export * from "./always-show-nametag";
export * from "./scale";
export * from "./bounding-height";
export * from "./bounding-width";
export * from "./skin-id";
// export * from "./visible";
// export * from "./on-fire";
export * from "./variant";
33 changes: 33 additions & 0 deletions packages/world/src/components/entity/data/variant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ActorDataId, ActorDataType } from "@serenityjs/protocol";

import { EntityDataComponent } from "./data";

import type { Entity } from "../../../entity";

class EntityVariantComponent extends EntityDataComponent<number> {
public static readonly identifier = "minecraft:variant";

public readonly key = ActorDataId.Variant;

public readonly type = ActorDataType.Int;

public defaultValue = 0;

/**
* Set a variant for the entity.
*
* @param entity The entity the component is binded to.
* @returns A new entity scale component
*/
public constructor(entity: Entity) {
super(entity, EntityVariantComponent.identifier);

// Check if the entity is a player
if (entity.isPlayer()) this.defaultValue = 1;

// Set the entity to have a variant
this.setCurrentValue(this.defaultValue, false);
}
}

export { EntityVariantComponent };
37 changes: 36 additions & 1 deletion packages/world/src/entity/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import {
EntityEffectsComponent,
EntityHealthComponent,
EntityNametagComponent,
EntityOnFireComponent
EntityOnFireComponent,
EntityVariantComponent
} from "../components";
import { ItemStack } from "../item";

Expand Down Expand Up @@ -791,6 +792,40 @@ class Entity {
this.removeComponent("minecraft:on_fire");
}

/**
* Gets the variant of the entity.
* @note This method is dependant on the entity having a `minecraft:variant` component, if not will result in an `error`.
* @returns The variant of the entity.
*/
public getVariant(): number {
// Check if the entity has a variant component
if (!this.hasComponent("minecraft:variant"))
throw new Error("The entity does not have a variant component.");

// Get the variant component
const variant = this.getComponent("minecraft:variant");

// Return the current variant value
return variant.getCurrentValue();
}

/**
* Sets the variant of the entity.
* @note This method is dependant on the entity having a `minecraft:variant` component, if the component does not exist it will be created.
* @param variant The variant to set.
*/
public setVariant(variant: number): void {
// Check if the entity has a variant component
if (!this.hasComponent("minecraft:variant"))
new EntityVariantComponent(this);

// Get the variant component
const variantComponent = this.getComponent("minecraft:variant");

// Set the variant value
variantComponent.setCurrentValue(variant);
}

/**
* Gets the cardinal direction of the entity.
* @returns The cardinal direction of the entity.
Expand Down
4 changes: 3 additions & 1 deletion packages/world/src/types/components/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import type {
EntityNametagComponent,
EntityOnFireComponent,
EntityPhysicsComponent,
EntityScaleComponent
EntityScaleComponent,
EntityVariantComponent
} from "../../components";

/**
Expand All @@ -38,6 +39,7 @@ interface EntityMetadataComponents {
"minecraft:physics": EntityPhysicsComponent;
"minecraft:is_visible": EntityIsVisibleComponent;
"minecraft:on_fire": EntityOnFireComponent;
"minecraft:variant": EntityVariantComponent;
}

/**
Expand Down

0 comments on commit 1c1af6b

Please sign in to comment.