IC Instance Restore on Ctrl + Z

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

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         IC Instance Restore on Ctrl + Z
// @namespace    http://tampermonkey.net/
// @version      1.6.1
// @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: [],
            InstanceIds: []
        },
        infinitecraft: null,

        updateIds: function (oldId, newId) {
            const replaceId = (list) => {
                for (let pair of list) {
                    const index = pair.indexOf(oldId);
                    if (index !== -1) {
                        pair[index] = newId;
                    }
                }
            };

            replaceId(this.resultInstances.InstanceIds);
            replaceId(this.ingredientInstances.deletedInstanceIds);
        },

        deleteInstance: function (id) {
            const instances = this.infinitecraft.instances;
            const index = instances.findIndex(instance => instance.id === id);

            if (index !== -1) {
                const deletedInstance = { ...instances[index] };
                instances.splice(index, 1);
                return deletedInstance;
            }
            return null;
        },

        makeInstance: function (instanceCopy) {
            instanceCopy.left -= 10;
            instanceCopy.top += 10;
            this.infinitecraft.duplicateInstance(instanceCopy);
            const newInstance = this.infinitecraft.instances.at(-1);

            if (newInstance) {
                this.updateIds(instanceCopy.id, newInstance.id);
            }

            return newInstance ? newInstance.id : null;
        },

        restoreInstances: function () {
            if (this.ingredientInstances.deletedInstances.length > 0) {
                const instancePair = this.ingredientInstances.deletedInstances.pop();

                const [instanceA, instanceB] = instancePair;
                const instanceAId = this.makeInstance(instanceA);
                const instanceBId = this.makeInstance(instanceB);

                if (instanceAId && instanceBId) {
                    this.ingredientInstances.deletedInstanceIds.push([instanceAId, instanceBId]);
                }

                const resultInstanceId = this.resultInstances.InstanceIds.pop()?.[0];
                if (resultInstanceId) {
                    const deletedInstance = this.deleteInstance(resultInstanceId);
                    if (deletedInstance) {
                        this.resultInstances.deletedInstances.push(deletedInstance);
                    }
                }
            }
        },

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

                const instanceA = this.deleteInstance(instanceAId);
                const instanceB = this.deleteInstance(instanceBId);

                if (instanceA && instanceB) {
                    this.ingredientInstances.deletedInstances.push([instanceA, instanceB]);
                }

                if (this.resultInstances.deletedInstances.length > 0) {
                    const deletedInstance = this.resultInstances.deletedInstances.pop();
                    const newInstanceId = this.makeInstance(deletedInstance);
                    if (newInstanceId) {
                        this.resultInstances.InstanceIds.push([newInstanceId]);
                    }
                }
            }
        }
    };

    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);

                if (instanceA.elem && instanceB.elem) {
                    window.controlzdata.ingredientInstances.deletedInstances.push([{ ...instanceA }, { ...instanceB }]);
                    window.controlzdata.resultInstances.InstanceIds.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, 2000);
        }
    }

    start();
})();