Skip to content

Commit

Permalink
RocketMod 5.
Browse files Browse the repository at this point in the history
  • Loading branch information
Trojaner committed May 30, 2018
1 parent 1544fbb commit d20be96
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 134 deletions.
182 changes: 91 additions & 91 deletions ReservedSlots.cs
Original file line number Diff line number Diff line change
@@ -1,148 +1,148 @@
using Rocket.Core.Logging;
using System.Linq;
using Rocket.Core.Logging;
using Rocket.Core.Plugins;
using Rocket.Unturned.Permissions;
using SDG.Unturned;
using Steamworks;
using Rocket.Core;
using Rocket.API;
using Rocket.API.Serialisation;
using Rocket.Core.RCON;
using Rocket.Unturned.Chat;
using Rocket.Unturned;
using Rocket.API.DependencyInjection;
using Rocket.API.Eventing;
using Rocket.API.Permissions;
using Rocket.API.Player;
using Rocket.Core.Player.Events;
using Rocket.Unturned.Player;
using Rocket.Unturned.Player.Events;

namespace ReservedSlots
{
public class ReservedSlots : RocketPlugin<ReservedSlotsConfig>
public class ReservedSlots : Plugin<ReservedSlotsConfig>,
IEventListener<UnturnedPlayerPreConnectEvent>,
IEventListener<PlayerConnectedEvent>,
IEventListener<PlayerDisconnectedEvent>
{
public static ReservedSlots Instance;
private byte lastMaxSlotCount;
public ReservedSlots(IDependencyContainer container) : base("ReservedSlots", container)
{
}

private byte _lastMaxSlotCount;

protected override void Load()
protected override void OnLoad(bool isFromReload)
{
Instance = this;
UnturnedPermissions.OnJoinRequested += Events_OnJoinRequested;
Logger.Log(string.Format("Reserved Slots enabled: {0}, Count: {1}, Allowfill: {2}, DynamicSlots: {3}, min: {4}, max:{5}.", Instance.Configuration.Instance.ReservedSlotEnable, Instance.Configuration.Instance.ReservedSlotCount, Instance.Configuration.Instance.AllowFill, Instance.Configuration.Instance.AllowDynamicMaxSlot, Instance.Configuration.Instance.MinSlotCount, Instance.Configuration.Instance.MaxSlotCount));
if (Instance.Configuration.Instance.AllowDynamicMaxSlot)
Logger.Log(
$"Reserved Slots enabled: {ConfigurationInstance.ReservedSlotEnable}, Count: {ConfigurationInstance.ReservedSlotCount}, Allowfill: {ConfigurationInstance.AllowFill}, DynamicSlots: {ConfigurationInstance.AllowDynamicMaxSlot}, min: {ConfigurationInstance.MinSlotCount}, max:{ConfigurationInstance.MaxSlotCount}.");
if (ConfigurationInstance.AllowDynamicMaxSlot)
{
if (Instance.Configuration.Instance.MinSlotCount < 2)
if (ConfigurationInstance.MinSlotCount < 2)
{
Logger.LogError("Reserved Slots Config Error: Minimum slots is set to 0, changing to the Unturned server default of 8 slots.");
Instance.Configuration.Instance.MinSlotCount = 8;
ConfigurationInstance.MinSlotCount = 8;
}
if (Instance.Configuration.Instance.MaxSlotCount > 48)
if (ConfigurationInstance.MaxSlotCount > 48)
{
Logger.LogWarning("Reserved Slots Config Error: Maximum slots is set to something higher than 48, limiting to 48.");
Instance.Configuration.Instance.MaxSlotCount = 48;
ConfigurationInstance.MaxSlotCount = 48;
}
if (Instance.Configuration.Instance.MaxSlotCount < Instance.Configuration.Instance.MinSlotCount)
if (ConfigurationInstance.MaxSlotCount < ConfigurationInstance.MinSlotCount)
{
Logger.LogError("Reserved Slots Config Error: Max slot count is less than initial slot count, Setting max slot count to min slot count + reserved slots, or max slot count, if over 48.");
byte tmp = (byte)(Instance.Configuration.Instance.MinSlotCount + Instance.Configuration.Instance.ReservedSlotCount);
Instance.Configuration.Instance.MaxSlotCount = tmp > Instance.Configuration.Instance.MaxSlotCount ? Instance.Configuration.Instance.MaxSlotCount : tmp;
byte tmp = (byte)(ConfigurationInstance.MinSlotCount + ConfigurationInstance.ReservedSlotCount);
ConfigurationInstance.MaxSlotCount = tmp > ConfigurationInstance.MaxSlotCount ? ConfigurationInstance.MaxSlotCount : tmp;
}
SetMaxPlayers();
U.Events.OnPlayerConnected += Events_OnPlayerConnected;
U.Events.OnPlayerDisconnected += Events_OnPlayerDisconnected;
}
UnturnedPermissions.OnJoinRequested += Events_OnJoinRequested;
Instance.Configuration.Save();
}

protected override void Unload()
{
UnturnedPermissions.OnJoinRequested -= Events_OnJoinRequested;
if (Instance.Configuration.Instance.AllowDynamicMaxSlot)
{
U.Events.OnPlayerConnected -= Events_OnPlayerConnected;
U.Events.OnPlayerDisconnected -= Events_OnPlayerDisconnected;
}
SaveConfiguration();
}

private void SetMaxPlayers(bool onDisconnect = false)
{
// Minus one if it is coming from disconnect, they are still accounted towards the total player count at this time.
int curPlayerNum = Provider.clients.Count - (onDisconnect ? 1 : 0);
byte curPlayerMax = Provider.maxPlayers;
if (curPlayerNum + Instance.Configuration.Instance.ReservedSlotCount < Instance.Configuration.Instance.MinSlotCount)
curPlayerMax = Instance.Configuration.Instance.MinSlotCount;
else if (curPlayerNum + Instance.Configuration.Instance.ReservedSlotCount > Instance.Configuration.Instance.MaxSlotCount)
curPlayerMax = Instance.Configuration.Instance.MaxSlotCount;
else if (curPlayerNum + Instance.Configuration.Instance.ReservedSlotCount >= Instance.Configuration.Instance.MinSlotCount && curPlayerNum + Instance.Configuration.Instance.ReservedSlotCount <= Instance.Configuration.Instance.MaxSlotCount)
if (curPlayerNum + ConfigurationInstance.ReservedSlotCount < ConfigurationInstance.MinSlotCount)
curPlayerMax = ConfigurationInstance.MinSlotCount;
else if (curPlayerNum + ConfigurationInstance.ReservedSlotCount > ConfigurationInstance.MaxSlotCount)
curPlayerMax = ConfigurationInstance.MaxSlotCount;
else if (curPlayerNum + ConfigurationInstance.ReservedSlotCount >= ConfigurationInstance.MinSlotCount && curPlayerNum + ConfigurationInstance.ReservedSlotCount <= ConfigurationInstance.MaxSlotCount)
{
curPlayerMax = (byte)(curPlayerNum + Instance.Configuration.Instance.ReservedSlotCount);
if (curPlayerMax > lastMaxSlotCount)
UnturnedChat.Say(CSteamID.Nil, "Max slots increased to: " + curPlayerMax);
if (curPlayerMax < lastMaxSlotCount)
UnturnedChat.Say(CSteamID.Nil, "Max slots Decreased to: " + curPlayerMax);
curPlayerMax = (byte)(curPlayerNum + ConfigurationInstance.ReservedSlotCount);
}
if (lastMaxSlotCount != curPlayerMax)
if (_lastMaxSlotCount != curPlayerMax)
{
Provider.maxPlayers = curPlayerMax;
lastMaxSlotCount = curPlayerMax;
_lastMaxSlotCount = curPlayerMax;
}
}

private void Events_OnJoinRequested(CSteamID CSteamID, ref ESteamRejection? rejectionReason)
private bool CheckReserved(UnturnedPlayer player)
{
if (Instance.Configuration.Instance.ReservedSlotEnable && Instance.Configuration.Instance.ReservedSlotCount > 0 && Instance.Configuration.Instance.Groups != null && Instance.Configuration.Instance.Groups.Count > 0)
if (SteamAdminlist.checkAdmin(player.CSteamID))
{
return true;
}

var permissionProvider = Container.Resolve<IPermissionProvider>();

foreach (var group in permissionProvider.GetGroups(player))
{
int numPlayers = Provider.clients.Count;
byte maxPlayers = Provider.maxPlayers;
// Run slot fill calculations, if it is enabled.
if (Instance.Configuration.Instance.AllowFill)
if (ConfigurationInstance.Groups.Contains(@group.Id))
{
foreach (SteamPlayer player in Provider.clients)
{
if (CheckReserved(player.playerID.steamID))
{
numPlayers--;
}
}
return true;
}
}
return false;
}

public void HandleEvent(IEventEmitter emitter, UnturnedPlayerPreConnectEvent @event)
{
var player = @event.Player as UnturnedPlayer;
if (player == null)
return;

if (!ConfigurationInstance.ReservedSlotEnable || ConfigurationInstance.ReservedSlotCount <= 0 ||
ConfigurationInstance.Groups == null || ConfigurationInstance.Groups.Length <= 0) return;

int numPlayers = Provider.clients.Count;
byte maxPlayers = Provider.maxPlayers;

// Check to see if dynamic slots are enabled, and adjust the max slot count on the server if they are.
if ((!Instance.Configuration.Instance.AllowDynamicMaxSlot && numPlayers + Instance.Configuration.Instance.ReservedSlotCount >= maxPlayers) || (Instance.Configuration.Instance.AllowDynamicMaxSlot && numPlayers + Instance.Configuration.Instance.ReservedSlotCount >= Instance.Configuration.Instance.MaxSlotCount))
var playerManager = Container.Resolve<IPlayerManager>();

// Run slot fill calculations, if it is enabled.
if (ConfigurationInstance.AllowFill)
{
foreach (var onlinePlayer in playerManager.OnlinePlayers)
{
// Kick if they aren't a reserved player.
if (!CheckReserved(CSteamID))
if (CheckReserved((UnturnedPlayer)onlinePlayer))
{
rejectionReason = ESteamRejection.SERVER_FULL;
numPlayers--;
}
}
}

// Check to see if dynamic slots are enabled, and adjust the max slot count on the server if they are.
if ((!ConfigurationInstance.AllowDynamicMaxSlot && numPlayers + ConfigurationInstance.ReservedSlotCount >= maxPlayers) || (ConfigurationInstance.AllowDynamicMaxSlot && numPlayers + ConfigurationInstance.ReservedSlotCount >= ConfigurationInstance.MaxSlotCount))
{
// Kick if they aren't a reserved player.
if (!CheckReserved(player))
{
@event.IsCancelled = true;
@event.UnturnedRejectionReason = ESteamRejection.SERVER_FULL;
}
}
}

// Adjust the max player count on player connect and disconnect, if the dynamic slots feature is enabled.
private void Events_OnPlayerConnected(UnturnedPlayer player)
public void HandleEvent(IEventEmitter emitter, PlayerConnectedEvent @event)
{
if (Instance.Configuration.Instance.AllowDynamicMaxSlot)
SetMaxPlayers();
}
if (!ConfigurationInstance.AllowDynamicMaxSlot)
return;

private void Events_OnPlayerDisconnected(UnturnedPlayer player)
{
if (Instance.Configuration.Instance.AllowDynamicMaxSlot)
SetMaxPlayers(true);
SetMaxPlayers();
}

private bool CheckReserved(CSteamID CSteamID)
public void HandleEvent(IEventEmitter emitter, PlayerDisconnectedEvent @event)
{
if (SteamAdminlist.checkAdmin(CSteamID))
{
return true;
}
else
{
foreach (RocketPermissionsGroup group in R.Permissions.GetGroups(new RocketPlayer(CSteamID.ToString()), true))
{
if (Instance.Configuration.Instance.Groups.Contains(group.Id))
{
return true;
}
}
return false;
}
if (!ConfigurationInstance.AllowDynamicMaxSlot)
return;

SetMaxPlayers(true);
}
}
}
63 changes: 40 additions & 23 deletions ReservedSlots.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,50 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Assembly-CSharp, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>lib\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
<HintPath>packages\Rocket.Unturned.5.0.0.135\lib\net35\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="Assembly-CSharp-firstpass, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>lib\Assembly-CSharp-firstpass.dll</HintPath>
<Private>False</Private>
<HintPath>packages\Rocket.Unturned.5.0.0.135\lib\net35\Assembly-CSharp-firstpass.dll</HintPath>
</Reference>
<Reference Include="Rocket.API, Version=2.6.2.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>lib\Rocket.API.dll</HintPath>
<Private>False</Private>
<Reference Include="ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
<HintPath>packages\Rocket.Core.5.0.0.485\lib\net35\ICSharpCode.SharpZipLib.dll</HintPath>
</Reference>
<Reference Include="Rocket.Core, Version=2.4.6.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>lib\Rocket.Core.dll</HintPath>
<Private>False</Private>
<Reference Include="Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Rocket.Core.5.0.0.485\lib\net35\Microsoft.Practices.ServiceLocation.dll</HintPath>
</Reference>
<Reference Include="Rocket.Unturned, Version=4.9.5.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>lib\Rocket.Unturned.dll</HintPath>
<Private>False</Private>
<Reference Include="Microsoft.Practices.Unity, Version=2.1.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Rocket.Core.5.0.0.485\lib\net35\Microsoft.Practices.Unity.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Practices.Unity.Configuration, Version=2.1.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Rocket.Core.5.0.0.485\lib\net35\Microsoft.Practices.Unity.Configuration.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Rocket.Core.5.0.0.485\lib\net35\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="RestSharp, Version=105.2.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\Rocket.Core.5.0.0.485\lib\net35\RestSharp.dll</HintPath>
</Reference>
<Reference Include="Rocket.API, Version=5.0.0.485, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\Rocket.API.5.0.0.485\lib\net35\Rocket.API.dll</HintPath>
</Reference>
<Reference Include="Rocket.Compatibility, Version=5.0.0.485, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\Rocket.Compatibility.5.0.0.485\lib\net35\Rocket.Compatibility.dll</HintPath>
</Reference>
<Reference Include="Rocket.Core, Version=5.0.0.485, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\Rocket.Core.5.0.0.485\lib\net35\Rocket.Core.dll</HintPath>
</Reference>
<Reference Include="Rocket.UnityEngine, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\Rocket.UnityEngine.5.0.0.29\lib\net35\Rocket.UnityEngine.dll</HintPath>
</Reference>
<Reference Include="Rocket.Unturned, Version=5.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\Rocket.Unturned.5.0.0.135\lib\net35\Rocket.Unturned.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization">
<HintPath>packages\Rocket.Core.5.0.0.485\lib\net35\System.Runtime.Serialization.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
Expand All @@ -75,13 +93,12 @@
<Compile Include="ReservedSlotsConfig.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="lib\Assembly-CSharp-firstpass.dll" />
<Content Include="lib\Assembly-CSharp.dll" />
<Content Include="lib\Rocket.API.dll" />
<Content Include="lib\Rocket.Core.dll" />
<Content Include="lib\Rocket.Unturned.dll" />
<Content Include="lib\UnityEngine.dll" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
34 changes: 14 additions & 20 deletions ReservedSlotsConfig.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
using Rocket.API;
using System.Collections.Generic;
using System.Xml.Serialization;
using Rocket.Core.Configuration;

namespace ReservedSlots
{
public class ReservedSlotsConfig : IRocketPluginConfiguration
public class ReservedSlotsConfig
{
public bool ReservedSlotEnable = true;
[XmlArray("Groups"), XmlArrayItem(ElementName = "Group")]
public List<string> Groups = new List<string>();
public byte ReservedSlotCount = 2;
public bool AllowFill = false;
public bool AllowDynamicMaxSlot = false;
public byte MaxSlotCount = 24;
public byte MinSlotCount = 16;
public bool ReservedSlotEnable { get; set; } = true;

public void LoadDefaults()
{
Groups = new List<string>
{
"moderator",
"admin"
};
}
[ConfigArray]
public string[] Groups { get; set; } = {
"moderator",
"admin"
};

public byte ReservedSlotCount { get; set; } = 2;
public bool AllowFill { get; set; } = false;
public bool AllowDynamicMaxSlot { get; set; } = false;
public byte MaxSlotCount { get; set; } = 24;
public byte MinSlotCount { get; set; } = 16;
}
}
11 changes: 11 additions & 0 deletions app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Binary file removed lib/Assembly-CSharp-firstpass.dll
Binary file not shown.
Binary file removed lib/Assembly-CSharp.dll
Binary file not shown.
Binary file removed lib/Rocket.API.dll
Binary file not shown.
Binary file removed lib/Rocket.Core.dll
Binary file not shown.
Binary file removed lib/Rocket.Unturned.dll
Binary file not shown.
8 changes: 8 additions & 0 deletions packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Rocket.API" version="5.0.0.485" targetFramework="net35" />
<package id="Rocket.Compatibility" version="5.0.0.485" targetFramework="net35" />
<package id="Rocket.Core" version="5.0.0.485" targetFramework="net35" />
<package id="Rocket.UnityEngine" version="5.0.0.29" targetFramework="net35" />
<package id="Rocket.Unturned" version="5.0.0.135" targetFramework="net35" />
</packages>

0 comments on commit d20be96

Please sign in to comment.