generated from AriasCreations/vsmodtemplate
parent
ac8ac62d27
commit
a88807045e
4 changed files with 164 additions and 43 deletions
131
AriasServerUtils/RustyGearUtils.cs
Normal file
131
AriasServerUtils/RustyGearUtils.cs
Normal file
|
@ -0,0 +1,131 @@
|
|||
using System;
|
||||
using AriasServerUtils;
|
||||
using Vintagestory.API.Common;
|
||||
using Vintagestory.API.Config;
|
||||
using Vintagestory.API.Datastructures;
|
||||
using Vintagestory.API.Server;
|
||||
using Vintagestory.API.Util;
|
||||
|
||||
public static class RustyGearUtils
|
||||
{
|
||||
|
||||
|
||||
internal static string[] saveInvTypes = new string[] {
|
||||
GlobalConstants.hotBarInvClassName,
|
||||
GlobalConstants.backpackInvClassName,
|
||||
GlobalConstants.craftingInvClassName,
|
||||
GlobalConstants.mousecursorInvClassName,
|
||||
GlobalConstants.characterInvClassName
|
||||
};
|
||||
|
||||
// Replace with the correct code if it's different
|
||||
private const string RustyGearCode = "currency-rustygear";
|
||||
|
||||
/// <summary>
|
||||
/// Counts the total number of rusty gears in the player's inventory.
|
||||
/// </summary>
|
||||
public static int CountRustyGears(IServerPlayer player)
|
||||
{
|
||||
int total = 0;
|
||||
|
||||
player.Entity.WalkInventory((slot) =>
|
||||
{
|
||||
if (slot is ItemSlotCreative || !(slot.Inventory is InventoryBasePlayer)) return true;
|
||||
|
||||
total += CurrencyValuePerItem(slot) * slot.StackSize;
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
private static int CurrencyValuePerItem(ItemSlot slot)
|
||||
{
|
||||
JsonObject obj = slot.Itemstack?.Collectible?.Attributes?["currency"];
|
||||
if (obj != null && obj.Exists)
|
||||
{
|
||||
JsonObject v = obj["value"];
|
||||
return v.Exists ? v.AsInt(0) : 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/// <summary>
|
||||
/// Attempts to subtract a specific number of rusty gears from the player's inventory.
|
||||
/// </summary>
|
||||
/// <param name="player">The player.</param>
|
||||
/// <param name="amount">How many gears to remove.</param>
|
||||
/// <returns>True if the full amount was successfully removed, false if not enough gears.</returns>
|
||||
public static bool SubtractRustyGears(IServerPlayer player, int amount)
|
||||
{
|
||||
// Check if the player has enough rusty gears
|
||||
int currentAmount = CountRustyGears(player);
|
||||
if (currentAmount < amount) return false;
|
||||
|
||||
// Subtract the specified amount of rusty gears from the player's inventory
|
||||
player.Entity.WalkInventory((slot) =>
|
||||
{
|
||||
if (slot is ItemSlotCreative || !(slot.Inventory is InventoryBasePlayer)) return true;
|
||||
|
||||
int value = CurrencyValuePerItem(slot) * slot.StackSize;
|
||||
if (value > 0 && slot.StackSize > 0)
|
||||
{
|
||||
// Calculate the amount of rusty gears to remove from this slot
|
||||
int amountToRemove = Math.Min(value, amount);
|
||||
|
||||
// If the slot size is less than or equal to the amount to remove, set the slot's itemstack to null
|
||||
if (slot.StackSize <= amountToRemove) slot.Itemstack = null;
|
||||
else
|
||||
{
|
||||
// Otherwise, subtract the amount to remove from the slot size and decrement the total amount
|
||||
slot.Itemstack.StackSize -= amountToRemove;
|
||||
amount -= amountToRemove;
|
||||
}
|
||||
}
|
||||
|
||||
// If the total amount has been removed, return true
|
||||
if (amount <= 0) return true; // we're done
|
||||
return true;
|
||||
});
|
||||
|
||||
// If the player's inventory still contains rusty gears, they don't have enough to remove
|
||||
return false;
|
||||
}
|
||||
|
||||
public static PlayerInventory GetAllItems(IServerPlayer player)
|
||||
{
|
||||
PlayerInventory inv = new PlayerInventory();
|
||||
|
||||
var invMgr = player.InventoryManager;
|
||||
|
||||
var iBackpackSlotNum = 0;
|
||||
foreach (var type in saveInvTypes)
|
||||
{
|
||||
foreach (var stack in invMgr.GetOwnInventory(type))
|
||||
{
|
||||
|
||||
if (iBackpackSlotNum >= 4)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (type == GlobalConstants.backpackInvClassName)
|
||||
{
|
||||
iBackpackSlotNum++;
|
||||
}
|
||||
if (stack.Empty) continue;
|
||||
if (stack.Inventory.ClassName == GlobalConstants.characterInvClassName)
|
||||
{
|
||||
if (stack.Itemstack.ItemAttributes?["protectionModifiers"].Exists ?? false)
|
||||
{
|
||||
inv.Items.Add(stack.Itemstack.Clone());
|
||||
}
|
||||
}
|
||||
else
|
||||
inv.Items.Add(stack.Itemstack.Clone());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return inv;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue