Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Access to Source of Dropped/Spawned Items for Server Plugins #2390

Merged
27 changes: 24 additions & 3 deletions Intersect.Server.Core/Entities/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Intersect.Server.Database.PlayerData.Players;
using Intersect.Server.Entities.Combat;
using Intersect.Server.Entities.Events;
using Intersect.Server.Framework;
using Intersect.Server.Framework.Entities;
using Intersect.Server.General;
using Intersect.Server.Localization;
using Intersect.Server.Maps;
Expand All @@ -21,12 +23,12 @@

namespace Intersect.Server.Entities;

public abstract partial class Entity : IDisposable
public abstract partial class Entity : IEntity
{
//Instance Values
private Guid _id = Guid.NewGuid();

public Guid MapInstanceId = Guid.Empty;
[NotMapped, JsonIgnore] public Guid MapInstanceId { get; set; } = Guid.Empty;
blinkuz marked this conversation as resolved.
Show resolved Hide resolved

[JsonProperty("MaxVitals"), NotMapped] private long[] _maxVital = new long[Enum.GetValues<Vital>().Length];

Expand Down Expand Up @@ -3056,7 +3058,26 @@ protected virtual void DropItems(Entity killer, bool sendUpdate = true)
// Spawn the actual item!
if (MapController.TryGetInstanceFromMap(MapId, MapInstanceId, out var instance))
{
instance.SpawnItem(X, Y, drop, drop.Quantity, lootOwner, sendUpdate);
var itemSource = new EntityItemSource
{
EntityType = this.GetEntityType(),
EntityReference = new WeakReference<IEntity>(this)
};

if (this is Player player)
{
itemSource.Id = player.Id;
}
else if (this is Npc npc)
{
itemSource.Id = npc.Base.Id;
}
else if (this is Resource resource)
{
itemSource.Id = resource.Base.Id;
}

instance.SpawnItem(itemSource, X, Y, drop, drop.Quantity, lootOwner, sendUpdate);
blinkuz marked this conversation as resolved.
Show resolved Hide resolved
}

// Process the drop (for players this would remove it from their inventory)
Expand Down
28 changes: 24 additions & 4 deletions Intersect.Server.Core/Entities/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
using Intersect.Server.Database.PlayerData.Security;
using Intersect.Server.Entities.Combat;
using Intersect.Server.Entities.Events;
using Intersect.Server.Framework;
using Intersect.Server.Framework.Entities;
using Intersect.Server.Localization;
using Intersect.Server.Maps;
using Intersect.Server.Networking;
Expand All @@ -35,7 +37,6 @@ public partial class Player : Entity
{
[NotMapped, JsonIgnore]
public Guid PreviousMapInstanceId = Guid.Empty;

//Online Players List
private static readonly ConcurrentDictionary<Guid, Player> OnlinePlayers = new ConcurrentDictionary<Guid, Player>();

Expand Down Expand Up @@ -2781,7 +2782,13 @@ public bool TryGiveItem(Item item, ItemHandling handler = ItemHandling.Normal, b
// Do we have any items to spawn to the map?
if (spawnAmount > 0 && MapController.TryGetInstanceFromMap(Map.Id, MapInstanceId, out var instance))
{
instance.SpawnItem(overflowTileX > -1 ? overflowTileX : X, overflowTileY > -1 ? overflowTileY : Y, item, spawnAmount, Id);
var itemSource = new EntityItemSource
{
EntityType = this.GetEntityType(),
EntityReference = new WeakReference<IEntity>(this),
Id = this.Id,
};
instance.SpawnItem(itemSource, overflowTileX > -1 ? overflowTileX : X, overflowTileY > -1 ? overflowTileY : Y, item, spawnAmount, Id);
return spawnAmount != item.Quantity;
}

Expand Down Expand Up @@ -3134,8 +3141,15 @@ public bool TryDropItemFrom(int slotIndex, int amount)
);
return false;
}

var itemSource = new EntityItemSource
{
EntityType = this.GetEntityType(),
EntityReference = new WeakReference<IEntity>(this),
Id = this.Id,
};

mapInstance.SpawnItem(X, Y, itemInSlot, itemDescriptor.IsStackable ? amount : 1, Id);
mapInstance.SpawnItem(itemSource,X, Y, itemInSlot, itemDescriptor.IsStackable ? amount : 1, Id);

itemInSlot.Quantity = Math.Max(0, itemInSlot.Quantity - amount);

Expand Down Expand Up @@ -4972,6 +4986,12 @@ public void ReturnTradeItems()
{
return;
}
var itemSource = new EntityItemSource
{
EntityType = this.GetEntityType(),
EntityReference = new WeakReference<IEntity>(this),
Id = this.Id,
};

foreach (var offer in Trading.Offer)
{
Expand All @@ -4982,7 +5002,7 @@ public void ReturnTradeItems()

if (!TryGiveItem(offer, -1) && MapController.TryGetInstanceFromMap(MapId, MapInstanceId, out var instance))
{
instance.SpawnItem(X, Y, offer, offer.Quantity, Id);
instance.SpawnItem(itemSource, X, Y, offer, offer.Quantity, Id);
PacketSender.SendChatMsg(this, Strings.Trading.ItemsDropped, ChatMessageType.Inventory, CustomColors.Alerts.Error);
}

Expand Down
11 changes: 10 additions & 1 deletion Intersect.Server.Core/Entities/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Intersect.Network.Packets.Server;
using Intersect.Server.Database;
using Intersect.Server.Database.PlayerData.Players;
using Intersect.Server.Framework;
using Intersect.Server.Framework.Entities;
using Intersect.Server.Maps;
using Intersect.Server.Networking;
using Intersect.Utilities;
Expand Down Expand Up @@ -156,6 +158,13 @@ public void SpawnResourceItems(Entity killer)
{
selectedTile = tiles[Randomization.Next(0, tiles.Count)];
}

var itemSource = new EntityItemSource
{
EntityType = this.GetEntityType(),
EntityReference = new WeakReference<IEntity>(this),
Id = this.Base.Id,
};

// Drop items
foreach (var item in Items)
Expand All @@ -165,7 +174,7 @@ public void SpawnResourceItems(Entity killer)
var mapId = selectedTile.GetMapId();
if (MapController.TryGetInstanceFromMap(mapId, MapInstanceId, out var mapInstance))
{
mapInstance.SpawnItem(selectedTile.GetX(), selectedTile.GetY(), item, item.Quantity, killer.Id);
mapInstance.SpawnItem(itemSource, selectedTile.GetX(), selectedTile.GetY(), item, item.Quantity, killer.Id);
}
}
}
Expand Down
19 changes: 13 additions & 6 deletions Intersect.Server.Core/Maps/MapInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Intersect.Server.Classes.Maps;
using MapAttribute = Intersect.Enums.MapAttribute;
using Intersect.Server.Core.MapInstancing;
using Intersect.Server.Framework;

namespace Intersect.Server.Maps;

Expand Down Expand Up @@ -703,7 +704,8 @@ private void DespawnResources()
/// <param name="x">The X location of this item.</param>
/// <param name="y">The Y location of this item.</param>
/// <param name="item">The <see cref="MapItem"/> to add to the map.</param>
private void AddItem(MapItem item)
/// <param name="source">The source of the item.</param>
blinkuz marked this conversation as resolved.
Show resolved Hide resolved
private void AddItem(IItemSource? source, MapItem item)
{
AllMapItems.TryAdd(item.UniqueId, item);

Expand All @@ -713,26 +715,31 @@ private void AddItem(MapItem item)
}

TileItems[item.TileIndex]?.TryAdd(item.UniqueId, item);

//TODO: Invoke the event for the item being added to the map, maybe:
// _mapHelper?.ItemAdded?.Invoke(this, new ItemEventArgs(source, item));
}

/// <summary>
/// Spawn an item to this map instance.
/// </summary>
/// <param name="source">The source of the item, which can be an entity or player</param>
blinkuz marked this conversation as resolved.
Show resolved Hide resolved
/// <param name="x">The horizontal location of this item</param>
/// <param name="y">The vertical location of this item.</param>
/// <param name="item">The <see cref="Item"/> to spawn on the map.</param>
/// <param name="amount">The amount of times to spawn this item to the map. Set to the <see cref="Item"/> quantity, overwrites quantity if stackable!</param>
public void SpawnItem(int x, int y, Item item, int amount) => SpawnItem(x, y, item, amount, Guid.Empty);
public void SpawnItem(IItemSource? source,int x, int y, Item item, int amount) => SpawnItem(source, x, y, item, amount, Guid.Empty);
blinkuz marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Spawn an item to this map instance.
/// </summary>
/// <param name="source">The source of the item, which can be an entity or player</param>
blinkuz marked this conversation as resolved.
Show resolved Hide resolved
/// <param name="x">The horizontal location of this item</param>
/// <param name="y">The vertical location of this item.</param>
/// <param name="item">The <see cref="Item"/> to spawn on the map.</param>
/// <param name="amount">The amount of times to spawn this item to the map. Set to the <see cref="Item"/> quantity, overwrites quantity if stackable!</param>
/// <param name="owner">The player Id that will be the temporary owner of this item.</param>
public void SpawnItem(int x, int y, Item item, int amount, Guid owner, bool sendUpdate = true)
public void SpawnItem(IItemSource? source ,int x, int y, Item item, int amount, Guid owner, bool sendUpdate = true)
blinkuz marked this conversation as resolved.
Show resolved Hide resolved
{
if (item == null)
{
Expand Down Expand Up @@ -792,7 +799,7 @@ public void SpawnItem(int x, int y, Item item, int amount, Guid owner, bool send
}

// Drop the new item.
AddItem(mapItem);
AddItem(source, mapItem);
if (sendUpdate)
{
PacketSender.SendMapItemUpdate(mMapController.Id, MapInstanceId, mapItem, false);
Expand Down Expand Up @@ -822,7 +829,7 @@ public void SpawnItem(int x, int y, Item item, int amount, Guid owner, bool send
return;
}

AddItem(mapItem);
AddItem(source, mapItem);
}
PacketSender.SendMapItemsToProximity(mMapController.Id, this);
}
Expand Down Expand Up @@ -924,7 +931,7 @@ private void SpawnAttributeItem(int x, int y)
{
mapItem.Quantity = 1;
}
AddItem(mapItem);
AddItem(null, mapItem);
blinkuz marked this conversation as resolved.
Show resolved Hide resolved
PacketSender.SendMapItemUpdate(mMapController.Id, MapInstanceId, mapItem, false);
}
}
Expand Down
13 changes: 13 additions & 0 deletions Intersect.Server.Framework/Entities/IEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Intersect.Enums;

namespace Intersect.Server.Framework.Entities;

public interface IEntity: IDisposable
{
Guid Id { get; }
string Name { get; }
Guid MapInstanceId { get; set; }
blinkuz marked this conversation as resolved.
Show resolved Hide resolved
int X { get; }
int Y { get; }
int Z { get; }
}
16 changes: 16 additions & 0 deletions Intersect.Server.Framework/Items/EntityItemSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Intersect.Enums;
using Intersect.Server.Framework.Entities;

namespace Intersect.Server.Framework;

public class EntityItemSource: IItemSource
{
public Guid Id { get; set; }
public EntityType EntityType { get; set; }
public WeakReference<IEntity> EntityReference { get; set; }
blinkuz marked this conversation as resolved.
Show resolved Hide resolved
}

public class EntityItemSource<TEntity> : EntityItemSource where TEntity : class, IEntity
blinkuz marked this conversation as resolved.
Show resolved Hide resolved
{
public new WeakReference<TEntity> EntityReference { get; set; }
blinkuz marked this conversation as resolved.
Show resolved Hide resolved
}
11 changes: 11 additions & 0 deletions Intersect.Server.Framework/Items/IItemSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Intersect.Enums;
using Intersect.Server.Framework.Entities;

namespace Intersect.Server.Framework;

public interface IItemSource
{
Guid Id { get; }
EntityType EntityType { get; }
blinkuz marked this conversation as resolved.
Show resolved Hide resolved
WeakReference<IEntity> EntityReference { get; }
}
Loading