index.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. module.exports = function Loot(dispatch) {
  2. let auto = false,
  3. enabled = true,
  4. lootInterval,
  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, 250);
  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(location)
  65. if(Math.abs(loot[item].x - location.x1) < 120 && Math.abs(loot[item].y - location.y1) < 120)
  66. dispatch.toServer('C_TRY_LOOT_DROPITEM', 1, {
  67. id: loot[item].id
  68. });
  69. }
  70. }
  71. function message(msg) {
  72. dispatch.toClient('S_CHAT', 1, {
  73. channel: 24,
  74. authorID: 0,
  75. unk1: 0,
  76. gm: 0,
  77. unk2: 0,
  78. authorName: '',
  79. message: ' (autoloot) ' + msg
  80. });
  81. }
  82. }