forked from TheMysticSword/MysticsItems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkPickupDiscovery.cs
83 lines (75 loc) · 2.49 KB
/
NetworkPickupDiscovery.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using R2API.Networking.Interfaces;
using RoR2;
using RoR2.UI;
using UnityEngine;
using UnityEngine.Networking;
using System.Linq;
using R2API.Networking;
using System.Collections.Generic;
namespace MysticsItems
{
public static class NetworkPickupDiscovery
{
internal static void Init()
{
NetworkingAPI.RegisterMessageType<SyncDiscoverPickup>();
}
public static void DiscoverPickup(CharacterMaster master, PickupIndex pickupIndex)
{
if (NetworkServer.active)
{
new SyncDiscoverPickup(
master.GetComponent<NetworkIdentity>().netId,
pickupIndex.value
).Send(NetworkDestination.Clients);
}
}
private class SyncDiscoverPickup : INetMessage
{
NetworkInstanceId objID;
int pickupIndex;
public SyncDiscoverPickup()
{
}
public SyncDiscoverPickup(NetworkInstanceId objID, int pickupIndex)
{
this.objID = objID;
this.pickupIndex = pickupIndex;
}
public void Deserialize(NetworkReader reader)
{
objID = reader.ReadNetworkId();
pickupIndex = reader.ReadInt32();
}
public void OnReceived()
{
GameObject obj = Util.FindNetworkObject(objID);
if (obj)
{
var master = obj.GetComponent<CharacterMaster>();
if (master)
{
PlayerCharacterMasterController pcmc = master.GetComponent<PlayerCharacterMasterController>();
if (pcmc)
{
NetworkUser networkUser = pcmc.networkUser;
if (networkUser)
{
LocalUser localUser = networkUser.localUser;
if (localUser != null)
{
localUser.userProfile.DiscoverPickup(new PickupIndex(pickupIndex));
}
}
}
}
}
}
public void Serialize(NetworkWriter writer)
{
writer.Write(objID);
writer.Write(pickupIndex);
}
}
}
}