using System; using System.Collections.Generic; using System.Text; using GTANetworkAPI; using reallife_gamemode.Server.Inventory.Interfaces; using reallife_gamemode.Server.Managers; using System.Linq; using reallife_gamemode.Server.Entities; using reallife_gamemode.Server.Extensions; using reallife_gamemode.Model; namespace reallife_gamemode.Server.Inventory { public class GroundItem : Script { public int ItemId { get; set; } public int Amount { get; set; } public Vector3 Position { get; set; } public static List GroundItems = new List(); public static List GroundObjects = new List(); public static List GroundTextLabels = new List(); public static void AddGroundItem(GroundItem grndItem, GTANetworkAPI.Object grndObject, TextLabel grndTextLabel) { GroundItems.Add(grndItem); GroundObjects.Add(grndObject); GroundTextLabels.Add(grndTextLabel); } public static void PickUpGroundItem(Client player) { GroundItem nearest = GroundItems.FirstOrDefault(d => d.Position.DistanceTo(player.Position) <= 1.2); if (nearest != null) { var invWeight = InventoryManager.GetUserInventoryWeight(player); var itemsToAdd = 0; GTANetworkAPI.Object nearestObject = GroundObjects.FirstOrDefault(d => d.Position == nearest.Position); TextLabel nearestTextLabel = GroundTextLabels.FirstOrDefault(d => d.Position == nearest.Position); IItem nearestItem = InventoryManager.GetItemById(nearest.ItemId); UserItem existingItem = InventoryManager.UserHasThisItem(player, nearest.ItemId); var user = player.GetUser(); if (nearestItem.Gewicht * nearest.Amount + invWeight > 40000) { for(var i = 1; i <= nearest.Amount; i++) { if(invWeight + (i * nearestItem.Gewicht) > 40000) { break; } else { itemsToAdd = i; } } if(itemsToAdd < 1) { player.SendNotification("~r~Du hast keinen Platz im Inventar!", false); } else { if(existingItem != null) { using (var context = new DatabaseContext()) { UserItem existingUserItem = context.UserItems.FirstOrDefault(i => i.Id == existingItem.Id); existingUserItem.Amount += itemsToAdd; context.SaveChanges(); } } else { UserItem newItem = new UserItem { ItemId = nearest.ItemId, UserId = user.Id, Amount = nearest.Amount }; InventoryManager.AddItemToInventory(player, newItem); } nearestTextLabel.Text = nearestItem.Name + " ~s~(~y~" + (nearest.Amount - itemsToAdd) + "~s~)"; player.SendNotification("Du hast nur ~g~" + itemsToAdd + " ~y~" + nearestItem.Name + " aufgehoben."); } } else { if (existingItem != null) { using (var context = new DatabaseContext()) { UserItem existingUserItem = context.UserItems.FirstOrDefault(i => i.Id == existingItem.Id && i.UserId == user.Id); existingUserItem.Amount += nearest.Amount; context.SaveChanges(); } } else { UserItem item = new UserItem() { ItemId = nearest.ItemId, UserId = user.Id, Amount = nearest.Amount }; InventoryManager.AddItemToInventory(player, item); } RemoveGroundItem(nearest, nearestObject, nearestTextLabel); player.SendNotification("Du hast ~g~" + nearest.Amount + " ~y~" + nearestItem.Name + " aufgehoben."); } } } public static void RemoveGroundItem(GroundItem grndItem, GTANetworkAPI.Object grndObject, TextLabel grndTextLabel) { GroundItems.Remove(grndItem); NAPI.Entity.DeleteEntity(grndObject); NAPI.Entity.DeleteEntity(grndTextLabel); GroundObjects.Remove(grndObject); GroundTextLabels.Remove(grndTextLabel); } } }