Some optimizations

This commit is contained in:
2026-10-10 20:36:00 -03:00
parent bf91450a9d
commit afe6e03d4c
3 changed files with 78 additions and 26 deletions
+7 -5
View File
@@ -119,20 +119,22 @@ export class ItemEntity extends Entity {
return true;
}
// whether two stacks could be one
// whether two stacks could be one. the cheap checks first, comparing data last
can_merge_with(other: ItemEntity) {
return other !== this && !other.removed && other.item.type_id === this.item.type_id &&
JSON.stringify(other.item.data) === JSON.stringify(this.item.data) &&
this.item.amount < this.item.max_amount && other.item.amount < other.item.max_amount;
this.item.amount < this.item.max_amount && other.item.amount < other.item.max_amount &&
(other.item.data === this.item.data || JSON.stringify(other.item.data) === JSON.stringify(this.item.data));
}
// the smaller stack goes into the bigger one, like minecraft's ItemEntity.tryToMerge
merge_with_neighbours() {
// a full stack can't take more or give any, so there's no one to look for
if (this.item.amount >= this.item.max_amount) return;
const reach = MERGE_DISTANCE + ITEM_SIZE;
for (const other of this.level.entities_near(this.x, this.y, this.z, reach)) {
if (this.removed) return;
if (!(other instanceof ItemEntity) || !this.can_merge_with(other)) continue;
if (Math.abs(this.y - other.y) > ITEM_SIZE) continue;
if (!(other instanceof ItemEntity) || Math.abs(this.y - other.y) > ITEM_SIZE) continue;
if (!this.can_merge_with(other)) continue;
const [into, from] = this.item.amount >= other.item.amount ? [this, other] : [other, this];
const moving = Math.min(from.item.amount, into.item.max_amount - into.item.amount);