Valheim
0 of 0

File information

Last updated

Original upload

Created by

InnoCreator

Uploaded by

InnoCreator

Virus scan

Safe to use

About this mod

This mod helps quickly equip weapon and shield, also weapon and torch.

Permissions and credits
Changelogs
Donations
This mod helps quickly equip weapon and shield, also weapon and torch.

using BepInEx;
using HarmonyLib;
using UnityEngine;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using BepInEx.Configuration;

namespace EquipOffHand
{
    [BepInPlugin("innocreator.equipoffhand", "EquipOffHand", "1.0")]
    [BepInProcess("valheim.exe")]
    public class EquipOffHand : BaseUnityPlugin
    {
        public static ConfigEntry<bool> showDebugMessages;
        public static ConfigEntry<bool> equipWeapon;
        public static ConfigEntry<bool> unequipShield;
        public static ConfigEntry<bool> unequipTorch;
        private readonly Harmony harmony = new Harmony("innocreator.equipoffhand");

        void Awake()
        {
            showDebugMessages = Config.Bind<bool>("General", "showDebugMessages", false, "Display script debug messages.");
            equipWeapon = Config.Bind<bool>("Options", "equipWeapon", true, "Equip your strongest WEAPON when you take out your torch.");
            unequipShield = Config.Bind<bool>("Options", "unequipShield", true, "Unequip your SHIELD when you put away your weapon.");
            unequipTorch = Config.Bind<bool>("Options", "unequipTorch", true, "Unequip your TORCH when you put away your weapon.");

            harmony.PatchAll();
        }

        [HarmonyPatch(typeof(Humanoid), nameof(Humanoid.EquipItem))]
        public static class EquipOffHand_EquipItem
        {
            private static bool Postfix(bool __result, Humanoid __instance, ItemDrop.ItemData item)
            {
                List<Player> playersList = Player.GetAllPlayers();
                Player playerOneList = playersList[0];
                Player playerOneReference = Player.GetPlayer(playerOneList.GetPlayerID());

                if (!__result || !playerOneReference.IsPlayer() || !playerOneReference)
                    return false;
                if (playerOneReference.RightItem?.m_shared.m_itemType == ItemDrop.ItemData.ItemType.OneHandedWeapon)
                {
                    if (showDebugMessages.Value)
                        Debug.Log("innoCretaor  Equip  Weapon");

                    if (playerOneReference.RightItem?.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Torch ||
                        playerOneReference.LeftItem?.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Torch ||
                        playerOneReference.LeftItem?.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Shield)
                    {
                        if (showDebugMessages.Value)
                            Debug.Log("innoCretaor  Equip  Torch or Shield already equipped");
                        return false;
                    }

                    List<ItemDrop.ItemData> inventoryItems = playerOneReference.GetInventory().GetAllItems();
                    ItemDrop.ItemData bestShield = inventoryItems.Where(inventoryItem =>
                        inventoryItem.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Shield &&
                        !inventoryItem.m_equipped).OrderByDescending(inventoryItem =>
                        inventoryItem.m_shared.m_blockPower).FirstOrDefault();

                    if (bestShield != null)
                        playerOneReference.EquipItem(bestShield, false);
                }
                if (equipWeapon.Value && playerOneReference.RightItem?.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Torch)
                {
                    if (showDebugMessages.Value)
                        Debug.Log("innoCretaor  Equip  Torch");

                    if (playerOneReference.LeftItem?.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Shield)
                    {
                        if (showDebugMessages.Value)
                            Debug.Log("innoCretaor  Equip  Shield already equipped");
                        return false;
                    }

                    List<ItemDrop.ItemData> inventoryItems = playerOneReference.GetInventory().GetAllItems();
                    ItemDrop.ItemData bestOneHandWeapon = inventoryItems.Where(inventoryItem =>
                        inventoryItem.m_shared.m_itemType == ItemDrop.ItemData.ItemType.OneHandedWeapon &&
                        !inventoryItem.m_equipped).OrderByDescending(inventoryItem =>
                        inventoryItem.m_shared.m_attack).FirstOrDefault();

                    if (bestOneHandWeapon != null)
                        playerOneReference.EquipItem(bestOneHandWeapon, false);
                }
                return __result;
            }
        }

        [HarmonyPatch(typeof(Humanoid), nameof(Humanoid.UnequipItem))]
        public static class EquipOffHand_UnequipItem
        {
            private static void Postfix(Humanoid __instance, ItemDrop.ItemData item)
            {
                List<Player> playersList = Player.GetAllPlayers();
                Player playerOneList = playersList[0];
                Player playerOneReference = Player.GetPlayer(playerOneList.GetPlayerID());

                if (!playerOneReference || !playerOneReference.IsPlayer() || item?.m_shared.m_itemType != ItemDrop.ItemData.ItemType.OneHandedWeapon)
                    return;
                if (showDebugMessages.Value)
                    Debug.Log("innoCretaor  Unequip  Weapon");

                List<ItemDrop.ItemData> inventoryItems = playerOneReference.GetInventory().GetAllItems();

                if (unequipShield.Value && playerOneReference.LeftItem?.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Shield)
                {
                    if (showDebugMessages.Value)
                        Debug.Log("innoCretaor  Unequip  Shield");

                    ItemDrop.ItemData equippedShield = inventoryItems.Where(itemEquipped =>
                        itemEquipped.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Shield &&
                        itemEquipped.m_equipped).FirstOrDefault();

                    playerOneReference.UnequipItem(equippedShield, false);
                    return;
                }
                if (unequipTorch.Value && playerOneReference.LeftItem?.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Torch)
                {
                    if (showDebugMessages.Value)
                        Debug.Log("innoCretaor  Unequip  Torch");

                    ItemDrop.ItemData equippedTorch = inventoryItems.Where(itemEquipped =>
                        itemEquipped.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Torch &&
                        itemEquipped.m_equipped).FirstOrDefault();

                    playerOneReference.UnequipItem(equippedTorch, false);
                    return;
                }  
            }
        }
    }
}