Skip to content

Commit

Permalink
fix(world): fixed item onStartUse and onStopUse methods
Browse files Browse the repository at this point in the history
  • Loading branch information
PMK744 committed Jul 17, 2024
1 parent 43c0b25 commit 8bda783
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test

This file was deleted.

39 changes: 38 additions & 1 deletion packages/serenity/src/handlers/inventory-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
Vector3f,
type ItemUseInventoryTransaction,
Gamemode,
type ItemUseOnEntityInventoryTransaction
type ItemUseOnEntityInventoryTransaction,
type ItemReleaseInventoryTransaction
} from "@serenityjs/protocol";
import { ItemUseCause, type Player } from "@serenityjs/world";
import { BlockIdentifier, BlockPermutation } from "@serenityjs/block";
Expand Down Expand Up @@ -64,6 +65,18 @@ class InventoryTransaction extends SerenityHandler {
this.handleItemUseOnEntityTransaction(itemUse, player);
break;
}

case ComplexInventoryTransaction.ItemReleaseTransaction: {
// Get the itemRelease object from the transaction
const itemRelease = packet.transaction.itemRelease;

// Check if the itemRelease object is valid, if not throw an error that the itemRelease object is missing.
if (!itemRelease) throw new Error("ItemRelease object is missing.");

// Handle the itemRelease transaction
this.handleItemReleaseTransaction(itemRelease, player);
break;
}
}
}

Expand Down Expand Up @@ -193,6 +206,11 @@ class InventoryTransaction extends SerenityHandler {
// Set the player's using item
player.usingItem = usingItem;

// Trigger the onStartUse method of the item components
for (const component of usingItem.components.values()) {
component.onStartUse?.(player, ItemUseCause.Use);
}

// Break the switch statement
break;
}
Expand Down Expand Up @@ -234,6 +252,25 @@ class InventoryTransaction extends SerenityHandler {
component.onInteract?.(player, transaction.type);
}
}

public static handleItemReleaseTransaction(
transaction: ItemReleaseInventoryTransaction,
player: Player
): void {
// Get the using item of the player
const usingItem = player.usingItem;

// Check if the using item is valid
if (!usingItem) return;

// Trigger the onRelease method of the item components
for (const component of usingItem.components.values()) {
component.onStopUse?.(player, ItemUseCause.Use);
}

// Set the player's using item to null
player.usingItem = null;
}
}

export { InventoryTransaction };

0 comments on commit 8bda783

Please sign in to comment.