Fix LR crashes

This commit is contained in:
MSWS
2025-02-13 09:47:59 -08:00
parent 6cc534e3a4
commit 09d5cd9129
2 changed files with 35 additions and 165 deletions

View File

@@ -1,6 +1,8 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Utils;
using Jailbreak.Formatting.Extensions;
using Jailbreak.Formatting.Views.LastRequest;
@@ -13,6 +15,30 @@ namespace Jailbreak.LastRequest;
public class LastRequestRebelCommand(ILastRequestManager lastRequestManager,
ILastRequestRebelManager lastRequestRebelManager, ILRLocale messages)
: IPluginBehavior {
private readonly Dictionary<int, int> rebellerHealths = [];
public void Start(BasePlugin basePlugin) {
basePlugin.RegisterListener<Listeners.OnEntityParentChanged>(OnDrop);
}
private void OnDrop(CEntityInstance entity, CEntityInstance newparent) {
if (!entity.IsValid || !Tag.WEAPONS.Contains(entity.DesignerName)) return;
var weapon = Utilities.GetEntityFromIndex<CCSWeaponBase>((int)entity.Index);
if (weapon == null
|| weapon.PrevOwner.Get()?.OriginalController.Get() == null)
return;
var owner = weapon.PrevOwner.Get()?.OriginalController.Get();
if (owner == null || newparent.IsValid) return;
if (!rebellerHealths.TryGetValue(owner.Slot, out var hp)) return;
if (owner.Pawn.Value != null)
owner.SetHealth(Math.Min(hp, owner.Pawn.Value.Health));
rebellerHealths.Remove(owner.Slot);
}
[ConsoleCommand("css_rebel", "Rebel during last request as a prisoner")]
[CommandHelper(whoCanExecute: CommandUsage.CLIENT_ONLY)]
public void Command_Rebel(CCSPlayerController? rebeller, CommandInfo info) {
@@ -38,12 +64,15 @@ public class LastRequestRebelCommand(ILastRequestManager lastRequestManager,
return;
}
if (rebeller.Pawn.Value != null)
rebellerHealths[rebeller.Slot] = rebeller.Pawn.Value.Health;
lastRequestRebelManager.StartLRRebelling(rebeller);
}
[GameEventHandler]
public HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info) {
lastRequestRebelManager.ClearLRRebelling();
rebellerHealths.Clear();
return HookResult.Continue;
}
}

View File

@@ -31,16 +31,13 @@ public class GunToss(BasePlugin plugin, ILastRequestManager manager,
/// </summary>
private int? bothThrewTick;
private Timer? guardGroundTimer, prisonerGroundTimer;
private Timer? guardGunTimer, prisonerGunTimer;
private bool guardTossed, prisonerTossed;
private CCSWeaponBase? prisonerWeapon, guardWeapon;
public override LRType Type => LRType.GUN_TOSS;
public void OnWeaponDrop(CCSPlayerController player, CCSWeaponBase weapon) {
if (bothThrewTick > 0) return;
if (State != LRState.ACTIVE) return;
if (State != LRState.ACTIVE || !player.IsValid) return;
bothThrewTick = bothThrewTick switch {
null => -Server.TickCount,
@@ -51,19 +48,14 @@ public class GunToss(BasePlugin plugin, ILastRequestManager manager,
if (player == Prisoner) {
prisonerTossed = true;
prisonerWeapon = weapon;
} else {
}
if (player == Guard) {
guardTossed = true;
guardWeapon = weapon;
}
if (Guard.Slot == Prisoner.Slot) bothThrewTick = Server.TickCount;
followWeapon(player, weapon);
if (player == Prisoner)
prisonerGroundTimer?.Kill();
else
guardGroundTimer?.Kill();
if (prisonerTossed && guardTossed) bothThrewTick = Server.TickCount;
}
public override void Setup() {
@@ -83,54 +75,10 @@ public class GunToss(BasePlugin plugin, ILastRequestManager manager,
Prisoner.GetWeaponBase("weapon_deagle").SetAmmo(0, 7);
Server.RunOnTick(Server.TickCount + 16, () => State = LRState.ACTIVE);
followPlayer(Prisoner);
if (Guard.Slot != Prisoner.Slot) followPlayer(Guard);
Plugin.RegisterListener<Listeners.OnTick>(OnTick);
}
private void OnTick() {
if (bothThrewTick > 0) return;
if (Guard is { IsValid: true, PlayerPawn.IsValid: true }
&& Guard.PlayerPawn.Value != null)
if ((Guard.PlayerPawn.Value.Flags & (uint)PlayerFlags.FL_ONGROUND) != 0)
onGround(Guard);
if (Prisoner is { IsValid: true, PlayerPawn.IsValid: true }
&& Prisoner.PlayerPawn.Value != null)
if ((Prisoner.PlayerPawn.Value.Flags & (uint)PlayerFlags.FL_ONGROUND)
!= 0)
onGround(Prisoner);
}
private void onGround(CCSPlayerController player) {
if (bothThrewTick > 0
|| State != LRState.PENDING && State != LRState.ACTIVE) {
Plugin.RemoveListener<Listeners.OnTick>(OnTick);
return;
}
if (player.Slot == Prisoner.Slot && prisonerTossed) return;
if (player.Slot == Guard.Slot && guardTossed) return;
var lines = player.Slot == Prisoner.Slot ? prisonerLines : guardLines;
lines.ForEach(l => l.Remove());
lines.Clear();
}
public override void OnEnd(LRResult result) {
State = LRState.COMPLETED;
Plugin.RemoveListener<Listeners.OnTick>(OnTick);
guardGroundTimer?.Kill();
prisonerGroundTimer?.Kill();
guardGunTimer?.Kill();
prisonerGunTimer?.Kill();
guardGroundTimer = null;
prisonerGroundTimer = null;
guardGunTimer = null;
prisonerGunTimer = null;
guardLines.ForEach(l => l.Remove());
guardLines.Clear();
@@ -138,113 +86,6 @@ public class GunToss(BasePlugin plugin, ILastRequestManager manager,
prisonerLines.Clear();
}
private void followPlayer(CCSPlayerController player) {
var lines = player == Prisoner ? prisonerLines : guardLines;
Vector? previous = null;
var timer = Plugin.AddTimer(0.1f, () => {
if (State != LRState.ACTIVE && State != LRState.PENDING) {
lines.ForEach(l => l.Remove());
lines.Clear();
prisonerGroundTimer?.Kill();
guardGroundTimer?.Kill();
prisonerGroundTimer = null;
guardGroundTimer = null;
return;
}
Debug.Assert(player.PlayerPawn.Value != null,
"player.PlayerPawn.Value != null");
if ((player.PlayerPawn.Value.Flags & (uint)PlayerFlags.FL_ONGROUND)
!= 0) {
// Player is on the ground
lines.ForEach(l => l.Remove());
lines.Clear();
return;
}
var position = player.PlayerPawn.Value?.AbsOrigin!.Clone()!
+ new Vector(0, 0, 64);
if (previous != null) {
var line = new BeamLine(Plugin, previous, position);
line.SetColor(player == Prisoner ? Color.Red : Color.Blue);
line.SetWidth(1f);
line.Draw(25);
lines.Add(line);
}
previous = position.Clone();
}, TimerFlags.REPEAT | TimerFlags.STOP_ON_MAPCHANGE);
if (player.Slot == Prisoner.Slot)
prisonerGroundTimer = timer;
else
guardGroundTimer = timer;
}
private void followWeapon(CCSPlayerController player, CCSWeaponBase weapon) {
var lines = player.Slot == Prisoner.Slot ? prisonerLines : guardLines;
var lastPos = lines.Count > 0 ? lines[^1].End : null;
Debug.Assert(player.PlayerPawn.Value != null,
"player.PlayerPawn.Value != null");
var timer = Plugin.AddTimer(0.1f, () => {
if (!player.IsValid || !Prisoner.IsValid) {
if (player.Slot == Prisoner.Slot) {
prisonerGunTimer?.Kill();
prisonerWeapon = null;
} else {
guardGunTimer?.Kill();
guardWeapon = null;
}
return;
}
if (weapon.AbsOrigin == null || !weapon.IsValid) {
if (player.Slot == Prisoner.Slot) {
prisonerGunTimer?.Kill();
prisonerWeapon = null;
} else {
guardGunTimer?.Kill();
guardWeapon = null;
}
return;
}
if (lastPos != null && lastPos.DistanceSquared(weapon.AbsOrigin) == 0) {
if (player.Slot == Prisoner.Slot) {
prisonerGunTimer?.Kill();
prisonerGunTimer = null;
} else {
guardGunTimer?.Kill();
guardGunTimer = null;
}
var firstPos = lines[0].Position;
locale.PlayerThrewGunDistance(player, lastPos.Distance(firstPos))
.ToAllChat();
return;
}
if (lastPos != null) {
var line = new BeamLine(Plugin, lastPos, weapon.AbsOrigin);
line.SetColor(player == Prisoner ? Color.DarkRed : Color.DarkBlue);
line.SetWidth(0.5f);
line.Draw(25);
lines.Add(line);
}
lastPos = weapon.AbsOrigin.Clone();
}, TimerFlags.REPEAT | TimerFlags.STOP_ON_MAPCHANGE);
if (player.Slot == Prisoner.Slot)
prisonerGunTimer = timer;
else
guardGunTimer = timer;
}
public override bool PreventEquip(CCSPlayerController player,
CCSWeaponBaseVData weapon) {
if (State != LRState.ACTIVE) return false;