-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4a2fda
commit 7d2885f
Showing
9 changed files
with
248 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
NitroxClient/Communication/Packets/Processors/PvPAttackProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using NitroxClient.Communication.Packets.Processors.Abstract; | ||
using NitroxModel.Packets; | ||
|
||
namespace NitroxClient.Communication.Packets.Processors; | ||
|
||
public class PvPAttackProcessor : ClientPacketProcessor<PvPAttack> | ||
{ | ||
public override void Process(PvPAttack packet) | ||
{ | ||
if (Player.main && Player.main.liveMixin) | ||
{ | ||
Player.main.liveMixin.TakeDamage(packet.Damage); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
|
||
namespace NitroxModel.Packets; | ||
|
||
[Serializable] | ||
public class PvPAttack : Packet | ||
{ | ||
public ushort TargetPlayerId { get; } | ||
public float Damage { get; set; } | ||
public AttackType Type { get; } | ||
|
||
public PvPAttack(ushort targetPlayerId, float damage, AttackType type) | ||
{ | ||
TargetPlayerId = targetPlayerId; | ||
Damage = damage; | ||
Type = type; | ||
} | ||
|
||
public enum AttackType : byte | ||
{ | ||
KnifeHit, | ||
HeatbladeHit | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
NitroxPatcher/Patches/Dynamic/Knife_OnToolUseAnim_Patch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using HarmonyLib; | ||
using NitroxModel.Helper; | ||
|
||
namespace NitroxPatcher.Patches.Dynamic; | ||
|
||
/// <summary> | ||
/// Registers knife hits's dealer as the main Player object | ||
/// </summary> | ||
public sealed partial class Knife_OnToolUseAnim_Patch : NitroxPatch, IDynamicPatch | ||
{ | ||
public static readonly MethodInfo TARGET_METHOD = Reflect.Method((Knife t) => t.OnToolUseAnim(default)); | ||
|
||
/* | ||
* | ||
* bool flag = liveMixin.IsAlive(); | ||
* REPLACE below line | ||
* liveMixin.TakeDamage(this.damage, vector, this.damageType, null); | ||
* | ||
* WITH: | ||
* liveMixin.TakeDamage(this.damage, vector, this.damageType, Player.mainObject); | ||
* this.GiveResourceOnDamage(gameObject, liveMixin.IsAlive(), flag); | ||
* | ||
*/ | ||
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
return new CodeMatcher(instructions).MatchEndForward([ | ||
new CodeMatch(OpCodes.Ldloc_0), | ||
new CodeMatch(OpCodes.Ldarg_0), | ||
new CodeMatch(OpCodes.Ldfld), | ||
new CodeMatch(OpCodes.Ldnull) | ||
]) | ||
.Set(OpCodes.Ldsfld, Reflect.Field(() => Player.mainObject)) | ||
.InstructionEnumeration(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
NitroxServer/Communication/Packets/Processors/PvPAttackProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.Collections.Generic; | ||
using NitroxModel.Packets; | ||
using NitroxServer.Communication.Packets.Processors.Abstract; | ||
using NitroxServer.GameLogic; | ||
using NitroxServer.Serialization; | ||
|
||
namespace NitroxServer.Communication.Packets.Processors; | ||
|
||
public class PvPAttackProcessor : AuthenticatedPacketProcessor<PvPAttack> | ||
{ | ||
private readonly ServerConfig serverConfig; | ||
private readonly PlayerManager playerManager; | ||
|
||
// TODO: In the future, do a whole config for damage sources | ||
private static readonly Dictionary<PvPAttack.AttackType, float> damageMultiplierByType = new() | ||
{ | ||
{ PvPAttack.AttackType.KnifeHit, 0.5f }, | ||
{ PvPAttack.AttackType.HeatbladeHit, 1f } | ||
}; | ||
|
||
public PvPAttackProcessor(ServerConfig serverConfig, PlayerManager playerManager) | ||
{ | ||
this.serverConfig = serverConfig; | ||
this.playerManager = playerManager; | ||
} | ||
|
||
public override void Process(PvPAttack packet, Player player) | ||
{ | ||
if (!serverConfig.PvPEnabled) | ||
{ | ||
return; | ||
} | ||
if (!playerManager.TryGetPlayerById(packet.TargetPlayerId, out Player targetPlayer)) | ||
{ | ||
return; | ||
} | ||
if (!damageMultiplierByType.TryGetValue(packet.Type, out float multiplier)) | ||
{ | ||
return; | ||
} | ||
|
||
packet.Damage *= multiplier; | ||
targetPlayer.SendPacket(packet); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System.IO; | ||
using NitroxModel.DataStructures.GameLogic; | ||
using NitroxServer.ConsoleCommands.Abstract; | ||
using NitroxServer.ConsoleCommands.Abstract.Type; | ||
using NitroxServer.Serialization; | ||
using NitroxServer.Serialization.World; | ||
|
||
namespace NitroxServer.ConsoleCommands; | ||
|
||
public class PvpCommand : Command | ||
{ | ||
private readonly ServerConfig serverConfig; | ||
|
||
public PvpCommand(ServerConfig serverConfig) : base("pvp", Perms.ADMIN, "Enables/Disables PvP") | ||
{ | ||
AddParameter(new TypeString("state", true, "on/off")); | ||
|
||
this.serverConfig = serverConfig; | ||
} | ||
|
||
protected override void Execute(CallArgs args) | ||
{ | ||
string state = args.Get<string>(0).ToLower(); | ||
|
||
bool pvpEnabled = false; | ||
switch (state) | ||
{ | ||
case "on": | ||
pvpEnabled = true; | ||
break; | ||
case "off": | ||
break; | ||
default: | ||
SendMessage(args.Sender, "Parameter must be \"on\" or \"off\""); | ||
return; | ||
} | ||
|
||
|
||
using (serverConfig.Update(Path.Combine(WorldManager.SavesFolderDir, serverConfig.SaveName))) | ||
{ | ||
if (serverConfig.PvPEnabled == pvpEnabled) | ||
{ | ||
SendMessage(args.Sender, $"PvP is already {state}"); | ||
return; | ||
} | ||
|
||
serverConfig.PvPEnabled = pvpEnabled; | ||
|
||
SendMessageToAllPlayers($"PvP is now {state}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters