Greasy Fork 还支持 简体中文。

IC Instance Restore on Ctrl + Z

Undo a craft with Ctrl + Z and redo with Ctrl + Y

目前為 2024-12-23 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name IC Instance Restore on Ctrl + Z
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.5.1
  5. // @license MIT
  6. // @description Undo a craft with Ctrl + Z and redo with Ctrl + Y
  7. // @icon https://i.imgur.com/WlkWOkU.png
  8. // @author @activetutorial on discord
  9. // @match https://neal.fun/infinite-craft/
  10. // @run-at document-end
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. 'use strict';
  16.  
  17. window.controlzdata = {
  18. ingredientInstances: {
  19. deletedInstances: [],
  20. deletedInstanceIds: []
  21. },
  22. resultInstances: {
  23. deletedInstances: [],
  24. deletedInstanceIds: []
  25. },
  26. infinitecraft: null,
  27.  
  28. restoreInstances: function () {
  29. if (this.ingredientInstances.deletedInstances.length > 0) {
  30. const instancePair = this.ingredientInstances.deletedInstances.pop();
  31. instancePair.forEach(instance => {
  32. instance.left -= 10;
  33. instance.top += 10;
  34. });
  35.  
  36. const [instanceA, instanceB] = instancePair;
  37.  
  38. this.infinitecraft.duplicateInstance(instanceA);
  39. const instancesAfterDuplication1 = this.infinitecraft.instances;
  40. const instanceAId = instancesAfterDuplication1[instancesAfterDuplication1.length - 1].id;
  41.  
  42. this.infinitecraft.duplicateInstance(instanceB);
  43. const instancesAfterDuplication2 = this.infinitecraft.instances;
  44. const instanceBId = instancesAfterDuplication2[instancesAfterDuplication2.length - 1].id;
  45.  
  46. this.ingredientInstances.deletedInstanceIds.push([instanceAId, instanceBId]);
  47.  
  48. const instances = this.infinitecraft.instances;
  49. const indexToRemove = instances.findIndex(instance => instance.id === this.resultInstances.deletedInstanceIds[this.resultInstances.deletedInstanceIds.length - 1][0]);
  50. this.resultInstances.deletedInstanceIds.pop();
  51.  
  52. if (indexToRemove !== -1) {
  53. const deletedInstance = instances[indexToRemove];
  54. this.resultInstances.deletedInstances.push({ ...deletedInstance });
  55. instances.splice(indexToRemove, 1);
  56. }
  57. }
  58. },
  59.  
  60. unrestoreInstances: function () {
  61. if (this.ingredientInstances.deletedInstanceIds.length > 0) {
  62. const lastRestoredData = this.ingredientInstances.deletedInstanceIds.pop();
  63. const [instanceAId, instanceBId] = lastRestoredData;
  64.  
  65. const instances = this.infinitecraft.instances;
  66.  
  67. const instanceAIndex = instances.findIndex(instance => instance.id === instanceAId);
  68. const instanceBIndex = instances.findIndex(instance => instance.id === instanceBId);
  69.  
  70. const instancePair = [
  71. instances[instanceAIndex],
  72. instances[instanceBIndex]
  73. ];
  74.  
  75. this.ingredientInstances.deletedInstances.push(instancePair);
  76.  
  77. if (instanceAIndex !== -1) {
  78. instances.splice(instanceAIndex, 1);
  79. }
  80. if (instanceBIndex !== -1) {
  81. instances.splice(instanceBIndex - 1, 1);
  82. }
  83.  
  84. if (this.resultInstances.deletedInstances.length > 0) {
  85. const deletedInstance = this.resultInstances.deletedInstances.pop();
  86. deletedInstance.left -= 10;
  87. deletedInstance.top += 10;
  88. deletedInstance.isNew = false;
  89. this.infinitecraft.duplicateInstance(deletedInstance);
  90. this.resultInstances.deletedInstanceIds.push([instances[instances.length - 1].id]);
  91. }
  92. }
  93. }
  94. };
  95.  
  96. function start() {
  97. if (window.$nuxt && window.$nuxt.$root && window.$nuxt.$root.$children[1] && window.$nuxt.$root.$children[1].$children[0]) {
  98. window.controlzdata.infinitecraft = window.$nuxt.$root.$children[1].$children[0].$children[0];
  99. const ogGCR = window.controlzdata.infinitecraft.getCraftResponse;
  100. window.controlzdata.infinitecraft.getCraftResponse = async function (instanceA, instanceB) {
  101. const response = await ogGCR.apply(this, arguments);
  102. if(instanceA.elem && instanceB.elem){
  103. window.controlzdata.ingredientInstances.deletedInstances.push([{ ...instanceA }, { ...instanceB }]);
  104. window.controlzdata.resultInstances.deletedInstanceIds.push([this.instanceId += 2]);
  105. window.controlzdata.resultInstances.deletedInstances = [];
  106. window.controlzdata.ingredientInstances.deletedInstanceIds = [];
  107. }
  108. return response;
  109. };
  110.  
  111. document.addEventListener("keydown", function(event) {
  112. if (event.ctrlKey && event.key === "z") {
  113. window.controlzdata.restoreInstances();
  114. }
  115. if (event.ctrlKey && event.key === "y") {
  116. window.controlzdata.unrestoreInstances();
  117. }
  118. });
  119. } else {
  120. setTimeout(start, 2000);
  121. }
  122. }
  123.  
  124. start();
  125. })();