Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. [Command("inventory")]
  2. public void CMD_Inventory(Client client)
  3. {
  4. PlayerStats pStats = PlayerHelper.GetPlayerStats(client);
  5.  
  6. if (pStats == null)
  7. return;
  8.  
  9. List<Item> items = pStats.GetItems();
  10.  
  11. if (items.Count == 0)
  12. {
  13. client.SendChatMessage("You don't have any items.");
  14. return;
  15. }
  16.  
  17. foreach(Item item in items)
  18. {
  19. client.SendChatMessage($"{item.Name}: {item.Quantity}");
  20. }
  21. }
  22.  
  23. [Command("additem")]
  24. public void CMD_AddItem(Client client, string item_name, double quantity)
  25. {
  26. PlayerStats pStats = PlayerHelper.GetPlayerStats(client);
  27.  
  28. if (pStats == null)
  29. return;
  30.  
  31. Item item = new Item();
  32. item.Name = item_name;
  33. item.Quantity = quantity;
  34.  
  35.  
  36. pStats.AddItem(item);
  37.  
  38. Database.Update(pStats);
  39.  
  40. client.SendChatMessage("Added a new item.");
  41. }
  42.  
  43. [Command("useitem")]
  44. public void CMD_UseItem(Client client, string item_name)
  45. {
  46. PlayerStats pStats = PlayerHelper.GetPlayerStats(client);
  47.  
  48. if (pStats == null)
  49. return;
  50.  
  51. Item item = new Item();
  52. item.Name = item_name;
  53. item.Quantity = 1;
  54.  
  55. bool result = pStats.RemoveItem(item);
  56.  
  57. if (!result)
  58. {
  59. client.SendChatMessage("That item does not exist!");
  60. return;
  61. }
  62.  
  63. Database.Update(pStats);
  64. client.SendChatMessage($"You used {item_name}");
  65. }
  66.  
  67.  
  68.  
  69.  
  70. public void AddItem(Item item)
  71. {
  72. int count = 0;
  73. bool found_item = false;
  74. string serialized_item = "";
  75.  
  76. foreach(string json_item in items)
  77. {
  78. Item des_item = NAPI.Util.FromJson<Item>(json_item);
  79.  
  80. if (des_item.Name.ToLower() == item.Name.ToLower())
  81. {
  82. des_item.Quantity += item.Quantity;
  83. serialized_item = NAPI.Util.ToJson(des_item);
  84. found_item = true;
  85. break;
  86. }
  87.  
  88. count++;
  89. }
  90.  
  91. if (found_item)
  92. {
  93. items[count] = serialized_item;
  94. return;
  95. }
  96.  
  97. items.Add(NAPI.Util.ToJson(item));
  98. }
  99.  
  100. public bool RemoveItem(Item item)
  101. {
  102. int count = 0;
  103. Tuple<bool, bool> found_and_quantity = new Tuple<bool, bool>(false, false);
  104. string serialized_item = "";
  105.  
  106. foreach (string json_item in items)
  107. {
  108. Item des_item = NAPI.Util.FromJson<Item>(json_item);
  109.  
  110. if (des_item.Name.ToLower() == item.Name.ToLower())
  111. {
  112. if (des_item.Quantity < item.Quantity)
  113. break;
  114.  
  115. des_item.Quantity -= item.Quantity;
  116. serialized_item = NAPI.Util.ToJson(des_item);
  117.  
  118. if (des_item.Quantity <= 0)
  119. {
  120. found_and_quantity = new Tuple<bool, bool>(true, false); // Found Item, Don't Re-insert
  121. } else
  122. {
  123. found_and_quantity = new Tuple<bool, bool>(true, true); // Found Item, Re-insert
  124. }
  125. break;
  126. }
  127.  
  128. count++;
  129. }
  130.  
  131. if (!found_and_quantity.Item1)
  132. return false;
  133.  
  134. if (found_and_quantity.Item2)
  135. {
  136. items[count] = serialized_item;
  137. return true;
  138. }
  139.  
  140. items.RemoveAt(count);
  141. return true;
  142. }
  143.  
  144. public List<Item> GetItems()
  145. {
  146. List<Item> des_items = new List<Item>();
  147.  
  148. foreach(string json_item in items)
  149. {
  150. des_items.Add(NAPI.Util.FromJson<Item>(json_item));
  151. }
  152.  
  153. return des_items;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement