index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. const SettingsUI = require('tera-mod-ui').Settings;
  2. module.exports = function InstantEverything(mod) {
  3. if (mod.proxyAuthor !== 'caali' || !global.TeraProxy)
  4. mod.warn('You are trying to use InstantEverything on an unsupported version of tera-proxy. It may not work as expected, and even if it does now it may break at any point in the future!');
  5. const PURPOSES = ['enchant', 'upgrade', 'soulbind', 'merge', 'dismantle'];
  6. let hooks = {};
  7. function hook(purpose, ...args) {
  8. if (!hooks[purpose])
  9. hooks[purpose] = [];
  10. hooks[purpose].push(mod.hook(...args));
  11. }
  12. let enchanting = null;
  13. let upgrading = null;
  14. function enable(purpose) {
  15. switch (purpose) {
  16. case 'enchant': {
  17. hook('enchant', 'C_REGISTER_ENCHANT_ITEM', 1, event => { enchanting = event });
  18. hook('enchant', 'C_START_ENCHANT', 1, event => {
  19. if (enchanting && event.contract === enchanting.contract) {
  20. mod.send('C_REQUEST_ENCHANT', 1, enchanting);
  21. return false;
  22. }
  23. });
  24. hook('enchant', 'C_REQUEST_ENCHANT', 'raw', _ => false);
  25. break;
  26. }
  27. case 'upgrade': {
  28. const upgrading_method = mod.majorPatchVersion >= 79 ? 'EVOLUTION' : 'UPGRADE';
  29. hook('upgrade', 'C_REGISTER_' + upgrading_method + '_ITEM', 1, event => { upgrading = event });
  30. hook('upgrade', 'C_START_' + upgrading_method, 1, event => {
  31. if (upgrading && event.contract === upgrading.contract) {
  32. mod.send('C_REQUEST_' + upgrading_method, 1, upgrading);
  33. return false;
  34. }
  35. });
  36. hook('upgrade', 'C_REQUEST_' + upgrading_method, 'raw', _ => false);
  37. break;
  38. }
  39. case 'soulbind': {
  40. hook('soulbind', 'C_BIND_ITEM_BEGIN_PROGRESS', 1, event => {
  41. mod.send('C_BIND_ITEM_EXECUTE', 1, {
  42. contractId: event.contractId,
  43. });
  44. process.nextTick(() => {
  45. mod.send('S_CANCEL_CONTRACT', 1, {
  46. type: 32,
  47. id: event.contractId,
  48. });
  49. });
  50. });
  51. hook('soulbind', 'C_BIND_ITEM_EXECUTE', 'raw', _ => false);
  52. break;
  53. }
  54. case 'merge': {
  55. hook('merge', 'S_REQUEST_CONTRACT', 1, event => {
  56. if (!mod.game.me.is(event.senderId) || event.type != 33)
  57. return;
  58. mod.send('C_MERGE_ITEM_EXECUTE', 1, {
  59. contractId: event.id,
  60. });
  61. process.nextTick(() => {
  62. mod.send('S_CANCEL_CONTRACT', 1, {
  63. type: 33,
  64. id: event.id,
  65. });
  66. });
  67. });
  68. hook('merge', 'C_MERGE_ITEM_EXECUTE', 'raw', _ => false);
  69. break;
  70. }
  71. case 'dismantle': {
  72. hook('dismantle', 'C_RQ_START_SOCIAL_ON_PROGRESS_DECOMPOSITION', 1, event => {
  73. mod.send('C_RQ_COMMIT_DECOMPOSITION_CONTRACT', 1, {
  74. contract: event.contract,
  75. });
  76. return false;
  77. });
  78. hook('dismantle', 'C_RQ_COMMIT_DECOMPOSITION_CONTRACT', 'raw', _ => false);
  79. break;
  80. }
  81. }
  82. }
  83. function disable(purpose) {
  84. if (hooks[purpose]) {
  85. hooks[purpose].forEach(h => mod.unhook(h));
  86. hooks[purpose] = [];
  87. }
  88. }
  89. // Main
  90. PURPOSES.forEach(purpose => {
  91. if (mod.settings[purpose])
  92. enable(purpose);
  93. });
  94. mod.command.add('instant', {
  95. $default(purpose) {
  96. if (PURPOSES.indexOf(purpose) < 0) {
  97. if (ui) {
  98. ui.show();
  99. } else {
  100. mod.command.message(purpose ? `Invalid mode: ${purpose}!` : 'Must specify mode!');
  101. mod.command.message(`Valid modes: ${PURPOSES.join(', ')}`);
  102. }
  103. return;
  104. }
  105. if (mod.settings[purpose]) {
  106. disable(purpose);
  107. mod.command.message(`Instant ${purpose} disabled!`);
  108. } else {
  109. enable(purpose);
  110. mod.command.message(`Instant ${purpose} enabled!`);
  111. }
  112. mod.settings[purpose] = !mod.settings[purpose];
  113. }
  114. });
  115. // Settings UI
  116. let ui = null;
  117. if (global.TeraProxy.GUIMode) {
  118. ui = new SettingsUI(mod, require('./settings_structure'), mod.settings, { height: 232 });
  119. ui.on('update', settings => { mod.settings = settings; });
  120. this.destructor = () => {
  121. if (ui) {
  122. ui.close();
  123. ui = null;
  124. }
  125. };
  126. }
  127. };