|
| 1 | +import { Player } from '@engine/world/actor'; |
| 2 | +import { Item, WorldItem } from '@engine/world'; |
| 3 | +import { ActionHook, getActionHooks, questHookFilter, ActionPipe, RunnableHooks } from '@engine/action'; |
| 4 | + |
| 5 | + |
| 6 | +/** |
| 7 | + * Defines an item-on-world-item action hook. |
| 8 | + * |
| 9 | + * @author jameskmonger |
| 10 | + */ |
| 11 | +export interface ItemOnWorldItemActionHook extends ActionHook<ItemOnWorldItemAction, itemOnWorldItemActionHandler> { |
| 12 | + /** |
| 13 | + * The item pairs being used. Both items are optional so that you can specify a single item, a pair of items, or neither. |
| 14 | + */ |
| 15 | + items: { item?: number, worldItem?: number }[]; |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +/** |
| 20 | + * The item-on-world-item action hook handler function to be called when the hook's conditions are met. |
| 21 | + */ |
| 22 | +export type itemOnWorldItemActionHandler = (itemOnWorldItemAction: ItemOnWorldItemAction) => void; |
| 23 | + |
| 24 | + |
| 25 | +/** |
| 26 | + * Details about an item-on-world-item action being performed. |
| 27 | + * |
| 28 | + * @author jameskmonger |
| 29 | + */ |
| 30 | +export interface ItemOnWorldItemAction { |
| 31 | + /** |
| 32 | + * The player performing the action. |
| 33 | + */ |
| 34 | + player: Player; |
| 35 | + |
| 36 | + /** |
| 37 | + * The item being used. |
| 38 | + */ |
| 39 | + usedItem: Item; |
| 40 | + |
| 41 | + /** |
| 42 | + * The WorldItem that the first item is being used on. |
| 43 | + */ |
| 44 | + usedWithItem: WorldItem; |
| 45 | + |
| 46 | + /** |
| 47 | + * The ID of the UI widget that the item being used is in. |
| 48 | + */ |
| 49 | + usedWidgetId: number; |
| 50 | + |
| 51 | + /** |
| 52 | + * The ID of the container that the item being used is in. |
| 53 | + */ |
| 54 | + usedContainerId: number; |
| 55 | + |
| 56 | + /** |
| 57 | + * The slot within the container that the item being used is in. |
| 58 | + */ |
| 59 | + usedSlot: number; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * The pipe that the game engine hands item-on-world-item actions off to. |
| 64 | + * |
| 65 | + * This will call the `item_on_world_item` action hooks, if any are registered and match the action being performed. |
| 66 | + * |
| 67 | + * Both `item` and `worldItem` are optional, but if they are provided then they must match the items in use. |
| 68 | + * |
| 69 | + * @author jameskmonger |
| 70 | + */ |
| 71 | +const itemOnWorldItemActionPipe = ( |
| 72 | + player: Player, |
| 73 | + usedItem: Item, usedWithItem: WorldItem, |
| 74 | + usedWidgetId: number, usedContainerId: number, usedSlot: number |
| 75 | +): RunnableHooks<ItemOnWorldItemAction> => { |
| 76 | + if(player.busy) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + // Find all item on item action plugins that match this action |
| 81 | + let matchingHooks = getActionHooks<ItemOnWorldItemActionHook>('item_on_world_item', plugin => { |
| 82 | + if(questHookFilter(player, plugin)) { |
| 83 | + const used = usedItem.itemId; |
| 84 | + const usedWith = usedWithItem.itemId; |
| 85 | + |
| 86 | + return (plugin.items.some(({ item, worldItem }) => { |
| 87 | + const itemMatch = item === undefined || item === used; |
| 88 | + const worldItemMatch = worldItem === undefined || worldItem === usedWith; |
| 89 | + |
| 90 | + return itemMatch && worldItemMatch; |
| 91 | + })); |
| 92 | + } |
| 93 | + |
| 94 | + return false; |
| 95 | + }); |
| 96 | + |
| 97 | + const questActions = matchingHooks.filter(plugin => plugin.questRequirement !== undefined); |
| 98 | + |
| 99 | + if(questActions.length !== 0) { |
| 100 | + matchingHooks = questActions; |
| 101 | + } |
| 102 | + |
| 103 | + if(matchingHooks.length === 0) { |
| 104 | + player.outgoingPackets.chatboxMessage( |
| 105 | + `Unhandled item on world item interaction: ${usedItem.itemId} on ${usedWithItem.itemId}`); |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + return { |
| 110 | + hooks: matchingHooks, |
| 111 | + action: { |
| 112 | + player, |
| 113 | + usedItem, usedWithItem, |
| 114 | + usedWidgetId, usedContainerId, usedSlot |
| 115 | + } |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | + |
| 120 | +/** |
| 121 | + * Item-on-world-item action pipe definition. |
| 122 | + */ |
| 123 | +export default [ 'item_on_world_item', itemOnWorldItemActionPipe ] as ActionPipe; |
0 commit comments