Skip to content

Commit

Permalink
Merge pull request #7 from steven4547466/master
Browse files Browse the repository at this point in the history
Expose player to replace for plugin devs. Update to Exiled 2.1.14
  • Loading branch information
thomasjosif authored Nov 10, 2020
2 parents ce1f00a + 409c60d commit a5bf031
Show file tree
Hide file tree
Showing 106 changed files with 8,415 additions and 79 deletions.
68 changes: 44 additions & 24 deletions UltimateAFK/AFKComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Exiled.Loader;
using PlayableScps;
using scp035.API;
using System.Reflection;

namespace UltimateAFK
{
Expand All @@ -31,6 +32,10 @@ public class AFKComponent : MonoBehaviour
private Player TryGet035() => Scp035Data.GetScp035();
private void TrySpawn035(Player player) => Scp035Data.Spawn035(player);

// Expose replacing player for plugin support
public Player PlayerToReplace;


void Awake()
{
ply = Player.Get(gameObject);
Expand All @@ -43,7 +48,16 @@ void Update()
{
timer = 0f;
if (!this.disabled)
AFKChecker();
{
try
{
AFKChecker();
}
catch (Exception e)
{
Log.Error(e);
}
}
}
}

Expand Down Expand Up @@ -73,6 +87,7 @@ private void AFKChecker()
this.AFKLastPosition = CurrentPos;
this.AFKLastAngle = CurrentAngle;
this.AFKTime = 0;
PlayerToReplace = null;
return;
}

Expand Down Expand Up @@ -101,9 +116,11 @@ private void AFKChecker()
// Let's make sure they are still alive before doing any replacement.
if (this.ply.Team == Team.RIP) return;

if (plugin.Config.TryReplace && !this.IsPastReplaceTime())
if (plugin.Config.TryReplace && !IsPastReplaceTime())
{
var roleEasyEvents = Loader.Plugins.FirstOrDefault(pl => pl.Name == "EasyEvents")?.Assembly.GetType("EasyEvents.Util")?.GetMethod("GetRole")?.Invoke(null, new object[] { this.ply });
Assembly easyEvents = Loader.Plugins.FirstOrDefault(pl => pl.Name == "EasyEvents")?.Assembly;

var roleEasyEvents = easyEvents?.GetType("EasyEvents.Util")?.GetMethod("GetRole")?.Invoke(null, new object[] { this.ply });

// SCP035 Support (Credit DCReplace)
bool is035 = false;
Expand Down Expand Up @@ -142,69 +159,72 @@ private void AFKChecker()
AP079 = this.ply.Energy;
}

Player player = Player.List.FirstOrDefault(x => x.Role == RoleType.Spectator && x.UserId != string.Empty && !x.IsOverwatchEnabled && x != this.ply);
if (player != null)
PlayerToReplace = Player.List.FirstOrDefault(x => x.Role == RoleType.Spectator && x.UserId != string.Empty && !x.IsOverwatchEnabled && x != this.ply);
if (PlayerToReplace != null)
{
player.SetRole(role);
// Make the player a spectator first so other plugins can do things on player changing role with uAFK.
this.ply.Inventory.Clear(); // Clear their items to prevent dupes.
this.ply.SetRole(RoleType.Spectator);
this.ply.Broadcast(30, $"{plugin.Config.MsgPrefix} {plugin.Config.MsgFspec}");

PlayerToReplace.SetRole(role);

Timing.CallDelayed(0.3f, () =>
{
if (is035)
{
try
{
TrySpawn035(player);
TrySpawn035(PlayerToReplace);
}
catch (Exception e)
{
Log.Debug($"SCP-035 is not installed, skipping method call: {e}");
}
}
player.Position = pos;
player.Inventory.Clear();
PlayerToReplace.Position = pos;
PlayerToReplace.Inventory.Clear();

foreach (Inventory.SyncItemInfo item in items)
{
player.Inventory.AddNewItem(item.id, item.durability, item.modSight, item.modBarrel, item.modOther);
PlayerToReplace.Inventory.AddNewItem(item.id, item.durability, item.modSight, item.modBarrel, item.modOther);
}

player.Health = health;
PlayerToReplace.Health = health;

foreach (Exiled.API.Enums.AmmoType atype in (Exiled.API.Enums.AmmoType[])Enum.GetValues(typeof(Exiled.API.Enums.AmmoType)))
{
uint amount;
if (ammo.TryGetValue(atype, out amount))
{
this.ply.Ammo[(int)atype] = amount;
PlayerToReplace.Ammo[(int)atype] = amount;
}
else
Log.Error($"[uAFK] ERROR: Tried to get a value from dict that did not exist! (Ammo)");
}

if (isScp079)
{
player.Level = Level079;
player.Experience = Exp079;
player.Energy = AP079;
PlayerToReplace.Level = Level079;
PlayerToReplace.Experience = Exp079;
PlayerToReplace.Energy = AP079;
}

player.Broadcast(10, $"{plugin.Config.MsgPrefix} {plugin.Config.MsgReplace}");
if (roleEasyEvents != null) Loader.Plugins.FirstOrDefault(pl => pl.Name == "EasyEvents")?.Assembly.GetType("EasyEvents.CustomRoles")?.GetMethod("ChangeRole")?.Invoke(null, new object[] { player, roleEasyEvents });

this.ply.Inventory.Clear(); // Clear their items to prevent dupes.
this.ply.SetRole(RoleType.Spectator);
this.ply.Broadcast(30, $"{plugin.Config.MsgPrefix} {plugin.Config.MsgFspec}");
PlayerToReplace.Broadcast(10, $"{plugin.Config.MsgPrefix} {plugin.Config.MsgReplace}");
if (roleEasyEvents != null) easyEvents?.GetType("EasyEvents.CustomRoles")?.GetMethod("ChangeRole")?.Invoke(null, new object[] { PlayerToReplace, roleEasyEvents });
PlayerToReplace = null;
});
}
else
{
// Couldn't find a valid player to spawn, just ForceToSpec anyways.
this.ForceToSpec(this.ply);
ForceToSpec(this.ply);
}
}
else
{
// Replacing is disabled, just ForceToSpec
this.ForceToSpec(this.ply);
ForceToSpec(this.ply);
}
// If it's -1 we won't be kicking at all.
if (plugin.Config.NumBeforeKick != -1)
Expand All @@ -213,7 +233,7 @@ private void AFKChecker()
this.AFKCount++;
if (this.AFKCount >= plugin.Config.NumBeforeKick)
{
// Since AFKCount is greater than the config we're going to kick that player for being AFK too many times in one match.
// Since this.AFKCount is greater than the config we're going to kick that player for being AFK too many times in one match.
ServerConsole.Disconnect(this.gameObject, plugin.Config.MsgKick);
}
}
Expand Down
2 changes: 1 addition & 1 deletion UltimateAFK/MainClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ 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, 0, 4);
public override Version Version { get; } = new Version(3, 1, 0);
public override Version RequiredExiledVersion { get; } = new Version(2, 0, 0);
public PlayerEvents PlayerEvents;

Expand Down
4 changes: 2 additions & 2 deletions UltimateAFK/PlayerEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public void OnSetClass(ChangingRoleEventArgs ev)
{
if (ev.Player == null) return;
AFKComponent afkComponent = ev.Player.GameObject.gameObject.GetComponent<AFKComponent>();

if (afkComponent != null)
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;

}
catch (Exception e)
{
Expand Down
2 changes: 1 addition & 1 deletion UltimateAFK/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyVersion("3.1.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
52 changes: 31 additions & 21 deletions UltimateAFK/UltimateAFK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<PlatformTarget>x64</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
Expand All @@ -46,52 +46,62 @@
<WCFMetadata Include="Connected Services\" />
</ItemGroup>
<ItemGroup>
<Reference Include="0Harmony">
<Reference Include="0Harmony, Version=2.0.3.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\EXILED_REFERENCES\0Harmony.dll</HintPath>
</Reference>
<Reference Include="Assembly-CSharp-firstpass">
<Reference Include="Assembly-CSharp-firstpass, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\EXILED_REFERENCES\Assembly-CSharp-firstpass.dll</HintPath>
</Reference>
<Reference Include="Assembly-CSharp-Publicized">
<HintPath>..\..\EXILED_REFERENCES\Assembly-CSharp-Publicized.dll</HintPath>
</Reference>
<Reference Include="CommandSystem.Core">
<Reference Include="CommandSystem.Core, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\EXILED_REFERENCES\CommandSystem.Core.dll</HintPath>
</Reference>
<Reference Include="Exiled.API, Version=2.1.9.0, Culture=neutral, processorArchitecture=AMD64">
<HintPath>..\packages\EXILED.2.1.9\lib\net472\Exiled.API.dll</HintPath>
<Reference Include="Exiled.API, Version=2.1.14.0, Culture=neutral, processorArchitecture=AMD64">
<HintPath>..\packages\EXILED.2.1.14\lib\net472\Exiled.API.dll</HintPath>
</Reference>
<Reference Include="Exiled.Bootstrap, Version=2.1.9.0, Culture=neutral, processorArchitecture=AMD64">
<HintPath>..\packages\EXILED.2.1.9\lib\net472\Exiled.Bootstrap.dll</HintPath>
<Reference Include="Exiled.Bootstrap, Version=2.1.14.0, Culture=neutral, processorArchitecture=AMD64">
<HintPath>..\packages\EXILED.2.1.14\lib\net472\Exiled.Bootstrap.dll</HintPath>
</Reference>
<Reference Include="Exiled.Events, Version=2.1.9.0, Culture=neutral, processorArchitecture=AMD64">
<HintPath>..\packages\EXILED.2.1.9\lib\net472\Exiled.Events.dll</HintPath>
<Reference Include="Exiled.Events, Version=2.1.14.0, Culture=neutral, processorArchitecture=AMD64">
<HintPath>..\packages\EXILED.2.1.14\lib\net472\Exiled.Events.dll</HintPath>
</Reference>
<Reference Include="Exiled.Loader, Version=2.1.9.0, Culture=neutral, processorArchitecture=AMD64">
<HintPath>..\packages\EXILED.2.1.9\lib\net472\Exiled.Loader.dll</HintPath>
<Reference Include="Exiled.Loader, Version=2.1.14.0, Culture=neutral, processorArchitecture=AMD64">
<HintPath>..\packages\EXILED.2.1.14\lib\net472\Exiled.Loader.dll</HintPath>
</Reference>
<Reference Include="Exiled.Permissions, Version=2.1.9.0, Culture=neutral, processorArchitecture=AMD64">
<HintPath>..\packages\EXILED.2.1.9\lib\net472\Exiled.Permissions.dll</HintPath>
<Reference Include="Exiled.Permissions, Version=2.1.14.0, Culture=neutral, processorArchitecture=AMD64">
<HintPath>..\packages\EXILED.2.1.14\lib\net472\Exiled.Permissions.dll</HintPath>
</Reference>
<Reference Include="Exiled.Updater, Version=3.0.3.0, Culture=neutral, processorArchitecture=AMD64">
<HintPath>..\packages\EXILED.2.1.9\lib\net472\Exiled.Updater.dll</HintPath>
<HintPath>..\packages\EXILED.2.1.14\lib\net472\Exiled.Updater.dll</HintPath>
</Reference>
<Reference Include="Mirror">
<Reference Include="Mirror, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\EXILED_REFERENCES\Mirror.dll</HintPath>
</Reference>
<Reference Include="scp035">
<Reference Include="scp035, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\EXILED_REFERENCES\scp035.dll</HintPath>
</Reference>
<Reference Include="UnityEngine">
<Reference Include="System" />
<Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\EXILED_REFERENCES\UnityEngine.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.CoreModule">
<Reference Include="UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\EXILED_REFERENCES\UnityEngine.CoreModule.dll</HintPath>
</Reference>
<Reference Include="YamlDotNet">
<Reference Include="YamlDotNet, Version=8.0.0.0, Culture=neutral, PublicKeyToken=ec19458f3c15af5e, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\EXILED_REFERENCES\YamlDotNet.dll</HintPath>
</Reference>
<Reference Include="zxing.unity">
<Reference Include="zxing.unity, Version=0.16.4.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\EXILED_REFERENCES\zxing.unity.dll</HintPath>
</Reference>
</ItemGroup>
Expand Down
Binary file added UltimateAFK/bin/Release/0Harmony.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added UltimateAFK/bin/Release/Exiled.API.dll
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added UltimateAFK/bin/Release/Exiled.Bootstrap.dll
Binary file not shown.
Binary file added UltimateAFK/bin/Release/Exiled.Events.dll
Binary file not shown.
Loading

0 comments on commit a5bf031

Please sign in to comment.