Skip to content
This repository was archived by the owner on Jun 23, 2023. It is now read-only.

Commit 88d01c3

Browse files
committed
Add feature to grid protection; players can't add blocks to protected grids
1 parent 8def9ba commit 88d01c3

File tree

3 files changed

+123
-34
lines changed

3 files changed

+123
-34
lines changed

EssentialsPlugin/Settings/PluginSettings.cs

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@ public class PluginSettings
8181
private bool _dynamicConcealEnabled;
8282
private float _dynamicConcealDistance;
8383
private bool _dynamicConcealIncludeLargeGrids;
84+
private bool _dynamicConcealIncludeStations;
8485
private string[] _dynamicConcealIgnoreSubTypeList = { };
8586
private bool _dynamicConcealIncludeMedBays;
8687
private bool _dynamicShowMessages;
8788
private bool _dynamicConcealPirates;
88-
private int _dynamicConcealUpdateSpeed;
89+
private bool _dynamicConcealPhysics;
90+
private bool _dynamicConcealProduction;
8991
private bool _dynamicTurretManagementEnabled;
9092
private int _dynamicTurretTargetDistance;
9193
private bool _dynamicTurretAllowExemption;
@@ -124,7 +126,9 @@ public class PluginSettings
124126
private bool _atmosphericCargoShipsEnabled;
125127
private float _atmosphericCargoShipSpawnTime;
126128

127-
private List<TicketPlayerItem> _ticketPlayers;
129+
private List<TicketPlayerItem> _ticketPlayers;
130+
131+
private MTObservableCollection<BlacklistItem> _blacklistItems;
128132

129133
#endregion
130134

@@ -621,7 +625,27 @@ public bool DynamicConcealPirates
621625
Save( );
622626
}
623627
}
624-
628+
629+
public bool DynamicConcealPhysics
630+
{
631+
get {return _dynamicConcealPhysics;}
632+
set
633+
{
634+
_dynamicConcealPhysics = value;
635+
Save();
636+
}
637+
}
638+
639+
public bool DynamicConcealProduction
640+
{
641+
get { return _dynamicConcealProduction; }
642+
set
643+
{
644+
_dynamicConcealProduction = value;
645+
Save();
646+
}
647+
}
648+
625649
public float DynamicConcealDistance
626650
{
627651
get { return _dynamicConcealDistance; }
@@ -642,12 +666,12 @@ public bool ConcealIncludeLargeGrids
642666
}
643667
}
644668

645-
public int DynamicConcealUpdateSpeed
669+
public bool ConcealIncludeStations
646670
{
647-
get {return _dynamicConcealUpdateSpeed;}
671+
get { return _dynamicConcealIncludeStations; }
648672
set
649673
{
650-
_dynamicConcealUpdateSpeed = value;
674+
_dynamicConcealIncludeStations = value;
651675
Save();
652676
}
653677
}
@@ -1001,6 +1025,17 @@ public List<TicketPlayerItem> TicketPlayers
10011025
Save( );
10021026
}
10031027
}
1028+
1029+
public MTObservableCollection<BlacklistItem> BlacklistItems
1030+
{
1031+
get { return _blacklistItems; }
1032+
set
1033+
{
1034+
_blacklistItems = value;
1035+
BlacklistManager.Instance.UpdateBlacklist();
1036+
Save();
1037+
}
1038+
}
10041039
#endregion
10051040

10061041
#region Constructor
@@ -1046,7 +1081,8 @@ public PluginSettings()
10461081

10471082
_dynamicConcealDistance = 8000;
10481083
_dynamicConcealPirates = false;
1049-
_dynamicConcealUpdateSpeed = 500;
1084+
_dynamicConcealPhysics = false;
1085+
_dynamicConcealProduction = true;
10501086
_dynamicShowMessages = false;
10511087
_dynamicTurretTargetDistance = 2000;
10521088
_dynamicTurretManagementEnabled = false;
@@ -1077,21 +1113,24 @@ public PluginSettings()
10771113
_timedCommandsItem = new MTObservableCollection<TimedCommandItem>( );
10781114
_timedCommandsItem.CollectionChanged += ItemsCollectionChanged;
10791115

1116+
_blacklistItems = new MTObservableCollection<BlacklistItem>();
1117+
_blacklistItems.CollectionChanged += ItemsCollectionChanged;
1118+
_blacklistItems.CollectionChanged += BlacklistManager.Instance._blacklistItems_CollectionChanged;
1119+
10801120
_atmosphericCargoShipsEnabled = false;
10811121
_atmosphericCargoShipSpawnTime = 10.0f;
10821122

10831123
_ticketPlayers = new List<TicketPlayerItem>();
10841124
}
1125+
1126+
#endregion
10851127

1128+
#region Loading and Saving
10861129

1087-
#endregion
1088-
1089-
#region Loading and Saving
1090-
1091-
/// <summary>
1092-
/// Loads our settings
1093-
/// </summary>
1094-
public void Load()
1130+
/// <summary>
1131+
/// Loads our settings
1132+
/// </summary>
1133+
public void Load()
10951134
{
10961135
_loading = true;
10971136

EssentialsPlugin/Settings/SettingsProtectedItem.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,19 @@ public long EntityId
1818
get { return entityId; }
1919
set { entityId = value; }
2020
}
21+
22+
private bool protectBlockAdd;
23+
public bool ProtectBockAdd
24+
{
25+
get { return protectBlockAdd; }
26+
set { protectBlockAdd = value; }
27+
}
28+
29+
private string protectBlockWarning;
30+
public string ProtectBlockWarning
31+
{
32+
get {return protectBlockWarning;}
33+
set { protectBlockWarning = value; }
34+
}
2135
}
2236
}

EssentialsPlugin/Utility/Protection.cs

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,86 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Linq;
6-
using System.Threading;
76
using EssentialsPlugin.Settings;
8-
using Sandbox.Common.ObjectBuilders.Definitions;
97
using Sandbox.Game.Entities;
108
using Sandbox.Game.Entities.Cube;
9+
using Sandbox.Game.World;
1110
using Sandbox.ModAPI;
12-
using Sandbox.Game.Weapons;
13-
using Sandbox.ModAPI.Interfaces;
1411
using SEModAPIInternal.API.Common;
1512
using VRage.Game;
13+
using VRage.Game.Entity.EntityComponents;
1614
using VRage.Game.ModAPI;
17-
using VRage.Game.ModAPI.Interfaces;
18-
using VRage.ModAPI;
19-
using VRage.ObjectBuilders;
20-
using VRage.Utils;
2115

22-
public static class Protection
16+
public class Protection
2317
{
24-
private static bool _init = false;
25-
private static DateTime _lastLog;
26-
private static SortedList<IMySlimBlock, float> toDamage = new SortedList<IMySlimBlock, float>( );
18+
private static Protection _instance;
19+
private bool _init = false;
20+
private DateTime _lastLog;
2721

28-
public static void Init( )
22+
public static Protection Instance
23+
{
24+
get
25+
{
26+
if(_instance == null)
27+
_instance = new Protection();
28+
return _instance;
29+
}
30+
}
31+
32+
public void Init( )
2933
{
3034
if ( _init )
3135
return;
3236

3337
_init = true;
3438
MyAPIGateway.Session.DamageSystem.RegisterBeforeDamageHandler( 0, DamageHandler );
39+
RegisterGridHandlers();
40+
}
41+
42+
public void RegisterGridHandlers()
43+
{
44+
foreach ( var item in PluginSettings.Instance.ProtectedItems )
45+
{
46+
var grid = MyEntities.GetEntityById( item.EntityId ) as MyCubeGrid;
47+
if ( grid == null )
48+
{
49+
Essentials.Log.Error( $"Error getting entity in Protection.RegisterGridHandlers. ID: {item.EntityId}" );
50+
continue;
51+
}
52+
53+
grid.OnBlockAdded -= OnBlockAdded;
54+
grid.OnBlockAdded += OnBlockAdded;
55+
}
3556
}
3657

37-
private static void DamageHandler( object target, ref MyDamageInformation info )
58+
private void OnBlockAdded( MySlimBlock block )
59+
{
60+
var protectionItem = PluginSettings.Instance.ProtectedItems.FirstOrDefault( x => x.EntityId == block.CubeGrid.EntityId );
61+
if ( protectionItem == null || !protectionItem.Enabled || !protectionItem.ProtectBockAdd )
62+
return;
63+
64+
if ( block.OwnerId != 0 )
65+
{
66+
var steamId = PlayerMap.Instance.GetSteamIdFromPlayerId( block.OwnerId );
67+
//if ( PlayerManager.Instance.IsUserAdmin( steamId ) )
68+
// return;
69+
70+
Communication.Notification( steamId, MyFontEnum.Red, 5, protectionItem.ProtectBlockWarning ?? "You cannot add blocks to this grid!" );
71+
}
72+
MyAPIGateway.Utilities.InvokeOnGameThread(()=>block.CubeGrid.RazeBlock( block.Position ));
73+
Essentials.Log.Info( $"Removed block from protected grid {protectionItem.EntityId}. Block owner: {PlayerMap.Instance.GetPlayerNameFromPlayerId( block.OwnerId )}" );
74+
}
75+
76+
private void DamageHandler( object target, ref MyDamageInformation info )
3877
{
3978
if ( !PluginSettings.Instance.ProtectedEnabled )
4079
return;
4180

42-
IMySlimBlock block = target as IMySlimBlock;
81+
MySlimBlock block = target as MySlimBlock;
4382
if ( block == null )
4483
return;
4584

46-
IMyCubeGrid grid = block.CubeGrid;
47-
48-
ulong steamId = PlayerMap.Instance.GetSteamId( info.AttackerId );
85+
MyCubeGrid grid = block.CubeGrid;
4986

5087
foreach ( ProtectedItem item in PluginSettings.Instance.ProtectedItems )
5188
{
@@ -59,7 +96,6 @@ private static void DamageHandler( object target, ref MyDamageInformation info )
5996
}
6097
}
6198
}
62-
6399
}
64100
}
65101
}

0 commit comments

Comments
 (0)