quantum-space-buddies/QSB/ItemSync/Events/SocketItemEvent.cs

58 lines
1.8 KiB
C#
Raw Normal View History

2021-02-25 21:24:10 +00:00
using QSB.Events;
2021-11-01 15:49:00 +00:00
using QSB.ItemSync.WorldObjects.Items;
using QSB.ItemSync.WorldObjects.Sockets;
2021-02-25 21:24:10 +00:00
using QSB.Player;
2021-03-26 20:56:57 +00:00
using QSB.Utility;
2021-02-25 21:24:10 +00:00
using QSB.WorldSync;
namespace QSB.ItemSync.Events
2021-02-23 14:42:18 +00:00
{
2021-02-25 21:24:10 +00:00
internal class SocketItemEvent : QSBEvent<SocketItemMessage>
2021-02-23 14:42:18 +00:00
{
public override bool RequireWorldObjectsReady => true;
2021-02-25 21:24:10 +00:00
public override void SetupListener()
=> GlobalMessenger<int, int, SocketEventType>.AddListener(EventNames.QSBSocketItem, Handler);
2021-02-25 21:24:10 +00:00
public override void CloseListener()
=> GlobalMessenger<int, int, SocketEventType>.RemoveListener(EventNames.QSBSocketItem, Handler);
2021-02-25 21:24:10 +00:00
private void Handler(int socketId, int itemId, SocketEventType type)
=> SendEvent(CreateMessage(socketId, itemId, type));
2021-02-25 21:24:10 +00:00
2021-11-20 19:49:50 +00:00
private SocketItemMessage CreateMessage(int socketId, int itemId, SocketEventType type) => new()
2021-02-25 21:24:10 +00:00
{
AboutId = QSBPlayerManager.LocalPlayerId,
SocketId = socketId,
ItemId = itemId,
SocketType = type
2021-02-25 21:24:10 +00:00
};
public override void OnReceiveRemote(bool server, SocketItemMessage message)
{
var socketWorldObject = QSBWorldSync.GetWorldFromId<IQSBOWItemSocket>(message.SocketId);
var itemWorldObject = QSBWorldSync.GetWorldFromId<IQSBOWItem>(message.ItemId);
2021-10-30 15:14:38 +00:00
var player = QSBPlayerManager.GetPlayer(message.FromId);
player.HeldItem = null;
switch (message.SocketType)
2021-02-25 22:45:32 +00:00
{
case SocketEventType.Socket:
socketWorldObject.PlaceIntoSocket(itemWorldObject);
return;
case SocketEventType.StartUnsocket:
2021-03-26 20:56:57 +00:00
if (!socketWorldObject.IsSocketOccupied())
{
DebugLog.ToConsole($"Warning - Trying to start unsocket on socket that is unoccupied! Socket:{socketWorldObject.Name}");
2021-03-26 20:56:57 +00:00
return;
}
2021-06-18 21:38:32 +00:00
socketWorldObject.RemoveFromSocket();
return;
case SocketEventType.CompleteUnsocket:
itemWorldObject.OnCompleteUnsocket();
return;
2021-02-25 22:45:32 +00:00
}
2021-02-25 21:24:10 +00:00
}
2021-02-23 14:42:18 +00:00
}
}