quantum-space-buddies/QSB/ItemSync/Events/DropItemEvent.cs
2021-12-05 11:03:09 +00:00

41 lines
1.2 KiB
C#

using QSB.Events;
using QSB.ItemSync.WorldObjects.Items;
using QSB.Player;
using QSB.Utility;
using QSB.WorldSync;
using UnityEngine;
namespace QSB.ItemSync.Events
{
internal class DropItemEvent : QSBEvent<DropItemMessage>
{
public override bool RequireWorldObjectsReady => true;
public override void SetupListener()
=> GlobalMessenger<int, Vector3, Vector3, Sector>.AddListener(EventNames.QSBDropItem, Handler);
public override void CloseListener()
=> GlobalMessenger<int, Vector3, Vector3, Sector>.RemoveListener(EventNames.QSBDropItem, Handler);
private void Handler(int objectId, Vector3 position, Vector3 normal, Sector sector)
=> SendEvent(CreateMessage(objectId, position, normal, sector));
private DropItemMessage CreateMessage(int objectId, Vector3 position, Vector3 normal, Sector sector) => new()
{
ObjectId = objectId,
Position = position,
Normal = normal,
Sector = sector
};
public override void OnReceiveRemote(bool server, DropItemMessage message)
{
var worldObject = QSBWorldSync.GetWorldFromId<IQSBOWItem>(message.ObjectId);
worldObject.DropItem(message.Position, message.Normal, message.Sector);
var player = QSBPlayerManager.GetPlayer(message.FromId);
player.HeldItem = worldObject;
}
}
}