Skip to content

Commit

Permalink
Merge pull request #118 from Vapok/vapok/r1.7.0
Browse files Browse the repository at this point in the history
Release of the Adventure Backpacks API
  • Loading branch information
Vapok authored Oct 31, 2023
2 parents e1d46fd + 6361fea commit cbc48d1
Show file tree
Hide file tree
Showing 28 changed files with 913 additions and 64 deletions.
12 changes: 8 additions & 4 deletions AdventureBackpacks.sln
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,24 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Translations", "Translation
Translations\AdventureBackpacks.Polish.json = Translations\AdventureBackpacks.Polish.json
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Documentation", "Documentation", "{D68E7D37-E82E-4419-BA0E-18C7EAD3F38B}"
ProjectSection(SolutionItems) = preProject
Docs\AdventureBackpacksAPI.md = Docs\AdventureBackpacksAPI.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
API|Any CPU = API|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{997CB563-FCC7-44B7-8F71-069747D27CC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{997CB563-FCC7-44B7-8F71-069747D27CC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{997CB563-FCC7-44B7-8F71-069747D27CC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{997CB563-FCC7-44B7-8F71-069747D27CC5}.Release|Any CPU.Build.0 = Release|Any CPU
{6215C559-83D3-4512-AD75-9F6DBD452083}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6215C559-83D3-4512-AD75-9F6DBD452083}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6215C559-83D3-4512-AD75-9F6DBD452083}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6215C559-83D3-4512-AD75-9F6DBD452083}.Release|Any CPU.Build.0 = Release|Any CPU
{997CB563-FCC7-44B7-8F71-069747D27CC5}.API|Any CPU.ActiveCfg = API|Any CPU
{997CB563-FCC7-44B7-8F71-069747D27CC5}.API|Any CPU.Build.0 = API|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{453B1EE0-9980-4B66-93B3-2DDBD21FEE03} = {125A2AE5-FB75-4EED-B971-3E0C9C056DA0}
Expand Down
2 changes: 2 additions & 0 deletions AdventureBackpacks.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=ABAPI/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
142 changes: 142 additions & 0 deletions AdventureBackpacks/API/ABAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
using System.Collections.Generic;
using JetBrains.Annotations;
#if ! API
using AdventureBackpacks.Extensions;
using AdventureBackpacks.Features;
#endif

namespace AdventureBackpacks.API;

/// <summary>
/// Adventure Backpacks API. Be sure to include the AdventureBackpacksAPI.dll as a dependency to your project.
/// </summary>
[PublicAPI]
// ReSharper disable once InconsistentNaming
public partial class ABAPI
{
/// <summary>
/// Notifies if the ABAPI is active or not.
/// </summary>
/// <returns>true of false</returns>
public static bool IsLoaded()
{
#if ! API
return true;
#else
return false;
#endif
}

/// <summary>
/// When provided with an ItemData object, will detect whether the Item is an Adventure Backpack or not.
/// </summary>
/// <param name="itemData">This is the ItemDrop.ItemData object of the item.</param>
/// <returns>true or false</returns>
public static bool IsBackpack(ItemDrop.ItemData itemData)
{
#if ! API
return itemData != null && itemData.IsBackpack();
#else
return false;
#endif
}


/// <summary>
/// Determines if the Player provided is currently wearing a backpack.
/// </summary>
/// <param name="player">Player, usually Player.m_localPlayer</param>
/// <returns>true or false</returns>
public static bool IsBackpackEquipped(Player player)
{
#if ! API
return player != null && player.IsBackpackEquipped();
#else
return false;
#endif
}

/// <summary>
/// Determines if the player is capable of currently opening the equipped backpack.
/// </summary>
/// <param name="player">Player, usually Player.m_localPlayer</param>
/// <returns>true or false</returns>
public static bool CanOpenBackpack(Player player)
{
#if ! API
return player != null && player.CanOpenBackpack();
#else
return false;
#endif
}

/// <summary>
/// Determines if the player provided is wearing the item provided and that it's a backpack.
/// </summary>
/// <param name="player">Player, usually Player.m_localPlayer</param>
/// <param name="itemData">Any ItemData</param>
/// <returns>true or false. If item provided is not a backpack, will return false.</returns>
public static bool IsThisBackpackEquipped(Player player, ItemDrop.ItemData itemData)
{
#if ! API
return player != null && player.IsThisBackpackEquipped(itemData);
#else
return false;
#endif
}

/// <summary>
/// Returns a Backpack object if the provided Player is currently wearing a backpack.
/// </summary>
/// <param name="player">Player, usually Player.m_localPlayer</param>
/// <returns>Nullable Backpack Object</returns>
public static Backpack? GetEquippedBackpack(Player player)
{
#if ! API
var backpackComponent = player.GetEquippedBackpack();
return ConvertBackpackItem(backpackComponent);
#else
return null;
#endif
}

/// <summary>
/// Returns Backpack object of the provided itemData. Operates similarly to a TryGet but with a nullable type.
/// </summary>
/// <param name="itemData">ItemDrop.ItemData object</param>
/// <returns>Nullable Backpack Object. Check HasValue.</returns>
public static Backpack? GetBackpack(ItemDrop.ItemData itemData)
{
#if ! API
return ConvertBackpackItem(itemData);
#else
return null;
#endif
}

/// <summary>
/// Retrieves the current Active Backpack StatusEffects running in the local players game.
/// </summary>
/// <returns>HashSet of Status Effects.</returns>
public static HashSet<StatusEffect> GetActiveBackpackStatusEffects()
{
#if ! API
return EquipmentEffectCache.ActiveEffects;
#else
return null;
#endif
}

/// <summary>
/// Method to activate the backpack on the local player's GUI and open it. Use in conjunction with CanOpenBackpack()
/// </summary>
/// <param name="player">Player, usually Player.m_localPlayer</param>
/// <param name="gui">The instance of InventoryGui</param>
public static void OpenBackpack(Player player, InventoryGui gui)
{
#if ! API
if (player != null)
player.OpenBackpack(gui);
#endif
}
}
72 changes: 72 additions & 0 deletions AdventureBackpacks/API/Privates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#if ! API
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Vapok.Common.Managers;using AdventureBackpacks.Assets;
using AdventureBackpacks.Assets.Items;
using AdventureBackpacks.Components;
using AdventureBackpacks.Extensions;


namespace AdventureBackpacks.API;


// ReSharper disable once InconsistentNaming
public partial class ABAPI
{
private static Backpack? ConvertBackpackItem(BackpackComponent component)
{
var definition = GetBackPackDefinitionFromComponent(component);
if (!definition.HasValue)
return null;

var backpackItem = new Backpack
{
Name = definition.Value.ItemName,
ItemData = component.Item,
Definition = definition.Value,
Inventory = component.GetInventory()
};
return backpackItem;
}

private static Backpack? ConvertBackpackItem(ItemDrop.ItemData itemData)
{
if (!itemData.IsBackpack())
return null;

var component = itemData.Data().GetOrCreate<BackpackComponent>();
return ConvertBackpackItem(component);
}

private static Dictionary<int, Vector2> GetBackpackSizing(BackpackItem backpack)
{
return backpack.BackpackSize.ToDictionary(entry => entry.Key, entry => entry.Value.Value);
}

private static BackpackDefinition? GetBackPackDefinitionFromComponent(BackpackComponent component)
{
var isBackpack = component.Item.TryGetBackpackItem(out var backpack);
if (isBackpack)
return null;

return GetBackPackDefinition(backpack);
}

private static BackpackDefinition GetBackPackDefinition(BackpackItem backpack)
{
var definition = new BackpackDefinition
{
ItemName = backpack.ItemName,
PrefabName = backpack.PrefabName,
BackpackSizeByQuality = GetBackpackSizing(backpack),
WeightMultiplier = backpack.WeightMultiplier.Value,
CarryBonus = backpack.CarryBonus.Value,
SpeedMod = backpack.SpeedMod.Value,
EnableFreezing = backpack.EnableFreezing.Value,
BackpackBiome = backpack.BackpackBiome.Value
};
return definition;
}
}
#endif
145 changes: 145 additions & 0 deletions AdventureBackpacks/API/Structs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace AdventureBackpacks.API;

/// <summary>
/// This is a Flags enum for determining Backpack Biomes. This is not representative of Heightmap.Biomes.
/// </summary>
[Flags]
public enum BackpackBiomes : uint
{
/// <summary>
/// None, no backpack effects are applied.
/// </summary>
None = 0,
/// <summary>
/// Meadows Backpack Effects
/// </summary>
Meadows = 1 << 0,
/// <summary>
/// Black Forest Backpack Effects
/// </summary>
BlackForest = 1 << 1,
/// <summary>
/// Swamp Backpack Effects
/// </summary>
Swamp = 1 << 2,
/// <summary>
/// Mountains Backpack Effects
/// </summary>
Mountains = 1 << 3,
/// <summary>
/// Plains Backpack Effects
/// </summary>
Plains = 1 << 4,
/// <summary>
/// Mistlands Backpack Effects
/// </summary>
Mistlands = 1 << 5,
/// <summary>
/// Special Biome configured for Cheb's Necromancy
/// </summary>
Necromancy = 1 << 6
}

// ReSharper disable once InconsistentNaming
public partial class ABAPI
{
/// <summary>
/// Instanced Backpack Information
/// </summary>
public struct Backpack
{
/// <summary>
/// Backpack Name
/// </summary>
public string Name;
/// <summary>
/// ItemData representative of the Backpack
/// </summary>
public ItemDrop.ItemData ItemData;
/// <summary>
/// Inventory object of the configured backpack
/// </summary>
public Inventory Inventory;
/// <summary>
/// Definition of the Backpack item
/// </summary>
public BackpackDefinition Definition;
}

/// <summary>
/// Backpack Definition Settings
/// </summary>
public struct BackpackDefinition
{
/// <summary>
/// Asset Bundle Name
/// </summary>
public string AssetName;
/// <summary>
/// Folder containing the Asset Bundle
/// </summary>
public string AssetFolderName;
/// <summary>
/// Prefab Name of the Backpack Asset
/// </summary>
public string PrefabName;
/// <summary>
/// Item Name of the Backpack. Use the $_name localize token.
/// </summary>
public string ItemName;
/// <summary>
/// Dictionary of Vector2's that contain the x and y sizing of the backpack at each Quality level'
/// Dictionary key is the Item's Quality level.
/// Dictionary value is the Vector2 object.
/// </summary>
public Dictionary<int,Vector2> BackpackSizeByQuality;
/// <summary>
/// Provides the configured weight multiplier that reduces the weight of the items in the backpack.
/// </summary>
public float WeightMultiplier;
/// <summary>
/// Provides the additional carry weight bonus applied to backpacks.
/// </summary>
public int CarryBonus;
/// <summary>
/// Provides the Speed Modification that is applied on the backpack.
/// </summary>
public float SpeedMod;
/// <summary>
/// Provides whether the wearer of the backpack will freeze or not.
/// </summary>
public bool EnableFreezing;
/// <summary>
/// Provides the configured biomes settings applied to the backpack.
/// </summary>
public BackpackBiomes BackpackBiome;

}

/// <summary>
/// Configuration of Drop Target
/// </summary>
public struct DropTarget
{
/// <summary>
/// Prefab name of creature
/// </summary>
public string Creature;
/// <summary>
/// Min number of items that can drop.
/// </summary>
public int Min;
/// <summary>
/// Maximum number of items that can drop.
/// </summary>
public int Max;
/// <summary>
/// Configured Drop Chance
/// </summary>
public float Chance;
}
}
Loading

0 comments on commit cbc48d1

Please sign in to comment.