Skip to content

Commit

Permalink
Merge pull request #412 from runejs/player-interactions-task
Browse files Browse the repository at this point in the history
refactor: convert player_interaction and item_on_player to task system
  • Loading branch information
Jameskmonger authored Jul 11, 2023
2 parents 2753326 + f1e556f commit 9dc557d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
29 changes: 9 additions & 20 deletions src/engine/action/pipe/item-on-player.action.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { logger } from '@runejs/common';

import { Player } from '@engine/world/actor';
import { Position, Item } from '@engine/world';
import { ActionHook, getActionHooks, advancedNumberHookFilter, questHookFilter, ActionPipe } from '@engine/action';
import { playerWalkTo } from '@engine/plugins';
import { WalkToActorPluginTask } from './task/walk-to-actor-plugin-task';


/**
Expand Down Expand Up @@ -77,23 +75,14 @@ const itemOnPlayerActionPipe = (player: Player, otherPlayer: Player, position: P
const walkToPlugins = interactionActions.filter(plugin => plugin.walkTo);
const immediateHooks = interactionActions.filter(plugin => !plugin.walkTo);

// Make sure we walk to the player before running any of the walk-to plugins
if(walkToPlugins.length !== 0) {
playerWalkTo(player, position)
.then(() => {
player.face(position);

walkToPlugins.forEach(plugin =>
plugin.handler({
player,
otherPlayer,
position,
item,
itemWidgetId,
itemContainerId
}));
})
.catch(() => logger.warn(`Unable to complete walk-to action.`));
if (walkToPlugins.length > 0) {
player.enqueueBaseTask(new WalkToActorPluginTask(walkToPlugins, player, 'otherPlayer', otherPlayer, {
item,
itemWidgetId,
itemContainerId
}));

return;
}

// Immediately run any non-walk-to plugins
Expand Down
10 changes: 9 additions & 1 deletion src/engine/action/pipe/player-interaction.action.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Player } from '@engine/world/actor';
import { Position } from '@engine/world';
import { ActionHook, getActionHooks, stringHookFilter, questHookFilter, RunnableHooks } from '@engine/action';
import { playerWalkTo } from '@engine/plugins';
import { WalkToActorPluginTask } from './task/walk-to-actor-plugin-task';


/**
Expand Down Expand Up @@ -57,6 +57,14 @@ const playerInteractionActionPipe = (player: Player, otherPlayer: Player, positi
return null;
}

const walkToPlugins = matchingHooks.filter(plugin => plugin.walkTo);

if (walkToPlugins.length > 0) {
player.enqueueBaseTask(new WalkToActorPluginTask(walkToPlugins, player, 'otherPlayer', otherPlayer, {}));

return null;
}

return {
hooks: matchingHooks,
actionPosition: position,
Expand Down
6 changes: 4 additions & 2 deletions src/engine/action/pipe/task/walk-to-actor-plugin-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ import { ActionHook } from '@engine/action/hook';
import { NpcInteractionAction } from '../npc-interaction.action';
import { ActorActorInteractionTask } from '@engine/task/impl/actor-actor-interaction-task';
import { ItemOnNpcAction } from '../item-on-npc.action';
import { PlayerInteractionAction } from '../player-interaction.action';
import { ItemOnPlayerAction } from '../item-on-player.action';

/**
* All actions supported by this plugin task.
*/
type ActorAction = NpcInteractionAction | ItemOnNpcAction;
type ActorAction = PlayerInteractionAction | ItemOnPlayerAction | NpcInteractionAction | ItemOnNpcAction;

/**
* An ActionHook for a supported ObjectAction.
*/
type ActorActionHook<TAction extends ActorAction> = ActionHook<TAction, (data: TAction) => void>;

type ActorKey = 'npc';
type ActorKey = 'otherPlayer' | 'npc';

/**
* The data unique to the action being executed (i.e. excluding shared data)
Expand Down

0 comments on commit 9dc557d

Please sign in to comment.