-
Notifications
You must be signed in to change notification settings - Fork 5
Added Drop Packet Handler #20
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using Moonlight.Clients; | ||
using Moonlight.Core; | ||
using Moonlight.Core.Enums; | ||
using Moonlight.Core.Logging; | ||
using Moonlight.Event; | ||
using Moonlight.Event.Maps; | ||
using Moonlight.Game.Entities; | ||
using Moonlight.Game.Factory; | ||
using Moonlight.Game.Inventories.Items; | ||
using Moonlight.Game.Maps; | ||
using Moonlight.Packet.Map; | ||
using System; | ||
|
||
namespace Moonlight.Handlers.Maps | ||
{ | ||
internal class DropPacketHandler : PacketHandler<DropPacket> | ||
{ | ||
private readonly IEntityFactory _entityFactory; | ||
private readonly IEventManager _eventManager; | ||
private readonly ILogger _logger; | ||
|
||
public DropPacketHandler(ILogger logger, IEntityFactory entityFactory, IEventManager eventManager) | ||
{ | ||
_logger = logger; | ||
_entityFactory = entityFactory; | ||
_eventManager = eventManager; | ||
} | ||
protected override void Handle(Client client, DropPacket packet) | ||
{ | ||
Map map = client.Character.Map; | ||
|
||
if (map == null) | ||
{ | ||
_logger.Warn("Handling InPacket but character map is null"); | ||
return; | ||
} | ||
|
||
if(map.Contains(EntityType.GROUND_ITEM, packet.VNum)) | ||
{ | ||
_logger.Warn($"Entity {EntityType.GROUND_ITEM} {packet.VNum} already on map"); | ||
return; | ||
} | ||
|
||
Entity entity = _entityFactory.CreateGroundItem(packet.DropId, packet.VNum, packet.Amount); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Set variable type as GroundItem instead of Entity since CreateGroundItem will always return a GroundItem There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it, ill will try to fix this and the rest of the issue maybe later this week. Thank you for your keen eye. |
||
entity.Position = new Position(packet.PositionX, packet.PositionY); | ||
|
||
if (entity is GroundItem drop) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By fixing previous problem, you can also remove this type check/cast. |
||
{ | ||
drop.Owner = map.GetEntity<Player>(packet.OwnerId); | ||
} | ||
|
||
map.AddEntity(entity); | ||
_logger.Info($"Entity {entity.EntityType} {entity.Id} joined map"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change info message to Drop with id {entity.Id} added to map instead or something like this |
||
|
||
_eventManager.Emit(new EntityJoinEvent(client) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you can also create a DropEvent or something with GroundItem & Map as property |
||
{ | ||
Map = map, | ||
Entity = entity | ||
}); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change warning message to Handling DropPacket instead of Handling InPacket