public static Vector3D RemoveGridsInSphere( ulong userId, Vector3D startPosition, float radius, RemoveGridTypes removeType ) { List<IMyEntity> entitiesToMove = new List<IMyEntity>( ); BoundingSphereD sphere = new BoundingSphereD( startPosition, radius ); Wrapper.GameAction( ( ) => { entitiesToMove = MyAPIGateway.Entities.GetEntitiesInSphere( ref sphere ); } ); List<IMyEntity> entitiesToRemove = new List<IMyEntity>( ); int count = 0; Wrapper.GameAction( ( ) => { foreach ( IMyEntity entity in entitiesToMove ) { if ( !( entity is IMyCubeGrid ) ) continue; IMyCubeGrid grid = (IMyCubeGrid)entity; MyObjectBuilder_CubeGrid cubeGrid = (MyObjectBuilder_CubeGrid)grid.GetObjectBuilder( ); if ( removeType == RemoveGridTypes.Ships && cubeGrid.IsStatic ) continue; if ( removeType == RemoveGridTypes.Stations && !cubeGrid.IsStatic ) continue; entitiesToRemove.Add( entity ); Communication.SendPrivateInformation( userId, string.Format( "Deleting entity '{0}' at {1}", entity.DisplayName, General.Vector3DToString( entity.GetPosition( ) ) ) ); count++; } } ); for ( int r = entitiesToRemove.Count - 1; r >= 0; r-- ) { IMyEntity entity = entitiesToRemove[ r ]; //MyAPIGateway.Entities.RemoveEntity(entity); CubeGridEntity gridEntity = new CubeGridEntity( (MyObjectBuilder_CubeGrid)entity.GetObjectBuilder( ), entity ); gridEntity.Dispose( ); } Communication.SendPrivateInformation( userId, string.Format( "Total entities removed: {0}", count ) ); return startPosition; }
public void Process() { lock (m_items) { if (!m_items.Any()) return; for (int r = m_items.Count() - 1; r >= 0; r--) { TimedEntityCleanupItem item = m_items[r]; if (DateTime.Now - item.addedTime > TimeSpan.FromSeconds(item.secondsAfterAdding)) { try { IMyEntity entity = null; Wrapper.GameAction(() => { MyAPIGateway.Entities.TryGetEntityById(item.entityId, out entity); }); if(entity != null) { MyObjectBuilder_CubeGrid gridBuilder = CubeGrids.SafeGetObjectBuilder((IMyCubeGrid)entity); if (gridBuilder == null) continue; CubeGridEntity entityToDispose = new CubeGridEntity((MyObjectBuilder_CubeGrid)entity.GetObjectBuilder(), entity); entityToDispose.Dispose(); } else Log.Info("Entity is null"); } catch (Exception ex) { Log.Info(string.Format("Dispose Error: {0}", ex.ToString())); } m_items.RemoveAt(r); } } } }
public override bool HandleCommand(ulong userId, string[] words) { HashSet<IMyEntity> entitiesFound = CubeGrids.ScanCleanup(userId, words); foreach (IMyEntity entity in entitiesFound) { CubeGridEntity removeEntity = new CubeGridEntity((MyObjectBuilder_CubeGrid)entity.GetObjectBuilder(), entity); removeEntity.Dispose(); IMyCubeGrid grid = (IMyCubeGrid)entity; long ownerId = 0; string ownerName = ""; if (grid.BigOwners.Count > 0) { ownerId = grid.BigOwners.First(); ownerName = PlayerMap.Instance.GetPlayerItemFromPlayerId(ownerId).Name; } Log.Info("Cleanup", string.Format("Cleanup Removed Grid - Id: {0} Display: {1} OwnerId: {2} OwnerName: {3}", entity.EntityId, entity.DisplayName, ownerId, ownerName)); } return true; }
// admin deletearea x y z radius public override bool HandleCommand(ulong userId, string[] words) { HashSet<IMyEntity> entities = new HashSet<IMyEntity>(); HashSet<IMyEntity> entitiesToConfirm = new HashSet<IMyEntity>(); HashSet<IMyEntity> entitiesConnected = new HashSet<IMyEntity>(); HashSet<IMyEntity> entitiesFound = new HashSet<IMyEntity>(); Wrapper.GameAction(() => { MyAPIGateway.Entities.GetEntities(entities, x => x is IMyCubeGrid); }); foreach (IMyEntity entity in entities) { if (!(entity is IMyCubeGrid)) continue; IMyCubeGrid grid = (IMyCubeGrid)entity; MyObjectBuilder_CubeGrid gridBuilder = CubeGrids.SafeGetObjectBuilder(grid); if (gridBuilder == null) continue; bool found = false; foreach (MyObjectBuilder_CubeBlock block in gridBuilder.CubeBlocks) { if (block.TypeId == typeof(MyObjectBuilder_Beacon)) { found = true; break; } } if (!found) entitiesToConfirm.Add(grid); } CubeGrids.GetGridsUnconnected(entitiesFound, entitiesToConfirm); foreach (IMyEntity entity in entitiesFound) { CubeGridEntity gridEntity = (CubeGridEntity)GameEntityManager.GetEntity(entity.EntityId); if (gridEntity == null) { Log.Info("A found entity gridEntity was null!"); continue; } Communication.SendPrivateInformation(userId, string.Format("Found entity '{0}' ({1}) at {2} with no beacon.", gridEntity.Name, entity.EntityId, General.Vector3DToString(entity.GetPosition()))); } for(int r = entitiesFound.Count - 1; r >= 0; r--) { //MyAPIGateway.Entities.RemoveEntity(entity); IMyEntity entity = entitiesFound.ElementAt(r); CubeGridEntity gridEntity = new CubeGridEntity((MyObjectBuilder_CubeGrid)entity.GetObjectBuilder(), entity); gridEntity.Dispose(); } Communication.SendPrivateInformation(userId, string.Format("Removed {0} grids with no beacons", entitiesFound.Count)); return true; }
// admin deletearea x y z radius public override bool HandleCommand(ulong userId, string[] words) { if (words.Count() > 3) return false; if (!words.Any()) { Communication.SendPrivateInformation(userId, GetHelp()); return true; } int days = -1; if (!int.TryParse(words[0], out days) || days < 0) { Communication.SendPrivateInformation(userId, string.Format("Invalid argument. Days argument must be an integer that is 0 or greater.")); return true; } // Just assume that anything after the days is going to "ignorenologin" bool removeNoLoginInformation = true; bool removeOwnerless = true; if (words.Count() > 1) { foreach (string word in words) { if (word.ToLower() == "ignorenologin") removeNoLoginInformation = false; if (word.ToLower() == "ignoreownerless") removeOwnerless = false; } } Communication.SendPrivateInformation(userId, string.Format("Scanning for grids with owners that haven't logged in {0} days. (Must Have Login Info={1})", days, removeNoLoginInformation)); HashSet<IMyEntity> entities = new HashSet<IMyEntity>(); Wrapper.GameAction(() => { MyAPIGateway.Entities.GetEntities(entities, x => x is IMyCubeGrid); }); HashSet<IMyEntity> entitiesFound = new HashSet<IMyEntity>(); foreach (IMyEntity entity in entities) { if (!(entity is IMyCubeGrid)) continue; IMyCubeGrid grid = (IMyCubeGrid)entity; CubeGridEntity gridEntity = (CubeGridEntity)GameEntityManager.GetEntity(grid.EntityId); MyObjectBuilder_CubeGrid gridBuilder = CubeGrids.SafeGetObjectBuilder(grid); if (gridBuilder == null) continue; // This entity is protected by whitelist if (PluginSettings.Instance.LoginEntityWhitelist.Length > 0 && PluginSettings.Instance.LoginEntityWhitelist.Contains(grid.EntityId.ToString())) continue; if (CubeGrids.GetAllOwners(gridBuilder).Count < 1 && removeOwnerless) { Communication.SendPrivateInformation(userId, string.Format("Found entity '{0}' ({1}) not owned by anyone.", entity.DisplayName, entity.EntityId)); entitiesFound.Add(entity); continue; } foreach (long player in CubeGrids.GetBigOwners(gridBuilder)) { // This playerId is protected by whitelist if (PluginSettings.Instance.LoginPlayerIdWhitelist.Length > 0 && PluginSettings.Instance.LoginPlayerIdWhitelist.Contains(player.ToString())) continue; MyObjectBuilder_Checkpoint.PlayerItem checkItem = PlayerMap.Instance.GetPlayerItemFromPlayerId(player); if (checkItem.IsDead || checkItem.Name == "") { Communication.SendPrivateInformation(userId, string.Format("Found entity '{0}' ({1}) owned by dead player - ID: {2}", entity.DisplayName, entity.EntityId, player)); entitiesFound.Add(entity); continue; } PlayerItem item = Players.Instance.GetPlayerById(player); if ( item == null ) { if ( removeNoLoginInformation ) { Communication.SendPrivateInformation( userId, string.Format( "Found entity '{0}' ({1}) owned by a player with no login info: {2}", entity.DisplayName, entity.EntityId, checkItem.Name ) ); entitiesFound.Add( entity ); } } else if ( item.LastLogin < DateTime.Now.AddDays( days * -1 ) ) { Communication.SendPrivateInformation( userId, string.Format( "Found entity '{0}' ({1}) owned by inactive player: {2}", entity.DisplayName, entity.EntityId, PlayerMap.Instance.GetPlayerItemFromPlayerId( player ).Name ) ); entitiesFound.Add( entity ); } else if ( item.LastLogin >= DateTime.Now.AddDays( days * -1 ) ) { if ( entitiesFound.Contains( entity ) ) entitiesFound.Remove( entity ); break; } } } Communication.SendPrivateInformation(userId, string.Format("Found {0} grids owned by inactive users", entitiesFound.Count)); foreach(IMyEntity entity in entitiesFound) { if (!(entity is IMyCubeGrid)) continue; CubeGridEntity gridEntity = new CubeGridEntity((MyObjectBuilder_CubeGrid)entity.GetObjectBuilder(), entity); gridEntity.Dispose(); } Communication.SendPrivateInformation(userId, string.Format("Removed {0} grids owned by inactive users", entitiesFound.Count())); return true; }
// /admin movefrom x y z x y z radius public override bool HandleCommand(ulong userId, string[] words) { if (words.Count() != 7 && words.Count() != 0) return false; if (words.Count() != 7) { Communication.SendPrivateInformation(userId, GetHelp()); return true; } // Test Input float test = 0; for(int r = 0; r < 7; r++) { if(!float.TryParse(words[r], out test)) { Communication.SendPrivateInformation(userId, string.Format("The value at position {0} - '{1}' is invalid. Please try the command again.", r + 1, words[r])); return true; } } Vector3D startPosition = new Vector3(float.Parse(words[0]), float.Parse(words[1]), float.Parse(words[2])); Vector3D movePosition = new Vector3(float.Parse(words[3]), float.Parse(words[4]), float.Parse(words[5])); Vector3D difference = startPosition - movePosition; float radius = float.Parse(words[6]); Communication.SendPrivateInformation(userId, string.Format("Moving all grids in a radius of {0} near {1} to {2}", radius, General.Vector3DToString(startPosition), General.Vector3DToString(movePosition))); List<MyObjectBuilder_CubeGrid> gridsToMove = new List<MyObjectBuilder_CubeGrid>(); BoundingSphereD sphere = new BoundingSphereD(startPosition, radius); List<IMyEntity> entitiesToMove = MyAPIGateway.Entities.GetEntitiesInSphere(ref sphere); // Wrapper.GameAction(() => // { foreach (IMyEntity entity in entitiesToMove) { if (!(entity is IMyCubeGrid)) continue; Communication.SendPrivateInformation(userId, string.Format("Moving '{0}' from {1} to {2}", entity.DisplayName, General.Vector3DToString(entity.GetPosition()), General.Vector3DToString(entity.GetPosition() + difference))); gridsToMove.Add((MyObjectBuilder_CubeGrid)entity.GetObjectBuilder()); //MyAPIGateway.Entities.RemoveEntity(entity); CubeGridEntity gridEntity = new CubeGridEntity((MyObjectBuilder_CubeGrid)entity.GetObjectBuilder(), entity); gridEntity.Dispose(); } // }); Log.Info("Entities Removed ... pausing"); Thread.Sleep(5000); Log.Info("Removing entities from closed entities"); Wrapper.GameAction(() => { foreach(IMyEntity entity in entitiesToMove) { if (!(entity is IMyCubeGrid)) continue; Log.Info(string.Format("Removing '{0}' for move", entity.DisplayName)); MyAPIGateway.Entities.RemoveFromClosedEntities(entity); } }); Thread.Sleep(10000); Wrapper.GameAction(() => { foreach(MyObjectBuilder_CubeGrid grid in gridsToMove) { grid.PositionAndOrientation = new MyPositionAndOrientation(grid.PositionAndOrientation.Value.Position + difference, grid.PositionAndOrientation.Value.Forward, grid.PositionAndOrientation.Value.Up); //Log.Info(string.Format("Adding '{0}' for move", grid.DisplayName)); Communication.SendPrivateInformation(userId, string.Format("Adding grid '{0}' back to world.", grid.DisplayName)); SectorObjectManager.Instance.AddEntity(new CubeGridEntity(grid)); Thread.Sleep(1000); } }); Communication.SendPrivateInformation(userId, string.Format("Moved {0} grids", gridsToMove.Count)); return true; }