The errors you're encountering are due to missing references to the Exiled API assemblies in your project. The compiler cannot find the types ChangingRoleEventArgs, DiedEventArgs, and LeftEventArgs because they are part of the Exiled API, and your project doesn't have a reference to it.
To fix your code and eliminate these errors, you need to ensure that your project references the Exiled API correctly. Here's how you can do it:
-
Target the Correct .NET Framework Version:
Ensure that your project is targeting .NET Framework 4.7.2, as Exiled plugins require this version.
- Right-click on your project in Visual Studio's Solution Explorer.
- Select Properties.
- In the Application tab, set the Target framework to .NET Framework 4.7.2.
-
Add Exiled API via NuGet Package Manager:
You need to add the Exiled API to your project using NuGet. This will automatically include all the necessary assemblies and ensure compatibility.
-
Right-click on your project in the Solution Explorer.
-
Select Manage NuGet Packages...
-
In the Browse tab, search for Exiled-API.
-
Install the version that matches your server's Exiled version. For example, if you're using Exiled 5.2.2, install version 5.2.2 of Exiled-API.
[image: NuGet Package Manager] open
-
Confirm the installation and accept any license agreements.
-
Check Your using Directives:
Ensure that you have the correct using directives at the top of your code file. Specifically, you need using Exiled.Events.EventArgs; for the event argument types.
using Exiled.Events.EventArgs;
Your using directives should look like this:
using System;
using System.Collections.Generic;
using System.Threading;
using Exiled.API.Features;
using Exiled.API.Interfaces;
using Exiled.Events.EventArgs;
using PlayerRoles;
-
Rebuild Your Project:
- Save all your changes.
- Clean your solution:
- In Visual Studio, go to Build > Clean Solution.
- Rebuild your solution:
- Go to Build > Rebuild Solution.
-
Review Your Event Handlers:
Make sure your event handler methods are correctly defined. Here's your updated code with the necessary references:
using System;
using System.Collections.Generic;
using System.Threading;
using Exiled.API.Features;
using Exiled.API.Interfaces;
using Exiled.Events.EventArgs;
using PlayerRoles;
namespace DClassInfoDisplay
{
public class DClassInfoDisplayPlugin : Plugin<Config>
{
public override string Name => "DClassInfoDisplay";
public override string Author => "NOT_MR";
public override Version Version => new Version(1, 0, 1);
public override string Prefix => "dclassinfodisplay";
public EventHandlers EventHandlers;
public override void OnEnabled()
{
EventHandlers = new EventHandlers(this);
Exiled.Events.Handlers.Player.ChangingRole += EventHandlers.OnChangingRole;
Exiled.Events.Handlers.Player.Died += EventHandlers.OnDied;
Exiled.Events.Handlers.Player.Left += EventHandlers.OnLeft;
Exiled.Events.Handlers.Server.RestartingRound += EventHandlers.OnRestartingRound;
base.OnEnabled();
}
public override void OnDisabled()
{
Exiled.Events.Handlers.Player.ChangingRole -= EventHandlers.OnChangingRole;
Exiled.Events.Handlers.Player.Died -= EventHandlers.OnDied;
Exiled.Events.Handlers.Player.Left -= EventHandlers.OnLeft;
Exiled.Events.Handlers.Server.RestartingRound -= EventHandlers.OnRestartingRound;
EventHandlers.StopAllTimers();
EventHandlers = null;
base.OnDisabled();
}
}
public class EventHandlers
{
private readonly DClassInfoDisplayPlugin plugin;
private readonly Dictionary<Player, Timer> playerTimers = new Dictionary<Player, Timer>();
public EventHandlers(DClassInfoDisplayPlugin plugin) => this.plugin = plugin;
public void OnChangingRole(ChangingRoleEventArgs ev)
{
if (ev.NewRole == RoleTypeId.ClassD)
{
StartInfoTimer(ev.Player);
}
else
{
StopInfoTimer(ev.Player);
}
}
public void OnDied(DiedEventArgs ev) => StopInfoTimer(ev.Player);
public void OnLeft(LeftEventArgs ev) => StopInfoTimer(ev.Player);
public void OnRestartingRound() => StopAllTimers();
private void StartInfoTimer(Player player)
{
StopInfoTimer(player);
Timer timer = new Timer(_ => ShowInfo(player), null, TimeSpan.Zero, TimeSpan.FromSeconds(1));
playerTimers[player] = timer;
}
private void StopInfoTimer(Player player)
{
if (playerTimers.TryGetValue(player, out Timer timer))
{
timer.Dispose();
playerTimers.Remove(player);
}
}
public void StopAllTimers()
{
foreach (var timer in playerTimers.Values)
{
timer.Dispose();
}
playerTimers.Clear();
}
private void ShowInfo(Player player)
{
if (player == null || !player.IsAlive || player.Role != RoleTypeId.ClassD)
{
StopInfoTimer(player);
return;
}
string message = $"<size=20><align=left>\n" +
$"<color=#FFA500><b>Имя:</b></color> {player.Nickname}\n" +
$"<color=#FFA500><b>Роль:</b></color> {player.Role}\n" +
$"<color=#FFA500><b>Ваши враги:</b></color> Охрана, МТФ, SCP\n" +
$"<color=#FFA500><b>Местоположение:</b></color> {player.CurrentRoom?.Name ?? "Неизвестно"}</align></size>";
player.ShowHint(message, 1);
}
}
public class Config : IConfig
{
public bool IsEnabled { get; set; } = true;
public bool Debug { get; set; } = false;
}
}
-
Ensure Exiled API Compatibility:
Make sure that the version of the Exiled API you're referencing matches the version used on your server. Using mismatched versions can lead to missing types or methods.
-
Verify Event Argument Names:
If you're still experiencing issues, double-check the names of the event argument classes. In some versions, event argument names might have changed. For example, in Exiled API version 5.x, the event arguments are named ChangingRoleEventArgs, DiedEventArgs, and LeftEventArgs.
Make sure that your event handler methods use the correct event argument types provided by the Exiled API you're referencing.
// Correct event handlers with the right event argument types
public void OnChangingRole(ChangingRoleEventArgs ev) { /* ... */ }
public void OnDied(DiedEventArgs ev) { /* ... */ }
public void OnLeft(LeftEventArgs ev) { /* ... */ }
-
Rebuild and Test Your Plugin:
After making these changes and ensuring that all references are correctly set up, rebuild your project again. If the build succeeds without errors, you can proceed to test your plugin on your server.
By following these steps, you should resolve the compilation errors and get your plugin working without issues. Remember, the key is to ensure that all necessary dependencies are correctly referenced in your project, and that you're using the correct versions that match the API's expectations.