Skip to content

Commit 8fc61ff

Browse files
committed
Make use of new command parsing system.
1 parent aed08bb commit 8fc61ff

8 files changed

Lines changed: 209 additions & 24 deletions

Essentials/EntityCommands.cs

Lines changed: 95 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using Sandbox.Game.Entities;
7+
using Sandbox.ModAPI;
68
using Torch.Commands;
79
using Torch.Commands.Permissions;
10+
using VRage.Game.Entity;
811
using VRage.Game.ModAPI;
912
using VRage.ModAPI;
1013
using VRageMath;
@@ -15,31 +18,114 @@ namespace Essentials
1518
public class EntityCommands : CommandModule
1619
{
1720
[Command("stop", "Stops an entity from moving")]
18-
[Permission(MyPromoteLevel.Moderator)]
19-
public void Stop()
21+
[Permission(MyPromoteLevel.SpaceMaster)]
22+
public void Stop(string entityName)
2023
{
21-
if (!Utilities.TryGetEntityByNameOrId(Context.Args.FirstOrDefault(), out IMyEntity entity))
24+
if (!Utilities.TryGetEntityByNameOrId(entityName, out IMyEntity entity))
2225
{
23-
Context.Respond("Entity not found.");
26+
Context.Respond($"Entity '{entityName}' not found.");
2427
return;
2528
}
2629

27-
entity?.Physics.ClearSpeed();
30+
entity.Physics?.ClearSpeed();
2831
Context.Respond($"Entity '{entity.DisplayName}' stopped");
2932
}
3033

3134
[Command("delete", "Delete an entity.")]
32-
[Permission(MyPromoteLevel.Moderator)]
33-
public void Delete()
35+
[Permission(MyPromoteLevel.SpaceMaster)]
36+
public void Delete(string entityName)
3437
{
35-
if (!Utilities.TryGetEntityByNameOrId(Context.Args.FirstOrDefault(), out IMyEntity entity))
38+
var name = entityName;
39+
if (string.IsNullOrEmpty(name))
40+
return;
41+
42+
if (!Utilities.TryGetEntityByNameOrId(name, out IMyEntity entity))
3643
{
37-
Context.Respond("Entity not found.");
44+
Context.Respond($"Entity '{name}' not found.");
45+
return;
46+
}
47+
48+
if (entity is IMyCharacter)
49+
{
50+
Context.Respond("You cannot delete characters.");
3851
return;
3952
}
4053

4154
entity.Close();
4255
Context.Respond($"Entity '{entity.DisplayName}' deleted");
4356
}
57+
58+
[Command("find", "Find entities with the given text in their name.")]
59+
[Permission(MyPromoteLevel.SpaceMaster)]
60+
public void Find(string name)
61+
{
62+
var search = name;
63+
if (string.IsNullOrEmpty(search))
64+
return;
65+
66+
var sb = new StringBuilder("Found entities:\n");
67+
foreach (var entity in MyEntities.GetEntities())
68+
{
69+
if (entity is IMyVoxelBase voxel && voxel.StorageName.Contains(search, StringComparison.CurrentCultureIgnoreCase))
70+
sb.AppendLine($"{voxel.StorageName} ({entity.EntityId})");
71+
else if (entity.DisplayName?.Contains(search, StringComparison.CurrentCultureIgnoreCase) ?? false)
72+
//This can be null??? :keen:
73+
sb.AppendLine($"{entity.DisplayName} ({entity.EntityId})");
74+
}
75+
76+
Context.Respond(sb.ToString());
77+
}
78+
79+
[Command("tp", "Teleport to another entity or teleport another entity to you.")]
80+
[Permission(MyPromoteLevel.SpaceMaster)]
81+
public void Teleport(string destination, string entityToMove = null)
82+
{
83+
/*
84+
IMyEntity targetEntity;
85+
IMyEntity destEntity;
86+
switch (Context.Args.Count)
87+
{
88+
case 1:
89+
targetEntity = Context.Player.Controller.ControlledEntity.Entity;
90+
Utilities.TryGetEntityByNameOrId(Context.Args[0], out destEntity);
91+
break;
92+
case 2:
93+
Utilities.TryGetEntityByNameOrId(Context.Args[0], out targetEntity);
94+
Utilities.TryGetEntityByNameOrId(Context.Args[1], out destEntity);
95+
break;
96+
default:
97+
Context.Respond("Wrong number of arguments.");
98+
return;
99+
}*/
100+
101+
Utilities.TryGetEntityByNameOrId(destination, out IMyEntity destEntity);
102+
103+
IMyEntity targetEntity;
104+
if (string.IsNullOrEmpty(entityToMove))
105+
targetEntity = Context.Player.Controller.ControlledEntity.Entity;
106+
else
107+
Utilities.TryGetEntityByNameOrId(entityToMove, out targetEntity);
108+
109+
if (targetEntity == null)
110+
{
111+
Context.Respond("Target entity not found.");
112+
return;
113+
}
114+
115+
if (destEntity == null)
116+
{
117+
Context.Respond("Destination entity not found");
118+
return;
119+
}
120+
121+
var targetPos = MyEntities.FindFreePlace(destEntity.GetPosition(), (float)targetEntity.WorldAABB.Extents.Max());
122+
if (targetPos == null)
123+
{
124+
Context.Respond("No free place to teleport.");
125+
return;
126+
}
127+
128+
targetEntity.SetPosition(targetPos.Value);
129+
}
44130
}
45131
}

Essentials/Essentials.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
<WarningLevel>4</WarningLevel>
3131
</PropertyGroup>
3232
<ItemGroup>
33+
<Reference Include="PresentationCore" />
34+
<Reference Include="PresentationFramework" />
3335
<Reference Include="Sandbox.Common">
3436
<HintPath>..\..\..\..\..\..\..\steamcmd\steamapps\common\SpaceEngineersDedicatedServer\DedicatedServer64\Sandbox.Common.dll</HintPath>
3537
</Reference>
@@ -50,6 +52,8 @@
5052
</Reference>
5153
<Reference Include="System" />
5254
<Reference Include="System.Core" />
55+
<Reference Include="System.Windows.Presentation" />
56+
<Reference Include="System.Xaml" />
5357
<Reference Include="System.Xml.Linq" />
5458
<Reference Include="System.Data.DataSetExtensions" />
5559
<Reference Include="Microsoft.CSharp" />
@@ -104,13 +108,24 @@
104108
<Reference Include="VRage.Scripting">
105109
<HintPath>..\..\..\..\..\..\..\steamcmd\steamapps\common\SpaceEngineersDedicatedServer\DedicatedServer64\VRage.Scripting.dll</HintPath>
106110
</Reference>
111+
<Reference Include="WindowsBase" />
107112
</ItemGroup>
108113
<ItemGroup>
109114
<Compile Include="EntityCommands.cs" />
115+
<Compile Include="EssentialsControl.xaml.cs">
116+
<DependentUpon>EssentialsControl.xaml</DependentUpon>
117+
</Compile>
110118
<Compile Include="EssentialsPlugin.cs" />
111119
<Compile Include="GridCommands.cs" />
120+
<Compile Include="PlayerCommands.cs" />
112121
<Compile Include="Properties\AssemblyInfo.cs" />
113122
<Compile Include="Utilities.cs" />
114123
</ItemGroup>
124+
<ItemGroup>
125+
<Page Include="EssentialsControl.xaml">
126+
<SubType>Designer</SubType>
127+
<Generator>MSBuild:Compile</Generator>
128+
</Page>
129+
</ItemGroup>
115130
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
116131
</Project>

Essentials/EssentialsControl.xaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<UserControl x:Class="Essentials.EssentialsControl"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:local="clr-namespace:Essentials"
7+
mc:Ignorable="d"
8+
d:DesignHeight="300" d:DesignWidth="300">
9+
<StackPanel>
10+
<TextBlock Text="WPF Control Test"/>
11+
<Button Content="Click Meh"></Button>
12+
</StackPanel>
13+
</UserControl>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows;
7+
using System.Windows.Controls;
8+
using System.Windows.Data;
9+
using System.Windows.Documents;
10+
using System.Windows.Input;
11+
using System.Windows.Media;
12+
using System.Windows.Media.Imaging;
13+
using System.Windows.Navigation;
14+
using System.Windows.Shapes;
15+
16+
namespace Essentials
17+
{
18+
/// <summary>
19+
/// Interaction logic for EssentialsControl.xaml
20+
/// </summary>
21+
public partial class EssentialsControl : UserControl
22+
{
23+
public EssentialsControl()
24+
{
25+
InitializeComponent();
26+
}
27+
}
28+
}

Essentials/EssentialsPlugin.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using System.Windows.Controls;
67
using Torch;
7-
using Torch.API;
8+
using Torch.API.Plugins;
89

910
namespace Essentials
1011
{
1112
[Plugin("Essentials", "1.0", "cbfdd6ab-4cda-4544-a201-f73efa3d46c0")]
12-
public class EssentialsPlugin : TorchPluginBase
13+
public class EssentialsPlugin : TorchPluginBase, IWpfPlugin
1314
{
15+
private EssentialsControl _control;
16+
17+
/// <inheritdoc />
18+
public UserControl GetControl() => _control ?? (_control = new EssentialsControl());
19+
1420
/// <inheritdoc />
1521
public override void Update()
1622
{

Essentials/GridCommands.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,26 @@ namespace Essentials
1717
public class GridCommands : CommandModule
1818
{
1919
[Command("setowner", "Sets grid ownership to the given player or ID.", "Usage: setowner <grid> <newowner>")]
20-
[Permission(MyPromoteLevel.Moderator)]
21-
public void SetOwner()
20+
[Permission(MyPromoteLevel.SpaceMaster)]
21+
public void SetOwner(string gridName, string playerName)
2222
{
2323
var firstArg = Context.Args.FirstOrDefault();
24-
Utilities.TryGetEntityByNameOrId(firstArg, out IMyEntity entity);
24+
Utilities.TryGetEntityByNameOrId(gridName, out IMyEntity entity);
2525

2626
if (!(entity is IMyCubeGrid grid))
2727
{
28-
Context.Respond($"Grid {firstArg} not found.");
28+
Context.Respond($"Grid {gridName} not found.");
2929
return;
3030
}
3131

3232
var secondArg = Context.Args.ElementAtOrDefault(1);
3333
long identityId;
34-
if (!long.TryParse(secondArg, out identityId))
34+
if (!long.TryParse(playerName, out identityId))
3535
{
36-
var player = Context.Torch.Multiplayer.GetPlayerByName(secondArg);
36+
var player = Context.Torch.Multiplayer.GetPlayerByName(playerName);
3737
if (player == null)
3838
{
39-
Context.Respond($"Player {secondArg} not found.");
39+
Context.Respond($"Player {playerName} not found.");
4040
return;
4141
}
4242
identityId = player.IdentityId;

Essentials/PlayerCommands.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,43 @@
55
using System.Threading.Tasks;
66
using Torch.Commands;
77
using Torch.Commands.Permissions;
8+
using VRage.Game;
89
using VRage.Game.ModAPI;
910

1011
namespace Essentials
1112
{
1213
public class PlayerCommands : CommandModule
1314
{
15+
[Command("w", "Send a private message to another player.")]
16+
public void Whisper(string playerName)
17+
{
18+
if (Context.Args.Count < 1)
19+
return;
20+
21+
//var playerName = Context.Args[0];
22+
Console.WriteLine($"'{playerName}'");
23+
var msgIndex = Context.RawArgs.IndexOf(" ", playerName.Length);
24+
if (msgIndex > Context.RawArgs.Length)
25+
return;
26+
27+
var message = Context.RawArgs.Substring(msgIndex);
28+
var player = Context.Torch.Multiplayer.GetPlayerByName(playerName);
29+
Console.WriteLine($"'{player?.DisplayName ?? "null"}'");
30+
31+
if (player == null)
32+
{
33+
Context.Respond($"Player '{playerName}' not found.");
34+
return;
35+
}
36+
37+
Context.Torch.Multiplayer.SendMessage(message, Context.Player.DisplayName, player.IdentityId, MyFontEnum.Red);
38+
}
39+
1440
[Command("kick", "Kick a player from the game.")]
1541
[Permission(MyPromoteLevel.Moderator)]
16-
public void Kick()
42+
public void Kick(string playerName)
1743
{
18-
var player = Utilities.GetPlayerByNameOrId(Context.Args.FirstOrDefault());
44+
var player = Utilities.GetPlayerByNameOrId(playerName);
1945
if (player != null)
2046
{
2147
Context.Torch.Multiplayer.KickPlayer(player.SteamUserId);
@@ -29,9 +55,9 @@ public void Kick()
2955

3056
[Command("ban", "Ban a player from the game.")]
3157
[Permission(MyPromoteLevel.Moderator)]
32-
public void Ban()
58+
public void Ban(string playerName)
3359
{
34-
var player = Utilities.GetPlayerByNameOrId(Context.Args.FirstOrDefault());
60+
var player = Utilities.GetPlayerByNameOrId(playerName);
3561
if (player != null)
3662
{
3763
Context.Torch.Multiplayer.BanPlayer(player.SteamUserId);
@@ -45,7 +71,7 @@ public void Ban()
4571

4672
[Command("unban", "Unban a player from the game.")]
4773
[Permission(MyPromoteLevel.Moderator)]
48-
public void Unban()
74+
public void Unban(string playerName)
4975
{
5076
var player = Utilities.GetPlayerByNameOrId(Context.Args.FirstOrDefault());
5177
if (player != null)

Essentials/Utilities.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using Sandbox.Game.Entities;
67
using Sandbox.ModAPI;
78
using Torch;
89
using VRage.Game.ModAPI;
@@ -17,7 +18,17 @@ public static bool TryGetEntityByNameOrId(string nameOrId, out IMyEntity entity)
1718
if (long.TryParse(nameOrId, out long id))
1819
return MyAPIGateway.Entities.TryGetEntityById(id, out entity);
1920

20-
return MyAPIGateway.Entities.TryGetEntityByName(nameOrId, out entity);
21+
foreach (var ent in MyEntities.GetEntities())
22+
{
23+
if (ent.DisplayName == nameOrId)
24+
{
25+
entity = ent;
26+
return true;
27+
}
28+
}
29+
30+
entity = null;
31+
return false;
2132
}
2233

2334
public static IMyPlayer GetPlayerByNameOrId(string nameOrSteamId)

0 commit comments

Comments
 (0)