/// <summary> ///Checks if grid will violate limit on conversion and updates limits after /// </summary> /// <param name="__instance"></param> /// <returns></returns> private static bool ToStatic(MyCubeGrid __instance) { if (!BlockLimiterConfig.Instance.EnableLimits || !BlockLimiterConfig.Instance.EnableConvertBlock) { return(true); } var grid = __instance; if (grid == null) { Log.Warn("Null grid in GridChange handler"); return(true); } if (grid.GridSizeEnum == MyCubeSize.Small) { return(true); } var remoteUserId = MyEventContext.Current.Sender.Value; var playerId = Utilities.GetPlayerIdFromSteamId(remoteUserId); if (Grid.AllowConversion(grid, out var blocks, out var count, out var limitName) || remoteUserId == 0 || playerId == 0) { var gridId = grid.EntityId; Task.Run(() => { Thread.Sleep(100); GridCache.TryGetGridById(gridId, out var newStateGrid); if (newStateGrid == null) { return; } UpdateLimits.GridLimit(newStateGrid); }); return(true); } var msg = Utilities.GetMessage(BlockLimiterConfig.Instance.DenyMessage, blocks, limitName, count); if (MySession.Static.Players.IsPlayerOnline(playerId)) { BlockLimiter.Instance.Torch.CurrentSession.Managers.GetManager <ChatManagerServer>()? .SendMessageAsOther(BlockLimiterConfig.Instance.ServerName, msg, Color.Red, remoteUserId); } BlockLimiter.Instance.Log.Info( $"Grid conversion blocked from {MySession.Static.Players.TryGetPlayerBySteamId(remoteUserId).DisplayName} due to possible violation"); Utilities.SendFailSound(remoteUserId); Utilities.ValidationFailed(); return(false); }
/// <summary> ///Checks if grid will violate limit on conversion and updates limits after /// </summary> /// <param name="__instance"></param> /// <returns></returns> private static bool ToStatic(MyCubeGrid __instance) { if (!BlockLimiterConfig.Instance.EnableLimits || !BlockLimiterConfig.Instance.EnableConvertBlock) { return(true); } var grid = __instance; if (grid == null) { if (BlockLimiterConfig.Instance.EnableLog) { BlockLimiter.Instance.Log.Warn("Null grid in GridChange handler"); } return(true); } if (grid.GridSizeEnum == MyCubeSize.Small) { return(true); } var remoteUserId = MyEventContext.Current.Sender.Value; var playerId = Utilities.GetPlayerIdFromSteamId(remoteUserId); if (Grid.AllowConversion(grid) || remoteUserId == 0 || playerId == 0) { var gridId = grid.EntityId; Task.Run(() => { Thread.Sleep(100); GridCache.TryGetGridById(gridId, out var newStateGrid); if (newStateGrid == null) { return; } UpdateLimits.GridLimit(newStateGrid); }); return(true); } MyVisualScriptLogicProvider.SendChatMessage($"{BlockLimiterConfig.Instance.DenyMessage}", BlockLimiterConfig.Instance.ServerName, playerId, MyFontEnum.Red); if (BlockLimiterConfig.Instance.EnableLog) { BlockLimiter.Instance.Log.Info( $"Grid conversion blocked from {MySession.Static.Players.TryGetPlayerBySteamId(remoteUserId).DisplayName} due to violation"); } Utilities.SendFailSound(remoteUserId); Utilities.ValidationFailed(); return(false); }
private static void AddBlocks(MyShipMergeBlock __instance) { var id = __instance.CubeGrid.EntityId; Task.Run((() => { Thread.Sleep(10000); if (!GridCache.TryGetGridById(id, out var grid)) { return; } UpdateLimits.GridLimit(grid); })); }
private static void MyCubeGridOnOnSplitGridCreated(MyCubeGrid grid) { if (grid == null) { return; } Task.Run(() => { Thread.Sleep(100); var entity = MyEntities.GetEntityByName(grid.Name); var newStateGrid = entity as MyCubeGrid; if (newStateGrid == null) { return; } UpdateLimits.GridLimit(newStateGrid); }); }
/// <summary> /// Updates limits on grid split /// </summary> /// <param name="originalGrid"></param> private static void OnCreateSplit(MyCubeGrid originalGrid) { if (!BlockLimiterConfig.Instance.EnableLimits) { return; } if (originalGrid == null) { return; } Task.Run(() => { Thread.Sleep(100); if (!GridCache.TryGetGridById(originalGrid.EntityId, out var newStateGrid)) { return; } UpdateLimits.GridLimit(newStateGrid); }); }
/// <summary> /// Updates limits on grid split /// </summary> /// <param name="from"></param> /// <param name="to"></param> private static void OnCreateSplit(ref MyCubeGrid from, ref MyCubeGrid to) { if (!BlockLimiterConfig.Instance.EnableLimits) { return; } var toBlocks = new HashSet <MySlimBlock>(to.CubeBlocks); if (toBlocks.Count == 0) { Log.Warn("Could not update count after grid separation"); return; } foreach (var block in toBlocks) { Block.DecreaseCount(block.BlockDefinition, block.BuiltBy == block.OwnerId ? new List <long> { block.BuiltBy } : new List <long> { block.BuiltBy, block.OwnerId }, 1, @from.EntityId); } var grid = from; if (grid == null) { return; } var removeSmallestGrid = false; var owners = new HashSet <long>(GridCache.GetOwners(from)); owners.UnionWith(GridCache.GetBuilders(grid)); if (owners.Count == 0) { return; } foreach (var owner in owners) { if (!Grid.CountViolation(grid, owner)) { continue; } removeSmallestGrid = true; break; } if (!removeSmallestGrid || BlockLimiterConfig.Instance.BlockType != BlockLimiterConfig.BlockingType.Hard) { return; } var grid1 = from; var grid2 = to; BlockLimiter.Instance.Torch.InvokeAsync(() => { Thread.Sleep(100); if (grid1.BlocksCount > grid2.BlocksCount) { grid2.SendGridCloseRequest(); UpdateLimits.GridLimit(grid1); } else { grid1.SendGridCloseRequest(); UpdateLimits.GridLimit(grid2); } }); }
public static void ResetLimits(bool updateGrids = true, bool updatePlayers = true, bool updateFactions = true) { if (!BlockLimiterConfig.Instance.EnableLimits) { foreach (var limit in BlockLimiterConfig.Instance.AllLimits) { limit.FoundEntities.Clear(); } return; } if (updateGrids) { var grids = new HashSet <MyCubeGrid>(); GridCache.GetGrids(grids); Task.Run(() => { Thread.Sleep(500); foreach (var grid in grids) { if (grid == null) { continue; } Parallel.Invoke(() => { Thread.Sleep(100); UpdateLimits.GridLimit(grid); }); } }); } if (updatePlayers) { Task.Run(() => { Thread.Sleep(300); foreach (var player in MySession.Static.Players.GetAllPlayers()) { if (player.SteamId == 0) { continue; } var identity = Utilities.GetPlayerIdFromSteamId(player.SteamId); if (identity == 0) { continue; } Parallel.Invoke(() => UpdateLimits.PlayerLimit(identity)); } }); } if (updateFactions) { Task.Run(() => { Thread.Sleep(100); foreach (var(id, faction) in MySession.Static.Factions.Factions) { if (faction.IsEveryoneNpc() || id == 0) { continue; } Parallel.Invoke(() => UpdateLimits.FactionLimit(id)); } }); } }
public static void ResetLimits(bool updateGrids = true, bool updatePlayers = true, bool updateFactions = true) { if (!BlockLimiterConfig.Instance.EnableLimits) { foreach (var limit in BlockLimiterConfig.Instance.AllLimits) { limit.FoundEntities.Clear(); } return; } if (updateGrids) { var grids = new HashSet <MyCubeGrid>(); GridCache.GetGrids(grids); Task.Run(() => { Parallel.ForEach(grids, grid => { if (grid == null) { return; } UpdateLimits.GridLimit(grid); }); }); } if (updatePlayers) { var players = MySession.Static.Players.GetAllPlayers(); if (players.Count > 0) { Task.Run(() => { Parallel.ForEach(players, player => { if (player.SteamId == 0) { return; } var identity = Utilities.GetPlayerIdFromSteamId(player.SteamId); if (identity == 0) { return; } UpdateLimits.PlayerLimit(identity); }); }); } } if (updateFactions) { Task.Run(() => { Parallel.ForEach(MySession.Static.Factions, factionInfo => { var(id, faction) = factionInfo; if (faction.IsEveryoneNpc() || id == 0) { return; } UpdateLimits.FactionLimit(id); }); }); } }
/// <summary> /// Updates limits on grid split /// </summary> /// <param name="originalGrid"></param> private static void OnCreateSplit(ref MyCubeGrid from, ref MyCubeGrid to) { if (!BlockLimiterConfig.Instance.EnableLimits) { return; } var toBlocks = new HashSet <MySlimBlock>(to.CubeBlocks); if (toBlocks.Count == 0) { Log.Error("Not updated breakage"); return; } foreach (var block in toBlocks) { if (block.BuiltBy == block.OwnerId) { Block.DecreaseCount(block.BlockDefinition, block.BuiltBy, 1, from.EntityId); } else { Block.DecreaseCount(block.BlockDefinition, block.BuiltBy, 1, from.EntityId); Block.DecreaseCount(block.BlockDefinition, block.OwnerId); } } var grid = from; if (grid == null) { return; } var removeSmallestGrid = false; var owners = GridCache.GetOwners(from); if (owners == null || owners.Count == 0) { return; } foreach (var owner in owners) { if (!Grid.CountViolation(grid, owner)) { continue; } removeSmallestGrid = true; break; } if (!removeSmallestGrid) { return; } var grid1 = from; var grid2 = to; BlockLimiter.Instance.Torch.InvokeAsync(() => { Thread.Sleep(100); if (grid1.BlocksCount > grid2.BlocksCount) { grid2.SendGridCloseRequest(); UpdateLimits.GridLimit(grid1); } else { grid1.SendGridCloseRequest(); UpdateLimits.GridLimit(grid2); } }); }