IC Instance Restore on Ctrl + Z

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

Per 23-12-2024. Zie de nieuwste versie.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

// ==UserScript==
// @name         IC Instance Restore on Ctrl + Z
// @namespace    http://tampermonkey.net/
// @version      1.4
// @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 = {
        deletedInstances: [],
        deletedInstanceId: 0,
        deletedInstanceIds: [],
        deletedInstance: null,
        deletedElement: null,
        undoProgress: true
    };

    function waitForNuxt() { // Wait for Nuxt
        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.deletedInstances = [{ ...instanceA }, { ...instanceB }]; // Simple patch of getCraftResponse to backup instances
                window.controlzdata.deletedInstanceId = this.instanceId += 2; // id is saved and not index because index can change if other instances disappear
                return response;
            };
        } else {
            setTimeout(waitForNuxt, 100);
        }
    }

    function restoreInstances() {
        if (window.controlzdata.deletedInstances.length > 0) {
            window.controlzdata.deletedInstances.forEach(instance => { // Displace instance because it will use the built in duplicateInsance which displaces it in the other direction
                instance.left -= 10;
                instance.top += 10;
            });

            const [instanceA, instanceB] = window.controlzdata.deletedInstances;

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

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

            window.controlzdata.deletedInstanceIds = [instanceAId, instanceBId]; // Update the ids for unrestoring/removing

            window.controlzdata.deletedInstances = [];

            const instances = window.controlzdata.infinitecraft.instances;
            const indexToRemove = instances.findIndex(instance => instance.id === window.controlzdata.deletedInstanceId); // The id is not the same as the index so this is mandatory.

            if (indexToRemove !== -1) {
                window.controlzdata.deletedInstance = { ...instances[indexToRemove] };
                instances.splice(indexToRemove, 1);
            }
        }
    }

    function unrestoreInstances() { // Mostly the same as restoreInstances but just reverses it
        if (window.controlzdata.deletedInstanceIds.length > 0) {
            const lastRestoredData = window.controlzdata.deletedInstanceIds;
            window.controlzdata.deletedInstance.left -= 10;
            window.controlzdata.deletedInstance.top += 10;

            const [instanceAId, instanceBId] = lastRestoredData;

            const instances = window.controlzdata.infinitecraft.instances;

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

            window.controlzdata.deletedInstances = [instances[instanceAIndex], instances[instanceBIndex]]; // Backup again if you redecide and press Ctrl+z again

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

            if (window.controlzdata.deletedInstance) {
                window.controlzdata.infinitecraft.duplicateInstance(window.controlzdata.deletedInstance);
                window.controlzdata.deletedInstance = null;
                window.controlzdata.deletedInstanceId = instances[instances.length - 1].id; // same here
            }
        }
    }

    document.addEventListener("keydown", function(event) {
        if (event.ctrlKey && event.key === "z") {
            restoreInstances();
        }
        if (event.ctrlKey && event.key === "y") {
            unrestoreInstances();
        }
    });

    waitForNuxt();
})();