2
2
using HarmonyLib ;
3
3
using InventorySystem . Items . Usables ;
4
4
using Mirror ;
5
- using Synapse . Api ;
6
5
using Synapse . Api . Events . SynapseEventArguments ;
6
+ using UnityEngine ;
7
+ using Utils . Networking ;
8
+ using Logger = Synapse . Api . Logger ;
7
9
8
10
namespace Synapse . Patches . EventsPatches . PlayerPatches
9
11
{
10
- [ HarmonyPatch ( typeof ( UsableItem ) , nameof ( UsableItem . OnUsingStarted ) ) ]
11
- internal static class UsableStartPatch
12
+ [ HarmonyPatch ( typeof ( UsableItemsController ) , nameof ( UsableItemsController . ServerReceivedStatus ) ) ]
13
+ internal static class ReceivedMessagePatch
12
14
{
13
15
[ HarmonyPrefix ]
14
- private static bool StartPatch ( UsableItem __instance )
16
+ private static bool OnMessage ( NetworkConnection conn , StatusMessage msg )
15
17
{
16
18
try
17
19
{
18
- var item = __instance . GetSynapseItem ( ) ;
19
- var player = item . ItemHolder ;
20
+ var player = conn . GetPlayer ( ) ;
21
+ if ( player == null ) return false ;
22
+ if ( player . ItemInHand . Serial != msg . ItemSerial ) return false ;
23
+ if ( ! ( player . ItemInHand . ItemBase is UsableItem usable ) ) return false ;
24
+ var handler = UsableItemsController . GetHandler ( player . Hub ) ;
20
25
var allow = true ;
21
-
22
- Server . Get . Events . Player . InvokePlayerItemUseEvent ( player , item , ItemInteractState . Initiating , ref allow ) ;
23
-
24
- return allow ;
26
+ var item = player . ItemInHand ;
27
+ switch ( msg . Status )
28
+ {
29
+ case StatusMessage . StatusType . Start :
30
+ if ( handler . CurrentUsable . ItemSerial != 0 ) return false ;
31
+ if ( ! usable . CanStartUsing ) return false ;
32
+ var cooldown = UsableItemsController . GetCooldown ( msg . ItemSerial , usable , handler ) ;
33
+ if ( cooldown > 0f )
34
+ {
35
+ conn . Send ( new ItemCooldownMessage ( msg . ItemSerial , cooldown ) ) ;
36
+ return false ;
37
+ }
38
+
39
+ Server . Get . Events . Player . InvokePlayerItemUseEvent ( player , item , ItemInteractState . Initiating , ref allow ) ;
40
+ if ( ! allow ) return false ;
41
+
42
+ handler . CurrentUsable = new CurrentlyUsedItem ( usable , msg . ItemSerial , Time . timeSinceLevelLoad ) ;
43
+ handler . CurrentUsable . Item . OnUsingStarted ( ) ;
44
+ new StatusMessage ( StatusMessage . StatusType . Start , msg . ItemSerial ) . SendToAuthenticated ( ) ;
45
+ break ;
46
+
47
+ case StatusMessage . StatusType . Cancel :
48
+ if ( handler . CurrentUsable . ItemSerial == 0 ) return false ;
49
+ if ( handler . CurrentUsable . StartTime + handler . CurrentUsable . Item . MaxCancellableTime <=
50
+ Time . timeSinceLevelLoad ) return false ;
51
+
52
+ Server . Get . Events . Player . InvokePlayerItemUseEvent ( player , item , ItemInteractState . Stopping , ref allow ) ;
53
+ if ( ! allow ) return false ;
54
+
55
+ handler . CurrentUsable . Item . OnUsingCancelled ( ) ;
56
+ handler . CurrentUsable = CurrentlyUsedItem . None ;
57
+ new StatusMessage ( StatusMessage . StatusType . Cancel , msg . ItemSerial ) . SendToAuthenticated ( ) ;
58
+ break ;
59
+ }
60
+
61
+ return false ;
25
62
}
26
- catch ( Exception e )
63
+ catch ( Exception ex )
27
64
{
28
- Logger . Get . Error ( $ "Synapse-Event: PlayerItemUseEvent Start failed!!\n { e } ") ;
65
+ Logger . Get . Error ( $ "Synapse-Event: PlayerItemUseEvent Receive Message failed!!\n { ex } ") ;
29
66
return true ;
30
67
}
31
68
}
@@ -34,25 +71,30 @@ private static bool StartPatch(UsableItem __instance)
34
71
[ HarmonyPatch ( typeof ( Consumable ) , nameof ( Consumable . ServerOnUsingCompleted ) ) ]
35
72
internal static class UsableUsingCompletePatch
36
73
{
74
+ internal static bool ExecuteFinalizingEvent ( Consumable consumable )
75
+ {
76
+ var item = consumable . GetSynapseItem ( ) ;
77
+ var player = item . ItemHolder ;
78
+ var allow = true ;
79
+
80
+ Server . Get . Events . Player . InvokePlayerItemUseEvent ( player , item , ItemInteractState . Finalizing , ref allow ) ;
81
+
82
+ if ( ! allow )
83
+ {
84
+ consumable . OnUsingCancelled ( ) ;
85
+ var handler = UsableItemsController . GetHandler ( consumable . Owner ) ;
86
+ handler . CurrentUsable = CurrentlyUsedItem . None ;
87
+ NetworkServer . SendToAll ( new StatusMessage ( StatusMessage . StatusType . Cancel , item . Serial ) , 0 , false ) ;
88
+ }
89
+ return allow ;
90
+ }
91
+
37
92
[ HarmonyPrefix ]
38
93
private static bool CompletePatch ( Consumable __instance )
39
94
{
40
95
try
41
96
{
42
- var item = __instance . GetSynapseItem ( ) ;
43
- var player = item . ItemHolder ;
44
- var allow = true ;
45
-
46
- Server . Get . Events . Player . InvokePlayerItemUseEvent ( player , item , ItemInteractState . Finalizing , ref allow ) ;
47
-
48
- if ( ! allow )
49
- {
50
- __instance . OnUsingCancelled ( ) ;
51
- var handler = UsableItemsController . GetHandler ( __instance . Owner ) ;
52
- handler . CurrentUsable = CurrentlyUsedItem . None ;
53
- NetworkServer . SendToAll ( new StatusMessage ( StatusMessage . StatusType . Cancel , item . Serial ) , 0 , false ) ;
54
- }
55
- return allow ;
97
+ return ExecuteFinalizingEvent ( __instance ) ;
56
98
}
57
99
catch ( Exception e )
58
100
{
@@ -70,20 +112,7 @@ private static bool CompletePatch(Consumable __instance)
70
112
{
71
113
try
72
114
{
73
- var item = __instance . GetSynapseItem ( ) ;
74
- var player = item . ItemHolder ;
75
- var allow = true ;
76
-
77
- Server . Get . Events . Player . InvokePlayerItemUseEvent ( player , item , ItemInteractState . Finalizing , ref allow ) ;
78
-
79
- if ( ! allow )
80
- {
81
- __instance . OnUsingCancelled ( ) ;
82
- var handler = UsableItemsController . GetHandler ( __instance . Owner ) ;
83
- handler . CurrentUsable = CurrentlyUsedItem . None ;
84
- NetworkServer . SendToAll ( new StatusMessage ( StatusMessage . StatusType . Cancel , item . Serial ) , 0 , false ) ;
85
- }
86
- return allow ;
115
+ return UsableUsingCompletePatch . ExecuteFinalizingEvent ( __instance ) ;
87
116
}
88
117
catch ( Exception e )
89
118
{
@@ -93,25 +122,51 @@ private static bool CompletePatch(Consumable __instance)
93
122
}
94
123
}
95
124
96
- [ HarmonyPatch ( typeof ( UsableItem ) , nameof ( UsableItem . OnUsingCancelled ) ) ]
97
- internal static class UsableCancelPatch
125
+ [ HarmonyPatch ( typeof ( Consumable ) , nameof ( Consumable . EquipUpdate ) ) ]
126
+ internal static class EquipPatch
98
127
{
99
128
[ HarmonyPrefix ]
100
- private static bool CancelPatch ( UsableItem __instance )
129
+ private static bool OnEquip ( Consumable __instance )
101
130
{
102
131
try
103
132
{
104
- var item = __instance . GetSynapseItem ( ) ;
105
- var player = item . ItemHolder ;
106
- var allow = true ;
133
+ if ( __instance . ActivationReady )
134
+ {
135
+ if ( UsableUsingCompletePatch . ExecuteFinalizingEvent ( __instance ) )
136
+ __instance . ActivateEffects ( ) ;
137
+ }
138
+ return false ;
139
+ }
140
+ catch ( Exception ex )
141
+ {
142
+ Logger . Get . Error ( $ "Synapse-Event: PlayerItemUseEvent Finalizing Equip failed!!\n { ex } ") ;
143
+ return true ;
144
+ }
145
+ }
146
+ }
107
147
108
- Server . Get . Events . Player . InvokePlayerItemUseEvent ( player , item , ItemInteractState . Stopping , ref allow ) ;
148
+ [ HarmonyPatch ( typeof ( Consumable ) , nameof ( Consumable . OnRemoved ) ) ]
149
+ internal static class RemovedPatch
150
+ {
151
+ [ HarmonyPrefix ]
152
+ private static bool OnRemove ( Consumable __instance , InventorySystem . Items . Pickups . ItemPickupBase pickup )
153
+ {
154
+ try
155
+ {
156
+ if ( __instance . ActivationReady && UsableUsingCompletePatch . ExecuteFinalizingEvent ( __instance ) )
157
+ __instance . ActivateEffects ( ) ;
158
+
159
+ if ( __instance . _alreadyActivated && pickup != null )
160
+ pickup . DestroySelf ( ) ;
161
+
162
+ if ( NetworkServer . active )
163
+ UsableItemsController . GetHandler ( __instance . Owner ) . CurrentUsable = CurrentlyUsedItem . None ;
109
164
110
- return allow ;
165
+ return false ;
111
166
}
112
- catch ( Exception e )
167
+ catch ( Exception ex )
113
168
{
114
- Logger . Get . Error ( $ "Synapse-Event: PlayerItemUseEvent Cancel failed!!\n { e } ") ;
169
+ Logger . Get . Error ( $ "Synapse-Event: PlayerItemUseEvent Finalizing Remove failed!!\n { ex } ") ;
115
170
return true ;
116
171
}
117
172
}
0 commit comments