Skip to content

Commit

Permalink
* Essentials 1.4
Browse files Browse the repository at this point in the history
    - Added automatic command triggers in the UI
    - Switched order of teleport command parameters
  • Loading branch information
Jimmacle committed Jul 1, 2017
1 parent 13262d9 commit 3d26352
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 40 deletions.
60 changes: 60 additions & 0 deletions Essentials/AutoCommand.cs
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();
}
}
}
98 changes: 98 additions & 0 deletions Essentials/Commands/BlocksModule.cs
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}.");
}
}
}
34 changes: 0 additions & 34 deletions Essentials/Commands/EntityModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,39 +119,5 @@ public void Find(string name)

Context.Respond(sb.ToString());
}

[Command("tp", "Teleport to another entity or teleport another entity to you.")]
[Permission(MyPromoteLevel.SpaceMaster)]
public void Teleport(string destination, string entityToMove = null)
{
Utilities.TryGetEntityByNameOrId(destination, out IMyEntity destEntity);

IMyEntity targetEntity;
if (string.IsNullOrEmpty(entityToMove))
targetEntity = Context.Player?.Controller.ControlledEntity.Entity;
else
Utilities.TryGetEntityByNameOrId(entityToMove, out targetEntity);

if (targetEntity == null)
{
Context.Respond("Target entity not found.");
return;
}

if (destEntity == null)
{
Context.Respond("Destination entity not found");
return;
}

var targetPos = MyEntities.FindFreePlace(destEntity.GetPosition(), (float)targetEntity.WorldAABB.Extents.Max());
if (targetPos == null)
{
Context.Respond("No free place to teleport.");
return;
}

targetEntity.SetPosition(targetPos.Value);
}
}
}
43 changes: 43 additions & 0 deletions Essentials/Commands/PlayerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,58 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sandbox.Game.Entities;
using Torch.Commands;
using Torch.Commands.Permissions;
using Torch.Managers;
using VRage.Game;
using VRage.Game.ModAPI;
using VRage.ModAPI;

namespace Essentials
{
public class PlayerModule : CommandModule
{
[Command("say", "Say a message as the server.")]
public void Say(string message)
{
Context.Torch.GetManager<MultiplayerManager>()?.SendMessage(Context.RawArgs);
}

[Command("tp", "Teleport one entity to another.")]
[Permission(MyPromoteLevel.SpaceMaster)]
public void Teleport(string entityToMove, string destination)
{
Utilities.TryGetEntityByNameOrId(destination, out IMyEntity destEntity);

IMyEntity targetEntity;
if (string.IsNullOrEmpty(entityToMove))
targetEntity = Context.Player?.Controller.ControlledEntity.Entity;
else
Utilities.TryGetEntityByNameOrId(entityToMove, out targetEntity);

if (targetEntity == null)
{
Context.Respond("Target entity not found.");
return;
}

if (destEntity == null)
{
Context.Respond("Destination entity not found");
return;
}

var targetPos = MyEntities.FindFreePlace(destEntity.GetPosition(), (float)targetEntity.WorldAABB.Extents.Max());
if (targetPos == null)
{
Context.Respond("No free place to teleport.");
return;
}

targetEntity.SetPosition(targetPos.Value);
}

[Command("w", "Send a private message to another player.")]
[Permission(MyPromoteLevel.None)]
public void Whisper(string playerName)
Expand Down
2 changes: 2 additions & 0 deletions Essentials/Essentials.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="AutoCommand.cs" />
<Compile Include="Commands\BlocksModule.cs" />
<Compile Include="Commands\VoxelModule.cs" />
<Compile Include="Commands\WorldModule.cs" />
<Compile Include="EssentialsConfig.cs" />
Expand Down
4 changes: 3 additions & 1 deletion Essentials/EssentialsConfig.cs
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>();
}
}
8 changes: 6 additions & 2 deletions Essentials/EssentialsControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Essentials"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
mc:Ignorable="d">
<UserControl.DataContext>
<local:EssentialsConfig/>
</UserControl.DataContext>
<StackPanel>
<Label Content="AutoCommands"></Label>
<DataGrid ItemsSource="{Binding AutoCommands, UpdateSourceTrigger=PropertyChanged}" KeyDown="UIElement_OnKeyDown"/>
</StackPanel>
</UserControl>
21 changes: 21 additions & 0 deletions Essentials/EssentialsControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,30 @@ namespace Essentials
/// </summary>
public partial class EssentialsControl : UserControl
{
private EssentialsPlugin Plugin { get; }

public EssentialsControl()
{
InitializeComponent();
}

public EssentialsControl(EssentialsPlugin plugin) : this()
{
Plugin = plugin;
DataContext = plugin.Config;
}

private void UIElement_OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Delete)
return;
var list = (DataGrid)sender;
var items = list.SelectedItems.Cast<AutoCommand>().ToList();
foreach (var item in items)
{
item.Enabled = false;
Plugin.Config.AutoCommands.Remove(item);
}
}
}
}
25 changes: 22 additions & 3 deletions Essentials/EssentialsPlugin.cs
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();
}
}
}

0 comments on commit 3d26352

Please sign in to comment.