Skip to content

Commit 3d26352

Browse files
committed
* Essentials 1.4
- Added automatic command triggers in the UI - Switched order of teleport command parameters
1 parent 13262d9 commit 3d26352

9 files changed

Lines changed: 255 additions & 40 deletions

‎Essentials/AutoCommand.cs‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using Torch;
8+
using Torch.Commands;
9+
10+
namespace Essentials
11+
{
12+
public class AutoCommand : ViewModel, IDisposable
13+
{
14+
private Timer _timer;
15+
16+
private bool _enabled;
17+
public bool Enabled { get => _enabled; set { _enabled = value; OnTimerChanged(); OnPropertyChanged(); } }
18+
private string _command;
19+
public string Command { get => _command; set { _command = value; OnTimerChanged(); OnPropertyChanged(); } }
20+
private int _dueTime;
21+
public int DueTime { get => _dueTime / 1000; set { _dueTime = value * 1000; OnTimerChanged(); OnPropertyChanged(); } }
22+
private int _period;
23+
public int Period { get => _period / 1000; set { _period = value * 1000; OnTimerChanged(); OnPropertyChanged(); } }
24+
25+
private void OnTimerChanged()
26+
{
27+
_timer?.Dispose();
28+
if (Enabled && Period > 0)
29+
_timer = new Timer(RunCommand, this, _dueTime, _period);
30+
}
31+
32+
private void RunCommand(object state)
33+
{
34+
var autoCommand = (AutoCommand)state;
35+
TorchBase.Instance.Invoke(() =>
36+
{
37+
var manager = TorchBase.Instance.GetManager<CommandManager>();
38+
manager?.HandleCommandFromServer(autoCommand.Command);
39+
});
40+
}
41+
42+
~AutoCommand()
43+
{
44+
try
45+
{
46+
Dispose();
47+
}
48+
catch
49+
{
50+
// ignored
51+
}
52+
}
53+
54+
/// <inheritdoc />
55+
public void Dispose()
56+
{
57+
_timer?.Dispose();
58+
}
59+
}
60+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Sandbox.Game.Entities;
7+
using Sandbox.Game.Entities.Cube;
8+
using Sandbox.ModAPI;
9+
using Torch.Commands;
10+
11+
namespace Essentials.Commands
12+
{
13+
[Category("blocks")]
14+
public class BlocksModule : CommandModule
15+
{
16+
[Command("on type", "Turn on all blocks of the given type.")]
17+
public void OnType(string type)
18+
{
19+
var count = 0;
20+
foreach (var grid in MyEntities.GetEntities().OfType<MyCubeGrid>())
21+
{
22+
foreach (var block in grid.GetFatBlocks().OfType<MyFunctionalBlock>())
23+
{
24+
var blockType = block.BlockDefinition.Id.TypeId.ToString().Substring(16);
25+
if (block != null && string.Compare(type, blockType, StringComparison.InvariantCultureIgnoreCase) == 0)
26+
{
27+
block.Enabled = true;
28+
count++;
29+
}
30+
}
31+
}
32+
33+
Context.Respond($"Enabled {count} blocks of type {type}.");
34+
}
35+
36+
[Command("on subtype", "Turn on all blocks of the given subtype.")]
37+
public void OnSubtype(string subtype)
38+
{
39+
var count = 0;
40+
foreach (var grid in MyEntities.GetEntities().OfType<MyCubeGrid>())
41+
{
42+
foreach (var block in grid.GetFatBlocks().OfType<MyFunctionalBlock>())
43+
{
44+
var blockType = block.BlockDefinition.Id.SubtypeName;
45+
if (block != null && string.Compare(subtype, blockType, StringComparison.InvariantCultureIgnoreCase) == 0)
46+
{
47+
block.Enabled = true;
48+
count++;
49+
}
50+
}
51+
}
52+
53+
Context.Respond($"Enabled {count} blocks of subtype {subtype}.");
54+
}
55+
56+
[Command("off type", "Turn off all blocks of the given type.")]
57+
public void OffType(string type)
58+
{
59+
var count = 0;
60+
foreach (var grid in MyEntities.GetEntities().OfType<MyCubeGrid>())
61+
{
62+
foreach (var block in grid.GetFatBlocks().OfType<MyFunctionalBlock>())
63+
{
64+
var blockType = block.BlockDefinition.Id.TypeId.ToString().Substring(16);
65+
if (block != null && string.Compare(type, blockType, StringComparison.InvariantCultureIgnoreCase) == 0)
66+
{
67+
block.Enabled = false;
68+
count++;
69+
}
70+
}
71+
}
72+
73+
74+
Context.Respond($"Disabled {count} blocks of type {type}.");
75+
}
76+
77+
[Command("off subtype", "Turn off all blocks of the given subtype.")]
78+
public void OffSubtype(string subtype)
79+
{
80+
var count = 0;
81+
foreach (var grid in MyEntities.GetEntities().OfType<MyCubeGrid>())
82+
{
83+
foreach (var block in grid.GetFatBlocks().OfType<MyFunctionalBlock>())
84+
{
85+
var blockType = block.BlockDefinition.Id.SubtypeName;
86+
if (block != null && string.Compare(subtype, blockType, StringComparison.InvariantCultureIgnoreCase) == 0)
87+
{
88+
block.Enabled = false;
89+
count++;
90+
}
91+
}
92+
}
93+
94+
95+
Context.Respond($"Disabled {count} blocks of subtype {subtype}.");
96+
}
97+
}
98+
}

‎Essentials/Commands/EntityModule.cs‎

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -119,39 +119,5 @@ public void Find(string name)
119119

120120
Context.Respond(sb.ToString());
121121
}
122-
123-
[Command("tp", "Teleport to another entity or teleport another entity to you.")]
124-
[Permission(MyPromoteLevel.SpaceMaster)]
125-
public void Teleport(string destination, string entityToMove = null)
126-
{
127-
Utilities.TryGetEntityByNameOrId(destination, out IMyEntity destEntity);
128-
129-
IMyEntity targetEntity;
130-
if (string.IsNullOrEmpty(entityToMove))
131-
targetEntity = Context.Player?.Controller.ControlledEntity.Entity;
132-
else
133-
Utilities.TryGetEntityByNameOrId(entityToMove, out targetEntity);
134-
135-
if (targetEntity == null)
136-
{
137-
Context.Respond("Target entity not found.");
138-
return;
139-
}
140-
141-
if (destEntity == null)
142-
{
143-
Context.Respond("Destination entity not found");
144-
return;
145-
}
146-
147-
var targetPos = MyEntities.FindFreePlace(destEntity.GetPosition(), (float)targetEntity.WorldAABB.Extents.Max());
148-
if (targetPos == null)
149-
{
150-
Context.Respond("No free place to teleport.");
151-
return;
152-
}
153-
154-
targetEntity.SetPosition(targetPos.Value);
155-
}
156122
}
157123
}

‎Essentials/Commands/PlayerModule.cs‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,58 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using Sandbox.Game.Entities;
67
using Torch.Commands;
78
using Torch.Commands.Permissions;
9+
using Torch.Managers;
810
using VRage.Game;
911
using VRage.Game.ModAPI;
12+
using VRage.ModAPI;
1013

1114
namespace Essentials
1215
{
1316
public class PlayerModule : CommandModule
1417
{
18+
[Command("say", "Say a message as the server.")]
19+
public void Say(string message)
20+
{
21+
Context.Torch.GetManager<MultiplayerManager>()?.SendMessage(Context.RawArgs);
22+
}
23+
24+
[Command("tp", "Teleport one entity to another.")]
25+
[Permission(MyPromoteLevel.SpaceMaster)]
26+
public void Teleport(string entityToMove, string destination)
27+
{
28+
Utilities.TryGetEntityByNameOrId(destination, out IMyEntity destEntity);
29+
30+
IMyEntity targetEntity;
31+
if (string.IsNullOrEmpty(entityToMove))
32+
targetEntity = Context.Player?.Controller.ControlledEntity.Entity;
33+
else
34+
Utilities.TryGetEntityByNameOrId(entityToMove, out targetEntity);
35+
36+
if (targetEntity == null)
37+
{
38+
Context.Respond("Target entity not found.");
39+
return;
40+
}
41+
42+
if (destEntity == null)
43+
{
44+
Context.Respond("Destination entity not found");
45+
return;
46+
}
47+
48+
var targetPos = MyEntities.FindFreePlace(destEntity.GetPosition(), (float)targetEntity.WorldAABB.Extents.Max());
49+
if (targetPos == null)
50+
{
51+
Context.Respond("No free place to teleport.");
52+
return;
53+
}
54+
55+
targetEntity.SetPosition(targetPos.Value);
56+
}
57+
1558
[Command("w", "Send a private message to another player.")]
1659
[Permission(MyPromoteLevel.None)]
1760
public void Whisper(string playerName)

‎Essentials/Essentials.csproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@
129129
<Reference Include="WindowsBase" />
130130
</ItemGroup>
131131
<ItemGroup>
132+
<Compile Include="AutoCommand.cs" />
133+
<Compile Include="Commands\BlocksModule.cs" />
132134
<Compile Include="Commands\VoxelModule.cs" />
133135
<Compile Include="Commands\WorldModule.cs" />
134136
<Compile Include="EssentialsConfig.cs" />

‎Essentials/EssentialsConfig.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
34
using System.Linq;
45
using System.Text;
56
using System.Threading.Tasks;
7+
using Torch;
68

79
namespace Essentials
810
{
911
public class EssentialsConfig
1012
{
11-
13+
public MTObservableCollection<AutoCommand> AutoCommands { get; } = new MTObservableCollection<AutoCommand>();
1214
}
1315
}

‎Essentials/EssentialsControl.xaml‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:local="clr-namespace:Essentials"
7-
mc:Ignorable="d"
8-
d:DesignHeight="300" d:DesignWidth="300">
7+
mc:Ignorable="d">
8+
<UserControl.DataContext>
9+
<local:EssentialsConfig/>
10+
</UserControl.DataContext>
911
<StackPanel>
12+
<Label Content="AutoCommands"></Label>
13+
<DataGrid ItemsSource="{Binding AutoCommands, UpdateSourceTrigger=PropertyChanged}" KeyDown="UIElement_OnKeyDown"/>
1014
</StackPanel>
1115
</UserControl>

‎Essentials/EssentialsControl.xaml.cs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,30 @@ namespace Essentials
2020
/// </summary>
2121
public partial class EssentialsControl : UserControl
2222
{
23+
private EssentialsPlugin Plugin { get; }
24+
2325
public EssentialsControl()
2426
{
2527
InitializeComponent();
2628
}
29+
30+
public EssentialsControl(EssentialsPlugin plugin) : this()
31+
{
32+
Plugin = plugin;
33+
DataContext = plugin.Config;
34+
}
35+
36+
private void UIElement_OnKeyDown(object sender, KeyEventArgs e)
37+
{
38+
if (e.Key != Key.Delete)
39+
return;
40+
var list = (DataGrid)sender;
41+
var items = list.SelectedItems.Cast<AutoCommand>().ToList();
42+
foreach (var item in items)
43+
{
44+
item.Enabled = false;
45+
Plugin.Config.AutoCommands.Remove(item);
46+
}
47+
}
2748
}
2849
}

‎Essentials/EssentialsPlugin.cs‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
1-
using System.Windows.Controls;
1+
using System;
2+
using System.IO;
3+
using System.Windows.Controls;
24
using Torch;
5+
using Torch.API;
36
using Torch.API.Plugins;
7+
using Torch.Commands;
48

59
namespace Essentials
610
{
7-
[Plugin("Essentials", "1.2", "cbfdd6ab-4cda-4544-a201-f73efa3d46c0")]
11+
[Plugin("Essentials", "1.4", "cbfdd6ab-4cda-4544-a201-f73efa3d46c0")]
812
public class EssentialsPlugin : TorchPluginBase, IWpfPlugin
913
{
14+
public EssentialsConfig Config => _config?.Data;
15+
1016
private EssentialsControl _control;
1117
private Persistent<EssentialsConfig> _config;
1218

1319
/// <inheritdoc />
14-
public UserControl GetControl() => _control ?? (_control = new EssentialsControl());
20+
public UserControl GetControl() => _control ?? (_control = new EssentialsControl(this));
21+
22+
/// <inheritdoc />
23+
public override void Init(ITorchBase torch)
24+
{
25+
base.Init(torch);
26+
_config = Persistent<EssentialsConfig>.Load(Path.Combine(StoragePath, "Essentials.cfg"));
27+
}
28+
29+
/// <inheritdoc />
30+
public override void Dispose()
31+
{
32+
_config.Save();
33+
}
1534
}
1635
}

0 commit comments

Comments
 (0)