Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to exiled 3 #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
463 changes: 228 additions & 235 deletions UltimateAFK/AFKComponent.cs

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions UltimateAFK/AFKData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Exiled.API.Features.Items;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace UltimateAFK
{
struct AFKData
{
public AFKComponent afkComp;

public Vector3 spawnLocation;
public Dictionary<ItemType, ushort> ammo;
public List<ItemType> items;

public float health;

public bool is079;
public byte level;
public float xp;
public float energy;

public object roleEasyEvents;
}
}
56 changes: 28 additions & 28 deletions UltimateAFK/Config.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
namespace UltimateAFK
{
using System.ComponentModel;
using Exiled.API.Interfaces;
using System.ComponentModel;
using Exiled.API.Interfaces;

public sealed class Config : IConfig
{
[Description("Is the plugin enabled?")]
public bool IsEnabled { get; set; } = true;
[Description("Minimum required players for uAFK to be active.")]
public int MinPlayers { get; set; } = 2;
[Description("Do AFK players get replaced by spectators?")]
public bool TryReplace { get; private set; } = true;
[Description("Should Tutorials be ignored?")]
public bool IgnoreTut { get; private set; } = false;
[Description("How long can player not move?")]
public int AfkTime { get; private set; } = 30;
[Description("How long to wait before player gets kicked after getting a warning for not moving?")]
public int GraceTime { get; private set; } = 15;
[Description("After how many changes to spectator for AFK should player get kicked?")]
public int NumBeforeKick { get; private set; } = 2;
[Description("Maximum replace time, if the round time is past this value it will not replace the player (Set to -1 to disable)")]
public int MaxReplaceTime { get; private set; } = -1;
[Description("Don't touch this if you do not understand the repercussions! - Ignore Perm and IP Checks.")]
public bool IgnorePermissionsAndIP { get; private set; } = false;
public string MsgPrefix { get; private set; } = "<color=white>[</color><color=green>uAFK</color><color=white>]</color>";
public string MsgGrace { get; private set; } = "<color=red>You will be moved to spectator in</color> <color=white>%timeleft% seconds</color><color=red> if you do not move!</color>";
public string MsgFspec { get; private set; } = "You were detected as AFK and automatically moved to spectator!";
public string MsgKick { get; private set; } = "[Kicked by uAFK] You were AFK for too long!";
public string MsgReplace { get; private set; } = "You have replaced a player that was AFK.";
}
public sealed class Config : IConfig
{
[Description("Is the plugin enabled?")]
public bool IsEnabled { get; set; } = true;
[Description("Minimum required players for uAFK to be active.")]
public int MinPlayers { get; set; } = 2;
[Description("Do AFK players get replaced by spectators?")]
public bool TryReplace { get; private set; } = true;
[Description("Should Tutorials be ignored?")]
public bool IgnoreTut { get; private set; } = false;
[Description("How long can player not move?")]
public int AfkTime { get; private set; } = 30;
[Description("How long to wait before player gets kicked after getting a warning for not moving?")]
public int GraceTime { get; private set; } = 15;
[Description("After how many changes to spectator for AFK should player get kicked?")]
public int NumBeforeKick { get; private set; } = 2;
[Description("Maximum replace time, if the round time is past this value it will not replace the player (Set to -1 to disable)")]
public int MaxReplaceTime { get; private set; } = -1;
[Description("Don't touch this if you do not understand the repercussions! - Ignore Perm and IP Checks.")]
public bool IgnorePermissionsAndIP { get; private set; } = false;
public string MsgPrefix { get; private set; } = "<color=white>[</color><color=green>uAFK</color><color=white>]</color>";
public string MsgGrace { get; private set; } = "<color=red>You will be moved to spectator in</color> <color=white>%timeleft% seconds</color><color=red> if you do not move!</color>";
public string MsgFspec { get; private set; } = "You were detected as AFK and automatically moved to spectator!";
public string MsgKick { get; private set; } = "[Kicked by uAFK] You were AFK for too long!";
public string MsgReplace { get; private set; } = "You have replaced a player that was AFK.";
}
}
96 changes: 50 additions & 46 deletions UltimateAFK/MainClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,59 +43,63 @@
using Handlers = Exiled.Events.Handlers;
using Exiled.API.Enums;
using Exiled.API.Features;
using HarmonyLib;

namespace UltimateAFK
{
public class MainClass : Plugin<Config>
{
public override string Author { get; } = "Thomasjosif";
public override string Name { get; } = "Ultimate AFK";
public override string Prefix { get; } = "uAFK";
public override Version Version { get; } = new Version(3, 1, 6);
public override Version RequiredExiledVersion { get; } = new Version(2, 2, 4);
public PlayerEvents PlayerEvents;
public class MainClass : Plugin<Config>
{
public override string Author { get; } = "Thomasjosif";
public override string Name { get; } = "Ultimate AFK";
public override string Prefix { get; } = "uAFK";
public override Version Version { get; } = new Version(3, 1, 6);
public PlayerEvents PlayerEvents;

public override PluginPriority Priority { get; } = PluginPriority.Medium;
public override PluginPriority Priority { get; } = PluginPriority.Medium;

public override void OnEnabled()
{
base.OnEnabled();
try
{
PlayerEvents = new PlayerEvents(this);
public override void OnEnabled()
{
base.OnEnabled();
try
{
PlayerEvents = new PlayerEvents(this);

Handlers.Player.Verified += PlayerEvents.OnPlayerVerified;
Handlers.Player.ChangingRole += PlayerEvents.OnSetClass;
Handlers.Player.Shooting += PlayerEvents.OnPlayerShoot;
Handlers.Player.InteractingDoor += PlayerEvents.OnDoorInteract;
Handlers.Scp914.Activating += PlayerEvents.On914Activate;
Handlers.Scp914.ChangingKnobSetting += PlayerEvents.On914Change;
Handlers.Player.InteractingLocker += PlayerEvents.OnLockerInteract;
Handlers.Player.ItemDropped += PlayerEvents.OnDropItem;
Handlers.Scp079.GainingExperience += PlayerEvents.OnSCP079Exp;
Handlers.Player.Verified += PlayerEvents.OnPlayerVerified;
Handlers.Player.ChangingRole += PlayerEvents.OnSetClass;
Handlers.Player.Shooting += PlayerEvents.OnPlayerShoot;
Handlers.Player.InteractingDoor += PlayerEvents.OnDoorInteract;
Handlers.Scp914.Activating += PlayerEvents.On914Activate;
Handlers.Scp914.ChangingKnobSetting += PlayerEvents.On914Change;
Handlers.Player.InteractingLocker += PlayerEvents.OnLockerInteract;
Handlers.Player.DroppingItem += PlayerEvents.OnDropItem;
Handlers.Scp079.GainingExperience += PlayerEvents.OnSCP079Exp;
Handlers.Player.Spawning += PlayerEvents.OnSpawning;
Handlers.Server.RoundStarted += PlayerEvents.OnRoundStarted;

Log.Info($"UltimateAFK plugin loaded.\n Written by Thomasjosif for King's Playground");
}
catch (Exception e)
{
Log.Error($"There was an error loading the plugin: {e}");
}
Log.Info($"UltimateAFK plugin loaded.\n Written by Thomasjosif for King's Playground");
}
catch (Exception e)
{
Log.Error($"There was an error loading the plugin: {e}");
}

}
public override void OnDisabled()
{
base.OnDisabled();
Handlers.Player.Verified -= PlayerEvents.OnPlayerVerified;
Handlers.Player.ChangingRole -= PlayerEvents.OnSetClass;
Handlers.Player.Shooting -= PlayerEvents.OnPlayerShoot;
Handlers.Player.InteractingDoor -= PlayerEvents.OnDoorInteract;
Handlers.Scp914.Activating -= PlayerEvents.On914Activate;
Handlers.Scp914.ChangingKnobSetting -= PlayerEvents.On914Change;
Handlers.Player.InteractingLocker -= PlayerEvents.OnLockerInteract;
Handlers.Player.ItemDropped -= PlayerEvents.OnDropItem;
Handlers.Scp079.GainingExperience -= PlayerEvents.OnSCP079Exp;
}
public override void OnDisabled()
{
base.OnDisabled();
Handlers.Player.Verified -= PlayerEvents.OnPlayerVerified;
Handlers.Player.ChangingRole -= PlayerEvents.OnSetClass;
Handlers.Player.Shooting -= PlayerEvents.OnPlayerShoot;
Handlers.Player.InteractingDoor -= PlayerEvents.OnDoorInteract;
Handlers.Scp914.Activating -= PlayerEvents.On914Activate;
Handlers.Scp914.ChangingKnobSetting -= PlayerEvents.On914Change;
Handlers.Player.InteractingLocker -= PlayerEvents.OnLockerInteract;
Handlers.Player.DroppingItem -= PlayerEvents.OnDropItem;
Handlers.Scp079.GainingExperience -= PlayerEvents.OnSCP079Exp;
Handlers.Player.Spawning -= PlayerEvents.OnSpawning;
Handlers.Server.RoundStarted -= PlayerEvents.OnRoundStarted;

PlayerEvents = null;
}
}
PlayerEvents = null;
}
}
}
61 changes: 54 additions & 7 deletions UltimateAFK/PlayerEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
using Exiled.Permissions.Extensions;
using Exiled.Loader;
using System.Linq;
using System.Collections.Generic;
using MEC;

namespace UltimateAFK
{
public class PlayerEvents
{
public class PlayerEvents
{
public MainClass plugin;

internal static Dictionary<Player, AFKData> ReplacingPlayers = new Dictionary<Player, AFKData>();

public PlayerEvents(MainClass plugin)
{
this.plugin = plugin;
Expand All @@ -33,14 +37,21 @@ public void OnSetClass(ChangingRoleEventArgs ev)
AFKComponent afkComponent = ev.Player.GameObject.gameObject.GetComponent<AFKComponent>();

if (afkComponent != null)
{
{
if (!plugin.Config.IgnorePermissionsAndIP)
if (ev.Player.CheckPermission("uafk.ignore") || ev.Player.IPAddress == "127.0.0.1") //127.0.0.1 is sometimes used for "Pets" which causes issues
afkComponent.disabled = true;
if (IsGhost(ev.Player))
afkComponent.disabled = true;
afkComponent.disabled = true;
}

if (ReplacingPlayers.ContainsKey(ev.Player))
{
AFKData data = ReplacingPlayers[ev.Player];
ev.Items.Clear();
ev.Items.AddRange(data.items);
}


}
catch (Exception e)
Expand Down Expand Up @@ -110,7 +121,7 @@ public void OnLockerInteract(InteractingLockerEventArgs ev)
Log.Error($"ERROR In OnLockerInteract(): {e}");
}
}
public void OnDropItem(ItemDroppedEventArgs ev)
public void OnDropItem(DroppingItemEventArgs ev)
{
try
{
Expand All @@ -134,6 +145,42 @@ public void OnSCP079Exp(GainingExperienceEventArgs ev)
}
}

public void OnRoundStarted()
{
ReplacingPlayers.Clear();
}

public void OnSpawning(SpawningEventArgs ev)
{
if (ReplacingPlayers.ContainsKey(ev.Player))
{
AFKData data = ReplacingPlayers[ev.Player];
data.afkComp.PlayerToReplace = null;
ev.Position = data.spawnLocation;
ev.Player.Broadcast(10, $"{plugin.Config.MsgPrefix} {plugin.Config.MsgReplace}");
Timing.CallDelayed(0.4f, () =>
{
Assembly easyEvents = Loader.Plugins.FirstOrDefault(pl => pl.Name == "EasyEvents")?.Assembly;
ev.Player.Health = data.health;
foreach (ItemType ammoType in data.ammo.Keys)
{
ev.Player.Inventory.UserInventory.ReserveAmmo[ammoType] = data.ammo[ammoType];
ev.Player.Inventory.SendAmmoNextFrame = true;
}

if (data.is079)
{
ev.Player.Level = data.level;
ev.Player.Experience = data.xp;
ev.Player.Energy = data.energy;
}

if (data.roleEasyEvents != null) easyEvents?.GetType("EasyEvents.CustomRoles")?.GetMethod("ChangeRole")?.Invoke(null, new object[] { ev.Player, data.roleEasyEvents });
PlayerEvents.ReplacingPlayers.Remove(ev.Player);
});
}
}

/// <summary>
/// Reset the AFK time of a player.
/// Thanks iopietro!
Expand All @@ -149,7 +196,7 @@ public void ResetAFKTime(Player player)

if (afkComponent != null)
afkComponent.AFKTime = 0;

}
catch (Exception e)
{
Expand Down
Loading