1. This forum is obsolete and read-only. Feel free to contact us at support.keenswh.com

Missing Multiplayer Dependancy?

Discussion in 'Modding API' started by Kyneroth, Nov 24, 2018.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Kyneroth

    Kyneroth Trainee Engineer

    Messages:
    6
    Hi Guys!
    I am pulling my hair out over this one.
    I am currently working on resurrecting an old mod used on the eclipse server. It deals with block limits by disabling them in the game depending on who the block ownership is.
    I can make this work on single player while I am working on it but it stops working when I upload it to the server.
    The code disables the block if set to no ownership. This seems to work fine on both single and server but it seems to fall down when trying to work out if the block limit has been reached while in the dedicated server.
    Below is the code, any help anyone can offer would be really helpful!

    Code:
    using Sandbox.Common.ObjectBuilders;
    using VRage.Game.Components;
    using VRage.ObjectBuilders;
    using Sandbox.ModAPI;
    using VRage.Game.ModAPI;
    using VRage.Game;
    using System.Collections.Generic;
    using SpaceEngineers.Game.ModAPI;
    using System;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Collections.Concurrent;
    using Sandbox.ModAPI.Interfaces;
    using Sandbox.ModAPI.Interfaces.Terminal;
    using System.Timers;
    using VRage.ModAPI;
    
    namespace OEN3Systems
    {
    
    	[MySessionComponentDescriptor(MyUpdateOrder.BeforeSimulation)]
    	public class BlockLimitCore : MySessionComponentBase {
    		public static int RefineryLimit = 8;
    
    		private Dictionary<long, List<long>> Assemblers = new Dictionary<long, List<long>>();
    		public ConcurrentQueue<BlockLimitPair> UpdateQueue = new ConcurrentQueue<BlockLimitPair>();
    		public static BlockLimitCore Instance;
    
    		public override void Init(MyObjectBuilder_SessionComponent sessionComponent){
    			Instance = this;
    		}
    		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    		public override void UpdateBeforeSimulation(){
    			if (MyAPIGateway.Session != null && MyAPIGateway.Session.IsServer)
    			{
    				while (!UpdateQueue.IsEmpty)
    				{
    					BlockLimitPair Pair;
    					if (UpdateQueue.TryDequeue(out Pair)) {
    						try {
    							switch (Pair.Block.BlockDefinition.SubtypeId){
    								case "LargeAssembler":
    									UpdateOwner(Pair.Block.OwnerId, ref Assemblers);
    									UpdatePair(Pair, Assemblers, AssemblerLimit);
    									break;
    								default:
    									break;
    							}
    						}
    						catch (InvalidOperationException)
    						{
    							UpdateQueue.Enqueue(Pair);
    						}
    					}
    					// else nothing
    				}
    			}
    		}
    		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    		private void UpdateOwner(long OwnerId, ref Dictionary<long, List<long>> BlockDict){
    			List<long> BlockIds;
    			if (BlockDict.TryGetValue(OwnerId, out BlockIds))
    			{
    				List<long> NewBlocks = new List<long>();
    				foreach (long BlockId in BlockIds)
    				{
    					IMyEntity ThisEntity = MyAPIGateway.Entities.GetEntityById(BlockId);
    					if (ThisEntity != null && ThisEntity is IMyFunctionalBlock)
    					{
    						IMyFunctionalBlock FBlock = ThisEntity as IMyFunctionalBlock;
    						if (FBlock.OwnerId == OwnerId && FBlock.IsWorking)
    						{
    							NewBlocks.Add(BlockId);
    						}
    					}
    				}
    				BlockDict.Remove(OwnerId);
    				BlockDict.Add(OwnerId, NewBlocks);
    			}
    		}
    		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    		private void UpdatePair(BlockLimitPair Pair, Dictionary<long, List<long>> BlockDict, int Limit){
    			if (Pair.Block.IsWorking)
    			{
    				if (BlockDict.ContainsKey(Pair.Block.OwnerId))
    				{		
    					List<long> Blocks = BlockDict.GetValueOrDefault(Pair.Block.OwnerId);
    					if (Blocks != null && !Blocks.Contains(Pair.Block.EntityId))
    					{
    						if (Blocks.Count < Limit)
    						{
    							Blocks.Add(Pair.Block.EntityId);
    							Pair.LimitReached = false;
    						}
    						else
    						{
    							Pair.Block.Enabled = false;
    							Pair.LimitReached = true;
    						}
    					}
    				}
    				else
    				{
    					List<long> Blocks = new List<long>();
    					Blocks.Add(Pair.Block.EntityId);
    					BlockDict.Add(Pair.Block.OwnerId, Blocks);
    					Pair.LimitReached = false;
    				}
    			}
    			else if (BlockDict.ContainsKey(Pair.Block.OwnerId))
    			{
    				List<long> Blocks = BlockDict.GetValueOrDefault(Pair.Block.OwnerId);
    				Blocks.RemoveAll(x => x == Pair.Block.EntityId);
    			}
    			Pair.Block.RefreshCustomInfo();
    		}
    		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    	}  /// End of Script definitions
    		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    	public class BlockLimitPair{
    		public IMyFunctionalBlock Block;
    		public bool LimitReached;
    	}
    		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    	[MyEntityComponentDescriptor(typeof(MyObjectBuilder_Assembler), false)]
    	public class BlockLimitRefinery : MyGameLogicComponent{
    		private BlockLimitPair Pair = new BlockLimitPair();
    		public override void Init(MyObjectBuilder_EntityBase objectBuilder)
    		{
    			base.Init(objectBuilder);
    			this.NeedsUpdate = MyEntityUpdateEnum.EACH_100TH_FRAME;
    			IMyFunctionalBlock FBlock = Entity as IMyFunctionalBlock;
    			Pair.Block = Entity as IMyFunctionalBlock;
    			FBlock.OwnershipChanged += Lock;
    			FBlock.IsWorkingChanged += Lock;
    			//FBlock.PropertiesChanged += PropertiesChanged;
    			FBlock.AppendingCustomInfo += AppInfo;
    		}
    
    		private void AppInfo(IMyTerminalBlock arg1, StringBuilder arg2)
    		{
    			if (arg1.OwnerId == 0) arg2.AppendLine("Set ownership to enable");
    			if (Pair.LimitReached) arg2.AppendLine(string.Format("Owner has {0} Assemblers on\nTurn another off to turn this one on", BlockLimitCore.RefineryLimit));
    		}
    
    		private void Lock(IMyCubeBlock obj)
    		{
    			if (obj != null)
    			{
    				if (obj.OwnerId == 0 && Pair.Block.IsWorking)
    				{
    					Pair.Block.Enabled = false;
    				}
    				else
    				{
    					BlockLimitCore.Instance.UpdateQueue.Enqueue(Pair);
    				}
    				Pair.Block.RefreshCustomInfo();
    			}
    		}
    		public override MyObjectBuilder_EntityBase GetObjectBuilder(bool copy = false)
    		{
    			return null;
    		}
    	}
    		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    }
     
Thread Status:
This last post in this thread was made more than 31 days old.