|
1 | 1 | namespace EssentialsPlugin.ProcessHandlers |
2 | 2 | { |
3 | 3 | using System.Collections.Generic; |
4 | | - using System.Threading; |
5 | 4 | using EssentialsPlugin.Settings; |
6 | 5 | using EssentialsPlugin.Utility; |
7 | 6 | using Sandbox.Common.ObjectBuilders; |
8 | 7 | using Sandbox.ModAPI; |
9 | | - using SEModAPIInternal.API.Common; |
10 | | - using SEModAPIInternal.API.Entity.Sector.SectorObject; |
11 | 8 |
|
12 | 9 | public class ProcessProtection : ProcessHandlerBase |
13 | 10 | { |
14 | | - public ProcessProtection() |
| 11 | + public ProcessProtection( ) |
15 | 12 | { |
16 | 13 | } |
17 | 14 |
|
18 | | - public override int GetUpdateResolution() |
| 15 | + public override int GetUpdateResolution( ) |
19 | 16 | { |
20 | 17 | return 1000; |
21 | 18 | } |
22 | 19 |
|
23 | | - public override void Handle() |
| 20 | + public override void Handle( ) |
24 | 21 | { |
25 | | - if (!PluginSettings.Instance.ProtectedEnabled) |
| 22 | + if ( !PluginSettings.Instance.ProtectedEnabled ) |
26 | 23 | return; |
27 | 24 |
|
28 | | - HashSet<IMyEntity> entities = new HashSet<IMyEntity>(); |
29 | | - Wrapper.GameAction(() => |
| 25 | + HashSet<IMyEntity> entities = new HashSet<IMyEntity>( ); |
| 26 | + Wrapper.GameAction( ( ) => |
30 | 27 | { |
31 | | - MyAPIGateway.Entities.GetEntities(entities); |
32 | | - }); |
| 28 | + MyAPIGateway.Entities.GetEntities( entities ); |
| 29 | + } ); |
33 | 30 |
|
34 | | - foreach(IMyEntity entity in entities) |
| 31 | + foreach ( IMyEntity entity in entities ) |
35 | 32 | { |
36 | | - if (!(entity is IMyCubeGrid)) |
| 33 | + if ( !( entity is IMyCubeGrid ) ) |
37 | 34 | continue; |
38 | 35 |
|
39 | | - foreach(ProtectedItem item in PluginSettings.Instance.ProtectedItems) |
| 36 | + foreach ( ProtectedItem item in PluginSettings.Instance.ProtectedItems ) |
40 | 37 | { |
41 | | - if (!item.Enabled) |
| 38 | + if ( !item.Enabled ) |
42 | 39 | continue; |
43 | 40 |
|
44 | | - if (entity.EntityId == item.EntityId) |
| 41 | + if ( entity.EntityId == item.EntityId ) |
45 | 42 | { |
46 | | - ProtectedEntity(entity, item); |
| 43 | + ProtectedEntity( entity ); |
47 | 44 | } |
48 | 45 | } |
49 | 46 | } |
50 | 47 |
|
51 | | - base.Handle(); |
| 48 | + base.Handle( ); |
52 | 49 | } |
53 | 50 |
|
54 | | - private void ProtectedEntity(IMyEntity entity, ProtectedItem item) |
| 51 | + private static void ProtectedEntity( IMyEntity entity ) |
55 | 52 | { |
56 | | - //Log.Info(string.Format("Protecting: {0}", entity.EntityId)); |
57 | | - //CubeGridEntity gridEntity = new CubeGridEntity((MyObjectBuilder_CubeGrid)entity.GetObjectBuilder(), entity); |
58 | | - CubeGridEntity gridEntity = (CubeGridEntity)GameEntityManager.GetEntity(entity.EntityId); |
59 | | - MyObjectBuilder_CubeGrid grid = (MyObjectBuilder_CubeGrid)entity.GetObjectBuilder(); |
60 | | - |
61 | | - int count = 0; |
62 | | - while (gridEntity.IsLoading) |
63 | | - { |
64 | | - if (count >= 20) |
65 | | - return; |
66 | | - |
67 | | - Thread.Sleep(100); |
68 | | - count++; |
69 | | - } |
| 53 | + if ( !entity.InScene ) |
| 54 | + return; |
| 55 | + Essentials.Log.Info( "Protecting: {0}", entity.EntityId ); |
| 56 | + IMyCubeGrid cubeGridEntity = entity as IMyCubeGrid; |
| 57 | + if ( cubeGridEntity == null ) |
| 58 | + return; |
70 | 59 |
|
| 60 | + List<IMySlimBlock> blocks = new List<IMySlimBlock>( ); |
| 61 | + cubeGridEntity.GetBlocks( blocks, block => true ); |
71 | 62 | bool found = false; |
72 | | - /* |
73 | | - foreach(CubeBlockEntity block in gridEntity.CubeBlocks) |
| 63 | + foreach ( IMySlimBlock block in blocks ) |
74 | 64 | { |
75 | | - if (block.IntegrityPercent != item.IntegrityIncrease || block.BuildPercent != item.IntegrityIncrease || block.BoneDamage > 0f) |
| 65 | + MyObjectBuilder_CubeBlock objectBuilderCubeBlock = block.GetObjectBuilder( ); |
| 66 | + if ( objectBuilderCubeBlock.BuildPercent < 1f ) |
| 67 | + { |
| 68 | + found = true; |
| 69 | + objectBuilderCubeBlock.BuildPercent = 1f; |
| 70 | + } |
| 71 | + if ( objectBuilderCubeBlock.IntegrityPercent < 1f ) |
76 | 72 | { |
77 | 73 | found = true; |
78 | | - block.FixBones(0, 100); |
79 | | - block.IntegrityPercent = item.IntegrityIncrease; |
80 | | - block.BuildPercent = item.IntegrityIncrease; |
| 74 | + objectBuilderCubeBlock.IntegrityPercent = 1f; |
81 | 75 | } |
| 76 | + if ( objectBuilderCubeBlock.DeformationRatio > 0f ) |
| 77 | + { |
| 78 | + found = true; |
| 79 | + objectBuilderCubeBlock.DeformationRatio = 0; |
| 80 | + } |
| 81 | + |
82 | 82 | } |
83 | | - */ |
84 | | - if(found) |
85 | | - Log.Info(string.Format("Repaired Grid: {0}", gridEntity.EntityId)); |
| 83 | + |
| 84 | + if ( found ) |
| 85 | + Essentials.Log.Info( "Repaired Grid: {0}", entity.EntityId ); |
86 | 86 | } |
87 | 87 | } |
88 | 88 | } |
|
0 commit comments