Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Sep 1, 2024
1 parent dd71c2c commit f1c0403
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 70 deletions.
6 changes: 3 additions & 3 deletions lang/Jailbreak.English/Warden/WardenCmdOpenLocale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ public IView CellsOpened
public IView CellsOpenedWithPrisoners(int prisoners) {
return new SimpleView {
WardenLocale.PREFIX,
"Detected",
ChatColors.Grey + "Detected",
prisoners,
ChatColors.Green + "prisoner" + (prisoners == 1 ? "" : "s")
ChatColors.Grey + "prisoner" + (prisoners == 1 ? "" : "s")
+ " still in cells, opening..."
};
}

public IView CellsOpenedSnitchPrisoners(int prisoners) {
return new SimpleView {
WardenLocale.PREFIX,
"Detected",
ChatColors.Grey + "Detected",
prisoners,
ChatColors.Green + "prisoner" + (prisoners == 1 ? "" : "s")
+ " still in cells..."
Expand Down
51 changes: 19 additions & 32 deletions mod/Jailbreak.RTD/Rewards/CannotPickupReward.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,37 @@

namespace Jailbreak.RTD.Rewards;

public class CannotPickupReward : IRTDReward {
private readonly HashSet<int> blockedPlayerIDs = [];
public class CannotPickupReward : AbstractOnTickReward {
private readonly ImmutableHashSet<string> blockedWeapons;

public CannotPickupReward(BasePlugin plugin, WeaponType blocked) : this(
plugin, blocked.GetItems().ToArray()) {
NameShort = blocked.ToString();
NameShort = blocked.ToString().ToTitleCase();
}

public CannotPickupReward(BasePlugin plugin, params string[] weapons) {
plugin.RegisterEventHandler<EventItemPickup>(onPickup);
plugin.RegisterEventHandler<EventRoundEnd>(onRoundEnd);

public CannotPickupReward(BasePlugin plugin, params string[] weapons) : base(
plugin) {
blockedWeapons = weapons.ToImmutableHashSet();
NameShort = string.Join(", ",
blockedWeapons.Select(s => s.GetFriendlyWeaponName()));
}

public virtual string Name => $"Cannot Pickup {NameShort}";
public override string Name => $"Cannot Use {NameShort}";
public string NameShort { get; }

public virtual string Description
=> $"You will not be able to pickup {NameShort} next round.";

public bool GrantReward(CCSPlayerController player) {
if (player.UserId == null) return false;
blockedPlayerIDs.Add(player.UserId.Value);
return true;
}

private HookResult onRoundEnd(EventRoundEnd @event, GameEventInfo info) {
blockedPlayerIDs.Clear();
return HookResult.Continue;
}

private HookResult onPickup(EventItemPickup @event, GameEventInfo info) {
var player = @event.Userid;
if (player == null || !player.IsValid || player.UserId == null)
return HookResult.Continue;
if (!blockedPlayerIDs.Contains(player.UserId.Value))
return HookResult.Continue;
var weapon = "weapon_" + @event.Item;
if (!blockedWeapons.Contains(weapon)) return HookResult.Continue;
player.RemoveWeapons();
return HookResult.Continue;
public override string Description
=> $"You will not be able to use {NameShort} next round.";

override protected void tick(CCSPlayerController player) {
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 (!blockedWeapons.Contains(activeWeapon.DesignerName)) return;
activeWeapon.NextPrimaryAttackTick = Server.TickCount + 500;
activeWeapon.NextSecondaryAttackTick = Server.TickCount + 500;
}
}
17 changes: 9 additions & 8 deletions mod/Jailbreak.SpecialDay/SpecialDays/HideAndSeekDay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,15 @@ public override void Execute() {
foreach (var ct in PlayerUtil.FromTeam(HiderTeam.Value)) ct.SetSpeed(1);
}

override protected HookResult OnPickup(EventItemPickup @event,
GameEventInfo info) {
var lrProvider = provider.GetService<ILastRequestManager>();
if (lrProvider == null) return base.OnPickup(@event, info);

return lrProvider.IsLREnabled ?
HookResult.Continue :
base.OnPickup(@event, info);
override protected void OnTick() {
var lr = Provider.GetService<ILastRequestManager>();
if (lr == null) {
base.OnTick();
return;
}

if (lr.IsLREnabled) return;
base.OnTick();
}

public class HnsSettings : SpecialDaySettings {
Expand Down
6 changes: 3 additions & 3 deletions mod/Jailbreak.SpecialDay/SpecialDays/OneInTheChamberDay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public override ISDInstanceLocale Locale

public override void Setup() {
base.Setup();
Plugin.RegisterEventHandler<EventItemPickup>(OnPickup);
Plugin.RegisterEventHandler<EventPlayerHurt>(OnPlayerDamage);
Plugin.RegisterEventHandler<EventPlayerDeath>(OnPlayerDeath);
}
Expand All @@ -53,9 +54,8 @@ public override void Execute() {
started = true;
}

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

var player = @event.Userid;
if (player == null || !player.IsValid) return HookResult.Continue;
Expand Down
8 changes: 3 additions & 5 deletions mod/Jailbreak.SpecialDay/SpecialDays/SpeedrunDay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,7 @@ private void endRound() {
foreach (var player in PlayerUtil.GetAlive()) {
if (player.Team == CsTeam.CounterTerrorist) ctMade = true;
if (player.Team == CsTeam.Terrorist) tMade = true;
if (finishedPlayers.Contains(player.Slot)) continue;
finishedPlayers.Add(player.Slot);
if (!finishedPlayers.Add(player.Slot)) continue;

var dist = player.PlayerPawn.Value?.AbsOrigin?.Distance(target);
if (dist == null) continue;
Expand Down Expand Up @@ -695,8 +694,6 @@ private void endRound() {

if (CV_WINNER_DAMAGEABLE.Value) EnableDamage(winner);

Plugin.DeregisterEventHandler<EventItemPickup>(OnPickup);

foreach (var weapon in CV_LOSERS_WEAPONS.Value.Split(','))
foreach (var loser in losers)
loser.GiveNamedItem(weapon);
Expand All @@ -705,6 +702,7 @@ private void endRound() {
winner.GiveNamedItem(weapon);

Plugin.RemoveListener<Listeners.OnTick>(checkFinishers);
Plugin.RemoveListener<Listeners.OnTick>(OnTick);
RoundUtil.SetTimeRemaining(Math.Min(timeToSet, CV_WIN_TIME_MAX.Value));
Server.ExecuteCommand("mp_ignore_round_win_conditions 0");
return;
Expand Down Expand Up @@ -818,7 +816,7 @@ override protected HookResult
return result;
}

public class SpeedrunSettings : SpecialDaySettings {
private class SpeedrunSettings : SpecialDaySettings {
public SpeedrunSettings() {
CtTeleport = TeleportType.RANDOM_STACKED;
TTeleport = TeleportType.RANDOM_STACKED;
Expand Down
6 changes: 3 additions & 3 deletions mod/Jailbreak.Warden/Global/WardenBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ public HookResult OnRoundStart(EventRoundStart ev, GameEventInfo info) {
var cellZone = getCellZone();

var prisoners = PlayerUtil.FromTeam(CsTeam.Terrorist)
.Count(p => p.Pawn.Value != null && p.Pawn.Value.AbsOrigin != null
&& cellZone.IsInsideZone(p.Pawn.Value?.AbsOrigin!));
.Count(p => p.Pawn.Value != null && p.PlayerPawn.Value?.AbsOrigin != null
&& cellZone.IsInsideZone(p.PlayerPawn.Value?.AbsOrigin!));

if (openCmd.OpenedCells) {
if (CV_WARDEN_AUTO_SNITCH.Value && prisoners > 0)
Expand All @@ -441,7 +441,7 @@ public HookResult OnRoundStart(EventRoundStart ev, GameEventInfo info) {
if (prisoners == 0) return;

if (CV_WARDEN_AUTO_SNITCH.Value) {
cmdLocale.CellsOpenedWithPrisoners(prisoners);
cmdLocale.CellsOpenedWithPrisoners(prisoners).ToAllChat();
} else { cmdLocale.CellsOpened.ToAllChat(); }
});

Expand Down
13 changes: 13 additions & 0 deletions public/Jailbreak.Public/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,17 @@ public static string Sanitize(this string unknown) {
public static bool IsVowel(this char c) {
return "aeiouAEIOU".IndexOf(c) >= 0;
}

// This is a comment => This Is A Comment
public static string ToTitleCase(this string unknown) {
var words = unknown.Split(' ');
for (var i = 0; i < words.Length; i++) {
if (words[i].Length == 0) continue;
var firstLetter = words[i][0];
words[i] = words[i][1..];
words[i] = firstLetter.ToString().ToUpper() + words[i].ToLower();
}

return string.Join(' ', words);
}
}
41 changes: 27 additions & 14 deletions public/Jailbreak.Public/Mod/SpecialDay/AbstractSpecialDay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ 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 @@ -255,7 +254,11 @@ protected void SetConvarValue(ConVar? cvar, object value) {
/// <summary>
/// Called when the actual action begins for the special day.
/// </summary>
public virtual void Execute() { EnableDamage(); }
public virtual void Execute() {
EnableDamage();
if (Settings.RestrictWeapons)
Plugin.RegisterListener<Listeners.OnTick>(OnTick);
}

virtual protected HookResult OnEnd(EventRoundEnd @event, GameEventInfo info) {
foreach (var entry in previousConvarValues) {
Expand All @@ -264,26 +267,36 @@ virtual protected HookResult OnEnd(EventRoundEnd @event, GameEventInfo info) {
SetConvarValue(cv, entry.Value);
}

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

previousConvarValues.Clear();

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(weapon)) return HookResult.Continue;
Server.NextFrame(() => { player.RemoveWeapons(); });
return HookResult.Continue;
virtual protected void OnTick() {
foreach (var player in PlayerUtil.GetAlive()) {
var weapons = Settings.AllowedWeapons(player);
if (weapons == null) continue;
disableWeapon(player, weapons);
}
}

[Obsolete("No longer used, you must manually register/unregister this")]
virtual protected void OnTick() { }
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;
}

protected void DisableDamage() {
foreach (var player in PlayerUtil.GetAlive()) DisableDamage(player);
Expand Down
2 changes: 0 additions & 2 deletions public/Jailbreak.Public/Mod/SpecialDay/SpecialDaySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ 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 f1c0403

Please sign in to comment.