IC Instance Restore on Ctrl + Z

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

Versione datata 23/12/2024. Vedi la nuova versione l'ultima versione.

// ==UserScript==
// @name         IC Instance Restore on Ctrl + Z
// @namespace    http://tampermonkey.net/
// @version      1.5
// @license      MIT
// @description  Undo a craft with Ctrl + Z and redo with Ctrl + Y
// @icon         https://i.imgur.com/WlkWOkU.png
// @author       @activetutorial on discord
// @match        https://neal.fun/infinite-craft/
// @run-at       document-end
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    window.controlzdata = {
        ingredientInstances: {
            deletedInstances: [],
            deletedInstanceIds: []
        },
        resultInstances: {
            deletedInstances: [],
            deletedInstanceIds: []
        },
        infinitecraft: null,

        restoreInstances: function () {
            if (this.ingredientInstances.deletedInstances.length > 0) {
                const instancePair = this.ingredientInstances.deletedInstances.pop();
                instancePair.forEach(instance => {
                    instance.left -= 10;
                    instance.top += 10;
                });

                const [instanceA, instanceB] = instancePair;

                this.infinitecraft.duplicateInstance(instanceA);
                const instancesAfterDuplication1 = this.infinitecraft.instances;
                const instanceAId = instancesAfterDuplication1[instancesAfterDuplication1.length - 1].id;

                this.infinitecraft.duplicateInstance(instanceB);
                const instancesAfterDuplication2 = this.infinitecraft.instances;
                const instanceBId = instancesAfterDuplication2[instancesAfterDuplication2.length - 1].id;

                this.ingredientInstances.deletedInstanceIds.push([instanceAId, instanceBId]);

                const instances = this.infinitecraft.instances;
                const indexToRemove = instances.findIndex(instance => instance.id === this.resultInstances.deletedInstanceIds[this.resultInstances.deletedInstanceIds.length - 1][0]);
                this.resultInstances.deletedInstanceIds.pop();

                if (indexToRemove !== -1) {
                    const deletedInstance = instances[indexToRemove];
                    this.resultInstances.deletedInstances.push({ ...deletedInstance });
                    instances.splice(indexToRemove, 1);
                }
            }
        },

        unrestoreInstances: function () {
            if (this.ingredientInstances.deletedInstanceIds.length > 0) {
                const lastRestoredData = this.ingredientInstances.deletedInstanceIds.pop();
                const [instanceAId, instanceBId] = lastRestoredData;

                const instances = this.infinitecraft.instances;

                const instanceAIndex = instances.findIndex(instance => instance.id === instanceAId);
                const instanceBIndex = instances.findIndex(instance => instance.id === instanceBId);

                const instancePair = [
                    instances[instanceAIndex],
                    instances[instanceBIndex]
                ];

                this.ingredientInstances.deletedInstances.push(instancePair);

                if (instanceAIndex !== -1) {
                    instances.splice(instanceAIndex, 1);
                }
                if (instanceBIndex !== -1) {
                    instances.splice(instanceBIndex - 1, 1);
                }

                if (this.resultInstances.deletedInstances.length > 0) {
                    const deletedInstance = this.resultInstances.deletedInstances.pop();
                    deletedInstance.left -= 10;
                    deletedInstance.top += 10;
                    this.infinitecraft.duplicateInstance(deletedInstance);
                    this.resultInstances.deletedInstanceIds.push([instances[instances.length - 1].id]);
                }
            }
        }
    };

    function start() {
        if (window.$nuxt && window.$nuxt.$root && window.$nuxt.$root.$children[1] && window.$nuxt.$root.$children[1].$children[0]) {
            window.controlzdata.infinitecraft = window.$nuxt.$root.$children[1].$children[0].$children[0];
            const ogGCR = window.controlzdata.infinitecraft.getCraftResponse;
            window.controlzdata.infinitecraft.getCraftResponse = async function (instanceA, instanceB) {
                const response = await ogGCR.apply(this, arguments);
                window.controlzdata.ingredientInstances.deletedInstances.push([{ ...instanceA }, { ...instanceB }]);
                window.controlzdata.resultInstances.deletedInstanceIds.push([this.instanceId += 2]);
                window.controlzdata.resultInstances.deletedInstances = [];
                window.controlzdata.ingredientInstances.deletedInstanceIds = [];
                return response;
            };

            document.addEventListener("keydown", function(event) {
                if (event.ctrlKey && event.key === "z") {
                    window.controlzdata.restoreInstances();
                }
                if (event.ctrlKey && event.key === "y") {
                    window.controlzdata.unrestoreInstances();
                }
            });
        } else {
            setTimeout(start, 100);
        }
    }

    start();
})();