mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-09 21:59:16 +00:00
44 lines
1.4 KiB
C#
44 lines
1.4 KiB
C#
using QSB.Events;
|
|
using QSB.ItemSync.WorldObjects;
|
|
using QSB.Player;
|
|
using QSB.Utility;
|
|
using QSB.WorldSync;
|
|
|
|
namespace QSB.ItemSync.Events
|
|
{
|
|
internal class SocketItemEvent : QSBEvent<SocketItemMessage>
|
|
{
|
|
public override EventType Type => EventType.SocketItem;
|
|
|
|
public override void SetupListener()
|
|
=> GlobalMessenger<int, int, bool>.AddListener(EventNames.QSBSocketItem, Handler);
|
|
|
|
public override void CloseListener()
|
|
=> GlobalMessenger<int, int, bool>.RemoveListener(EventNames.QSBSocketItem, Handler);
|
|
|
|
private void Handler(int socketId, int itemId, bool inserting)
|
|
=> SendEvent(CreateMessage(socketId, itemId, inserting));
|
|
|
|
private SocketItemMessage CreateMessage(int socketId, int itemId, bool inserting) => new SocketItemMessage
|
|
{
|
|
AboutId = QSBPlayerManager.LocalPlayerId,
|
|
SocketId = socketId,
|
|
ItemId = itemId,
|
|
Inserting = inserting
|
|
};
|
|
|
|
public override void OnReceiveRemote(bool server, SocketItemMessage message)
|
|
{
|
|
var socketWorldObject = QSBWorldSync.GetWorldFromId<IQSBOWItemSocket>(message.SocketId);
|
|
var itemWorldObject = QSBWorldSync.GetWorldFromId<IQSBOWItem>(message.ItemId);
|
|
DebugLog.DebugWrite($"Socket {(itemWorldObject as IWorldObject).Name} to socket {(socketWorldObject as IWorldObject).Name}. Inserting:{message.Inserting}");
|
|
if (message.Inserting)
|
|
{
|
|
socketWorldObject.PlaceIntoSocket(itemWorldObject);
|
|
return;
|
|
}
|
|
socketWorldObject.RemoveFromSocket();
|
|
}
|
|
}
|
|
}
|