Skip to content

Commit

Permalink
Merge pull request #7 from Vapok/bug/BagDuplicationOnInception
Browse files Browse the repository at this point in the history
Fixes Bag Duplication on Inception and ensures Weightless works from EpicLoot
  • Loading branch information
Vapok authored Jan 16, 2023
2 parents 1b77d57 + b3745d0 commit 47c3f55
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 48 deletions.
44 changes: 15 additions & 29 deletions AdventureBackpacks/Assets/Backpacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using AdventureBackpacks.Components;
using AdventureBackpacks.Configuration;
using ItemManager;
using UnityEngine;
using Vapok.Common.Abstractions;
using Vapok.Common.Managers;
using Vapok.Common.Managers.StatusEffects;
Expand Down Expand Up @@ -304,40 +305,25 @@ public static void OpenBackpack()
InventoryGui.instance.Show(_backpackContainer);
}

public static void EjectBackpack(ItemDrop.ItemData item, Player player, Inventory backpackInventory)
public static bool CheckForInception(Inventory __instance, ItemDrop.ItemData item)
{
if (item == null || player == null || backpackInventory == null)
return;

var playerInventory = player.GetInventory();

// Move the backpack to the player's Inventory if there's room.
if (playerInventory.HaveEmptySlot())
{
playerInventory.MoveItemToThis(backpackInventory, item);
}

// Otherwise drop the backpack.
else
if (__instance.m_name.Equals(Backpacks.BackpacksInventoryName))
{
_log.Message("Are you trying to put a backpack in a backpack? What am I? A Bag of Holding?");
// If the item is a backpack...
if (Backpacks.BackpackTypes.Contains(item.m_shared.m_name))
{
if (Player.m_localPlayer != null)
{
Player.m_localPlayer.Message(MessageHud.MessageType.Center, "$vapok_mod_no_inception");
}

// Remove the backpack item from the Inventory instance and then drop the backpack item in front of the player.
backpackInventory.RemoveItem(item);

var transform = player.transform;

if (transform == null)
return;

var itemDrop = ItemDrop.DropItem(item, 1, transform.position + transform.forward + transform.up, transform.rotation);
itemDrop.Save();
// Nope!
AdventureBackpacks.Log.Message("You can't put a backpack inside a backpack, silly!");
return false;
}
}

//Save Backpack Inventory
var backpackComponent = item.Data().GetOrCreate<BackpackComponent>();
backpackComponent.Save(backpackInventory);

return true;
}

public static void QuickDropBackpack()
Expand Down
46 changes: 35 additions & 11 deletions AdventureBackpacks/Patches/Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,41 @@ static void Postfix(Inventory __instance)
}
}
}

[HarmonyPatch(typeof(Inventory), nameof(Inventory.MoveItemToThis), new[] {typeof(Inventory), typeof(ItemDrop.ItemData), typeof(int), typeof(int), typeof(int)})]
[HarmonyPriority(Priority.First)]
static class MoveItemToThisPatch
{
static bool Prefix(Inventory __0, ItemDrop.ItemData __1, int __2, int __3, int __4, Inventory __instance)
{
var fromInventory = __0;
var item = __1;
if (fromInventory == null || item == null)
return false;

return Backpacks.CheckForInception(__instance, item);
}


}

[HarmonyPatch(typeof(Inventory), nameof(Inventory.MoveItemToThis), new[] {typeof(Inventory), typeof(ItemDrop.ItemData)})]
[HarmonyPriority(Priority.First)]
static class MoveItemToThisOtherPatch
{
static bool Prefix(Inventory __0, ItemDrop.ItemData __1, Inventory __instance)
{
var fromInventory = __0;
var item = __1;
if (fromInventory == null || item == null)
return false;

return Backpacks.CheckForInception(__instance, item);
}


}


[HarmonyPatch(typeof(Inventory), nameof(Inventory.UpdateTotalWeight))]
static class UpdateTotalWeightPatch
Expand All @@ -47,17 +82,6 @@ static void Prefix(Inventory __instance)
// Go through all the items, match them for any of the names in backpackTypes.
foreach (ItemDrop.ItemData item in items)
{
// If the item is a backpack...
if (Backpacks.BackpackTypes.Contains(item.m_shared.m_name))
{
// Chuck it out!
AdventureBackpacks.Log.Message("You can't put a backpack inside a backpack, silly!");
Backpacks.EjectBackpack(item, player, __instance);

// There is only ever one backpack in the backpack inventory, so we don't need to continue the loop once we've chucked it out.
// Besides, you'll get a "InvalidOperationExecution: Collection was modified; enumeration operation may not execute" error if you don't break the loop here :p
break;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions AdventureBackpacks/Patches/ItemDrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace AdventureBackpacks.Patches;
public class ItemDropPatches
{
[HarmonyPatch(typeof(ItemDrop.ItemData), nameof(ItemDrop.ItemData.GetWeight))]
[HarmonyBefore(new string[]{"randyknapp.mods.epicloot"})]
static class GetWeightPatch
{
static void Postfix(ItemDrop.ItemData __instance, ref float __result)
Expand Down
4 changes: 2 additions & 2 deletions AdventureBackpacks/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
4 changes: 3 additions & 1 deletion AdventureBackpacks/Translations/English.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
"vapok_mod_se_arcticbackpackeffects_start": "Backpack equipped",
"vapok_mod_se_arcticbackpack": "Arctic Effect",

"vapok_mod_ui_backpack_inventoryname": "Adventure Backpack"
"vapok_mod_ui_backpack_inventoryname": "Adventure Backpack",

"vapok_mod_no_inception": "You have angered the gods!"
}
3 changes: 3 additions & 0 deletions PATCHNOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Adventure Backpacks Patchnotes

# 1.0.1.0
* Adds in Weightless Compatibility with Epic Loot to ensure maximum epicness. (also you're cheating... lol)
* Resolves a Bag Duplication that was occuring when trying to insert a backpack into a backpack. (The gods are watching you.)
# 1.0.0.0
* Initial Release of Adventure Backpacks
* This is a full refactor and completely re-writen version of JotunnBackpacks.
Expand Down
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
# Adventure Backpacks by Vapok

This Valheim mod seeks to introduce the concept of Backpacks throughout the Valheim progression. Currently,
Adventure Backpacks is a complete rewrite of the original Jotunn Backpacks, which preserves the Rugged and Artic Backpacks
Adventure Backpacks is a complete rewrite of the original Jotunn Backpacks 2.1.0+, which preserves the Rugged and Artic Backpacks
that you've come to enjoy.

## Roadmap
* v1.0.0 - Initial Release - Get existing functionality ported.
* v1.0.0 - Initial Release - Get existing functionality ported. (Completed)
* v1.3.0 - Mistlands Backpack - Introduce a new Backpack that when crafted will include the Feather Fall effect.
* v1.5.0 - Adventure Begins - Revamping all backpacks to be upgradable and offer new perks as upgrades happen. and offer backpacks at each
stage of maturity. This includes a backpack progression that will start at Meadows and continue through Mistlands
* v2.0.0 - To be determined!

## Mod Author Details
# Current Patch Notes
## 1.0.1.0
* Adds in Weightless Compatibility with Epic Loot to ensure maximum epicness. (also you're cheating... lol)
* Resolves a Bag Duplication that was occuring when trying to insert a backpack into a backpack. (The gods are watching you.)

## Compatible Mods (Verified)
* Epic Loot 0.9.3+
* Equipment and Quickslots
* Advanced Portals
* AnyPortal
* Project Auga
* _There's probably a ton of others. We are friendly to most mods. If you see a conflict though, let me know!_

## Incompatible Mods
* JotunnBackpacks - (This will convert bags, but safe to revert back to JotunnBackpacks)

### Mod Author Details

Author: [Vapok](https://github.com/Vapok)

Source: [Github](https://github.com/Vapok/AdventureBackpacks)

Discord: [RandyKnapp's Mod Community](https://discord.gg/randyknappmods)

Patch notes: [Github Patchnotes](https://github.com/RandyKnapp/ValheimMods/blob/main/EpicLoot/patchnotes.md)
Patch notes: [Github Patchnotes](https://github.com/Vapok/AdventureBackpacks/blob/main/PATCHNOTES.md)


2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "AdventureBackpacks",
"version_number": "1.0.0",
"version_number": "1.0.1",
"website_url": "https://github.com/Vapok/AdventureBackpacks",
"description": "A Valheim Mod to add Adventuring Backpacks to the Game. These packs will grow and become more useful as the game progresses.",
"dependencies": [
Expand Down

0 comments on commit 47c3f55

Please sign in to comment.