Add command permissions based on game promote level
This commit is contained in:
@@ -14,26 +14,4 @@ namespace Torch.API.ModAPI.Ingame
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static class PistonExtensions
|
||||
{
|
||||
public static IMyCubeGrid GetConnectedGrid(this IMyPistonBase pistonBase)
|
||||
{
|
||||
if (!pistonBase.IsAttached)
|
||||
return null;
|
||||
|
||||
return ((Sandbox.ModAPI.IMyPistonBase)pistonBase).TopGrid;
|
||||
}
|
||||
}
|
||||
|
||||
public static class RotorExtensions
|
||||
{
|
||||
public static IMyCubeGrid GetConnectedGrid(this IMyMotorStator rotorBase)
|
||||
{
|
||||
if (!rotorBase.IsAttached)
|
||||
return null;
|
||||
|
||||
return ((Sandbox.ModAPI.IMyMotorStator)rotorBase).RotorGrid;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -3,12 +3,14 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Torch.API;
|
||||
using Torch.Commands.Permissions;
|
||||
using VRage.Game.ModAPI;
|
||||
|
||||
namespace Torch.Commands
|
||||
{
|
||||
public class Command
|
||||
{
|
||||
public MyPromoteLevel MinimumPromoteLevel { get; }
|
||||
public string Name { get; }
|
||||
public string Description { get; }
|
||||
public string HelpText { get; }
|
||||
@@ -25,6 +27,9 @@ namespace Torch.Commands
|
||||
if (commandAttribute == null)
|
||||
throw new TypeLoadException($"Method does not have a {nameof(CommandAttribute)}");
|
||||
|
||||
var permissionAttribute = commandMethod.GetCustomAttribute<PermissionAttribute>();
|
||||
MinimumPromoteLevel = permissionAttribute?.PromoteLevel ?? MyPromoteLevel.None;
|
||||
|
||||
if (!commandMethod.DeclaringType.IsSubclassOf(typeof(CommandModule)))
|
||||
throw new TypeLoadException($"Command {commandMethod.Name}'s declaring type {commandMethod.DeclaringType.FullName} is not a subclass of {nameof(CommandModule)}");
|
||||
|
||||
|
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
using NLog;
|
||||
using Sandbox.Game.World;
|
||||
using Torch.API;
|
||||
using Torch.Managers;
|
||||
using VRage.Game.ModAPI;
|
||||
@@ -30,8 +31,8 @@ namespace Torch.Commands
|
||||
|
||||
public bool HasPermission(ulong steamId, Command command)
|
||||
{
|
||||
_log.Warn("Command permissions not implemented");
|
||||
return true;
|
||||
var userLevel = MySession.Static.GetUserPromoteLevel(steamId);
|
||||
return userLevel >= command.MinimumPromoteLevel;
|
||||
}
|
||||
|
||||
public bool IsCommand(string command)
|
||||
@@ -97,6 +98,7 @@ namespace Torch.Commands
|
||||
if (!HasPermission(msg.Author, command))
|
||||
{
|
||||
_log.Info($"{player.DisplayName} tried to use command {cmdPath} without permission");
|
||||
_torch.Multiplayer.SendMessage($"You need to be a {command.MinimumPromoteLevel} or higher to use that command.", playerId: player.IdentityId);
|
||||
return;
|
||||
}
|
||||
|
||||
|
19
Torch/Commands/Permissions/PermissionAttribute.cs
Normal file
19
Torch/Commands/Permissions/PermissionAttribute.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using VRage.Game.ModAPI;
|
||||
|
||||
namespace Torch.Commands.Permissions
|
||||
{
|
||||
public class PermissionAttribute : Attribute
|
||||
{
|
||||
public MyPromoteLevel PromoteLevel { get; }
|
||||
|
||||
public PermissionAttribute(MyPromoteLevel promoteLevel)
|
||||
{
|
||||
PromoteLevel = promoteLevel;
|
||||
}
|
||||
}
|
||||
}
|
@@ -3,7 +3,9 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Torch.Commands.Permissions;
|
||||
using Torch.Managers;
|
||||
using VRage.Game.ModAPI;
|
||||
|
||||
namespace Torch.Commands
|
||||
{
|
||||
@@ -65,6 +67,7 @@ namespace Torch.Commands
|
||||
}
|
||||
|
||||
[Command("stop", "Stops the server.")]
|
||||
[Permission(MyPromoteLevel.Admin)]
|
||||
public void Stop()
|
||||
{
|
||||
Context.Respond("Stopping server.");
|
||||
|
@@ -154,6 +154,7 @@
|
||||
<Compile Include="Commands\CommandManager.cs" />
|
||||
<Compile Include="Commands\CommandTree.cs" />
|
||||
<Compile Include="Commands\DefaultPermissionAttribute.cs" />
|
||||
<Compile Include="Commands\Permissions\PermissionAttribute.cs" />
|
||||
<Compile Include="Commands\Permissions\PermissonsSystem.cs" />
|
||||
<Compile Include="Commands\TorchCommands.cs" />
|
||||
<Compile Include="Managers\ChatManager.cs" />
|
||||
|
Reference in New Issue
Block a user