Skip to content

Commit

Permalink
Converting drop item action into a plugin and changing widget id nami…
Browse files Browse the repository at this point in the history
…ng to just widget.
  • Loading branch information
TheBlackParade committed Feb 28, 2020
1 parent 04a5cf9 commit e8bf1d4
Show file tree
Hide file tree
Showing 18 changed files with 116 additions and 121 deletions.
4 changes: 2 additions & 2 deletions src/net/incoming-packet-sizes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export const incomingPacketSizes: number[] = [
6, -3, -3, -3, -3, -3, -3, -3, 8, -3, //30
16, -3, -3, -3, -3, -3, -3, -3, -3, -3, //40
-3, -3, -3, -3, -3, -3, -3, -3, 4, -3, //50
-3, -3, -3, 2, 4, -3, -3, -3, -3, -3, //60
-3, -3, -3, 2, 4, 6, -3, -3, -3, -3, //60
-3, -3, -3, -1, -3, -3, -3, -3, -3, -3, //70
-3, -3, -3, 9, -3, -3, -3, -3, -3, -1, //80
-3, -3, -3, 9, -3, 6, -3, -3, -3, -1, //80
-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, //90
-3, -3, 8, -3, -3, -3, -3, -3, -3, -3, //100
-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, //110
Expand Down
4 changes: 2 additions & 2 deletions src/net/incoming-packets/character-design-packet.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { incomingPacket } from '../incoming-packet';
import { Player } from '../../world/actor/player/player';
import { RsBuffer } from '@server/net/rs-buffer';
import { widgetIds } from '../../world/config/widget';
import { widgets } from '../../world/config/widget';

export const characterDesignPacket: incomingPacket = (player: Player, packetId: number, packetSize: number, packet: RsBuffer): void => {
if(!player.activeWidget || player.activeWidget.widgetId !== widgetIds.characterDesign) {
if(!player.activeWidget || player.activeWidget.widgetId !== widgets.characterDesign) {
return;
}

Expand Down
28 changes: 2 additions & 26 deletions src/net/incoming-packets/drop-item-packet.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
import { incomingPacket } from '../incoming-packet';
import { Player } from '../../world/actor/player/player';
import { RsBuffer } from '@server/net/rs-buffer';
import { logger } from '@runejs/logger/dist/logger';
import { widgetIds } from '../../world/config/widget';
import { dropItemAction } from '@server/world/actor/player/action/drop-item-action';
import { itemAction } from '@server/world/actor/player/action/item-action';

export const dropItemPacket: incomingPacket = (player: Player, packetId: number, packetSize: number, packet: RsBuffer): void => {
const widgetId = packet.readUnsignedShortLE();
const containerId = packet.readUnsignedShortLE();
const slot = packet.readNegativeOffsetShortBE();
const itemId = packet.readUnsignedShortLE();

if(widgetId !== widgetIds.inventory.widgetId || containerId !== widgetIds.inventory.containerId) {
logger.warn(`${player.username} attempted to drop item from incorrect widget id ${widgetId}.`);
return;
}

if(slot < 0 || slot > 27) {
logger.warn(`${player.username} attempted to drop item ${itemId} in invalid slot ${slot}.`);
return;
}

const itemInSlot = player.inventory.items[slot];

if(!itemInSlot) {
logger.warn(`${player.username} attempted to drop item ${itemId} in slot ${slot}, but they do not have that item.`);
return;
}

if(itemInSlot.itemId !== itemId) {
logger.warn(`${player.username} attempted to drop item ${itemId} in slot ${slot}, but ${itemInSlot.itemId} was found there instead.`);
return;
}

dropItemAction(player, itemInSlot, slot);
itemAction(player, itemId, slot, widgetId, containerId, 'drop');
};
6 changes: 3 additions & 3 deletions src/net/incoming-packets/item-on-item-packet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { incomingPacket } from '../incoming-packet';
import { Player } from '../../world/actor/player/player';
import { RsBuffer } from '@server/net/rs-buffer';
import { widgetIds } from '@server/world/config/widget';
import { widgets } from '@server/world/config/widget';
import { logger } from '@runejs/logger/dist/logger';
import { itemOnItemAction } from '@server/world/actor/player/action/item-on-item-action';

Expand All @@ -15,8 +15,8 @@ export const itemOnItemPacket: incomingPacket = (player: Player, packetId: numbe
const usedItemId = packet.readUnsignedShortLE();
const usedSlot = packet.readNegativeOffsetShortBE();

if(usedWidgetId === widgetIds.inventory.widgetId && usedContainerId === widgetIds.inventory.containerId &&
usedWithWidgetId === widgetIds.inventory.widgetId && usedWithContainerId === widgetIds.inventory.containerId) {
if(usedWidgetId === widgets.inventory.widgetId && usedContainerId === widgets.inventory.containerId &&
usedWithWidgetId === widgets.inventory.widgetId && usedWithContainerId === widgets.inventory.containerId) {
if(usedSlot < 0 || usedSlot > 27 || usedWithSlot < 0 || usedWithSlot > 27) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/buttons/logout-button-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { buttonAction } from '@server/world/actor/player/action/button-action';
import { ActionType, RunePlugin } from '@server/plugins/plugin';
import { widgetIds } from '@server/world/config/widget';
import { widgets } from '@server/world/config/widget';

export const action: buttonAction = (details) => {
const { player } = details;
player.logout();
};

export default new RunePlugin({ type: ActionType.BUTTON, widgetId: widgetIds.logoutTab, buttonIds: 6, action });
export default new RunePlugin({ type: ActionType.BUTTON, widgetId: widgets.logoutTab, buttonIds: 6, action });
4 changes: 2 additions & 2 deletions src/plugins/buttons/player-setting-button-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { buttonAction } from '@server/world/actor/player/action/button-action';
import { ActionType, RunePlugin } from '@server/plugins/plugin';
import { widgetIds } from '@server/world/config/widget';
import { widgets } from '@server/world/config/widget';

const buttonIds: number[] = [
0, // walk/run
Expand All @@ -20,4 +20,4 @@ export const action: buttonAction = (details) => {
player.settingChanged(buttonId);
};

export default new RunePlugin({ type: ActionType.BUTTON, widgetId: widgetIds.settingsTab, buttonIds: buttonIds, action });
export default new RunePlugin({ type: ActionType.BUTTON, widgetId: widgets.settingsTab, buttonIds: buttonIds, action });
12 changes: 6 additions & 6 deletions src/plugins/equipment/equip-item-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ActionType, RunePlugin } from '@server/plugins/plugin';
import { widgetIds } from '@server/world/config/widget';
import { widgets } from '@server/world/config/widget';
import { getItemFromContainer, itemAction } from '@server/world/actor/player/action/item-action';
import { updateBonusStrings } from '@server/plugins/equipment/equipment-stats-plugin';
import { EquipmentSlot, equipmentSlotIndex, ItemDetails, WeaponType } from '@server/world/config/item-data';
Expand Down Expand Up @@ -94,11 +94,11 @@ export const action: itemAction = (details) => {
player.updateBonuses();

// @TODO change packets to only update modified container slots
player.outgoingPackets.sendUpdateAllWidgetItems(widgetIds.inventory, inventory);
player.outgoingPackets.sendUpdateAllWidgetItems(widgetIds.equipment, equipment);
player.outgoingPackets.sendUpdateAllWidgetItems(widgets.inventory, inventory);
player.outgoingPackets.sendUpdateAllWidgetItems(widgets.equipment, equipment);

if(player.hasWidgetOpen(widgetIds.equipmentStats.widgetId)) {
player.outgoingPackets.sendUpdateAllWidgetItems(widgetIds.equipmentStats, equipment);
if(player.hasWidgetOpen(widgets.equipmentStats.widgetId)) {
player.outgoingPackets.sendUpdateAllWidgetItems(widgets.equipmentStats, equipment);
updateBonusStrings(player);
}

Expand All @@ -107,7 +107,7 @@ export const action: itemAction = (details) => {

export default new RunePlugin({
type: ActionType.ITEM_ACTION,
widgets: widgetIds.inventory,
widgets: widgets.inventory,
options: 'equip',
action,
cancelOtherActions: false
Expand Down
14 changes: 7 additions & 7 deletions src/plugins/equipment/equipment-stats-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { buttonAction } from '@server/world/actor/player/action/button-action';
import { ActionType, RunePlugin } from '@server/plugins/plugin';
import { widgetIds } from '@server/world/config/widget';
import { widgets } from '@server/world/config/widget';
import { Player } from '@server/world/actor/player/player';

export function updateBonusStrings(player: Player) {
Expand All @@ -17,7 +17,7 @@ export function updateBonusStrings(player: Player) {
{ id: 117, text: 'Range', value: player.bonuses.defencive.ranged },
{ id: 119, text: 'Strength', value: player.bonuses.skill.strength },
{ id: 120, text: 'Prayer', value: player.bonuses.skill.prayer },
].forEach(bonus => player.outgoingPackets.updateWidgetString(widgetIds.equipmentStats.widgetId, bonus.id,
].forEach(bonus => player.outgoingPackets.updateWidgetString(widgets.equipmentStats.widgetId, bonus.id,
`${bonus.text}: ${bonus.value > 0 ? `+${bonus.value}` : bonus.value}`));
}

Expand All @@ -28,15 +28,15 @@ export const action: buttonAction = (details) => {

updateBonusStrings(player);

player.outgoingPackets.sendUpdateAllWidgetItems(widgetIds.equipmentStats, player.equipment);
player.outgoingPackets.sendUpdateAllWidgetItems(widgetIds.inventory, player.inventory);
player.outgoingPackets.sendUpdateAllWidgetItems(widgets.equipmentStats, player.equipment);
player.outgoingPackets.sendUpdateAllWidgetItems(widgets.inventory, player.inventory);

player.activeWidget = {
widgetId: widgetIds.equipmentStats.widgetId,
secondaryWidgetId: widgetIds.inventory.widgetId,
widgetId: widgets.equipmentStats.widgetId,
secondaryWidgetId: widgets.inventory.widgetId,
type: 'SCREEN_AND_TAB',
closeOnWalk: true
};
};

export default new RunePlugin({ type: ActionType.BUTTON, widgetId: widgetIds.equipment.widgetId, buttonIds: 24, action });
export default new RunePlugin({ type: ActionType.BUTTON, widgetId: widgets.equipment.widgetId, buttonIds: 24, action });
14 changes: 7 additions & 7 deletions src/plugins/equipment/unequip-item-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ActionType, RunePlugin } from '@server/plugins/plugin';
import { widgetIds } from '@server/world/config/widget';
import { widgets } from '@server/world/config/widget';
import { getItemFromContainer, itemAction } from '@server/world/actor/player/action/item-action';
import { updateBonusStrings } from '@server/plugins/equipment/equipment-stats-plugin';

Expand Down Expand Up @@ -27,11 +27,11 @@ export const action: itemAction = (details) => {

player.updateBonuses();

player.outgoingPackets.sendUpdateSingleWidgetItem(widgetIds.inventory, inventorySlot, item);
player.outgoingPackets.sendUpdateSingleWidgetItem(widgetIds.equipment, itemSlot, null);
player.outgoingPackets.sendUpdateSingleWidgetItem(widgets.inventory, inventorySlot, item);
player.outgoingPackets.sendUpdateSingleWidgetItem(widgets.equipment, itemSlot, null);

if(widgetId === widgetIds.equipmentStats.widgetId) {
player.outgoingPackets.sendUpdateSingleWidgetItem(widgetIds.equipmentStats, itemSlot, null);
if(widgetId === widgets.equipmentStats.widgetId) {
player.outgoingPackets.sendUpdateSingleWidgetItem(widgets.equipmentStats, itemSlot, null);
updateBonusStrings(player);
}

Expand All @@ -41,8 +41,8 @@ export const action: itemAction = (details) => {
export default new RunePlugin({
type: ActionType.ITEM_ACTION,
widgets: [
widgetIds.equipment,
widgetIds.equipmentStats
widgets.equipment,
widgets.equipmentStats
],
options: 'option-1',
action,
Expand Down
30 changes: 30 additions & 0 deletions src/plugins/items/drop-item-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ActionType, RunePlugin } from '@server/plugins/plugin';
import { widgets } from '@server/world/config/widget';
import { getItemFromContainer, itemAction } from '@server/world/actor/player/action/item-action';
import { world } from '@server/game-server';
import { soundIds } from '@server/world/config/sound-ids';

export const action: itemAction = (details) => {
const { player, itemId, itemSlot } = details;

const inventory = player.inventory;
const item = getItemFromContainer(itemId, itemSlot, inventory);

if(!item) {
// The specified item was not found in the specified slot.
return;
}

inventory.remove(itemSlot);
player.outgoingPackets.sendUpdateSingleWidgetItem(widgets.inventory, itemSlot, null);
player.outgoingPackets.playSound(soundIds.dropItem, 5);
world.chunkManager.spawnWorldItem(item, player.position, player, 300);
};

export default new RunePlugin({
type: ActionType.ITEM_ACTION,
widgets: widgets.inventory,
options: 'drop',
action,
cancelOtherActions: false
});
28 changes: 14 additions & 14 deletions src/plugins/skills/skill-guide-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { readFileSync } from 'fs';
import { ActionType, RunePlugin } from '@server/plugins/plugin';
import { Player } from '@server/world/actor/player/player';
import { widgetAction } from '@server/world/actor/player/action/widget-action';
import { widgetIds } from '@server/world/config/widget';
import { widgets } from '@server/world/config/widget';

// @TODO fix me!

Expand Down Expand Up @@ -54,48 +54,48 @@ function loadGuide(player: Player, guideId: number, subGuideId: number = 0, refr
let guide: SkillGuide = guides.find(g => g.id === guideId);

if(refreshSidebar) {
player.outgoingPackets.updateWidgetString(widgetIds.skillGuide, 133, guide.members ? 'Members only skill' : '');
player.outgoingPackets.updateWidgetString(widgets.skillGuide, 133, guide.members ? 'Members only skill' : '');

for(let i = 0; i < sidebarTextIds.length; i++) {
const sidebarId = sidebarIds[i];
let hide: boolean = true;

if(i >= guide.subGuides.length) {
player.outgoingPackets.updateWidgetString(widgetIds.skillGuide, sidebarTextIds[i], '');
player.outgoingPackets.updateWidgetString(widgets.skillGuide, sidebarTextIds[i], '');
hide = true;
} else {
player.outgoingPackets.updateWidgetString(widgetIds.skillGuide, sidebarTextIds[i], guide.subGuides[i].name);
player.outgoingPackets.updateWidgetString(widgets.skillGuide, sidebarTextIds[i], guide.subGuides[i].name);
hide = false;
}

if(sidebarId !== -1) {
// Apparently you can never have only TWO subguides...
// Because childId 98 deletes both options 2 AND 3. So, good thing there are no guides with only 2 sections, I guess?...
// Verified this in an interface editor, and they are indeed grouped in a single layer for some reason...
player.outgoingPackets.toggleWidgetVisibility(widgetIds.skillGuide, sidebarIds[i], hide);
player.outgoingPackets.toggleWidgetVisibility(widgets.skillGuide, sidebarIds[i], hide);
}
}
}

const subGuide: SkillSubGuide = guide.subGuides[subGuideId];

player.outgoingPackets.updateWidgetString(widgetIds.skillGuide, 1, guide.name + ' - ' + subGuide.name);
player.outgoingPackets.updateWidgetString(widgets.skillGuide, 1, guide.name + ' - ' + subGuide.name);

const itemIds: number[] = subGuide.lines.map(g => g.itemId).concat(new Array(30 - subGuide.lines.length).fill(null));
player.outgoingPackets.sendUpdateAllWidgetItemsById({ widgetId: widgetIds.skillGuide, containerId: 132 }, itemIds);
player.outgoingPackets.sendUpdateAllWidgetItemsById({ widgetId: widgets.skillGuide, containerId: 132 }, itemIds);

for(let i = 0; i < 30; i++) {
if(subGuide.lines.length <= i) {
player.outgoingPackets.updateWidgetString(widgetIds.skillGuide, 5 + i, '');
player.outgoingPackets.updateWidgetString(widgetIds.skillGuide, 45 + i, '');
player.outgoingPackets.updateWidgetString(widgets.skillGuide, 5 + i, '');
player.outgoingPackets.updateWidgetString(widgets.skillGuide, 45 + i, '');
} else {
player.outgoingPackets.updateWidgetString(widgetIds.skillGuide, 5 + i, subGuide.lines[i].level.toString());
player.outgoingPackets.updateWidgetString(widgetIds.skillGuide, 45 + i, subGuide.lines[i].text);
player.outgoingPackets.updateWidgetString(widgets.skillGuide, 5 + i, subGuide.lines[i].level.toString());
player.outgoingPackets.updateWidgetString(widgets.skillGuide, 45 + i, subGuide.lines[i].text);
}
}

player.activeWidget = {
widgetId: widgetIds.skillGuide,
widgetId: widgets.skillGuide,
type: 'SCREEN',
closeOnWalk: false
};
Expand Down Expand Up @@ -127,6 +127,6 @@ export const openSubGuideAction: widgetAction = (details) => {
};

export default new RunePlugin([
{ type: ActionType.BUTTON, widgetId: widgetIds.skillsTab, buttonIds, action: openGuideAction },
{ type: ActionType.WIDGET_ACTION, widgetIds: widgetIds.skillGuide, childIds: sidebarTextIds, optionId: 0, action: openSubGuideAction }
{ type: ActionType.BUTTON, widgetId: widgets.skillsTab, buttonIds, action: openGuideAction },
{ type: ActionType.WIDGET_ACTION, widgetIds: widgets.skillGuide, childIds: sidebarTextIds, optionId: 0, action: openSubGuideAction }
]);
12 changes: 6 additions & 6 deletions src/world/actor/player/action/buy-item-action.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Player } from '@server/world/actor/player/player';
import { gameCache } from '@server/game-server';
import { widgetIds } from '@server/world/config/widget';
import { widgets } from '@server/world/config/widget';

export const buyItemAction = (player: Player, itemId: number, amount: number, slot: number, interfaceId: number) => {
const purchasedItem = gameCache.itemDefinitions.get(itemId);
Expand All @@ -11,7 +11,7 @@ export const buyItemAction = (player: Player, itemId: number, amount: number, sl
return;
}

if(interfaceId !== widgetIds.shop.shopInventory) {
if(interfaceId !== widgets.shop.shopInventory) {
return;
}

Expand All @@ -38,12 +38,12 @@ export const buyItemAction = (player: Player, itemId: number, amount: number, sl
player.inventory.add(itemId);
}

if(interfaceId === widgetIds.shop.shopInventory) {
console.log(interfaceId, widgetIds.shop.shopInventory);
player.outgoingPackets.sendUpdateAllWidgetItems(widgetIds.shop.playerInventory, player.inventory);
if(interfaceId === widgets.shop.shopInventory) {
console.log(interfaceId, widgets.shop.shopInventory);
player.outgoingPackets.sendUpdateAllWidgetItems(widgets.shop.playerInventory, player.inventory);
}

// Update the inventory items.
player.outgoingPackets.sendUpdateAllWidgetItems(widgetIds.inventory, player.inventory);
player.outgoingPackets.sendUpdateAllWidgetItems(widgets.inventory, player.inventory);

};
12 changes: 0 additions & 12 deletions src/world/actor/player/action/drop-item-action.ts

This file was deleted.

Loading

0 comments on commit e8bf1d4

Please sign in to comment.