Skip to content

Commit

Permalink
feat(world): starts of item stack merging
Browse files Browse the repository at this point in the history
  • Loading branch information
PMK744 committed Jul 9, 2024
1 parent e746a2c commit 2a34d5e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
65 changes: 65 additions & 0 deletions packages/world/src/components/entity/item.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { EntityIdentifier } from "@serenityjs/entity";
import {
ActorEventIds,
ActorEventPacket,
LevelSoundEvent,
LevelSoundEventPacket,
TakeItemActorPacket,
Expand Down Expand Up @@ -94,10 +96,73 @@ class EntityItemComponent extends EntityComponent {
this.pickupTick = this.entity.dimension.world.currentTick;
}

public increment(amount?: number): void {
this.itemStack.increment(amount);

const packet = new ActorEventPacket();
packet.eventId = ActorEventIds.ITEM_ENTITY_MERGE;
packet.eventData = this.itemStack.amount;
packet.actorRuntimeId = this.entity.runtime;

this.entity.dimension.broadcast(packet);
}

public decrement(amount?: number): void {
this.itemStack.decrement(amount);

const packet = new ActorEventPacket();
packet.eventId = ActorEventIds.ITEM_ENTITY_MERGE;
packet.eventData = this.itemStack.amount;
packet.actorRuntimeId = this.entity.runtime;

this.entity.dimension.broadcast(packet);
}

public onTick(): void {
// Get the current tick
const current = this.entity.dimension.world.currentTick;

// Check if the item is on the ground and the current tick is a multiple of 25
if (
this.entity.onGround &&
this.entity.dimension.world.currentTick % 25n === 0n
) {
// Get all the item entities in the dimension
const entities = this.entity.dimension
.getEntities()
.filter((x) => x.isItem());

// Check if there is a existing item stack nearby within a 0.5 block radius
for (const entity of entities) {
// Check if the entity is the same as the item
if (entity === this.entity) continue;

// Calculate the distance between the entities
const distance = entity.position.subtract(this.entity.position);

// Check if the distance is less than 0.5 blocks
if (
Math.abs(distance.x) <= 0.75 &&
Math.abs(distance.y) <= 0.75 &&
Math.abs(distance.z) <= 0.75
) {
// Get the item component of the entity
const component = entity.getComponent(
EntityItemComponent.identifier
) as EntityItemComponent;

// Check if the item stacks are the same
if (component.itemStack.equals(this.itemStack)) {
// Increment the item stack
component.increment(this.itemStack.amount);

// Remove the item from the dimension
this.entity.despawn();
}
}
}
}

// Check if there is a target player
if (
this.target && // Check if the pickup tick is not null
Expand Down
14 changes: 14 additions & 0 deletions packages/world/src/item/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ class ItemStack<T extends keyof Items = keyof Items> {
this.setAmount(this.amount + (amount ?? 1));
}

/**
* Checks if the item stack is equal to another item stack.
* @param item The item stack to compare.
* @returns If the item stack is equal to the other item stack.
*/
public equals(item: ItemStack): boolean {
// TODO: Check if the item nbts are equal, and if the item components are equal.

return (
this.type.identifier === item.type.identifier &&
this.metadata === item.metadata
);
}

/**
* Gets a component from the item.
* @param identifier The identifier of the component.
Expand Down

0 comments on commit 2a34d5e

Please sign in to comment.