quantum-space-buddies/QSB/ItemSync/Messages/SocketItemMessage.cs
2022-01-26 00:12:44 -08:00

73 lines
1.8 KiB
C#

using Mirror;
using QSB.ItemSync.WorldObjects.Items;
using QSB.ItemSync.WorldObjects.Sockets;
using QSB.Messaging;
using QSB.Player;
using QSB.Utility;
using QSB.WorldSync;
namespace QSB.ItemSync.Messages
{
internal class SocketItemMessage : QSBEnumMessage<SocketMessageType>
{
private int SocketId;
private int ItemId;
public SocketItemMessage(SocketMessageType type, int socketId = -1, int itemId = -1)
{
Value = type;
SocketId = socketId;
ItemId = itemId;
}
public override void Serialize(NetworkWriter writer)
{
base.Serialize(writer);
writer.Write(SocketId);
writer.Write(ItemId);
}
public override void Deserialize(NetworkReader reader)
{
base.Deserialize(reader);
SocketId = reader.Read<int>();
ItemId = reader.Read<int>();
}
public override bool ShouldReceive => QSBWorldSync.AllObjectsReady;
public override void OnReceiveRemote()
{
QSBItemSocket socketWorldObject;
QSBItem itemWorldObject;
var player = QSBPlayerManager.GetPlayer(From);
player.HeldItem = null;
switch (Value)
{
case SocketMessageType.Socket:
socketWorldObject = SocketId.GetWorldObject<QSBItemSocket>();
itemWorldObject = ItemId.GetWorldObject<QSBItem>();
socketWorldObject.PlaceIntoSocket(itemWorldObject);
return;
case SocketMessageType.StartUnsocket:
socketWorldObject = SocketId.GetWorldObject<QSBItemSocket>();
if (!socketWorldObject.IsSocketOccupied())
{
DebugLog.ToConsole($"Warning - Trying to start unsocket on socket that is unoccupied! Socket:{socketWorldObject.Name}");
return;
}
socketWorldObject.RemoveFromSocket();
return;
case SocketMessageType.CompleteUnsocket:
itemWorldObject = ItemId.GetWorldObject<QSBItem>();
itemWorldObject.OnCompleteUnsocket();
return;
}
}
}
}