Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [Command("inventory")]
- public void CMD_Inventory(Client client)
- {
- PlayerStats pStats = PlayerHelper.GetPlayerStats(client);
- if (pStats == null)
- return;
- List<Item> items = pStats.GetItems();
- if (items.Count == 0)
- {
- client.SendChatMessage("You don't have any items.");
- return;
- }
- foreach(Item item in items)
- {
- client.SendChatMessage($"{item.Name}: {item.Quantity}");
- }
- }
- [Command("additem")]
- public void CMD_AddItem(Client client, string item_name, double quantity)
- {
- PlayerStats pStats = PlayerHelper.GetPlayerStats(client);
- if (pStats == null)
- return;
- Item item = new Item();
- item.Name = item_name;
- item.Quantity = quantity;
- pStats.AddItem(item);
- Database.Update(pStats);
- client.SendChatMessage("Added a new item.");
- }
- [Command("useitem")]
- public void CMD_UseItem(Client client, string item_name)
- {
- PlayerStats pStats = PlayerHelper.GetPlayerStats(client);
- if (pStats == null)
- return;
- Item item = new Item();
- item.Name = item_name;
- item.Quantity = 1;
- bool result = pStats.RemoveItem(item);
- if (!result)
- {
- client.SendChatMessage("That item does not exist!");
- return;
- }
- Database.Update(pStats);
- client.SendChatMessage($"You used {item_name}");
- }
- public void AddItem(Item item)
- {
- int count = 0;
- bool found_item = false;
- string serialized_item = "";
- foreach(string json_item in items)
- {
- Item des_item = NAPI.Util.FromJson<Item>(json_item);
- if (des_item.Name.ToLower() == item.Name.ToLower())
- {
- des_item.Quantity += item.Quantity;
- serialized_item = NAPI.Util.ToJson(des_item);
- found_item = true;
- break;
- }
- count++;
- }
- if (found_item)
- {
- items[count] = serialized_item;
- return;
- }
- items.Add(NAPI.Util.ToJson(item));
- }
- public bool RemoveItem(Item item)
- {
- int count = 0;
- Tuple<bool, bool> found_and_quantity = new Tuple<bool, bool>(false, false);
- string serialized_item = "";
- foreach (string json_item in items)
- {
- Item des_item = NAPI.Util.FromJson<Item>(json_item);
- if (des_item.Name.ToLower() == item.Name.ToLower())
- {
- if (des_item.Quantity < item.Quantity)
- break;
- des_item.Quantity -= item.Quantity;
- serialized_item = NAPI.Util.ToJson(des_item);
- if (des_item.Quantity <= 0)
- {
- found_and_quantity = new Tuple<bool, bool>(true, false); // Found Item, Don't Re-insert
- } else
- {
- found_and_quantity = new Tuple<bool, bool>(true, true); // Found Item, Re-insert
- }
- break;
- }
- count++;
- }
- if (!found_and_quantity.Item1)
- return false;
- if (found_and_quantity.Item2)
- {
- items[count] = serialized_item;
- return true;
- }
- items.RemoveAt(count);
- return true;
- }
- public List<Item> GetItems()
- {
- List<Item> des_items = new List<Item>();
- foreach(string json_item in items)
- {
- des_items.Add(NAPI.Util.FromJson<Item>(json_item));
- }
- return des_items;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement