Skip to content

Commit

Permalink
Merge pull request #392 from Jameskmonger/strict-mode
Browse files Browse the repository at this point in the history
build: ♻️ 👷 🔧 turn on `strictNullChecks`
  • Loading branch information
Promises authored Feb 24, 2023
2 parents 9b1d4a8 + e6e66cf commit bc1b899
Show file tree
Hide file tree
Showing 131 changed files with 1,602 additions and 729 deletions.
4 changes: 2 additions & 2 deletions src/engine/action/action-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ export class ActionPipeline {
.subscribe(async () => this.cancelRunningTasks());
}

public static getPipe(action: ActionType): ActionPipeHandler {
return ActionPipeline.pipes.get(action);
public static getPipe(action: ActionType): ActionPipeHandler | null {
return ActionPipeline.pipes.get(action) || null;
}

public static register(action: ActionType, actionPipeHandlerFn: ActionPipeHandler): void {
Expand Down
8 changes: 4 additions & 4 deletions src/engine/action/hook/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@ export class TaskExecutor<T> {
public async run(): Promise<void> {
this.running = true;

/* eslint-disable @typescript-eslint/no-non-null-assertion */
if(!!this.task.delay || !!this.task.delayMs) {
await lastValueFrom(timer(this.task.delayMs !== undefined ? this.task.delayMs :
(this.task.delay * World.TICK_LENGTH)));
(this.task.delay! * World.TICK_LENGTH)));
}

if(!!this.task.interval || !!this.task.intervalMs) {
// Looping execution task
const intervalMs = this.task.intervalMs !== undefined ? this.task.intervalMs :
(this.task.interval * World.TICK_LENGTH);
(this.task.interval! * World.TICK_LENGTH);
/* eslint-enable @typescript-eslint/no-non-null-assertion */

await new Promise<void>(resolve => {
this.intervalSubscription = timer(0, intervalMs).subscribe(
Expand Down Expand Up @@ -142,8 +144,6 @@ export class TaskExecutor<T> {
if(this.task?.onComplete) {
await this.task.onComplete(this, this.iteration);
}

this.session = null;
}

public getDetails(): TaskDetails<T> {
Expand Down
2 changes: 1 addition & 1 deletion src/engine/action/pipe/button.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface ButtonAction {
* @param widgetId
* @param buttonId
*/
const buttonActionPipe = (player: Player, widgetId: number, buttonId: number): RunnableHooks<ButtonAction> => {
const buttonActionPipe = (player: Player, widgetId: number, buttonId: number): RunnableHooks<ButtonAction> | null => {
let matchingHooks = getActionHooks<ButtonActionHook>('button')
.filter(plugin =>
questHookFilter(player, plugin) && (
Expand Down
12 changes: 10 additions & 2 deletions src/engine/action/pipe/equipment-change.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { findItem, EquipmentSlot, ItemDetails } from '@engine/config';
import {
ActionHook, getActionHooks, numberHookFilter, stringHookFilter, questHookFilter, ActionPipe, RunnableHooks
} from '@engine/action';
import { logger } from '@runejs/common';


/**
Expand Down Expand Up @@ -53,7 +54,7 @@ export interface EquipmentChangeAction {
* @param slot
*/
const equipmentChangeActionPipe = (player: Player, itemId: number,
eventType: EquipmentChangeType, slot: EquipmentSlot): RunnableHooks<EquipmentChangeAction> => {
eventType: EquipmentChangeType, slot: EquipmentSlot): RunnableHooks<EquipmentChangeAction> | null => {
let matchingHooks = getActionHooks<EquipmentChangeActionHook>('equipment_change', equipActionHook => {
if(!questHookFilter(player, equipActionHook)) {
return false;
Expand Down Expand Up @@ -84,12 +85,19 @@ const equipmentChangeActionPipe = (player: Player, itemId: number,
return null;
}

const itemDetails = findItem(itemId);

if(!itemDetails) {
logger.error(`Item ${itemId} not registered on the server [equipment-change action pipe]`);
return null;
}

return {
hooks: matchingHooks,
action: {
player,
itemId,
itemDetails: findItem(itemId),
itemDetails,
eventType,
equipmentSlot: slot
}
Expand Down
12 changes: 10 additions & 2 deletions src/engine/action/pipe/item-interaction.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { findItem, ItemDetails } from '@engine/config';
import {
ActionHook, getActionHooks, numberHookFilter, stringHookFilter, questHookFilter, ActionPipe, RunnableHooks
} from '@engine/action';
import { logger } from '@runejs/common';


/**
Expand Down Expand Up @@ -57,7 +58,7 @@ export interface ItemInteractionAction {
* @param option
*/
const itemInteractionActionPipe = (player: Player, itemId: number, slot: number, widgetId: number,
containerId: number, option: string): RunnableHooks<ItemInteractionAction> => {
containerId: number, option: string): RunnableHooks<ItemInteractionAction> | null => {
const playerWidget = Object.values(player.interfaceState.widgetSlots).find((widget) => widget && widget.widgetId === widgetId);

if(playerWidget && playerWidget.fakeWidget != undefined) {
Expand Down Expand Up @@ -116,6 +117,13 @@ const itemInteractionActionPipe = (player: Player, itemId: number, slot: number,
return null;
}

const itemDetails = findItem(itemId);

if(!itemDetails) {
logger.error(`Item ${itemId} not registered on the server [item-interaction action pipe]`);
return null;
}

return {
hooks: matchingHooks,
action: {
Expand All @@ -124,7 +132,7 @@ const itemInteractionActionPipe = (player: Player, itemId: number, slot: number,
itemSlot: slot,
widgetId,
containerId,
itemDetails: findItem(itemId),
itemDetails,
option
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/engine/action/pipe/item-on-item.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ export interface ItemOnItemAction {
* @param usedWithWidgetId
*/
const itemOnItemActionPipe = (player: Player, usedItem: Item, usedSlot: number, usedWidgetId: number,
usedWithItem: Item, usedWithSlot: number, usedWithWidgetId: number): RunnableHooks<ItemOnItemAction> => {
usedWithItem: Item, usedWithSlot: number, usedWithWidgetId: number): RunnableHooks<ItemOnItemAction> | null => {
if(player.busy) {
return;
return null;
}

// Find all item on item action plugins that match this action
Expand Down
2 changes: 1 addition & 1 deletion src/engine/action/pipe/item-on-npc.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface ItemOnNpcAction {
* @param itemContainerId
*/
const itemOnNpcActionPipe = (player: Player, npc: Npc, position: Position, item: Item,
itemWidgetId: number, itemContainerId: number): RunnableHooks<ItemOnNpcAction> => {
itemWidgetId: number, itemContainerId: number): RunnableHooks<ItemOnNpcAction> | null => {
const morphedNpc = player.getMorphedNpcDetails(npc);

// Find all item on npc action plugins that reference this npc and item
Expand Down
2 changes: 1 addition & 1 deletion src/engine/action/pipe/item-on-object.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export interface ItemOnObjectAction {
const itemOnObjectActionPipe = (player: Player, landscapeObject: LandscapeObject,
objectConfig: ObjectConfig, position: Position,
item: Item, itemWidgetId: number, itemContainerId: number,
cacheOriginal: boolean): RunnableHooks<ItemOnObjectAction> => {
cacheOriginal: boolean): RunnableHooks<ItemOnObjectAction> | null => {
// Find all item on object action plugins that reference this location object
let matchingHooks = getActionHooks<ItemOnObjectActionHook>('item_on_object')
.filter(plugin => questHookFilter(player, plugin) &&
Expand Down
4 changes: 2 additions & 2 deletions src/engine/action/pipe/item-on-world-item.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ const itemOnWorldItemActionPipe = (
player: Player,
usedItem: Item, usedWithItem: WorldItem,
usedWidgetId: number, usedContainerId: number, usedSlot: number
): RunnableHooks<ItemOnWorldItemAction> => {
): RunnableHooks<ItemOnWorldItemAction> | null => {
if(player.busy) {
return;
return null;
}

// Find all item on item action plugins that match this action
Expand Down
8 changes: 6 additions & 2 deletions src/engine/action/pipe/item-swap.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ export interface ItemSwapAction {
* @param widget
*/
const itemSwapActionPipe = (player: Player, fromSlot: number, toSlot: number,
widget: { widgetId: number, containerId: number }): RunnableHooks<ItemSwapAction> => {
widget: { widgetId: number, containerId: number }): RunnableHooks<ItemSwapAction> | null => {
const matchingHooks = getActionHooks<ItemSwapActionHook>('item_swap')
.filter(plugin => numberHookFilter(plugin.widgetId || plugin.widgetIds, widget.widgetId));
.filter(plugin => (
(plugin.widgetId || plugin.widgetIds)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
&& numberHookFilter((plugin.widgetId || plugin.widgetIds)!, widget.widgetId)
));

if(!matchingHooks || matchingHooks.length === 0) {
player.sendMessage(`Unhandled Swap Items action: widget[${widget.widgetId}] container[${widget.containerId}] fromSlot[${fromSlot} toSlot${toSlot}`);
Expand Down
8 changes: 6 additions & 2 deletions src/engine/action/pipe/move-item.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ export interface MoveItemAction {
* @param widget
*/
const moveItemActionPipe = (player: Player, fromSlot: number, toSlot: number,
widget: { widgetId: number, containerId: number }): RunnableHooks<MoveItemAction> => {
widget: { widgetId: number, containerId: number }): RunnableHooks<MoveItemAction> | null => {
const matchingHooks = getActionHooks<MoveItemActionHook>('move_item')
.filter(plugin => numberHookFilter(plugin.widgetId || plugin.widgetIds, widget.widgetId));
.filter(plugin => (
(plugin.widgetId || plugin.widgetIds)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
&& numberHookFilter((plugin.widgetId || plugin.widgetIds)!, widget.widgetId)
));

if(!matchingHooks || matchingHooks.length === 0) {
player.sendMessage(`Unhandled Move Item action: widget[${widget.widgetId}] container[${widget.containerId}] fromSlot[${fromSlot} toSlot${toSlot}`);
Expand Down
8 changes: 7 additions & 1 deletion src/engine/action/pipe/npc-init.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ export interface NpcInitAction {
const npcInitActionPipe = ({ npc }: NpcInitAction): void => {
const actionHooks = getActionHooks<NpcInitActionHook>('npc_init')
.filter(plugin => (!plugin.npcs || stringHookFilter(plugin.npcs, npc.key)));
actionHooks.forEach(actionHook => actionHook.handler({ npc }));
actionHooks.forEach(actionHook => {
if (!actionHook.handler) {
return;
}

actionHook.handler({ npc });
});
};


Expand Down
8 changes: 3 additions & 5 deletions src/engine/action/pipe/npc-interaction.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,11 @@ export interface NpcInteractionAction {
* @param position
* @param option
*/
const npcInteractionActionPipe = (player: Player, npc: Npc, position: Position, option: string): RunnableHooks<NpcInteractionAction> => {
const npcInteractionActionPipe = (player: Player, npc: Npc, position: Position, option: string): RunnableHooks<NpcInteractionAction> | null => {
if(player.busy) {
return;
return null;
}



const morphedNpc = player.getMorphedNpcDetails(npc);

// Find all NPC action plugins that reference this NPC
Expand All @@ -61,7 +59,7 @@ const npcInteractionActionPipe = (player: Player, npc: Npc, position: Position,
(!plugin.npcs || stringHookFilter(plugin.npcs, morphedNpc?.key || npc.key)) &&
(!plugin.options || stringHookFilter(plugin.options, option)));
const questActions = matchingHooks.filter(plugin => plugin.questRequirement !== undefined);


if(questActions.length !== 0) {
matchingHooks = questActions;
Expand Down
2 changes: 1 addition & 1 deletion src/engine/action/pipe/object-interaction.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export interface ObjectInteractionAction {
* @param cacheOriginal
*/
const objectInteractionActionPipe = (player: Player, landscapeObject: LandscapeObject, objectConfig: ObjectConfig,
position: Position, option: string, cacheOriginal: boolean): RunnableHooks<ObjectInteractionAction> => {
position: Position, option: string, cacheOriginal: boolean): RunnableHooks<ObjectInteractionAction> | null => {
if(player.metadata.blockObjectInteractions) {
return null;
}
Expand Down
8 changes: 4 additions & 4 deletions src/engine/action/pipe/player-command.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ export interface PlayerCommandAction {
* @param inputArgs
*/
const playerCommandActionPipe = (player: Player, command: string, isConsole: boolean,
inputArgs: string[]): RunnableHooks<PlayerCommandAction> => {
inputArgs: string[]): RunnableHooks<PlayerCommandAction> | null => {
command = command.toLowerCase();

// Reload game content
if (reloadContentCommands.indexOf(command) !== -1) {
reloadContent(player, isConsole).catch(logger.error);
return;
return null;
}

const actionArgs = {};
Expand Down Expand Up @@ -87,7 +87,7 @@ const playerCommandActionPipe = (player: Player, command: string, isConsole: boo


for(let i = 0; i < actionHook.args.length; i++) {
let argValue: string | number = inputArgs[i] || null;
let argValue: string | number | null = inputArgs[i] || null;
const pluginArg = actionHook.args[i];

if(argValue === null || argValue === undefined) {
Expand Down Expand Up @@ -121,7 +121,7 @@ const playerCommandActionPipe = (player: Player, command: string, isConsole: boo

if(plugins.length === 0) {
player.sendLogMessage(`Unhandled command: ${ command }`, isConsole);
return;
return null;
}

return {
Expand Down
8 changes: 7 additions & 1 deletion src/engine/action/pipe/player-init.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ export interface PlayerInitAction {
*/
const playerInitActionPipe = ({ player }: PlayerInitAction): void => {
const actionHooks = getActionHooks<PlayerInitActionHook>('player_init');
actionHooks.forEach(actionHook => actionHook.handler({ player }));
actionHooks.forEach(actionHook => {
if (!actionHook.handler) {
return;
}

actionHook.handler({ player });
});
};


Expand Down
2 changes: 1 addition & 1 deletion src/engine/action/pipe/player-interaction.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface PlayerInteractionAction {
* @param option
*/
const playerInteractionActionPipe = (player: Player, otherPlayer: Player, position: Position,
option: string): RunnableHooks<PlayerInteractionAction> => {
option: string): RunnableHooks<PlayerInteractionAction> | null => {
// Find all player action plugins that reference this option
let matchingHooks = getActionHooks<PlayerInteractionActionHook>('player_interaction')
.filter(plugin => questHookFilter(player, plugin) && stringHookFilter(plugin.options, option));
Expand Down
11 changes: 9 additions & 2 deletions src/engine/action/pipe/region-change.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,18 @@ export interface RegionChangeAction {

/**
* Creates a RegionChangeAction object from the given inputs.
*
* TODO (Jameskmonger) I changed this function's return type to `| null` to satisfy TypeScript,
* not sure if this is correct or if the code was just wrong before.
*
* @param player The player.
* @param originalPosition The player's original position.
* @param currentPosition The player's current position.
* @param teleporting Whether or not the player is teleporting; defaults to false.
*/
export const regionChangeActionFactory = (player: Player,
originalPosition: Position, currentPosition: Position,
teleporting: boolean = false): RegionChangeAction => {
teleporting: boolean = false): RegionChangeAction | null => {
const regionTypes: RegionType[] = [];
const originalMapRegionId: number = ((originalPosition.x >> 6) << 8) + (originalPosition.y >> 6);
const currentMapRegionId: number = ((currentPosition.x >> 6) << 8) + (currentPosition.y >> 6);
Expand Down Expand Up @@ -157,7 +161,10 @@ const regionChangeActionPipe = (actionData: RegionChangeAction): void => {

actionList.forEach(async actionHook =>
new Promise<void>(resolve => {
actionHook.handler(actionData);
if (actionHook && actionHook.handler) {
actionHook.handler(actionData);
}

resolve();
}));
};
Expand Down
8 changes: 7 additions & 1 deletion src/engine/action/pipe/spawned-item-interaction.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ItemDetails, findItem } from '@engine/config';
import {
ActionHook, getActionHooks, numberHookFilter, stringHookFilter, questHookFilter, ActionPipe, RunnableHooks
} from '@engine/action';
import { logger } from '@runejs/common';


/**
Expand Down Expand Up @@ -44,7 +45,7 @@ export interface SpawnedItemInteractionAction {
* @param worldItem
* @param option
*/
const spawnedItemInteractionPipe = (player: Player, worldItem: WorldItem, option: string): RunnableHooks<SpawnedItemInteractionAction> => {
const spawnedItemInteractionPipe = (player: Player, worldItem: WorldItem, option: string): RunnableHooks<SpawnedItemInteractionAction> | null => {
// Find all world item action plugins that reference this world item
let matchingHooks = getActionHooks<SpawnedItemInteractionHook>('spawned_item_interaction').filter(plugin => {
if(!questHookFilter(player, plugin)) {
Expand Down Expand Up @@ -77,6 +78,11 @@ const spawnedItemInteractionPipe = (player: Player, worldItem: WorldItem, option

const itemDetails = findItem(worldItem.itemId);

if(!itemDetails) {
logger.error(`Item ${worldItem.itemId} not registered on the server [spawned-item-interaction action pipe]`);
return null;
}

return {
hooks: matchingHooks,
actionPosition: worldItem.position,
Expand Down
2 changes: 1 addition & 1 deletion src/engine/action/pipe/widget-interaction.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface WidgetInteractionAction {
* @param childId The ID of the widget child being interacted with.
* @param optionId The widget context option chosen by the player.
*/
const widgetActionPipe = (player: Player, widgetId: number, childId: number, optionId: number): RunnableHooks<WidgetInteractionAction> => {
const widgetActionPipe = (player: Player, widgetId: number, childId: number, optionId: number): RunnableHooks<WidgetInteractionAction> | null => {
const playerWidget = Object.values(player.interfaceState.widgetSlots)
.find((widget) => widget && widget.widgetId === widgetId);

Expand Down
Loading

0 comments on commit bc1b899

Please sign in to comment.