3
3
using HarmonyLib ;
4
4
using Vintagestory . API . Common ;
5
5
using Vintagestory . API . Common . Entities ;
6
+ using Vintagestory . API . Datastructures ;
6
7
using Vintagestory . API . Server ;
7
8
using Vintagestory . GameContent ;
8
9
@@ -42,6 +43,7 @@ public void OverwriteNativeFunctions(Instance _instance)
42
43
[ HarmonyPatch ( typeof ( Entity ) , "ReceiveDamage" ) ]
43
44
public static void ReceiveDamageStart ( Entity __instance , DamageSource damageSource , ref float damage )
44
45
{
46
+
45
47
#region compatibility
46
48
// Compatibility Layer Start Calculation
47
49
float compatibilityStartDamage = __instance . Attributes . GetFloat ( "LevelUP_DamageInteraction_Compatibility_ExtendDamageStart_ReceiveDamage" ) ;
@@ -55,19 +57,23 @@ public static void ReceiveDamageStart(Entity __instance, DamageSource damageSour
55
57
damage += damage * compatibilityStartDamageMultiply ;
56
58
#endregion
57
59
60
+
58
61
// Damage bug treatment
59
62
if ( damage > 0 && __instance . ShouldReceiveDamage ( damageSource , damage ) )
60
63
{
64
+
61
65
// Player Does Damage
62
66
// Checking if damage sources is from a player and from a server and if entity is alive
63
67
if ( damageSource . SourceEntity is EntityPlayer || damageSource . GetCauseEntity ( ) is EntityPlayer && __instance . World . Side == EnumAppSide . Server && __instance . Alive )
64
68
{
69
+
65
70
if ( Configuration . enableExtendedLog )
66
- Debug . Log ( $ "{ ( damageSource . SourceEntity as EntityPlayer ) ? . GetName ( ) ?? "PlayerProjectile" } previous damage: { damage } ") ;
71
+ Debug . Log ( $ "{ ( damageSource . SourceEntity as EntityPlayer ) ? . GetName ( ) ?? "PlayerProjectile" } previous damage: { damage } ") ;
67
72
68
73
// Melee Action
69
74
if ( damageSource . SourceEntity is EntityPlayer )
70
75
{
76
+
71
77
// Get player source
72
78
EntityPlayer playerEntity = damageSource . SourceEntity as EntityPlayer ;
73
79
// Get player instance
@@ -187,6 +193,7 @@ public static void ReceiveDamageStart(Entity __instance, DamageSource damageSour
187
193
#endregion
188
194
}
189
195
196
+
190
197
#region hand
191
198
if ( Configuration . enableLevelHand && player . InventoryManager . ActiveHotbarSlot != null )
192
199
{
@@ -202,16 +209,19 @@ public static void ReceiveDamageStart(Entity __instance, DamageSource damageSour
202
209
}
203
210
}
204
211
#endregion
212
+
205
213
}
206
214
// Ranged Action
207
215
else if ( damageSource . GetCauseEntity ( ) is EntityPlayer && damageSource . SourceEntity is EntityProjectile )
208
216
{
217
+
209
218
// Get entities
210
219
EntityPlayer playerEntity = damageSource . GetCauseEntity ( ) as EntityPlayer ;
211
220
212
221
if ( damageSource . SourceEntity is EntityProjectile itemDamage )
213
222
{
214
223
224
+
215
225
// Get player instance
216
226
IPlayer player = __instance . Api . World . PlayerByUid ( playerEntity . PlayerUID ) ;
217
227
@@ -244,15 +254,17 @@ public static void ReceiveDamageStart(Entity __instance, DamageSource damageSour
244
254
245
255
} ;
246
256
#endregion
257
+
247
258
}
248
259
}
249
260
// Invalid
250
261
else Debug . Log ( $ "ERROR: Invalid damage type in OverwriteDamageInteraction, cause entity is invalid: { damageSource . GetCauseEntity ( ) } or source entity is invalid: { damageSource . SourceEntity } ") ;
251
-
252
262
if ( Configuration . enableExtendedLog )
253
263
Debug . Log ( $ "{ ( damageSource . SourceEntity as EntityPlayer ) ? . GetName ( ) ?? "PlayerProjectile" } final damage: { damage } ") ;
264
+
254
265
}
255
266
267
+
256
268
#region compatibility
257
269
// Compatibility Layer Extend Final Calculation
258
270
float compatibilityFinalDamage = __instance . Attributes . GetFloat ( "LevelUP_DamageInteraction_Compatibility_ExtendDamageFinish_ReceiveDamage" ) ;
@@ -266,13 +278,14 @@ public static void ReceiveDamageStart(Entity __instance, DamageSource damageSour
266
278
// Receive damage by the compatibility layer
267
279
damage += damage * compatibilityFinalDamageMultiply ;
268
280
#endregion
269
-
270
281
// Player Receive Damage
271
282
// Checking if received damage is a player and if is a server and if is alive
272
283
if ( __instance is EntityPlayer && __instance . World . Side == EnumAppSide . Server && __instance . Alive )
273
284
{
285
+
286
+
274
287
if ( Configuration . enableExtendedLog )
275
- Debug . Log ( $ "{ ( damageSource . SourceEntity as EntityPlayer ) . Player . PlayerName } received damage: { damage } ") ;
288
+ Debug . Log ( $ "{ ( damageSource . SourceEntity as EntityPlayer ) ? . GetName ( ) } received damage: { damage } ") ;
276
289
277
290
// Get player source
278
291
EntityPlayer playerEntity = __instance as EntityPlayer ;
@@ -298,7 +311,6 @@ public static void ReceiveDamageStart(Entity __instance, DamageSource damageSour
298
311
299
312
}
300
313
#endregion
301
-
302
314
// Check if the damage received is from a valid entity source damage
303
315
// in others cases the armor shouldn't reduce damage
304
316
if ( damageSource . GetCauseEntity ( ) != null || damageSource . SourceEntity != null )
@@ -329,7 +341,6 @@ public static void ReceiveDamageStart(Entity __instance, DamageSource damageSour
329
341
}
330
342
}
331
343
#endregion
332
-
333
344
#region leatherarmor
334
345
if ( Configuration . enableLevelLeatherArmor && damage < Configuration . DamageLimitLeatherArmor )
335
346
{
@@ -517,12 +528,13 @@ public static void ReceiveDamageStart(Entity __instance, DamageSource damageSour
517
528
}
518
529
}
519
530
#endregion
531
+
520
532
}
521
533
522
534
if ( Configuration . enableExtendedLog )
523
- Debug . Log ( $ "{ ( damageSource . SourceEntity as EntityPlayer ) . Player . PlayerName } received final damage: { damage } ") ;
524
- } ;
535
+ Debug . Log ( $ "{ ( damageSource . SourceEntity as EntityPlayer ) ? . GetName ( ) } received final damage: { damage } ") ;
525
536
537
+ } ;
526
538
// Double check bug only if is a player hitting in single player
527
539
if ( damageSource . SourceEntity is EntityPlayer || damageSource . GetCauseEntity ( ) is EntityPlayer )
528
540
singlePlayerDoubleCheck = ! singlePlayerDoubleCheck ;
@@ -652,18 +664,33 @@ public static void ApplyShieldProtectionStart(ModSystemWearableStats __instance,
652
664
{
653
665
if ( ! Configuration . enableLevelShield ) return ;
654
666
655
- // Reduces the damage received more than normal based on shield level
656
- double damageReduced = damage * Configuration . ShieldGetReductionMultiplyByLevel ( player . Entity . WatchedAttributes . GetInt ( "LevelUP_Level_Shield" ) ) ;
657
- damage -= ( float ) damageReduced ;
658
- if ( damage < 0 ) damage = 0 ;
659
- if ( Configuration . enableExtendedLog ) Debug . Log ( $ "{ player . PlayerName } reduced: { damageReduced } in shield damage") ;
660
-
661
- // Servers
662
- if ( instance . serverAPI != null )
663
- instance . serverAPI . OnExperienceEarned ( player as IServerPlayer , "Increase_Shield_Hit" ) ;
664
- // Single player treatment
665
- else if ( instance . clientAPI ? . api . IsSinglePlayer ?? false )
666
- instance . clientAPI . compatibilityChannel . SendPacket ( $ "Increase_Shield_Hit&lanplayername={ player . PlayerName } ") ;
667
+ ItemSlot [ ] shieldSlots =
668
+ [
669
+ player . Entity . LeftHandItemSlot ,
670
+ player . Entity . RightHandItemSlot
671
+ ] ;
672
+ // Swipe all shields from player hands
673
+ for ( int i = 0 ; i < shieldSlots . Length ; i ++ )
674
+ {
675
+ ItemSlot shieldSlot = shieldSlots [ i ] ;
676
+ JsonObject attr = shieldSlot . Itemstack ? . ItemAttributes ? [ "shield" ] ;
677
+ // Checking if is a shield if not continues
678
+ if ( attr == null || ! attr . Exists )
679
+ continue ;
680
+
681
+ // Reduces the damage received more than normal based on shield level
682
+ double damageReduced = damage * Configuration . ShieldGetReductionMultiplyByLevel ( player . Entity . WatchedAttributes . GetInt ( "LevelUP_Level_Shield" ) ) ;
683
+ damage -= ( float ) damageReduced ;
684
+ if ( damage < 0 ) damage = 0 ;
685
+ if ( Configuration . enableExtendedLog ) Debug . Log ( $ "{ player . PlayerName } reduced: { damageReduced } in shield damage") ;
686
+
687
+ // Servers
688
+ if ( instance . serverAPI != null )
689
+ instance . serverAPI . OnExperienceEarned ( player as IServerPlayer , "Increase_Shield_Hit" ) ;
690
+ // Single player treatment
691
+ else if ( instance . clientAPI ? . api . IsSinglePlayer ?? false )
692
+ instance . clientAPI . compatibilityChannel . SendPacket ( $ "Increase_Shield_Hit&lanplayername={ player . PlayerName } ") ;
693
+ }
667
694
}
668
695
#endregion
669
696
}
0 commit comments