index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. module.exports = function Loot(dispatch) {
  2. let auto = false,
  3. enabled = true,
  4. location;
  5. let loot = {};
  6. let commands = {
  7. auto: {
  8. alias: ['auto', 'autoloot', 'toggle'],
  9. run: function() {
  10. auto = !auto;
  11. message(`Autoloot mode toggled: ${auto}`);
  12. }
  13. },
  14. enable: {
  15. alias: ['enable', 'on'],
  16. run: function() {
  17. enabled = true;
  18. message('Easy looting is enabled.');
  19. }
  20. },
  21. disable: {
  22. alias: ['disable', 'off'],
  23. run: function() {
  24. enabled = false;
  25. message('Easy looting is disabled.');
  26. }
  27. }
  28. }
  29. dispatch.hook('C_CHAT', 1, (event) => {
  30. if(!event.message.includes('!loot'))
  31. return;
  32. let command = event.message.replace(/<\/?[^<>]*>/gi, '').split(' ');
  33. if(command.length > 1) {
  34. for(let cmd in commands) {
  35. if(commands[cmd].alias.indexOf(command[1].toString()) > -1)
  36. commands[cmd].run();
  37. }
  38. }
  39. return false;
  40. });
  41. dispatch.hook('S_LOAD_TOPO', 1, (event) => {
  42. loot = {};
  43. });
  44. dispatch.hook('C_PLAYER_LOCATION', 1, (event) => {
  45. location = event;
  46. });
  47. dispatch.hook('S_SPAWN_DROPITEM', 1, (event) => {
  48. loot[event.id.toString()] = event;
  49. if(auto) {
  50. tryLootAll();
  51. }
  52. });
  53. dispatch.hook('C_TRY_LOOT_DROPITEM', 1, (event) => {
  54. if(enabled) tryLootAll();
  55. });
  56. dispatch.hook('S_DESPAWN_DROPITEM', 1, (event) => {
  57. if(event.id.toString() in loot) delete loot[event.id.toString()];
  58. });
  59. function tryLootAll() {
  60. for(let item in loot) {
  61. if(Math.abs(loot[item].x - location.x1) < 100 && Math.abs(loot[item].y - location.y1) < 100)
  62. dispatch.toServer('C_TRY_LOOT_DROPITEM', 1, {
  63. id: loot[item].id
  64. });
  65. }
  66. }
  67. function message(msg) {
  68. dispatch.toClient('S_CHAT', 1, {
  69. channel: 24,
  70. authorID: 0,
  71. unk1: 0,
  72. gm: 0,
  73. unk2: 0,
  74. authorName: '',
  75. message: ' (autoloot) ' + msg
  76. });
  77. }
  78. }