Files
reallife-gamemode/ReallifeGamemode.Server/Inventory/GroundItem.cs
2021-04-05 14:25:47 +02:00

125 lines
4.9 KiB
C#

using System.Collections.Generic;
using System.Linq;
using GTANetworkAPI;
using ReallifeGamemode.Database.Entities;
using ReallifeGamemode.Database.Models;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Inventory.Interfaces;
using ReallifeGamemode.Server.Managers;
namespace ReallifeGamemode.Server.Inventory
{
public class GroundItem : Script
{
public int ItemId { get; set; }
public int Amount { get; set; }
public Vector3 Position { get; set; }
public static List<GroundItem> GroundItems = new List<GroundItem>();
public static List<GTANetworkAPI.Object> GroundObjects = new List<GTANetworkAPI.Object>();
public static List<TextLabel> GroundTextLabels = new List<TextLabel>();
public static void AddGroundItem(GroundItem grndItem, GTANetworkAPI.Object grndObject, TextLabel grndTextLabel)
{
GroundItems.Add(grndItem);
GroundObjects.Add(grndObject);
GroundTextLabels.Add(grndTextLabel);
}
public static bool PickUpGroundItem(Player 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 == new Vector3(nearest.Position.X, nearest.Position.Y, nearest.Position.Z + 1.05) || d.Position == new Vector3(nearest.Position.X, nearest.Position.Y, nearest.Position.Z + 0.8) || d.Position == nearest.Position);
IItem nearestItem = InventoryManager.GetItemById(nearest.ItemId);
UserItem existingItem = InventoryManager.UserHasThisItem(player, nearest.ItemId);
var user = player.GetUser();
if (player.HasAttachment("ammobox"))
{ player.SendNotification("~r~Du kannst momentan nichts tragen!", false); return; }
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.ItemId, newItem.Amount);
}
nearest.Amount -= itemsToAdd;
nearestTextLabel.Text = nearestItem.Name + " ~s~(~y~" + nearest.Amount + "~s~)";
player.SendNotification("Du hast nur ~g~" + itemsToAdd + " ~y~" + nearestItem.Name + "~s~ 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.ItemId, item.Amount);
}
RemoveGroundItem(nearest, nearestObject, nearestTextLabel);
player.SendNotification("Du hast ~g~" + nearest.Amount + " ~y~" + nearestItem.Name + " ~s~aufgehoben.");
}
if (nearestItem is IWeaponDealItem obj)
{
if (!player.HasAttachment("ammobox"))
{
player.PlayAnimation("anim@heists@box_carry@", "idle", 49);
player.AddAttachment("ammobox", false);
NAPI.Player.SetPlayerCurrentWeapon(player, WeaponHash.Unarmed);
}
}
return true;
}
return false;
}
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);
}
}
}