index.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. module.exports = function NpcSummoner(mod) {
  2. let debug = false;
  3. let debugData = [];
  4. mod.dispatch.addDefinition("C_REQUEST_CONTRACT", 50, [
  5. ["name", "refString"],
  6. ["data", "refBytes"],
  7. ["type", "int32"],
  8. ["target", "int64"],
  9. ["value", "int32"],
  10. ["name", "string"],
  11. ["data", "bytes"]
  12. ]);
  13. Object.keys(mod.settings.npc).forEach(name => {
  14. mod.command.add(name, () => {
  15. const npc = mod.settings.npc[name];
  16. const buffer = Buffer.alloc(4);
  17. buffer.writeUInt32LE(npc.value);
  18. mod.send("C_REQUEST_CONTRACT", 50, {
  19. "type": npc.type,
  20. "target": npc.gameId,
  21. "value": npc.value,
  22. "name": "",
  23. "data": buffer
  24. });
  25. });
  26. });
  27. mod.command.add("broker", () =>
  28. mod.send("S_NPC_MENU_SELECT", 1, { "type": 28 })
  29. );
  30. mod.command.add("npcsummoner", () => {
  31. debug = !debug;
  32. mod.command.message(`Module debug ${debug ? "enabled" : "disabled"}`);
  33. });
  34. mod.hook("S_SPAWN_NPC", mod.majorPatchVersion >= 101 ? 12 : 11, event => {
  35. Object.entries(mod.settings.npc).forEach(([name, npc]) => {
  36. if (npc.opts === undefined) return;
  37. const opt = npc.opts.find(option => option.templateId === event.templateId && option.huntingZoneId === event.huntingZoneId);
  38. if (opt) {
  39. mod.settings.npc[name].value = opt._value;
  40. mod.settings.npc[name].gameId = parseInt(event.gameId);
  41. }
  42. });
  43. });
  44. mod.hook("S_DIALOG", 2, event => {
  45. if (!debug) return;
  46. debugData = [
  47. "Detected NPC:",
  48. ` "value": ${event.options[0]?.type}`,
  49. ` "gameId": ${event.gameId}`,
  50. ` "templateId": ${event.questId}`,
  51. ` "huntingZoneId": ${event.huntingZoneId}`
  52. ];
  53. });
  54. mod.hook("S_REQUEST_CONTRACT", 1, event => {
  55. if (!debug) return;
  56. debugData.push(` "type": ${event.type}`);
  57. debugData.forEach(data => {
  58. console.log(data);
  59. mod.command.message(data);
  60. });
  61. debugData = [];
  62. });
  63. this.destructor = () => {
  64. mod.command.remove(["broker", ...Object.keys(mod.settings.npc)]);
  65. };
  66. };