mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-09 12:54:51 +00:00
41 lines
1.2 KiB
C#
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;
|
|
}
|
|
}
|
|
}
|