IdlePixel Keybinds - GodofNades Fork

Adds customizable keybinds for panel switching, casting spells in combat and starting combat in the different areas.

// ==UserScript==
// @name         IdlePixel Keybinds - GodofNades Fork
// @namespace    com.godofnades.idlepixel
// @version      1.1.1
// @description  Adds customizable keybinds for panel switching, casting spells in combat and starting combat in the different areas.
// @author       Original Author: Anwinity || Modded By: GodofNades
// @license      MIT
// @match        *://idle-pixel.com/login/play*
// @grant        none
// @require      https://greasyfork.org/scripts/441206-idlepixel/code/IdlePixel+.js?anticache=20220905
// ==/UserScript==

(function() {
    'use strict';

    const SPELL_DEFAULTS = {
        heal: "h",
        fire: "f",
        reflect: "r",
        invisibility: "i"
    };
    const SPELLS = Object.keys(Magic.spell_info);
    const PANELS = ["keyitems", "mining", "crafting", "gathering", "farming", "brewing", "woodcutting", "cooking", "fishing", "combat", "combat-canvas", "quests", "settings", "shop", "player-market", "donor-shop", "achievements", "idlepixelplus","history"];
    const COMBAT = ["field", "forest", "cave", "volcano", "northern_field", "mansion", "beach", "blood_field", "blood_forest", "blood_cave", "blood_volcano"];
    const POTION = ["combat_loot_potion","rain_potion","rare_monster_potion","super_rare_monster_potion"];

    let SPELL_MAPPING = {};
    let PANEL_MAPPING = {};
    let COMBAT_MAPPING = {};
    let POTION_MAPPING = {};

    class KeyBindPlugin extends IdlePixelPlusPlugin {
        constructor() {
            super("keybinds", {
                about: {
                    name: GM_info.script.name + " (ver: " + GM_info.script.version + ")",
                    version: GM_info.script.version,
                    author: GM_info.script.author,
                    description: GM_info.script.description + `<br />In general, lowercase letters map to their respective keys. For other keys, check out <a href="https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values">this page</a>. Note that this does NOT use KeyboardEvent.keyCode, it uses KeyboardEvent.key.`
                },
                config: [
                    {
                        label: "Combat Areas",
                        type: "label"
                    },
                    ... COMBAT.map(combat => {
                        return {
                            id: combat,
                            label: combat,
                            type: "string",
                            default: ""
                        }
                    }),
                    {
                        label: "Combat Spells",
                        type: "label"
                    },
                    ... SPELLS.map(spell => {
                        return {
                            id: spell,
                            label: spell,
                            type: "string",
                            default: SPELL_DEFAULTS[spell]||""
                        }
                    }),{
                        label: "Potions",
                        type: "label"
                    },
                    ... POTION.map(potion => {
                        return {
                            id: potion,
                            label: potion,
                            type: "string",
                            default: ""
                        }
                    }),
                    {
                        id: "selectMonster",
                        label: "Monster for Select Potion (Direct Trigger)",
                        type: "select",
                        default: "default",
                        options: [
                            {value: "default", label: "Select Monster"},
                            {value: "chicken", label: "Chicken (Fields)"},
                            {value: "rat", label: "Rat (Fields)"},
                            {value: "spider", label: "Spider (Fields)"},
                            {value: "bee", label: "Bee (Fields)"},
                            {value: "lizard", label: "Lizard (Fields)"},
                            {value: "snake", label: "Snake (Forest)"},
                            {value: "ants", label: "Ants (Forest)"},
                            {value: "wolf", label: "Wolf (Forest)"},
                            {value: "thief", label: "Thief (Forest)"},
                            {value: "forest_ent", label: "Forest Ent (Forest)"},
                            {value: "bear", label: "Bear (Cave)"},
                            {value: "goblin", label: "Goblin (Cave)"},
                            {value: "bat", label: "Bat (Cave)"},
                            {value: "skeleton", label: "Skeleton (Cave)"},
                            {value: "fire_hawk", label: "Fire Hawk (Volcano)"},
                            {value: "fire_snake", label: "Fire Snake (Volcano)"},
                            {value: "fire_golem", label: "Fire Golem (Volcano)"},
                            {value: "fire_witch", label: "Fire Witch (Volcano)"}
                        ]
                    },
                    {
                        id: "selectPlusMonster",
                        label: "Monster for Select Plus Potion (Direct Trigger)",
                        type: "select",
                        default: "default",
                        options: [
                            {value: "default", label: "Select Monster"},
                            {value: "ice_hawk", label: "Ice Hawk (Northern Fields)"},
                            {value: "ice_golem", label: "Ice Golem (Northern Fields)"},
                            {value: "ice_witch", label: "Ice Witch (Northern Fields)"},
                            {value: "yeti", label: "Yeti (Northern Fields)"},
                            {value: "ghost", label: "Ghost (Haunted Mansion)"},
                            {value: "grandma", label: "Grandma (Haunted Mansion)"},
                            {value: "exorcist", label: "Exorcist (Haunted Mansion)"},
                            {value: "reaper", label: "Reaper (Haunted Mansion)"},
                            {value: "shark", label: "Shark (Beach)"},
                            {value: "sea_soldier", label: "Shark (Beach)"},
                            {value: "puffer_fish", label: "Puffer Fish (Beach)"},
                            {value: "saltwater_crocodile", label: "Crocodile (Beach)"}
                        ]
                    },
                    {
                        label: "Panel Switching",
                        type: "label"
                    },
                    ... PANELS.map(panel => {
                        return {
                            id: panel,
                            label: panel,
                            type: "string",
                            default: ""
                        }
                    })
                ]
            });
        }

        onConfigsChanged() {
            SPELL_MAPPING = {};
            PANEL_MAPPING = {};
            COMBAT_MAPPING = {};
            POTION_MAPPING = {};

            COMBAT.forEach(combat => {
                let key = this.getConfig(combat);
                if(key) {
                    COMBAT_MAPPING[key] = combat;
                }
            });

            SPELLS.forEach(spell => {
                let key = this.getConfig(spell);
                if(key) {
                    SPELL_MAPPING[key] = spell;
                }
            });

            POTION.forEach(potion => {
                let key = this.getConfig(potion);
                if(key) {
                    POTION_MAPPING[key] = potion;
                }
            });

            PANELS.forEach(panel => {
                let key = this.getConfig(panel);
                if(key) {
                    PANEL_MAPPING[key] = panel;
                }
            });
        }

        handleKeyEvent(event) {
            if(event.keyCode == 9 && IdlePixelPlus.plugins.keybinds.getConfig("history") == "tab") {
                websocket.send(switch_panels('panel-history'));
            }
            if(event.key in COMBAT_MAPPING) {
                if(Globals.currentPanel == "panel-combat") {
                    const combat = COMBAT_MAPPING[event.key];
                    var chat_focused = $('#chat-area-input').is(':focus');
                    if(!chat_focused){
                        IdlePixelPlus.plugins.slapchop.quickFight(combat);
                    }
                }
            }
            if(event.key in SPELL_MAPPING) {
                if(Globals.currentPanel == "panel-combat-canvas") {
                    const spell = SPELL_MAPPING[event.key];
                    IdlePixelPlus.sendMessage(`SPELL=${spell}`);
                }
            }
            if(event.key in POTION_MAPPING) {
                if(Globals.currentPanel == "panel-combat-canvas" && (POTION_MAPPING[event.key] == "combat_loot_potion" || POTION_MAPPING[event.key] == "rain_potion")) {
                    const potion_combat = POTION_MAPPING[event.key];
                    if(potion_combat == "combat_loot_potion") {
                        websocket.send('BREWING_DRINK_COMBAT_LOOT_POTION');
                    } else {
                        websocket.send('DRINK=rain_potion')
                    }
                } else if(Globals.currentPanel == "panel-combat" && POTION_MAPPING[event.key] == "rare_monster_potion") {
                    const potion_other = POTION_MAPPING[event.key];
                    var chat_focused_other = $('#chat-area-input').is(':focus');
                    if(!chat_focused_other) {
                        var monsterSelect = this.getConfig("selectMonster");
                        if(monsterSelect != "default") {
                            websocket.send('DRINK_SELECT_POTION=' + monsterSelect)
                        } else {
                            Modals.clicks_rare_monster_potion();
                        }
                    }
                } else if(Globals.currentPanel == "panel-combat" && POTION_MAPPING[event.key] == "super_rare_monster_potion") {
                    const potion_other = POTION_MAPPING[event.key];
                    var chat_focused_rare_plus = $('#chat-area-input').is(':focus');
                    if(!chat_focused_rare_plus){
                        var monsterSelectPlus = this.getConfig("selectPlusMonster");
                        if(monsterSelectPlus != "default") {
                            websocket.send('DRINK_SUPER_SELECT_POTION=' + monsterSelectPlus);
                        } else {
                            Modals.clicks_super_rare_monster_potion();
                        }
                    }
                }
                if(event.key in PANEL_MAPPING) {
                    const focused = document.activeElement;
                    if(!focused || !["INPUT", "SELECT"].includes((focused.tagName||"").toUpperCase())) {
                        const panel = PANEL_MAPPING[event.key];
                        if(panel == "combat-canvas") {
                            if(Combat.in_combat()) {
                                IdlePixelPlus.setPanel(panel);
                            }
                            else {
                                IdlePixelPlus.setPanel(panel);
                            }
                        }
                    }
                }
            }
        }

        onLogin() {
            const self = this;

            document.addEventListener("keyup", e => {
                this.handleKeyEvent(e);
            });

            this.onConfigsChanged();
        }

    }

    const plugin = new KeyBindPlugin();
    IdlePixelPlus.registerPlugin(plugin);

})();