index.js 2.5 KB

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