Skip to content

Commit

Permalink
Switch to preventing pickups instead of listening ontick
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Aug 28, 2024
1 parent 6189efb commit b213b9a
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 43 deletions.
1 change: 0 additions & 1 deletion lang/Jailbreak.English/Mute/WardenPeaceLocale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Jailbreak.Formatting.Logistics;
using Jailbreak.Formatting.Objects;
using Jailbreak.Formatting.Views.Warden;
using Microsoft.Extensions.Primitives;

namespace Jailbreak.English.Mute;

Expand Down
5 changes: 2 additions & 3 deletions mod/Jailbreak.SpecialDay/SpecialDays/InfectionDay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ override protected HookResult

public class InfectionSettings : SpecialDaySettings {
public InfectionSettings() {
CtTeleport = TeleportType.ARMORY;
TTeleport = TeleportType.RANDOM;
RestrictWeapons = true;
CtTeleport = TeleportType.ARMORY;
TTeleport = TeleportType.RANDOM;

WithRespawns(CsTeam.CounterTerrorist);
}
Expand Down
16 changes: 12 additions & 4 deletions mod/Jailbreak.SpecialDay/SpecialDays/NoScopeDay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ public override void Execute() {
player.GiveNamedItem(weapon);
}

Plugin.RegisterListener<Listeners.OnTick>(OnTick);

base.Execute();
}

override protected void OnTick() {
protected void OnTick() {

Check warning on line 68 in mod/Jailbreak.SpecialDay/SpecialDays/NoScopeDay.cs

View workflow job for this annotation

GitHub Actions / build

'NoScopeDay.OnTick()' hides inherited member 'AbstractSpecialDay.OnTick()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Check warning on line 68 in mod/Jailbreak.SpecialDay/SpecialDays/NoScopeDay.cs

View workflow job for this annotation

GitHub Actions / build

'NoScopeDay.OnTick()' hides inherited member 'AbstractSpecialDay.OnTick()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Check warning on line 68 in mod/Jailbreak.SpecialDay/SpecialDays/NoScopeDay.cs

View workflow job for this annotation

GitHub Actions / build

'NoScopeDay.OnTick()' hides inherited member 'AbstractSpecialDay.OnTick()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Check warning on line 68 in mod/Jailbreak.SpecialDay/SpecialDays/NoScopeDay.cs

View workflow job for this annotation

GitHub Actions / build

'NoScopeDay.OnTick()' hides inherited member 'AbstractSpecialDay.OnTick()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
foreach (var player in PlayerUtil.GetAlive()) disableScope(player);
}

Expand All @@ -83,11 +85,17 @@ private void disableScope(CCSPlayerController player) {
activeWeapon.NextPrimaryAttackTick = Server.TickCount + 500;
}

override protected HookResult
OnEnd(EventRoundEnd @event, GameEventInfo info) {
var result = base.OnEnd(@event, info);
Plugin.RemoveListener<Listeners.OnTick>(OnTick);
return result;
}

private class NoScopeSettings : FFASettings {
public NoScopeSettings() {
CtTeleport = TeleportType.RANDOM;
TTeleport = TeleportType.RANDOM;
RestrictWeapons = true;
CtTeleport = TeleportType.RANDOM;
TTeleport = TeleportType.RANDOM;

ConVarValues["sv_gravity"] = CV_GRAVITY.Value;
ConVarValues["sv_infinite_ammo"] = 2;
Expand Down
23 changes: 20 additions & 3 deletions mod/Jailbreak.SpecialDay/SpecialDays/OneInTheChamberDay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public override ISDInstanceLocale Locale
"Additional (non-ammo restricted) weapons to give for the day",
"weapon_knife", ConVarFlags.FCVAR_NONE, new ItemValidator());

private bool started;

public override void Setup() {
base.Setup();
Plugin.RegisterEventHandler<EventPlayerHurt>(OnPlayerDamage);
Expand All @@ -48,12 +50,28 @@ public override void Execute() {
player.GetWeaponBase(CV_WEAPON.Value)?.SetAmmo(1, 0);
}
}

started = true;
}

override protected HookResult OnPickup(EventItemPickup @event,
GameEventInfo info) {
var result = base.OnPickup(@event, info);
if (!started) return result;

var player = @event.Userid;
if (player == null || !player.IsValid) return result;
player.RemoveWeapons();
player.SetHealth(1);
return result;
}

private HookResult
OnPlayerDamage(EventPlayerHurt @event, GameEventInfo info) {
if (@event.Userid == null || !@event.Userid.IsValid)
return HookResult.Continue;
if (@event.Attacker == null || !@event.Attacker.IsValid)
return HookResult.Continue;
@event.Userid?.SetHealth(0);
return HookResult.Changed;
}
Expand All @@ -74,9 +92,8 @@ override protected HookResult

public class OitcSettings : SpecialDaySettings {
public OitcSettings() {
CtTeleport = TeleportType.RANDOM;
TTeleport = TeleportType.RANDOM;
RestrictWeapons = true;
CtTeleport = TeleportType.RANDOM;
TTeleport = TeleportType.RANDOM;
WithFriendlyFire();

ConVarValues["mp_death_drop_gun"] = 0;
Expand Down
1 change: 0 additions & 1 deletion mod/Jailbreak.SpecialDay/SpecialDays/SpeedrunDay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,6 @@ public SpeedrunSettings() {
CtTeleport = TeleportType.RANDOM_STACKED;
TTeleport = TeleportType.RANDOM_STACKED;
StripToKnife = true;
RestrictWeapons = true;
ConVarValues["mp_ignore_round_win_conditions"] = true;
if (new Random().Next(5) == 0) WithAutoBhop();
WithFriendlyFire();
Expand Down
2 changes: 1 addition & 1 deletion mod/Jailbreak.Warden/Global/WardenBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public bool TrySetWarden(CCSPlayerController controller) {

logs.Append(logs.Player(Warden), "is now the warden.");

unblueTimer = parent!.AddTimer(3, unmarkPrisonersBlue);
unblueTimer = parent.AddTimer(3, unmarkPrisonersBlue);
mute.PeaceMute(firstWarden ?
MuteReason.INITIAL_WARDEN :
MuteReason.WARDEN_TAKEN);
Expand Down
48 changes: 18 additions & 30 deletions public/Jailbreak.Public/Mod/SpecialDay/AbstractSpecialDay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public abstract class AbstractSpecialDay(BasePlugin plugin,
public virtual void Setup() {
Plugin.RegisterFakeConVars(this);
Plugin.RegisterEventHandler<EventRoundEnd>(OnEnd);
Plugin.RegisterEventHandler<EventItemPickup>(OnPickup);

foreach (var entry in Settings.ConVarValues) {
var cv = ConVar.Find(entry.Key);
Expand Down Expand Up @@ -307,33 +308,7 @@ protected void SetConvarValue(ConVar? cvar, object value) {
/// <summary>
/// Called when the actual action begins for the special day.
/// </summary>
public virtual void Execute() {
EnableDamage();
if (Settings.RestrictWeapons)
Plugin.RegisterListener<Listeners.OnTick>(OnTick);
}

virtual protected void OnTick() {
foreach (var player in PlayerUtil.GetAlive()) {
var weapons = Settings.AllowedWeapons(player);
if (weapons == null) continue;
disableWeapon(player, weapons);
}
}

private void disableWeapon(CCSPlayerController player,
ICollection<string> allowed) {
if (!player.IsReal()) return;
var pawn = player.PlayerPawn.Value;
if (pawn == null || !pawn.IsValid) return;
var weaponServices = pawn.WeaponServices;
if (weaponServices == null) return;
var activeWeapon = weaponServices.ActiveWeapon.Value;
if (activeWeapon == null || !activeWeapon.IsValid) return;
if (allowed.Contains(activeWeapon.DesignerName)) return;
activeWeapon.NextSecondaryAttackTick = Server.TickCount + 500;
activeWeapon.NextPrimaryAttackTick = Server.TickCount + 500;
}
public virtual void Execute() { EnableDamage(); }

virtual protected HookResult OnEnd(EventRoundEnd @event, GameEventInfo info) {
foreach (var entry in previousConvarValues) {
Expand All @@ -344,13 +319,26 @@ virtual protected HookResult OnEnd(EventRoundEnd @event, GameEventInfo info) {

previousConvarValues.Clear();

if (Settings.RestrictWeapons)
Plugin.RemoveListener<Listeners.OnTick>(OnTick);

Plugin.DeregisterEventHandler<EventRoundEnd>(OnEnd);
Plugin.DeregisterEventHandler<EventItemPickup>(OnPickup);
return HookResult.Continue;
}

virtual protected HookResult OnPickup(EventItemPickup @event,
GameEventInfo info) {
var player = @event.Userid;
if (player == null || !player.IsValid) return HookResult.Continue;
var allowed = Settings.AllowedWeapons(player);
var weapon = "weapon_" + @event.Item;
if (allowed == null || allowed.Contains(@event.Item))
return HookResult.Continue;
player.RemoveItemByDesignerName(weapon, true);
return HookResult.Continue;
}

[Obsolete("No longer used, you must manually register/unregister this")]
virtual protected void OnTick() { }

protected void DisableDamage() {
foreach (var player in PlayerUtil.GetAlive()) DisableDamage(player);
}
Expand Down
2 changes: 2 additions & 0 deletions public/Jailbreak.Public/Mod/SpecialDay/SpecialDaySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public enum TeleportType {
/// <summary>
/// Used to avoid registring a costly OnTick listener if false
/// </summary>
[Obsolete(
"With the new optimization, we now remove weapons if disallowed, making this obsolete.")]
public bool RestrictWeapons = false;

/// <summary>
Expand Down

0 comments on commit b213b9a

Please sign in to comment.