33using System . Linq ;
44using System . Text ;
55using System . Threading . Tasks ;
6+ using Sandbox . Game . Entities ;
7+ using Sandbox . ModAPI ;
68using Torch . Commands ;
79using Torch . Commands . Permissions ;
10+ using VRage . Game . Entity ;
811using VRage . Game . ModAPI ;
912using VRage . ModAPI ;
1013using 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}
0 commit comments