-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for world item action plugins and a plugin for picking…
… up world items.
- Loading branch information
1 parent
d842877
commit 8547d03
Showing
8 changed files
with
192 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { incomingPacket } from '../incoming-packet'; | ||
import { Player } from '../../world/actor/player/player'; | ||
import { RsBuffer } from '@server/net/rs-buffer'; | ||
import { world } from '@server/game-server'; | ||
import { Position } from '@server/world/position'; | ||
import { worldItemAction } from '@server/world/actor/player/action/world-item-action'; | ||
|
||
export const pickupItemPacket: incomingPacket = (player: Player, packetId: number, packetSize: number, packet: RsBuffer): void => { | ||
const y = packet.readNegativeOffsetShortBE(); | ||
const itemId = packet.readNegativeOffsetShortBE(); | ||
const x = packet.readUnsignedShortLE(); | ||
|
||
const level = player.position.level; | ||
const worldItemPosition = new Position(x, y, level); | ||
const chunk = world.chunkManager.getChunkForWorldPosition(worldItemPosition); | ||
const worldItem = chunk.getWorldItem(itemId, worldItemPosition); | ||
|
||
if(!worldItem || worldItem.removed) { | ||
return; | ||
} | ||
|
||
if(worldItem.initiallyVisibleTo && !worldItem.initiallyVisibleTo.equals(player)) { | ||
return; | ||
} | ||
|
||
worldItemAction(player, worldItem, 'pick-up'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { ActionType, RunePlugin } from '@server/plugins/plugin'; | ||
import { worldItemAction } from '@server/world/actor/player/action/world-item-action'; | ||
import { world } from '../../game-server'; | ||
import { Item } from '../../world/items/item'; | ||
import { widgets } from '../../world/config/widget'; | ||
import { soundIds } from '@server/world/config/sound-ids'; | ||
|
||
export const action: worldItemAction = (details) => { | ||
const { player, worldItem } = details; | ||
|
||
const inventory = player.inventory; | ||
let slot = -1; | ||
const itemData = world.itemData.get(worldItem.itemId); | ||
let amount = worldItem.amount; | ||
|
||
if(itemData.stackable) { | ||
const existingItemIndex = inventory.findIndex(worldItem.itemId); | ||
if(existingItemIndex !== -1) { | ||
const existingItem = inventory.items[existingItemIndex]; | ||
if(existingItem.amount + worldItem.amount < 2147483647) { | ||
existingItem.amount += worldItem.amount; | ||
amount += existingItem.amount; | ||
slot = existingItemIndex; | ||
} | ||
} | ||
} | ||
|
||
if(slot === -1) { | ||
slot = inventory.getFirstOpenSlot(); | ||
} | ||
|
||
if(slot === -1) { | ||
player.outgoingPackets.chatboxMessage(`You don't have enough free space to do that.`); | ||
return; | ||
} | ||
|
||
world.chunkManager.removeWorldItem(worldItem); | ||
|
||
const item: Item = { | ||
itemId: worldItem.itemId, | ||
amount | ||
}; | ||
|
||
inventory.add(item); | ||
player.outgoingPackets.sendUpdateSingleWidgetItem(widgets.inventory, slot, item); | ||
player.outgoingPackets.playSound(soundIds.pickupItem, 3); | ||
}; | ||
|
||
export default new RunePlugin({ | ||
type: ActionType.WORLD_ITEM_ACTION, | ||
options: 'pick-up', | ||
action, | ||
walkTo: true | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Player } from '@server/world/actor/player/player'; | ||
import { walkToAction } from '@server/world/actor/player/action/action'; | ||
import { basicNumberFilter, basicStringFilter } from '@server/plugins/plugin-loader'; | ||
import { logger } from '@runejs/logger/dist/logger'; | ||
import { ActionPlugin } from '@server/plugins/plugin'; | ||
import { WorldItem } from '@server/world/items/world-item'; | ||
|
||
/** | ||
* The definition for a world item action function. | ||
*/ | ||
export type worldItemAction = (details: WorldItemActionDetails) => void; | ||
|
||
/** | ||
* Details about a world item being interacted with. | ||
*/ | ||
export interface WorldItemActionDetails { | ||
player: Player; | ||
worldItem: WorldItem; | ||
} | ||
|
||
/** | ||
* Defines an world item interaction plugin. | ||
*/ | ||
export interface WorldItemActionPlugin extends ActionPlugin { | ||
itemIds?: number | number[]; | ||
options: string | string[]; | ||
walkTo: boolean; | ||
action: worldItemAction; | ||
} | ||
|
||
/** | ||
* A directory of all world item interaction plugins. | ||
*/ | ||
let worldItemInteractions: WorldItemActionPlugin[] = [ | ||
]; | ||
|
||
/** | ||
* Sets the list of world item interaction plugins. | ||
* @param plugins The plugin list. | ||
*/ | ||
export const setWorldItemPlugins = (plugins: ActionPlugin[]): void => { | ||
worldItemInteractions = plugins as WorldItemActionPlugin[]; | ||
}; | ||
|
||
// @TODO priority and cancelling other (lower priority) actions | ||
export const worldItemAction = (player: Player, worldItem: WorldItem, option: string): void => { | ||
// Find all world item action plugins that reference this world item | ||
const interactionPlugins = worldItemInteractions.filter(plugin => { | ||
if(plugin.itemIds !== undefined) { | ||
if(!basicNumberFilter(plugin.itemIds, worldItem.itemId)) { | ||
return false; | ||
} | ||
} | ||
|
||
if(!basicStringFilter(plugin.options, option)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}); | ||
|
||
if(interactionPlugins.length === 0) { | ||
player.outgoingPackets.chatboxMessage(`Unhandled world item interaction: ${option} ${worldItem.itemId}`); | ||
return; | ||
} | ||
|
||
player.actionsCancelled.next(); | ||
|
||
// Separate out walk-to actions from immediate actions | ||
const walkToPlugins = interactionPlugins.filter(plugin => plugin.walkTo); | ||
const immediatePlugins = interactionPlugins.filter(plugin => !plugin.walkTo); | ||
|
||
// Make sure we walk to the NPC before running any of the walk-to plugins | ||
if(walkToPlugins.length !== 0) { | ||
walkToAction(player, worldItem.position) | ||
.then(() => walkToPlugins.forEach(plugin => plugin.action({ player, worldItem }))) | ||
.catch(() => logger.warn(`Unable to complete walk-to action.`)); | ||
} | ||
|
||
// Immediately run any non-walk-to plugins | ||
if(immediatePlugins.length !== 0) { | ||
immediatePlugins.forEach(plugin => plugin.action({ player, worldItem })); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export const soundIds = { | ||
dropItem: 2739, | ||
pickupItem: 2582, | ||
milkCow: 372, | ||
lightingFire: 2599, | ||
fireLit: 2594, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters