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
5 changes: 3 additions & 2 deletions Intersect.Server.Core/Database/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
6 changes: 3 additions & 3 deletions Intersect.Server.Core/Maps/MapInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -143,7 +144,7 @@ public partial class MapInstance : IMapInstance
// Animations & Text
private MapActionMessages mActionMessages = new MapActionMessages();
private MapAnimations mMapAnimations = new MapAnimations();

public MapInstance(MapController map, Guid mapInstanceId, Player creator)
{
mMapController = map;
Expand Down Expand Up @@ -715,8 +716,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.Instance.InvokeItemAdded(source, item);
}

/// <summary>
Expand Down
27 changes: 27 additions & 0 deletions Intersect.Server.Core/Plugins/Helpers/MapHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Intersect.Server.Framework.Items;
using Intersect.Server.Framework.Maps;
using Intersect.Server.Maps;

namespace Intersect.Server.Plugins.Helpers;

public partial class MapHelper : IMapHelper
{
private static readonly Lazy<MapHelper> _instance = new Lazy<MapHelper>(() => new MapHelper());

public static MapHelper Instance => _instance.Value;

private MapHelper() { }

public event Action<IItemSource, IItem> 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);
}
}
3 changes: 3 additions & 0 deletions Intersect.Server.Core/Plugins/ServerPluginContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ public IPluginContext Create(params object[] args)
public ServerPluginContext(Plugin plugin) : base(plugin)
{
Lifecycle = new ServerLifecycleHelper(this);
MapHelper = Helpers.MapHelper.Instance;
}

/// <inheritdoc />
public override IServerLifecycleHelper Lifecycle { get; }

public IMapHelper MapHelper { get; }
}
12 changes: 12 additions & 0 deletions Intersect.Server.Framework/Items/IItem.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
11 changes: 11 additions & 0 deletions Intersect.Server.Framework/Plugins/Helpers/IMapHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Intersect.Server.Framework.Items;
using Intersect.Server.Framework.Maps;

namespace Intersect.Server.Plugins.Helpers;

public interface IMapHelper
{
event Action<IItemSource, IItem> ItemAdded;
IMapInstance GetMapInstanceByDescriptorId(Guid mapId, Guid mapInstanceId);
void InvokeItemAdded(IItemSource source, IItem item);
}
2 changes: 1 addition & 1 deletion Intersect.Server.Framework/Plugins/IServerPluginContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ namespace Intersect.Server.Plugins
/// </summary>
public interface IServerPluginContext : IPluginContext<IServerPluginContext, IServerLifecycleHelper>
{

IMapHelper MapHelper { get; }
}
}
Loading