index.js 2.4 KB

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