index.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. const config = require('./config.js');
  3. module.exports = function AutoLootOld(mod) {
  4. const cmd = mod.command || mod.require.command;
  5. let enable = config.enable,
  6. enableAuto = config.enableAuto,
  7. interval = config.interval,
  8. throttleMax = config.throttleMax,
  9. scanInterval = config.scanInterval,
  10. radius = config.radius;
  11. let location = null,
  12. items = new Map(),
  13. lootTimeout = null;
  14. cmd.add('loot', {
  15. $default() {
  16. enable = !enable;
  17. mod.command.message(`${enable ? 'en' : 'dis'}abled`);
  18. },
  19. auto() {
  20. enableAuto = !enableAuto;
  21. mod.command.message(`auto-loot ${enableAuto ? 'en' : 'dis'}abled`);
  22. }
  23. });
  24. mod.game.me.on('change_zone', () => { items.clear(); });
  25. mod.hook('S_RETURN_TO_LOBBY', 1, () => { items.clear(); });
  26. mod.hook('C_PLAYER_LOCATION', 5, (e) => { location = e.loc; });
  27. mod.hook('S_SYSTEM_MESSAGE', 1, (e) => { if (e.message === '@41') return false });
  28. mod.hook('C_TRY_LOOT_DROPITEM', 4, () => { if(enable && !lootTimeout) lootTimeout = setTimeout(tryLoot, interval); });
  29. mod.hook('S_DESPAWN_DROPITEM', 4, (e) => { items.delete(e.gameId); });
  30. mod.hook('S_SPAWN_DROPITEM', 7, (e) => {
  31. if(!(config.blacklist.includes(e.item)) && (e.item < 8000 || e.item > 8025) && e.owners.some(owner => owner.playerId === mod.game.me.playerId)){
  32. //items.set(e.gameId, e);
  33. items.set(e.gameId, Object.assign(e, {priority: 0}));
  34. if(enableAuto && !lootTimeout) tryLoot();
  35. }
  36. });
  37. function tryLoot() {
  38. clearTimeout(lootTimeout);
  39. lootTimeout = null;
  40. if(!items.size || mod.game.me.mounted) return;
  41. for(let item of [...items.values()].sort((a, b) => a.priority - b.priority)){
  42. if(location.dist3D(item.loc) <= radius){
  43. mod.toServer('C_TRY_LOOT_DROPITEM', 4, item);
  44. //lootTimeout = setTimeout(tryLoot, interval);
  45. lootTimeout = setTimeout(tryLoot, Math.min(interval * ++item.priority, throttleMax));
  46. return;
  47. }
  48. }
  49. if(enableAuto) setTimeout(tryLoot, scanInterval);
  50. }
  51. }