Created
March 18, 2019 13:02
-
-
Save Stuyk/00e1cd450929f39fba888f3ae0cc1a69 to your computer and use it in GitHub Desktop.
RAGE:MP C# Tutorials - 22 -> 23
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment