diff --git a/AriasServerUtils/Globals.cs b/AriasServerUtils/Globals.cs index 0040c91..5da42ab 100644 --- a/AriasServerUtils/Globals.cs +++ b/AriasServerUtils/Globals.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Runtime.ConstrainedExecution; +using Vintagestory.API.Common; using Vintagestory.API.Server; namespace AriasServerUtils @@ -114,10 +115,39 @@ namespace AriasServerUtils public Dictionary warps = new Dictionary(); } + /// + /// This contains per command costs, as well as helper functions that can refund a player, or check if the player has the required balance, or payment + /// + /// [Serializable] public class Costs { + public Dictionary costs = new Dictionary(); + /// + /// Checks if the player has the balance required to use the command + /// + /// The command the player is trying to use + /// The player + /// True if the player has the required balance + public bool PlayerHasBalance(string cmd, EntityPlayer player) + { + int gears = 0; + int required = 0; + if (costs.ContainsKey(cmd)) + { + // Get the cost for that command + required = costs[cmd]; + } + + // Scan the player inventory, check gears + + + + + if (gears >= required) return true; + return false; + } } diff --git a/AriasServerUtils/ModSystems/ASUServer.cs b/AriasServerUtils/ModSystems/ASUServer.cs index e640c0e..1eed677 100644 --- a/AriasServerUtils/ModSystems/ASUServer.cs +++ b/AriasServerUtils/ModSystems/ASUServer.cs @@ -32,14 +32,6 @@ namespace AriasServerUtils internal bool isFirstStart = true; - internal static string[] saveInvTypes = new string[] { - GlobalConstants.hotBarInvClassName, - GlobalConstants.backpackInvClassName, - GlobalConstants.craftingInvClassName, - GlobalConstants.mousecursorInvClassName, - GlobalConstants.characterInvClassName - }; - List SleepingPlayers { get; set; } = new(); float OriginalSpeed { get; set; } = 0.0f; public double Hours { get; private set; } = 0.0; @@ -503,42 +495,10 @@ namespace AriasServerUtils private void OnPlayerDeath(IServerPlayer player, DamageSource damageSource) { - PlayerInventory inv = new PlayerInventory(); - var invMgr = player.InventoryManager; - + PlayerInventory inv = RustyGearUtils.GetAllItems(player); NewBackCacheForPlayer(player); - 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()); - - API.Logger.Notification($"SAVED STORAGE ITEM TYPE: {stack.Itemstack}"); - - } - } - backupInventory[player.PlayerName] = inv; } diff --git a/AriasServerUtils/RustyGearUtils.cs b/AriasServerUtils/RustyGearUtils.cs new file mode 100644 index 0000000..229a7be --- /dev/null +++ b/AriasServerUtils/RustyGearUtils.cs @@ -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"; + + /// + /// Counts the total number of rusty gears in the player's inventory. + /// + 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; + } + /// + /// Attempts to subtract a specific number of rusty gears from the player's inventory. + /// + /// The player. + /// How many gears to remove. + /// True if the full amount was successfully removed, false if not enough gears. + 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; + } +} diff --git a/AriasServerUtils/modinfo.json b/AriasServerUtils/modinfo.json index 5a7b9ec..47b0a7a 100644 --- a/AriasServerUtils/modinfo.json +++ b/AriasServerUtils/modinfo.json @@ -3,8 +3,8 @@ "modid": "ariasserverutils", "name": "Aria's Server Utilities", "authors": ["zontreck"], - "description": "A collection of server utilities\n\nBuild Date: 06-05-2025 @ 12:38 PM MST", - "version": "1.1.0-dev.2", + "description": "A collection of server utilities\n\nBuild Date: 06-05-2025 @ 4:44 PM MST", + "version": "1.1.0-dev.3", "dependencies": { "game": "" }