From fe6bff9fa3b40638cbd59f94b12cdf494706c50c Mon Sep 17 00:00:00 2001 From: Bryan Date: Sun, 20 Oct 2024 23:27:23 -0400 Subject: [PATCH 1/9] chore: :recycle: WIP Add item source to spawn item logic --- Intersect.Server.Core/Entities/Entity.cs | 27 ++++++++++++++++-- Intersect.Server.Core/Entities/Player.cs | 28 ++++++++++++++++--- Intersect.Server.Core/Entities/Resource.cs | 11 +++++++- Intersect.Server.Core/Maps/MapInstance.cs | 19 +++++++++---- .../Entities/IEntity.cs | 13 +++++++++ .../Items/EntityItemSource.cs | 16 +++++++++++ .../Items/IItemSource.cs | 11 ++++++++ 7 files changed, 111 insertions(+), 14 deletions(-) create mode 100644 Intersect.Server.Framework/Entities/IEntity.cs create mode 100644 Intersect.Server.Framework/Items/EntityItemSource.cs create mode 100644 Intersect.Server.Framework/Items/IItemSource.cs diff --git a/Intersect.Server.Core/Entities/Entity.cs b/Intersect.Server.Core/Entities/Entity.cs index a8a480b120..5fe3afa3be 100644 --- a/Intersect.Server.Core/Entities/Entity.cs +++ b/Intersect.Server.Core/Entities/Entity.cs @@ -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; @@ -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; [JsonProperty("MaxVitals"), NotMapped] private long[] _maxVital = new long[Enum.GetValues().Length]; @@ -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(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); } // Process the drop (for players this would remove it from their inventory) diff --git a/Intersect.Server.Core/Entities/Player.cs b/Intersect.Server.Core/Entities/Player.cs index b23d674e02..273888f335 100644 --- a/Intersect.Server.Core/Entities/Player.cs +++ b/Intersect.Server.Core/Entities/Player.cs @@ -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; @@ -35,7 +37,6 @@ public partial class Player : Entity { [NotMapped, JsonIgnore] public Guid PreviousMapInstanceId = Guid.Empty; - //Online Players List private static readonly ConcurrentDictionary OnlinePlayers = new ConcurrentDictionary(); @@ -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(this), + Id = this.Id, + }; + instance.SpawnItem(itemSource, overflowTileX > -1 ? overflowTileX : X, overflowTileY > -1 ? overflowTileY : Y, item, spawnAmount, Id); return spawnAmount != item.Quantity; } @@ -3134,8 +3141,15 @@ public bool TryDropItemFrom(int slotIndex, int amount) ); return false; } + + var itemSource = new EntityItemSource + { + EntityType = this.GetEntityType(), + EntityReference = new WeakReference(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); @@ -4972,6 +4986,12 @@ public void ReturnTradeItems() { return; } + var itemSource = new EntityItemSource + { + EntityType = this.GetEntityType(), + EntityReference = new WeakReference(this), + Id = this.Id, + }; foreach (var offer in Trading.Offer) { @@ -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); } diff --git a/Intersect.Server.Core/Entities/Resource.cs b/Intersect.Server.Core/Entities/Resource.cs index 12fc1bdf26..a53f9d593a 100644 --- a/Intersect.Server.Core/Entities/Resource.cs +++ b/Intersect.Server.Core/Entities/Resource.cs @@ -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; @@ -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(this), + Id = this.Base.Id, + }; // Drop items foreach (var item in Items) @@ -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); } } } diff --git a/Intersect.Server.Core/Maps/MapInstance.cs b/Intersect.Server.Core/Maps/MapInstance.cs index 546bf18900..33a26ea918 100644 --- a/Intersect.Server.Core/Maps/MapInstance.cs +++ b/Intersect.Server.Core/Maps/MapInstance.cs @@ -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; @@ -703,7 +704,8 @@ private void DespawnResources() /// The X location of this item. /// The Y location of this item. /// The to add to the map. - private void AddItem(MapItem item) + /// The source of the item. + private void AddItem(IItemSource? source, MapItem item) { AllMapItems.TryAdd(item.UniqueId, item); @@ -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)); } /// /// Spawn an item to this map instance. /// + /// The source of the item, which can be an entity or player /// The horizontal location of this item /// The vertical location of this item. /// The to spawn on the map. /// The amount of times to spawn this item to the map. Set to the quantity, overwrites quantity if stackable! - 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); /// /// Spawn an item to this map instance. /// + /// The source of the item, which can be an entity or player /// The horizontal location of this item /// The vertical location of this item. /// The to spawn on the map. /// The amount of times to spawn this item to the map. Set to the quantity, overwrites quantity if stackable! /// The player Id that will be the temporary owner of this item. - 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) { if (item == null) { @@ -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); @@ -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); } @@ -924,7 +931,7 @@ private void SpawnAttributeItem(int x, int y) { mapItem.Quantity = 1; } - AddItem(mapItem); + AddItem(null, mapItem); PacketSender.SendMapItemUpdate(mMapController.Id, MapInstanceId, mapItem, false); } } diff --git a/Intersect.Server.Framework/Entities/IEntity.cs b/Intersect.Server.Framework/Entities/IEntity.cs new file mode 100644 index 0000000000..1bc5cff964 --- /dev/null +++ b/Intersect.Server.Framework/Entities/IEntity.cs @@ -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; } + int X { get; } + int Y { get; } + int Z { get; } +} \ No newline at end of file diff --git a/Intersect.Server.Framework/Items/EntityItemSource.cs b/Intersect.Server.Framework/Items/EntityItemSource.cs new file mode 100644 index 0000000000..7df8528b66 --- /dev/null +++ b/Intersect.Server.Framework/Items/EntityItemSource.cs @@ -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 EntityReference { get; set; } +} + +public class EntityItemSource : EntityItemSource where TEntity : class, IEntity +{ + public new WeakReference EntityReference { get; set; } +} \ No newline at end of file diff --git a/Intersect.Server.Framework/Items/IItemSource.cs b/Intersect.Server.Framework/Items/IItemSource.cs new file mode 100644 index 0000000000..91cae06d05 --- /dev/null +++ b/Intersect.Server.Framework/Items/IItemSource.cs @@ -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; } + WeakReference EntityReference { get; } +} \ No newline at end of file From c2dae40511aa912c15f79fc94cbd000bf6a26b7e Mon Sep 17 00:00:00 2001 From: Blinkuz Date: Thu, 24 Oct 2024 01:03:51 -0400 Subject: [PATCH 2/9] fix: :bug: Add abstract method for create item source in entities --- Intersect.Server.Core/Entities/Entity.cs | 26 +++--------- .../Entities/Events/EventPageInstance.cs | 6 +++ Intersect.Server.Core/Entities/Npc.cs | 12 ++++++ Intersect.Server.Core/Entities/Player.cs | 42 +++++++++---------- Intersect.Server.Core/Entities/Projectile.cs | 6 +++ Intersect.Server.Core/Entities/Resource.cs | 17 +++++--- .../Entities/IEntity.cs | 6 --- 7 files changed, 59 insertions(+), 56 deletions(-) diff --git a/Intersect.Server.Core/Entities/Entity.cs b/Intersect.Server.Core/Entities/Entity.cs index 5fe3afa3be..4866ae471c 100644 --- a/Intersect.Server.Core/Entities/Entity.cs +++ b/Intersect.Server.Core/Entities/Entity.cs @@ -27,8 +27,8 @@ public abstract partial class Entity : IEntity { //Instance Values private Guid _id = Guid.NewGuid(); - - [NotMapped, JsonIgnore] public Guid MapInstanceId { get; set; } = Guid.Empty; + + public Guid MapInstanceId = Guid.Empty; [JsonProperty("MaxVitals"), NotMapped] private long[] _maxVital = new long[Enum.GetValues().Length]; @@ -3058,25 +3058,7 @@ protected virtual void DropItems(Entity killer, bool sendUpdate = true) // Spawn the actual item! if (MapController.TryGetInstanceFromMap(MapId, MapInstanceId, out var instance)) { - var itemSource = new EntityItemSource - { - EntityType = this.GetEntityType(), - EntityReference = new WeakReference(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; - } - + var itemSource = this.CreateItemSource(); instance.SpawnItem(itemSource, X, Y, drop, drop.Quantity, lootOwner, sendUpdate); } @@ -3085,6 +3067,8 @@ protected virtual void DropItems(Entity killer, bool sendUpdate = true) } } + protected abstract EntityItemSource CreateItemSource(); + public bool IsDead() { return Dead; diff --git a/Intersect.Server.Core/Entities/Events/EventPageInstance.cs b/Intersect.Server.Core/Entities/Events/EventPageInstance.cs index 9ac20b12db..5cce82001d 100644 --- a/Intersect.Server.Core/Entities/Events/EventPageInstance.cs +++ b/Intersect.Server.Core/Entities/Events/EventPageInstance.cs @@ -3,6 +3,7 @@ using Intersect.GameObjects.Events; using Intersect.Network.Packets.Server; using Intersect.Server.Entities.Pathfinding; +using Intersect.Server.Framework; using Intersect.Server.Maps; using Intersect.Server.Networking; using Intersect.Utilities; @@ -892,5 +893,10 @@ public bool ShouldDespawn(MapController map) return false; } + + protected override EntityItemSource CreateItemSource() + { + return null; + } } diff --git a/Intersect.Server.Core/Entities/Npc.cs b/Intersect.Server.Core/Entities/Npc.cs index 61b27c3525..1cdbb19805 100644 --- a/Intersect.Server.Core/Entities/Npc.cs +++ b/Intersect.Server.Core/Entities/Npc.cs @@ -8,6 +8,8 @@ using Intersect.Server.Entities.Combat; using Intersect.Server.Entities.Events; using Intersect.Server.Entities.Pathfinding; +using Intersect.Server.Framework; +using Intersect.Server.Framework.Entities; using Intersect.Server.Maps; using Intersect.Server.Networking; using Intersect.Utilities; @@ -1671,5 +1673,15 @@ public override EntityPacket EntityPacket(EntityPacket packet = null, Player for return pkt; } + + protected override EntityItemSource CreateItemSource() + { + return new EntityItemSource + { + EntityType = this.GetEntityType(), + EntityReference = new WeakReference(this), + Id = this.Base.Id + }; + } } diff --git a/Intersect.Server.Core/Entities/Player.cs b/Intersect.Server.Core/Entities/Player.cs index 273888f335..7083550f16 100644 --- a/Intersect.Server.Core/Entities/Player.cs +++ b/Intersect.Server.Core/Entities/Player.cs @@ -2782,13 +2782,7 @@ 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)) { - var itemSource = new EntityItemSource - { - EntityType = this.GetEntityType(), - EntityReference = new WeakReference(this), - Id = this.Id, - }; - instance.SpawnItem(itemSource, overflowTileX > -1 ? overflowTileX : X, overflowTileY > -1 ? overflowTileY : Y, item, spawnAmount, Id); + instance.SpawnItem(CreateItemSource(), overflowTileX > -1 ? overflowTileX : X, overflowTileY > -1 ? overflowTileY : Y, item, spawnAmount, Id); return spawnAmount != item.Quantity; } @@ -2827,8 +2821,21 @@ public bool TryGiveItem(Item item, ItemHandling handler = ItemHandling.Normal, b var bankInterface = new BankInterface(this, ((IEnumerable)Bank).ToList(), new object(), null, Options.Instance.Bank.MaxSlots); return bankOverflow && bankInterface.TryDepositItem(item, sendUpdate); } - - + + /// + /// Creates an item source for the player entity. + /// + /// A new object. + protected override EntityItemSource CreateItemSource() + { + return new EntityItemSource + { + EntityType = this.GetEntityType(), + EntityReference = new WeakReference(this), + Id = this.Id + }; + } + /// /// Gives the player an item. NOTE: This method MAKES ZERO CHECKS to see if this is possible! /// Use TryGiveItem where possible! @@ -3141,15 +3148,8 @@ public bool TryDropItemFrom(int slotIndex, int amount) ); return false; } - - var itemSource = new EntityItemSource - { - EntityType = this.GetEntityType(), - EntityReference = new WeakReference(this), - Id = this.Id, - }; - mapInstance.SpawnItem(itemSource,X, Y, itemInSlot, itemDescriptor.IsStackable ? amount : 1, Id); + mapInstance.SpawnItem(CreateItemSource(),X, Y, itemInSlot, itemDescriptor.IsStackable ? amount : 1, Id); itemInSlot.Quantity = Math.Max(0, itemInSlot.Quantity - amount); @@ -4986,12 +4986,8 @@ public void ReturnTradeItems() { return; } - var itemSource = new EntityItemSource - { - EntityType = this.GetEntityType(), - EntityReference = new WeakReference(this), - Id = this.Id, - }; + + var itemSource = CreateItemSource(); foreach (var offer in Trading.Offer) { diff --git a/Intersect.Server.Core/Entities/Projectile.cs b/Intersect.Server.Core/Entities/Projectile.cs index c7be99c6ee..b7563320dd 100644 --- a/Intersect.Server.Core/Entities/Projectile.cs +++ b/Intersect.Server.Core/Entities/Projectile.cs @@ -4,6 +4,7 @@ using Intersect.Network.Packets.Server; using Intersect.Server.Database; using Intersect.Server.Entities.Combat; +using Intersect.Server.Framework; using Intersect.Server.Maps; using Intersect.Utilities; using MapAttribute = Intersect.Enums.MapAttribute; @@ -654,5 +655,10 @@ public override EntityType GetEntityType() { return EntityType.Projectile; } + + protected override EntityItemSource CreateItemSource() + { + return null; + } } diff --git a/Intersect.Server.Core/Entities/Resource.cs b/Intersect.Server.Core/Entities/Resource.cs index a53f9d593a..f448c81f0f 100644 --- a/Intersect.Server.Core/Entities/Resource.cs +++ b/Intersect.Server.Core/Entities/Resource.cs @@ -159,12 +159,7 @@ public void SpawnResourceItems(Entity killer) selectedTile = tiles[Randomization.Next(0, tiles.Count)]; } - var itemSource = new EntityItemSource - { - EntityType = this.GetEntityType(), - EntityReference = new WeakReference(this), - Id = this.Base.Id, - }; + var itemSource = CreateItemSource(); // Drop items foreach (var item in Items) @@ -182,6 +177,16 @@ public void SpawnResourceItems(Entity killer) Items.Clear(); } + + protected override EntityItemSource CreateItemSource() + { + return new EntityItemSource + { + EntityType = this.GetEntityType(), + EntityReference = new WeakReference(this), + Id = this.Base.Id + }; + } public override void ProcessRegen() { diff --git a/Intersect.Server.Framework/Entities/IEntity.cs b/Intersect.Server.Framework/Entities/IEntity.cs index 1bc5cff964..587b7d6e55 100644 --- a/Intersect.Server.Framework/Entities/IEntity.cs +++ b/Intersect.Server.Framework/Entities/IEntity.cs @@ -1,13 +1,7 @@ -using Intersect.Enums; - namespace Intersect.Server.Framework.Entities; public interface IEntity: IDisposable { Guid Id { get; } string Name { get; } - Guid MapInstanceId { get; set; } - int X { get; } - int Y { get; } - int Z { get; } } \ No newline at end of file From fdc281156074e079c8481d054f60ae827ed05c49 Mon Sep 17 00:00:00 2001 From: Blinkuz Date: Thu, 24 Oct 2024 22:15:37 -0400 Subject: [PATCH 3/9] fix: :bug: Change properties and logic in interfaces --- Intersect.Server.Core/Entities/Entity.cs | 2 ++ .../Entities/Events/EventPageInstance.cs | 1 + Intersect.Server.Core/Entities/Npc.cs | 2 +- Intersect.Server.Core/Entities/Player.cs | 2 +- Intersect.Server.Core/Entities/Projectile.cs | 2 +- Intersect.Server.Core/Entities/Resource.cs | 2 +- Intersect.Server.Core/Maps/MapInstance.cs | 2 +- .../Entities/IEntity.cs | 1 + .../Items/EntityItemSource.cs | 13 ++++-------- .../Items/EntityItemSource`1.cs | 21 +++++++++++++++++++ .../Items/IItemSource.cs | 2 +- 11 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 Intersect.Server.Framework/Items/EntityItemSource`1.cs diff --git a/Intersect.Server.Core/Entities/Entity.cs b/Intersect.Server.Core/Entities/Entity.cs index 4866ae471c..d61687c9dd 100644 --- a/Intersect.Server.Core/Entities/Entity.cs +++ b/Intersect.Server.Core/Entities/Entity.cs @@ -12,6 +12,7 @@ using Intersect.Server.Entities.Events; using Intersect.Server.Framework; using Intersect.Server.Framework.Entities; +using Intersect.Server.Framework.Items; using Intersect.Server.General; using Intersect.Server.Localization; using Intersect.Server.Maps; @@ -29,6 +30,7 @@ public abstract partial class Entity : IEntity private Guid _id = Guid.NewGuid(); public Guid MapInstanceId = Guid.Empty; + Guid IEntity.MapInstanceId => MapInstanceId; [JsonProperty("MaxVitals"), NotMapped] private long[] _maxVital = new long[Enum.GetValues().Length]; diff --git a/Intersect.Server.Core/Entities/Events/EventPageInstance.cs b/Intersect.Server.Core/Entities/Events/EventPageInstance.cs index 5cce82001d..442b6d0626 100644 --- a/Intersect.Server.Core/Entities/Events/EventPageInstance.cs +++ b/Intersect.Server.Core/Entities/Events/EventPageInstance.cs @@ -4,6 +4,7 @@ using Intersect.Network.Packets.Server; using Intersect.Server.Entities.Pathfinding; using Intersect.Server.Framework; +using Intersect.Server.Framework.Items; using Intersect.Server.Maps; using Intersect.Server.Networking; using Intersect.Utilities; diff --git a/Intersect.Server.Core/Entities/Npc.cs b/Intersect.Server.Core/Entities/Npc.cs index 1cdbb19805..24292f24c3 100644 --- a/Intersect.Server.Core/Entities/Npc.cs +++ b/Intersect.Server.Core/Entities/Npc.cs @@ -8,8 +8,8 @@ using Intersect.Server.Entities.Combat; using Intersect.Server.Entities.Events; using Intersect.Server.Entities.Pathfinding; -using Intersect.Server.Framework; using Intersect.Server.Framework.Entities; +using Intersect.Server.Framework.Items; using Intersect.Server.Maps; using Intersect.Server.Networking; using Intersect.Utilities; diff --git a/Intersect.Server.Core/Entities/Player.cs b/Intersect.Server.Core/Entities/Player.cs index 7083550f16..322e1e2292 100644 --- a/Intersect.Server.Core/Entities/Player.cs +++ b/Intersect.Server.Core/Entities/Player.cs @@ -19,8 +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.Framework.Items; using Intersect.Server.Localization; using Intersect.Server.Maps; using Intersect.Server.Networking; diff --git a/Intersect.Server.Core/Entities/Projectile.cs b/Intersect.Server.Core/Entities/Projectile.cs index b7563320dd..676bdd0cd3 100644 --- a/Intersect.Server.Core/Entities/Projectile.cs +++ b/Intersect.Server.Core/Entities/Projectile.cs @@ -4,7 +4,7 @@ using Intersect.Network.Packets.Server; using Intersect.Server.Database; using Intersect.Server.Entities.Combat; -using Intersect.Server.Framework; +using Intersect.Server.Framework.Items; using Intersect.Server.Maps; using Intersect.Utilities; using MapAttribute = Intersect.Enums.MapAttribute; diff --git a/Intersect.Server.Core/Entities/Resource.cs b/Intersect.Server.Core/Entities/Resource.cs index f448c81f0f..08c21c0b77 100644 --- a/Intersect.Server.Core/Entities/Resource.cs +++ b/Intersect.Server.Core/Entities/Resource.cs @@ -3,8 +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.Framework.Items; using Intersect.Server.Maps; using Intersect.Server.Networking; using Intersect.Utilities; diff --git a/Intersect.Server.Core/Maps/MapInstance.cs b/Intersect.Server.Core/Maps/MapInstance.cs index 33a26ea918..9229673206 100644 --- a/Intersect.Server.Core/Maps/MapInstance.cs +++ b/Intersect.Server.Core/Maps/MapInstance.cs @@ -13,7 +13,7 @@ using Intersect.Server.Classes.Maps; using MapAttribute = Intersect.Enums.MapAttribute; using Intersect.Server.Core.MapInstancing; -using Intersect.Server.Framework; +using Intersect.Server.Framework.Items; namespace Intersect.Server.Maps; diff --git a/Intersect.Server.Framework/Entities/IEntity.cs b/Intersect.Server.Framework/Entities/IEntity.cs index 587b7d6e55..7a9f84df2c 100644 --- a/Intersect.Server.Framework/Entities/IEntity.cs +++ b/Intersect.Server.Framework/Entities/IEntity.cs @@ -4,4 +4,5 @@ public interface IEntity: IDisposable { Guid Id { get; } string Name { get; } + Guid MapInstanceId { get; } } \ No newline at end of file diff --git a/Intersect.Server.Framework/Items/EntityItemSource.cs b/Intersect.Server.Framework/Items/EntityItemSource.cs index 7df8528b66..bfaab6d066 100644 --- a/Intersect.Server.Framework/Items/EntityItemSource.cs +++ b/Intersect.Server.Framework/Items/EntityItemSource.cs @@ -1,16 +1,11 @@ using Intersect.Enums; using Intersect.Server.Framework.Entities; -namespace Intersect.Server.Framework; +namespace Intersect.Server.Framework.Items; public class EntityItemSource: IItemSource { - public Guid Id { get; set; } - public EntityType EntityType { get; set; } - public WeakReference EntityReference { get; set; } -} - -public class EntityItemSource : EntityItemSource where TEntity : class, IEntity -{ - public new WeakReference EntityReference { get; set; } + public Guid Id { get; init; } + public EntityType EntityType { get; init; } + public WeakReference EntityReference { get; init; } } \ No newline at end of file diff --git a/Intersect.Server.Framework/Items/EntityItemSource`1.cs b/Intersect.Server.Framework/Items/EntityItemSource`1.cs new file mode 100644 index 0000000000..9e5254500e --- /dev/null +++ b/Intersect.Server.Framework/Items/EntityItemSource`1.cs @@ -0,0 +1,21 @@ +using Intersect.Server.Framework.Entities; + +namespace Intersect.Server.Framework.Items; + +public class EntityItemSource : EntityItemSource where TEntity : class, IEntity +{ + private WeakReference _entityReference; + + public new WeakReference EntityReference + { + get => _entityReference; + init + { + _entityReference = value; + if (_entityReference.TryGetTarget(out TEntity target)) + { + base.EntityReference = new WeakReference(target); + } + } + } +} \ No newline at end of file diff --git a/Intersect.Server.Framework/Items/IItemSource.cs b/Intersect.Server.Framework/Items/IItemSource.cs index 91cae06d05..6c7853a6a9 100644 --- a/Intersect.Server.Framework/Items/IItemSource.cs +++ b/Intersect.Server.Framework/Items/IItemSource.cs @@ -1,7 +1,7 @@ using Intersect.Enums; using Intersect.Server.Framework.Entities; -namespace Intersect.Server.Framework; +namespace Intersect.Server.Framework.Items; public interface IItemSource { From 05a0997af6dac18b36c89d611ecf63849a75f2aa Mon Sep 17 00:00:00 2001 From: Blinkuz Date: Fri, 25 Oct 2024 00:05:49 -0400 Subject: [PATCH 4/9] feat: :sparkles: Add logic for MapItemSource class in MapInstance --- Intersect.Server.Core/Entities/Entity.cs | 9 ++++----- .../Entities/Events/EventPageInstance.cs | 2 +- Intersect.Server.Core/Entities/Npc.cs | 4 ++-- Intersect.Server.Core/Entities/Player.cs | 10 +++++----- Intersect.Server.Core/Entities/Projectile.cs | 2 +- Intersect.Server.Core/Entities/Resource.cs | 6 +++--- Intersect.Server.Core/Maps/MapInstance.cs | 17 +++++++++++++---- Intersect.Server.Framework/Entities/IEntity.cs | 2 +- .../Items/EntityItemSource.cs | 3 ++- Intersect.Server.Framework/Items/IItemSource.cs | 6 +----- .../Items/ItemSourceType.cs | 8 ++++++++ .../Items/MapItemSource.cs | 12 ++++++++++++ Intersect.Server.Framework/Maps/IMapInstance.cs | 7 +++++++ 13 files changed, 60 insertions(+), 28 deletions(-) create mode 100644 Intersect.Server.Framework/Items/ItemSourceType.cs create mode 100644 Intersect.Server.Framework/Items/MapItemSource.cs create mode 100644 Intersect.Server.Framework/Maps/IMapInstance.cs diff --git a/Intersect.Server.Core/Entities/Entity.cs b/Intersect.Server.Core/Entities/Entity.cs index d61687c9dd..3bfb7ec346 100644 --- a/Intersect.Server.Core/Entities/Entity.cs +++ b/Intersect.Server.Core/Entities/Entity.cs @@ -10,7 +10,6 @@ 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.Framework.Items; using Intersect.Server.General; @@ -29,8 +28,8 @@ public abstract partial class Entity : IEntity //Instance Values private Guid _id = Guid.NewGuid(); - public Guid MapInstanceId = Guid.Empty; - Guid IEntity.MapInstanceId => MapInstanceId; + [NotMapped] + public Guid MapInstanceId { get; set; } = Guid.Empty; [JsonProperty("MaxVitals"), NotMapped] private long[] _maxVital = new long[Enum.GetValues().Length]; @@ -3060,7 +3059,7 @@ protected virtual void DropItems(Entity killer, bool sendUpdate = true) // Spawn the actual item! if (MapController.TryGetInstanceFromMap(MapId, MapInstanceId, out var instance)) { - var itemSource = this.CreateItemSource(); + var itemSource = this.GetItemSource(); instance.SpawnItem(itemSource, X, Y, drop, drop.Quantity, lootOwner, sendUpdate); } @@ -3069,7 +3068,7 @@ protected virtual void DropItems(Entity killer, bool sendUpdate = true) } } - protected abstract EntityItemSource CreateItemSource(); + protected abstract EntityItemSource GetItemSource(); public bool IsDead() { diff --git a/Intersect.Server.Core/Entities/Events/EventPageInstance.cs b/Intersect.Server.Core/Entities/Events/EventPageInstance.cs index 442b6d0626..eb43bda19a 100644 --- a/Intersect.Server.Core/Entities/Events/EventPageInstance.cs +++ b/Intersect.Server.Core/Entities/Events/EventPageInstance.cs @@ -895,7 +895,7 @@ public bool ShouldDespawn(MapController map) return false; } - protected override EntityItemSource CreateItemSource() + protected override EntityItemSource GetItemSource() { return null; } diff --git a/Intersect.Server.Core/Entities/Npc.cs b/Intersect.Server.Core/Entities/Npc.cs index 24292f24c3..1dc2b53e99 100644 --- a/Intersect.Server.Core/Entities/Npc.cs +++ b/Intersect.Server.Core/Entities/Npc.cs @@ -1674,11 +1674,11 @@ public override EntityPacket EntityPacket(EntityPacket packet = null, Player for return pkt; } - protected override EntityItemSource CreateItemSource() + protected override EntityItemSource GetItemSource() { return new EntityItemSource { - EntityType = this.GetEntityType(), + EntityType = GetEntityType(), EntityReference = new WeakReference(this), Id = this.Base.Id }; diff --git a/Intersect.Server.Core/Entities/Player.cs b/Intersect.Server.Core/Entities/Player.cs index 322e1e2292..92e27768e1 100644 --- a/Intersect.Server.Core/Entities/Player.cs +++ b/Intersect.Server.Core/Entities/Player.cs @@ -2782,7 +2782,7 @@ 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(CreateItemSource(), overflowTileX > -1 ? overflowTileX : X, overflowTileY > -1 ? overflowTileY : Y, item, spawnAmount, Id); + instance.SpawnItem(GetItemSource(), overflowTileX > -1 ? overflowTileX : X, overflowTileY > -1 ? overflowTileY : Y, item, spawnAmount, Id); return spawnAmount != item.Quantity; } @@ -2826,11 +2826,11 @@ public bool TryGiveItem(Item item, ItemHandling handler = ItemHandling.Normal, b /// Creates an item source for the player entity. /// /// A new object. - protected override EntityItemSource CreateItemSource() + protected override EntityItemSource GetItemSource() { return new EntityItemSource { - EntityType = this.GetEntityType(), + EntityType = GetEntityType(), EntityReference = new WeakReference(this), Id = this.Id }; @@ -3149,7 +3149,7 @@ public bool TryDropItemFrom(int slotIndex, int amount) return false; } - mapInstance.SpawnItem(CreateItemSource(),X, Y, itemInSlot, itemDescriptor.IsStackable ? amount : 1, Id); + mapInstance.SpawnItem(GetItemSource(),X, Y, itemInSlot, itemDescriptor.IsStackable ? amount : 1, Id); itemInSlot.Quantity = Math.Max(0, itemInSlot.Quantity - amount); @@ -4987,7 +4987,7 @@ public void ReturnTradeItems() return; } - var itemSource = CreateItemSource(); + var itemSource = GetItemSource(); foreach (var offer in Trading.Offer) { diff --git a/Intersect.Server.Core/Entities/Projectile.cs b/Intersect.Server.Core/Entities/Projectile.cs index 676bdd0cd3..aba9f550e8 100644 --- a/Intersect.Server.Core/Entities/Projectile.cs +++ b/Intersect.Server.Core/Entities/Projectile.cs @@ -656,7 +656,7 @@ public override EntityType GetEntityType() return EntityType.Projectile; } - protected override EntityItemSource CreateItemSource() + protected override EntityItemSource GetItemSource() { return null; } diff --git a/Intersect.Server.Core/Entities/Resource.cs b/Intersect.Server.Core/Entities/Resource.cs index 08c21c0b77..5ab3d00f33 100644 --- a/Intersect.Server.Core/Entities/Resource.cs +++ b/Intersect.Server.Core/Entities/Resource.cs @@ -159,7 +159,7 @@ public void SpawnResourceItems(Entity killer) selectedTile = tiles[Randomization.Next(0, tiles.Count)]; } - var itemSource = CreateItemSource(); + var itemSource = GetItemSource(); // Drop items foreach (var item in Items) @@ -178,11 +178,11 @@ public void SpawnResourceItems(Entity killer) Items.Clear(); } - protected override EntityItemSource CreateItemSource() + protected override EntityItemSource GetItemSource() { return new EntityItemSource { - EntityType = this.GetEntityType(), + EntityType = GetEntityType(), EntityReference = new WeakReference(this), Id = this.Base.Id }; diff --git a/Intersect.Server.Core/Maps/MapInstance.cs b/Intersect.Server.Core/Maps/MapInstance.cs index 9229673206..59baacd795 100644 --- a/Intersect.Server.Core/Maps/MapInstance.cs +++ b/Intersect.Server.Core/Maps/MapInstance.cs @@ -14,6 +14,7 @@ using MapAttribute = Intersect.Enums.MapAttribute; using Intersect.Server.Core.MapInstancing; using Intersect.Server.Framework.Items; +using Intersect.Server.Framework.Maps; namespace Intersect.Server.Maps; @@ -48,7 +49,7 @@ namespace Intersect.Server.Maps; /// /// /// -public partial class MapInstance : IDisposable +public partial class MapInstance : IMapInstance { /// /// Reference to stay consistent/easy-to-read with overworld behavior @@ -79,7 +80,7 @@ public partial class MapInstance : IDisposable /// Note that this is NOT the Instance instance identifier - that is /// /// - public Guid Id; + public Guid Id { get; set; } /// /// An ID referring to which instance this processer belongs to. @@ -88,7 +89,7 @@ public partial class MapInstance : IDisposable /// will be processed and fed packets by that processer. /// /// - public Guid MapInstanceId; + public Guid MapInstanceId { get; set; } /// /// The last time the made a call to . @@ -931,7 +932,15 @@ private void SpawnAttributeItem(int x, int y) { mapItem.Quantity = 1; } - AddItem(null, mapItem); + + var mapItemSource = new MapItemSource + { + MapInstanceId = MapInstanceId, + MapInstanceReference = new WeakReference(this), + MapControllerId = mMapController.Id, + }; + + AddItem(mapItemSource, mapItem); PacketSender.SendMapItemUpdate(mMapController.Id, MapInstanceId, mapItem, false); } } diff --git a/Intersect.Server.Framework/Entities/IEntity.cs b/Intersect.Server.Framework/Entities/IEntity.cs index 7a9f84df2c..f468e2964a 100644 --- a/Intersect.Server.Framework/Entities/IEntity.cs +++ b/Intersect.Server.Framework/Entities/IEntity.cs @@ -4,5 +4,5 @@ public interface IEntity: IDisposable { Guid Id { get; } string Name { get; } - Guid MapInstanceId { get; } + Guid MapInstanceId { get; } } \ No newline at end of file diff --git a/Intersect.Server.Framework/Items/EntityItemSource.cs b/Intersect.Server.Framework/Items/EntityItemSource.cs index bfaab6d066..66bc82381e 100644 --- a/Intersect.Server.Framework/Items/EntityItemSource.cs +++ b/Intersect.Server.Framework/Items/EntityItemSource.cs @@ -3,9 +3,10 @@ namespace Intersect.Server.Framework.Items; -public class EntityItemSource: IItemSource +public partial class EntityItemSource: IItemSource { public Guid Id { get; init; } + public ItemSourceType SourceType => ItemSourceType.Entity; public EntityType EntityType { get; init; } public WeakReference EntityReference { get; init; } } \ No newline at end of file diff --git a/Intersect.Server.Framework/Items/IItemSource.cs b/Intersect.Server.Framework/Items/IItemSource.cs index 6c7853a6a9..e63415fa93 100644 --- a/Intersect.Server.Framework/Items/IItemSource.cs +++ b/Intersect.Server.Framework/Items/IItemSource.cs @@ -1,11 +1,7 @@ -using Intersect.Enums; -using Intersect.Server.Framework.Entities; - namespace Intersect.Server.Framework.Items; public interface IItemSource { Guid Id { get; } - EntityType EntityType { get; } - WeakReference EntityReference { get; } + ItemSourceType SourceType { get; } } \ No newline at end of file diff --git a/Intersect.Server.Framework/Items/ItemSourceType.cs b/Intersect.Server.Framework/Items/ItemSourceType.cs new file mode 100644 index 0000000000..caa78a4149 --- /dev/null +++ b/Intersect.Server.Framework/Items/ItemSourceType.cs @@ -0,0 +1,8 @@ +namespace Intersect.Server.Framework.Items; + +public enum ItemSourceType +{ + Unknown = 0, + Entity = 1, + Map = 2, +} \ No newline at end of file diff --git a/Intersect.Server.Framework/Items/MapItemSource.cs b/Intersect.Server.Framework/Items/MapItemSource.cs new file mode 100644 index 0000000000..870e550f3e --- /dev/null +++ b/Intersect.Server.Framework/Items/MapItemSource.cs @@ -0,0 +1,12 @@ +using Intersect.Server.Framework.Maps; + +namespace Intersect.Server.Framework.Items; + +public partial class MapItemSource: IItemSource +{ + public Guid Id { get; init; } + public ItemSourceType SourceType => ItemSourceType.Map; + public WeakReference MapInstanceReference { get; init; } + public Guid MapInstanceId { get; init; } + public Guid MapControllerId { get; init; } +} \ No newline at end of file diff --git a/Intersect.Server.Framework/Maps/IMapInstance.cs b/Intersect.Server.Framework/Maps/IMapInstance.cs new file mode 100644 index 0000000000..e45493f40d --- /dev/null +++ b/Intersect.Server.Framework/Maps/IMapInstance.cs @@ -0,0 +1,7 @@ +namespace Intersect.Server.Framework.Maps; + +public interface IMapInstance: IDisposable +{ + Guid Id { get; } + public Guid MapInstanceId { get; } +} \ No newline at end of file From 3d4d27155f0f7be908ebfefdc8c39375a4530503 Mon Sep 17 00:00:00 2001 From: Blinkuz Date: Fri, 25 Oct 2024 00:14:30 -0400 Subject: [PATCH 5/9] docs: :pencil2: Update documentation text for parameter source --- Intersect.Server.Core/Maps/MapInstance.cs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Intersect.Server.Core/Maps/MapInstance.cs b/Intersect.Server.Core/Maps/MapInstance.cs index 59baacd795..7dcf3bed75 100644 --- a/Intersect.Server.Core/Maps/MapInstance.cs +++ b/Intersect.Server.Core/Maps/MapInstance.cs @@ -702,10 +702,8 @@ private void DespawnResources() /// /// Add a map item to this map. /// - /// The X location of this item. - /// The Y location of this item. + /// The source of the item, e.g. a player who dropped it, or a monster who spawned it on death, or the map instance in which it was spawned. /// The to add to the map. - /// The source of the item. private void AddItem(IItemSource? source, MapItem item) { AllMapItems.TryAdd(item.UniqueId, item); @@ -724,23 +722,23 @@ private void AddItem(IItemSource? source, MapItem item) /// /// Spawn an item to this map instance. /// - /// The source of the item, which can be an entity or player + /// The source of the item, e.g. a player who dropped it, or a monster who spawned it on death, or the map instance in which it was spawned /// The horizontal location of this item /// The vertical location of this item. /// The to spawn on the map. /// The amount of times to spawn this item to the map. Set to the quantity, overwrites quantity if stackable! - public void SpawnItem(IItemSource? source,int x, int y, Item item, int amount) => SpawnItem(source, 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); /// /// Spawn an item to this map instance. /// - /// The source of the item, which can be an entity or player + /// The source of the item, e.g. a player who dropped it, or a monster who spawned it on death, or the map instance in which it was spawned /// The horizontal location of this item /// The vertical location of this item. /// The to spawn on the map. /// The amount of times to spawn this item to the map. Set to the quantity, overwrites quantity if stackable! /// The player Id that will be the temporary owner of this item. - public void SpawnItem(IItemSource? source ,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) { if (item == null) { From 4156cbd03374d51c23657bc6a7dd06afb18ba512 Mon Sep 17 00:00:00 2001 From: Blinkuz Date: Sat, 26 Oct 2024 20:32:02 -0400 Subject: [PATCH 6/9] fix: :recycle: Applied necessary changes from the last review --- Intersect.Server.Core/Entities/Entity.cs | 4 ++-- .../Entities/Events/EventPageInstance.cs | 2 +- Intersect.Server.Core/Entities/Npc.cs | 2 +- Intersect.Server.Core/Entities/Player.cs | 8 ++++---- Intersect.Server.Core/Entities/Projectile.cs | 2 +- Intersect.Server.Core/Entities/Resource.cs | 4 ++-- Intersect.Server.Core/Maps/MapInstance.cs | 4 ++-- Intersect.Server.Framework/Items/MapItemSource.cs | 5 ++--- 8 files changed, 15 insertions(+), 16 deletions(-) diff --git a/Intersect.Server.Core/Entities/Entity.cs b/Intersect.Server.Core/Entities/Entity.cs index 3bfb7ec346..5c08fb5bbb 100644 --- a/Intersect.Server.Core/Entities/Entity.cs +++ b/Intersect.Server.Core/Entities/Entity.cs @@ -3059,7 +3059,7 @@ protected virtual void DropItems(Entity killer, bool sendUpdate = true) // Spawn the actual item! if (MapController.TryGetInstanceFromMap(MapId, MapInstanceId, out var instance)) { - var itemSource = this.GetItemSource(); + var itemSource = this.AsItemSource(); instance.SpawnItem(itemSource, X, Y, drop, drop.Quantity, lootOwner, sendUpdate); } @@ -3068,7 +3068,7 @@ protected virtual void DropItems(Entity killer, bool sendUpdate = true) } } - protected abstract EntityItemSource GetItemSource(); + protected abstract EntityItemSource? AsItemSource(); public bool IsDead() { diff --git a/Intersect.Server.Core/Entities/Events/EventPageInstance.cs b/Intersect.Server.Core/Entities/Events/EventPageInstance.cs index eb43bda19a..163e27804b 100644 --- a/Intersect.Server.Core/Entities/Events/EventPageInstance.cs +++ b/Intersect.Server.Core/Entities/Events/EventPageInstance.cs @@ -895,7 +895,7 @@ public bool ShouldDespawn(MapController map) return false; } - protected override EntityItemSource GetItemSource() + protected override EntityItemSource? AsItemSource() { return null; } diff --git a/Intersect.Server.Core/Entities/Npc.cs b/Intersect.Server.Core/Entities/Npc.cs index 1dc2b53e99..db0125106e 100644 --- a/Intersect.Server.Core/Entities/Npc.cs +++ b/Intersect.Server.Core/Entities/Npc.cs @@ -1674,7 +1674,7 @@ public override EntityPacket EntityPacket(EntityPacket packet = null, Player for return pkt; } - protected override EntityItemSource GetItemSource() + protected override EntityItemSource? AsItemSource() { return new EntityItemSource { diff --git a/Intersect.Server.Core/Entities/Player.cs b/Intersect.Server.Core/Entities/Player.cs index 92e27768e1..1c1a83ba30 100644 --- a/Intersect.Server.Core/Entities/Player.cs +++ b/Intersect.Server.Core/Entities/Player.cs @@ -2782,7 +2782,7 @@ 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(GetItemSource(), overflowTileX > -1 ? overflowTileX : X, overflowTileY > -1 ? overflowTileY : Y, item, spawnAmount, Id); + instance.SpawnItem(AsItemSource(), overflowTileX > -1 ? overflowTileX : X, overflowTileY > -1 ? overflowTileY : Y, item, spawnAmount, Id); return spawnAmount != item.Quantity; } @@ -2826,7 +2826,7 @@ public bool TryGiveItem(Item item, ItemHandling handler = ItemHandling.Normal, b /// Creates an item source for the player entity. /// /// A new object. - protected override EntityItemSource GetItemSource() + protected override EntityItemSource? AsItemSource() { return new EntityItemSource { @@ -3149,7 +3149,7 @@ public bool TryDropItemFrom(int slotIndex, int amount) return false; } - mapInstance.SpawnItem(GetItemSource(),X, Y, itemInSlot, itemDescriptor.IsStackable ? amount : 1, Id); + mapInstance.SpawnItem(AsItemSource(),X, Y, itemInSlot, itemDescriptor.IsStackable ? amount : 1, Id); itemInSlot.Quantity = Math.Max(0, itemInSlot.Quantity - amount); @@ -4987,7 +4987,7 @@ public void ReturnTradeItems() return; } - var itemSource = GetItemSource(); + var itemSource = AsItemSource(); foreach (var offer in Trading.Offer) { diff --git a/Intersect.Server.Core/Entities/Projectile.cs b/Intersect.Server.Core/Entities/Projectile.cs index aba9f550e8..f132707070 100644 --- a/Intersect.Server.Core/Entities/Projectile.cs +++ b/Intersect.Server.Core/Entities/Projectile.cs @@ -656,7 +656,7 @@ public override EntityType GetEntityType() return EntityType.Projectile; } - protected override EntityItemSource GetItemSource() + protected override EntityItemSource? AsItemSource() { return null; } diff --git a/Intersect.Server.Core/Entities/Resource.cs b/Intersect.Server.Core/Entities/Resource.cs index 5ab3d00f33..5d42f7ab01 100644 --- a/Intersect.Server.Core/Entities/Resource.cs +++ b/Intersect.Server.Core/Entities/Resource.cs @@ -159,7 +159,7 @@ public void SpawnResourceItems(Entity killer) selectedTile = tiles[Randomization.Next(0, tiles.Count)]; } - var itemSource = GetItemSource(); + var itemSource = AsItemSource(); // Drop items foreach (var item in Items) @@ -178,7 +178,7 @@ public void SpawnResourceItems(Entity killer) Items.Clear(); } - protected override EntityItemSource GetItemSource() + protected override EntityItemSource? AsItemSource() { return new EntityItemSource { diff --git a/Intersect.Server.Core/Maps/MapInstance.cs b/Intersect.Server.Core/Maps/MapInstance.cs index 7dcf3bed75..3f281ce146 100644 --- a/Intersect.Server.Core/Maps/MapInstance.cs +++ b/Intersect.Server.Core/Maps/MapInstance.cs @@ -933,9 +933,9 @@ private void SpawnAttributeItem(int x, int y) var mapItemSource = new MapItemSource { - MapInstanceId = MapInstanceId, + Id = MapInstanceId, MapInstanceReference = new WeakReference(this), - MapControllerId = mMapController.Id, + DescriptorId = mMapController.Id, }; AddItem(mapItemSource, mapItem); diff --git a/Intersect.Server.Framework/Items/MapItemSource.cs b/Intersect.Server.Framework/Items/MapItemSource.cs index 870e550f3e..041506e509 100644 --- a/Intersect.Server.Framework/Items/MapItemSource.cs +++ b/Intersect.Server.Framework/Items/MapItemSource.cs @@ -2,11 +2,10 @@ namespace Intersect.Server.Framework.Items; -public partial class MapItemSource: IItemSource +public partial class MapItemSource : IItemSource { public Guid Id { get; init; } public ItemSourceType SourceType => ItemSourceType.Map; public WeakReference MapInstanceReference { get; init; } - public Guid MapInstanceId { get; init; } - public Guid MapControllerId { get; init; } + public Guid DescriptorId { get; init; } } \ No newline at end of file From 11d13807eca3d9a956c00df3ee83b68e1f8a3f0a Mon Sep 17 00:00:00 2001 From: Blinkuz Date: Sat, 26 Oct 2024 23:54:10 -0400 Subject: [PATCH 7/9] feat: :recycle: WIP Add logic for MapHelper --- Intersect.Server.Core/Database/Item.cs | 5 +++-- Intersect.Server.Core/Maps/MapInstance.cs | 7 +++++-- .../Plugins/Helpers/MapHelper.cs | 21 +++++++++++++++++++ .../Plugins/ServerPluginContext.cs | 3 +++ Intersect.Server.Framework/Items/IItem.cs | 12 +++++++++++ .../Plugins/Helpers/IMapHelper.cs | 11 ++++++++++ .../Plugins/IServerPluginContext.cs | 2 +- 7 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 Intersect.Server.Core/Plugins/Helpers/MapHelper.cs create mode 100644 Intersect.Server.Framework/Items/IItem.cs create mode 100644 Intersect.Server.Framework/Plugins/Helpers/IMapHelper.cs diff --git a/Intersect.Server.Core/Database/Item.cs b/Intersect.Server.Core/Database/Item.cs index 6dbaf60b91..f45309155e 100644 --- a/Intersect.Server.Core/Database/Item.cs +++ b/Intersect.Server.Core/Database/Item.cs @@ -5,13 +5,14 @@ using Intersect.GameObjects; using Intersect.Network.Packets.Server; using Intersect.Server.Database.PlayerData.Players; +using Intersect.Server.Framework.Items; using Newtonsoft.Json; namespace Intersect.Server.Database; -public class Item +public class Item: IItem { - [JsonIgnore][NotMapped] public double DropChance = 100; + [JsonIgnore][NotMapped] public double DropChance { get; set; } = 100; public Item() { diff --git a/Intersect.Server.Core/Maps/MapInstance.cs b/Intersect.Server.Core/Maps/MapInstance.cs index 3f281ce146..fe7cfbe7d7 100644 --- a/Intersect.Server.Core/Maps/MapInstance.cs +++ b/Intersect.Server.Core/Maps/MapInstance.cs @@ -15,6 +15,7 @@ using Intersect.Server.Core.MapInstancing; using Intersect.Server.Framework.Items; using Intersect.Server.Framework.Maps; +using Intersect.Server.Plugins.Helpers; namespace Intersect.Server.Maps; @@ -143,6 +144,9 @@ public partial class MapInstance : IMapInstance // Animations & Text private MapActionMessages mActionMessages = new MapActionMessages(); private MapAnimations mMapAnimations = new MapAnimations(); + + // Map Helper for this instance - used for invoke itemAdded event + private MapHelper _mapHelper = new MapHelper(); public MapInstance(MapController map, Guid mapInstanceId, Player creator) { @@ -715,8 +719,7 @@ private void AddItem(IItemSource? source, 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)); + _mapHelper.InvokeItemAdded(source, item); } /// diff --git a/Intersect.Server.Core/Plugins/Helpers/MapHelper.cs b/Intersect.Server.Core/Plugins/Helpers/MapHelper.cs new file mode 100644 index 0000000000..b8eaf76da5 --- /dev/null +++ b/Intersect.Server.Core/Plugins/Helpers/MapHelper.cs @@ -0,0 +1,21 @@ +using Intersect.Server.Framework.Items; +using Intersect.Server.Framework.Maps; +using Intersect.Server.Maps; + +namespace Intersect.Server.Plugins.Helpers; + +public partial class MapHelper: IMapHelper +{ + public event Action ItemAdded; + + public IMapInstance GetMapInstanceByDescriptorId(Guid mapId, Guid mapInstanceId) + { + MapController.TryGetInstanceFromMap(mapId, mapInstanceId, out var instance); + return instance; + } + + public void InvokeItemAdded(IItemSource source, IItem item) + { + ItemAdded?.Invoke(source, item); + } +} \ No newline at end of file diff --git a/Intersect.Server.Core/Plugins/ServerPluginContext.cs b/Intersect.Server.Core/Plugins/ServerPluginContext.cs index 0a497ab67b..d58786f29c 100644 --- a/Intersect.Server.Core/Plugins/ServerPluginContext.cs +++ b/Intersect.Server.Core/Plugins/ServerPluginContext.cs @@ -38,8 +38,11 @@ public IPluginContext Create(params object[] args) public ServerPluginContext(Plugin plugin) : base(plugin) { Lifecycle = new ServerLifecycleHelper(this); + MapHelper = new MapHelper(); } /// public override IServerLifecycleHelper Lifecycle { get; } + + public IMapHelper MapHelper { get; } } diff --git a/Intersect.Server.Framework/Items/IItem.cs b/Intersect.Server.Framework/Items/IItem.cs new file mode 100644 index 0000000000..6ce2bdd441 --- /dev/null +++ b/Intersect.Server.Framework/Items/IItem.cs @@ -0,0 +1,12 @@ +using Intersect.GameObjects; + +namespace Intersect.Server.Framework.Items; + +public interface IItem +{ + Guid ItemId { get; } + ItemBase Descriptor { get; } + int Quantity { get; } + string ItemName { get; } + double DropChance { get; } +} \ No newline at end of file diff --git a/Intersect.Server.Framework/Plugins/Helpers/IMapHelper.cs b/Intersect.Server.Framework/Plugins/Helpers/IMapHelper.cs new file mode 100644 index 0000000000..469932b2a1 --- /dev/null +++ b/Intersect.Server.Framework/Plugins/Helpers/IMapHelper.cs @@ -0,0 +1,11 @@ +using Intersect.Server.Framework.Items; +using Intersect.Server.Framework.Maps; + +namespace Intersect.Server.Plugins.Helpers; + +public interface IMapHelper +{ + event Action ItemAdded; + IMapInstance GetMapInstanceByDescriptorId(Guid mapId, Guid mapInstanceId); + void InvokeItemAdded(IItemSource source, IItem item); +} \ No newline at end of file diff --git a/Intersect.Server.Framework/Plugins/IServerPluginContext.cs b/Intersect.Server.Framework/Plugins/IServerPluginContext.cs index 8898062945..d7fe8a1761 100644 --- a/Intersect.Server.Framework/Plugins/IServerPluginContext.cs +++ b/Intersect.Server.Framework/Plugins/IServerPluginContext.cs @@ -8,6 +8,6 @@ namespace Intersect.Server.Plugins /// public interface IServerPluginContext : IPluginContext { - + IMapHelper MapHelper { get; } } } From 275a17d7e530eef810cc3821a2a395e7b053869d Mon Sep 17 00:00:00 2001 From: Blinkuz Date: Sun, 27 Oct 2024 23:07:31 -0400 Subject: [PATCH 8/9] fix: :bug: Changed MapHelper to a singleton to maintain the reference for event subscriptions --- Intersect.Server.Core/Maps/MapInstance.cs | 5 +---- Intersect.Server.Core/Plugins/Helpers/MapHelper.cs | 12 +++++++++--- Intersect.Server.Core/Plugins/ServerPluginContext.cs | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Intersect.Server.Core/Maps/MapInstance.cs b/Intersect.Server.Core/Maps/MapInstance.cs index fe7cfbe7d7..a00e5b05ca 100644 --- a/Intersect.Server.Core/Maps/MapInstance.cs +++ b/Intersect.Server.Core/Maps/MapInstance.cs @@ -145,9 +145,6 @@ public partial class MapInstance : IMapInstance private MapActionMessages mActionMessages = new MapActionMessages(); private MapAnimations mMapAnimations = new MapAnimations(); - // Map Helper for this instance - used for invoke itemAdded event - private MapHelper _mapHelper = new MapHelper(); - public MapInstance(MapController map, Guid mapInstanceId, Player creator) { mMapController = map; @@ -719,7 +716,7 @@ private void AddItem(IItemSource? source, MapItem item) TileItems[item.TileIndex]?.TryAdd(item.UniqueId, item); - _mapHelper.InvokeItemAdded(source, item); + MapHelper.Instance.InvokeItemAdded(source, item); } /// diff --git a/Intersect.Server.Core/Plugins/Helpers/MapHelper.cs b/Intersect.Server.Core/Plugins/Helpers/MapHelper.cs index b8eaf76da5..bae57d6c06 100644 --- a/Intersect.Server.Core/Plugins/Helpers/MapHelper.cs +++ b/Intersect.Server.Core/Plugins/Helpers/MapHelper.cs @@ -4,16 +4,22 @@ namespace Intersect.Server.Plugins.Helpers; -public partial class MapHelper: IMapHelper +public partial class MapHelper : IMapHelper { + private static readonly Lazy _instance = new Lazy(() => new MapHelper()); + + public static MapHelper Instance => _instance.Value; + + private MapHelper() { } + public event Action ItemAdded; - + public IMapInstance GetMapInstanceByDescriptorId(Guid mapId, Guid mapInstanceId) { MapController.TryGetInstanceFromMap(mapId, mapInstanceId, out var instance); return instance; } - + public void InvokeItemAdded(IItemSource source, IItem item) { ItemAdded?.Invoke(source, item); diff --git a/Intersect.Server.Core/Plugins/ServerPluginContext.cs b/Intersect.Server.Core/Plugins/ServerPluginContext.cs index d58786f29c..b5a8f212d4 100644 --- a/Intersect.Server.Core/Plugins/ServerPluginContext.cs +++ b/Intersect.Server.Core/Plugins/ServerPluginContext.cs @@ -38,7 +38,7 @@ public IPluginContext Create(params object[] args) public ServerPluginContext(Plugin plugin) : base(plugin) { Lifecycle = new ServerLifecycleHelper(this); - MapHelper = new MapHelper(); + MapHelper = Helpers.MapHelper.Instance; } /// From 1d7f69a0018e5968a09576c8de007ede41eabeb2 Mon Sep 17 00:00:00 2001 From: Blinkuz Date: Tue, 29 Oct 2024 22:32:46 -0400 Subject: [PATCH 9/9] fix: :recycle: Apply review changes in MapHelper --- .../Plugins/Helpers/MapHelper.cs | 23 +++++++++++-------- .../Plugins/Helpers/IMapHelper.cs | 12 ++++++---- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/Intersect.Server.Core/Plugins/Helpers/MapHelper.cs b/Intersect.Server.Core/Plugins/Helpers/MapHelper.cs index bae57d6c06..5e3cb7fe23 100644 --- a/Intersect.Server.Core/Plugins/Helpers/MapHelper.cs +++ b/Intersect.Server.Core/Plugins/Helpers/MapHelper.cs @@ -1,4 +1,5 @@ -using Intersect.Server.Framework.Items; +using System.Diagnostics.CodeAnalysis; +using Intersect.Server.Framework.Items; using Intersect.Server.Framework.Maps; using Intersect.Server.Maps; @@ -7,19 +8,21 @@ namespace Intersect.Server.Plugins.Helpers; public partial class MapHelper : IMapHelper { private static readonly Lazy _instance = new Lazy(() => new MapHelper()); - public static MapHelper Instance => _instance.Value; - private MapHelper() { } - - public event Action ItemAdded; - - public IMapInstance GetMapInstanceByDescriptorId(Guid mapId, Guid mapInstanceId) + public event ItemAddedHandler ItemAdded; + + public bool TryGetMapInstance(Guid mapId, Guid instanceId, [NotNullWhen(true)] out IMapInstance? instance) { - MapController.TryGetInstanceFromMap(mapId, mapInstanceId, out var instance); - return instance; + instance = null; + if (MapController.TryGetInstanceFromMap(mapId, instanceId, out var mapInstance)) + { + instance = mapInstance; + return mapInstance != null; + } + return false; } - + public void InvokeItemAdded(IItemSource source, IItem item) { ItemAdded?.Invoke(source, item); diff --git a/Intersect.Server.Framework/Plugins/Helpers/IMapHelper.cs b/Intersect.Server.Framework/Plugins/Helpers/IMapHelper.cs index 469932b2a1..6522af5e20 100644 --- a/Intersect.Server.Framework/Plugins/Helpers/IMapHelper.cs +++ b/Intersect.Server.Framework/Plugins/Helpers/IMapHelper.cs @@ -1,11 +1,13 @@ -using Intersect.Server.Framework.Items; +using System.Diagnostics.CodeAnalysis; +using Intersect.Server.Framework.Items; using Intersect.Server.Framework.Maps; namespace Intersect.Server.Plugins.Helpers; public interface IMapHelper { - event Action ItemAdded; - IMapInstance GetMapInstanceByDescriptorId(Guid mapId, Guid mapInstanceId); - void InvokeItemAdded(IItemSource source, IItem item); -} \ No newline at end of file + event ItemAddedHandler ItemAdded; + bool TryGetMapInstance(Guid mapId, Guid instanceId, [NotNullWhen(true)] out IMapInstance? instance); +} + +public delegate void ItemAddedHandler(IItemSource? source, IItem item);