ketchu13

7dtd-server-fixes - AddPrefab Command

Sep 26th, 2014
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.72 KB | None | 0 0
  1. //7 Days To Die - Add prefab(s) to a given position or load a custom Prefabs.xml
  2. //@Author: Alloc => 7dtd-server-fixes => https://7dtd.illy.bz/browser/binary-improvements/7dtd-server-fixes
  3. //@Author: ketchu13 => LoadPrefabs console command => [email protected]
  4. using System;
  5. using System.Collections.Generic;
  6. using AllocsFixes.PersistentData;
  7. using System.IO;
  8.  
  9. using System.Xml;
  10.  
  11. namespace AllocsFixes.CustomCommands
  12. {
  13.     public class LoadPrefabs: ConsoleCommand
  14.     {
  15.         private readonly string prefabsPath = UnityEngine.Application.dataPath + "/../Data/Prefabs";
  16.         private readonly string prefabsXMLpath =  UnityEngine.Application.dataPath + "/../Data/Worlds/Random Gen/PrefabsKFP.xml";
  17.         private readonly World world = CommonMappingFunctions.GetGameManager ().World;
  18.  
  19.         public LoadPrefabs(ConsoleSdtd cons) : base(cons)
  20.         {
  21.         }
  22.  
  23.         public override string Description ()
  24.         {
  25.             return "Load in game a prefab.";
  26.         }
  27.  
  28.         public override string[] Names ()
  29.         {
  30.             return new string[] { "addPrefab", "adp" };
  31.            
  32.         }
  33.  
  34.         public override void Run (string[] _params)
  35.         {
  36.             try {
  37.                 if (_params.Length != 5 && _params.Length != 3 && _params.Length != 1) {
  38.                     m_Console.SendResult ("Usage: addPrefab <prefabname> <x> <y> <z> <rot>");
  39.                     m_Console.SendResult ("   or: addPrefab <prefabname> <target entityid|playername|steamid> <rot>");
  40.                     return;
  41.                 }
  42.                 if (_params.Length == 1 ){
  43.                     LoadAllPrefabs ();
  44.                 }
  45.                 if (_params.Length == 5 || _params.Length == 3) {
  46.                     LoadOnePrefab (_params);
  47.                 }
  48.             } catch (Exception ex) {
  49.                 Log.Out("LoadPrefab error: " + ex.Message);
  50.             }
  51.         }
  52.  
  53.         private void LoadAllPrefabs ()
  54.         {
  55.             if (!File.Exists (prefabsXMLpath))
  56.             {
  57.                 m_Console.SendResult ("Prefabs XML not found...");
  58.                 return;
  59.             }
  60.             XmlDocument xmlDocument = new XmlDocument();
  61.             xmlDocument.LoadXml(File.ReadAllText(prefabsXMLpath));
  62.             XmlNodeList xmlNodeList = xmlDocument.SelectNodes("prefabs");
  63.             XmlNode xmlNode = xmlNodeList[0];
  64.  
  65.             foreach (XmlNode prefabNode in xmlNode.ChildNodes)
  66.             {
  67.                 if (prefabNode is XmlElement)
  68.                 {
  69.                     XmlElement prefabElement = (XmlElement)prefabNode;
  70.                     string prefabName = prefabElement.GetAttribute("name");
  71.                     string[] pos = prefabElement.GetAttribute("position").Split(',');
  72.                     string prefabRotation = prefabElement.GetAttribute("rotation");
  73.  
  74.                     Vector3i location =  new Vector3i(int.Parse(pos[0]), int.Parse(pos[1]), int.Parse(pos[2]));
  75.                     int rot = int.Parse(prefabRotation);
  76.  
  77.                     LoadSinglePrefab(prefabName, location, rot);
  78.                 }
  79.             }
  80.         }
  81.  
  82.         private void LoadOnePrefab (string[] _params){
  83.             int rotation = 0;
  84.             string fileName = _params[0];
  85.  
  86.             Vector3i position = default(Vector3i);
  87.             if (_params.Length == 5) {
  88.                 int x, y, z;
  89.  
  90.                 if (!int.TryParse (_params [1], out x) || !int.TryParse (_params [2], out y) || !int.TryParse (_params [3], out z)) {
  91.                     m_Console.SendResult ("At least one of the given coordinates is not a valid integer");
  92.                     return;
  93.                 }
  94.                 if (!int.TryParse (_params [4], out rotation) ) {
  95.                     m_Console.SendResult ("The given rotation is not a valid integer.");
  96.                     return;
  97.                 }
  98.                 if (rotation < 0 || rotation > 3) {
  99.                     m_Console.SendResult ("The given rotation is not a valid rotation value. (Use 0 to 3)");
  100.                     return;
  101.                 }
  102.                 position.x = x;
  103.                 position.y = y;
  104.                 position.z = z;
  105.             }
  106.             if (_params.Length == 3) {
  107.                 Player p1 = PersistentContainer.Instance.Players.GetPlayerByNameOrId (_params [1], true);
  108.                 if (p1 == null) {
  109.                     m_Console.SendResult ("Target playername or entity/steamid id not found.");
  110.                     return;
  111.                 }
  112.                 if (!p1.IsOnline) {
  113.                     m_Console.SendResult ("Target player not online.");
  114.                     return;
  115.                 }
  116.                 if (!int.TryParse (_params [2], out rotation)) {
  117.                     m_Console.SendResult ("The given rotation is not a valid integer");
  118.                     return;
  119.                 }
  120.                 position = p1.LastPosition;
  121.             }
  122.             LoadSinglePrefab (fileName, position, rotation);
  123.         }
  124.  
  125.         private void LoadSinglePrefab (string fileName, Vector3i position, int rotation) {
  126.             if (File.Exists(string.Concat(prefabsPath, "/" , fileName, ".tts"))){
  127.  
  128.                 List<BlockChangeInfo> list = new List<BlockChangeInfo>();
  129.                 Prefab prefab = new Prefab();
  130.  
  131.                 prefab.Load(prefabsPath,fileName);
  132.                 for (int a = 0; a < rotation; a++)
  133.                 {
  134.                     prefab.RotateY(false);
  135.                 }
  136.                 m_Console.SendResult(string.Concat(
  137.                     "Prefab ",
  138.                     fileName,
  139.                     " is Loading. Please wait...")
  140.                 );
  141.                 Random rand = new Random(DateTime.Now.Millisecond);
  142.  
  143.                 Vector3i prefabPos = default(Vector3i);
  144.                 Vector3i blockPos = default(Vector3i);
  145.                 for (prefabPos.x = 0; prefabPos.x < prefab.size.x; prefabPos.x++)   {
  146.                     for (prefabPos.z = 0; prefabPos.z < prefab.size.z; prefabPos.z++)   {
  147.                         for (prefabPos.y = 0; prefabPos.y < prefab.size.y; prefabPos.y++)   {
  148.                             c011AA block = prefab.GetBlock(prefabPos.x, prefabPos.y, prefabPos.z);
  149.                             // Use vector arithmetics (Prefab class does not support GetBlock with Vector3i)
  150.                             blockPos = position + prefabPos;
  151.                             c011AA block2 = world.GetBlock(prefabPos);
  152.                             if (block.c821AA != block2.c821AA && (block.k431AA != 0 || prefab.bCopyAirBlocks)) {
  153.                                 uint int1 = LootContainer.lootPlaceholderMap.Replace(block, rand).c821AA;
  154.                                 BlockChangeInfo item = new BlockChangeInfo(blockPos, int1, true);
  155.                                 list.Add(item);
  156.                             }
  157.                         }
  158.                     }
  159.                 }
  160.                 if (list.Count > 0) {
  161.                     CommonMappingFunctions.GetGameManager ().SetBlocksRPC (list);
  162.                     m_Console.SendResult(string.Concat("Your prefab is Loaded: ", fileName));
  163.                 }else {
  164.                     m_Console.SendResult(string.Concat("Prefab ", fileName, " contains no blocks..."));
  165.                 }
  166.             } else {
  167.                 m_Console.SendResult(string.Concat("Prefab ", fileName , " not found..."));
  168.             }
  169.         }
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment