-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added automatic command triggers in the UI - Switched order of teleport command parameters
- Loading branch information
Showing
9 changed files
with
255 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Torch; | ||
using Torch.Commands; | ||
|
||
namespace Essentials | ||
{ | ||
public class AutoCommand : ViewModel, IDisposable | ||
{ | ||
private Timer _timer; | ||
|
||
private bool _enabled; | ||
public bool Enabled { get => _enabled; set { _enabled = value; OnTimerChanged(); OnPropertyChanged(); } } | ||
private string _command; | ||
public string Command { get => _command; set { _command = value; OnTimerChanged(); OnPropertyChanged(); } } | ||
private int _dueTime; | ||
public int DueTime { get => _dueTime / 1000; set { _dueTime = value * 1000; OnTimerChanged(); OnPropertyChanged(); } } | ||
private int _period; | ||
public int Period { get => _period / 1000; set { _period = value * 1000; OnTimerChanged(); OnPropertyChanged(); } } | ||
|
||
private void OnTimerChanged() | ||
{ | ||
_timer?.Dispose(); | ||
if (Enabled && Period > 0) | ||
_timer = new Timer(RunCommand, this, _dueTime, _period); | ||
} | ||
|
||
private void RunCommand(object state) | ||
{ | ||
var autoCommand = (AutoCommand)state; | ||
TorchBase.Instance.Invoke(() => | ||
{ | ||
var manager = TorchBase.Instance.GetManager<CommandManager>(); | ||
manager?.HandleCommandFromServer(autoCommand.Command); | ||
}); | ||
} | ||
|
||
~AutoCommand() | ||
{ | ||
try | ||
{ | ||
Dispose(); | ||
} | ||
catch | ||
{ | ||
// ignored | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Dispose() | ||
{ | ||
_timer?.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Sandbox.Game.Entities; | ||
using Sandbox.Game.Entities.Cube; | ||
using Sandbox.ModAPI; | ||
using Torch.Commands; | ||
|
||
namespace Essentials.Commands | ||
{ | ||
[Category("blocks")] | ||
public class BlocksModule : CommandModule | ||
{ | ||
[Command("on type", "Turn on all blocks of the given type.")] | ||
public void OnType(string type) | ||
{ | ||
var count = 0; | ||
foreach (var grid in MyEntities.GetEntities().OfType<MyCubeGrid>()) | ||
{ | ||
foreach (var block in grid.GetFatBlocks().OfType<MyFunctionalBlock>()) | ||
{ | ||
var blockType = block.BlockDefinition.Id.TypeId.ToString().Substring(16); | ||
if (block != null && string.Compare(type, blockType, StringComparison.InvariantCultureIgnoreCase) == 0) | ||
{ | ||
block.Enabled = true; | ||
count++; | ||
} | ||
} | ||
} | ||
|
||
Context.Respond($"Enabled {count} blocks of type {type}."); | ||
} | ||
|
||
[Command("on subtype", "Turn on all blocks of the given subtype.")] | ||
public void OnSubtype(string subtype) | ||
{ | ||
var count = 0; | ||
foreach (var grid in MyEntities.GetEntities().OfType<MyCubeGrid>()) | ||
{ | ||
foreach (var block in grid.GetFatBlocks().OfType<MyFunctionalBlock>()) | ||
{ | ||
var blockType = block.BlockDefinition.Id.SubtypeName; | ||
if (block != null && string.Compare(subtype, blockType, StringComparison.InvariantCultureIgnoreCase) == 0) | ||
{ | ||
block.Enabled = true; | ||
count++; | ||
} | ||
} | ||
} | ||
|
||
Context.Respond($"Enabled {count} blocks of subtype {subtype}."); | ||
} | ||
|
||
[Command("off type", "Turn off all blocks of the given type.")] | ||
public void OffType(string type) | ||
{ | ||
var count = 0; | ||
foreach (var grid in MyEntities.GetEntities().OfType<MyCubeGrid>()) | ||
{ | ||
foreach (var block in grid.GetFatBlocks().OfType<MyFunctionalBlock>()) | ||
{ | ||
var blockType = block.BlockDefinition.Id.TypeId.ToString().Substring(16); | ||
if (block != null && string.Compare(type, blockType, StringComparison.InvariantCultureIgnoreCase) == 0) | ||
{ | ||
block.Enabled = false; | ||
count++; | ||
} | ||
} | ||
} | ||
|
||
|
||
Context.Respond($"Disabled {count} blocks of type {type}."); | ||
} | ||
|
||
[Command("off subtype", "Turn off all blocks of the given subtype.")] | ||
public void OffSubtype(string subtype) | ||
{ | ||
var count = 0; | ||
foreach (var grid in MyEntities.GetEntities().OfType<MyCubeGrid>()) | ||
{ | ||
foreach (var block in grid.GetFatBlocks().OfType<MyFunctionalBlock>()) | ||
{ | ||
var blockType = block.BlockDefinition.Id.SubtypeName; | ||
if (block != null && string.Compare(subtype, blockType, StringComparison.InvariantCultureIgnoreCase) == 0) | ||
{ | ||
block.Enabled = false; | ||
count++; | ||
} | ||
} | ||
} | ||
|
||
|
||
Context.Respond($"Disabled {count} blocks of subtype {subtype}."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Torch; | ||
|
||
namespace Essentials | ||
{ | ||
public class EssentialsConfig | ||
{ | ||
|
||
public MTObservableCollection<AutoCommand> AutoCommands { get; } = new MTObservableCollection<AutoCommand>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,35 @@ | ||
using System.Windows.Controls; | ||
using System; | ||
using System.IO; | ||
using System.Windows.Controls; | ||
using Torch; | ||
using Torch.API; | ||
using Torch.API.Plugins; | ||
using Torch.Commands; | ||
|
||
namespace Essentials | ||
{ | ||
[Plugin("Essentials", "1.2", "cbfdd6ab-4cda-4544-a201-f73efa3d46c0")] | ||
[Plugin("Essentials", "1.4", "cbfdd6ab-4cda-4544-a201-f73efa3d46c0")] | ||
public class EssentialsPlugin : TorchPluginBase, IWpfPlugin | ||
{ | ||
public EssentialsConfig Config => _config?.Data; | ||
|
||
private EssentialsControl _control; | ||
private Persistent<EssentialsConfig> _config; | ||
|
||
/// <inheritdoc /> | ||
public UserControl GetControl() => _control ?? (_control = new EssentialsControl()); | ||
public UserControl GetControl() => _control ?? (_control = new EssentialsControl(this)); | ||
|
||
/// <inheritdoc /> | ||
public override void Init(ITorchBase torch) | ||
{ | ||
base.Init(torch); | ||
_config = Persistent<EssentialsConfig>.Load(Path.Combine(StoragePath, "Essentials.cfg")); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void Dispose() | ||
{ | ||
_config.Save(); | ||
} | ||
} | ||
} |