View difference between Paste ID: 1cuP8naP and KgqvgNka
SHOW: | | - or go back to the newest paste.
1-
//7 Days To Die - Add prefab(s) to a given position or load a custom Prefabs.xml
1+
2-
//@Author: Alloc => 7dtd-server-fixes => https://7dtd.illy.bz/browser/binary-improvements/7dtd-server-fixes
2+
3-
//@Author: ketchu13 => LoadPrefabs console command => [email protected]
3+
4
using System.IO;
5
6
using System.Xml;
7
8
namespace AllocsFixes.CustomCommands
9
{
10
	public class LoadPrefabs: ConsoleCommand
11
	{
12
		// Generate once (compiler should optimize at least the string constants and the World-requests anyway but this makes the whole thing a bit cleaner)
13
		private readonly string prefabsPath = UnityEngine.Application.dataPath + "/../Data/Prefabs";
14
		private readonly string prefabsXMLpath =  UnityEngine.Application.dataPath + "/../Data/Worlds/Random Gen/PrefabsKFP.xml";
15
		private readonly World world = CommonMappingFunctions.GetGameManager ().World;
16
	
17
		public LoadPrefabs(ConsoleSdtd cons) : base(cons)
18
		{
19
		}
20
21
		public override string Description ()
22
		{
23
			return "Load in game a prefab.";
24
		}
25
26
		public override string[] Names ()
27
		{
28
			return new string[] { "addPrefab", "adp" };
29
		}
30
31-
            
31+
32
		{
33
			try {
34
				if (_params.Length != 5 && _params.Length != 3 && _params.Length != 1) {
35
					m_Console.SendResult ("Usage: addPrefab <prefabname> <x> <y> <z> <rot>");
36
					m_Console.SendResult ("   or: addPrefab <prefabname> <target entityid|playername|steamid> <rot>");
37
					return;
38
				}
39
				if (_params.Length == 1 && _params[0].Equals("All")){
40
					LoadAllPrefabs ();
41
				}
42-
				if (_params.Length == 1 ){
42+
// x != 5 || x != 3 -> always true ;)
43
				if (_params.Length == 5 || _params.Length == 3) {
44
					LoadOnePrefab (_params);
45
				}
46
			} catch (Exception ex) {
47
				Log.Out("LoadPrefab error: " + ex.Message);
48
			}
49
		}
50
51
		private void LoadAllPrefabs ()
52
		{
53
			if (!File.Exists (prefabsXMLpath))
54
			{
55
				m_Console.SendResult ("Prefabs XML not found...");
56
				return;
57
			}
58
			XmlDocument xmlDocument = new XmlDocument();
59
			xmlDocument.LoadXml(File.ReadAllText(prefabsXMLpath));
60
			XmlNodeList xmlNodeList = xmlDocument.SelectNodes("prefabs");
61
			XmlNode xmlNode = xmlNodeList[0];
62
// "Speaking names"
63
			foreach (XmlNode prefabNode in xmlNode.ChildNodes)
64
			{
65
				if (prefabNode is XmlElement)
66
				{
67
					XmlElement prefabElement = (XmlElement)prefabNode;
68
					string prefabName = prefabElement.GetAttribute("name");
69
					string[] pos = prefabElement.GetAttribute("position").Split(',');
70
					string prefabRotation = prefabElement.GetAttribute("rotation");
71
                    				
72
					Vector3i location =  new Vector3i(int.Parse(pos[0]), int.Parse(pos[1]), int.Parse(pos[2]));
73
					int rot = int.Parse(prefabRotation);
74
75
					LoadSinglePrefab(prefabName, location, rot);
76
				}
77
			}
78
		}
79
80
		private void LoadOnePrefab (string[] _params){
81
			int rot = 0;
82
			string fileName = _params[0];
83-
			int rotation = 0;
83+
84
			Vector3i location = default(Vector3i);
85
			if (_params.Length == 5) {
86-
			Vector3i position = default(Vector3i);
86+
87
// I think I did that sanity check the same way somewhere but TryParse tells you if it was a valid integer anyway :)
88
// Only required up here
89
				if (!int.TryParse (_params [1], out x) || !int.TryParse (_params [2], out y) || !int.TryParse (_params [3], out z)) {
90
					m_Console.SendResult ("At least one of the given coordinates is not a valid integer");
91
					return;
92
				}
93
				if (!int.TryParse (_params [4], out rot)) {
94-
				if (!int.TryParse (_params [4], out rotation) ) {
94+
95-
					m_Console.SendResult ("The given rotation is not a valid integer.");
95+
96
				}
97
				location.x = x;
98-
				if (rotation < 0 || rotation > 3) {
98+
				location.y = y;
99-
					m_Console.SendResult ("The given rotation is not a valid rotation value. (Use 0 to 3)");
99+
				location.z = z;
100
			}
101
			if (_params.Length == 3) {
102-
				position.x = x;
102+
103-
				position.y = y;
103+
104-
				position.z = z;
104+
105
					return;
106
				}
107
				if (!p1.IsOnline) {
108
					m_Console.SendResult ("Target player not online.");
109
					return;
110
				}
111
112
				if (!int.TryParse (_params [2], out rot)) {
113
					m_Console.SendResult ("The given rotation is not a valid integer");
114
					return;
115
				}
116-
				if (!int.TryParse (_params [2], out rotation)) {
116+
117
				location = p1.LastPosition;
118
			}
119
120-
				position = p1.LastPosition;
120+
			LoadSinglePrefab (fileName, location, rot);
121
		}
122-
			LoadSinglePrefab (fileName, position, rotation);
122+
		
123
// Reuse common code
124
		private void LoadSinglePrefab (string filename, Vector3i position, int rotation) {
125-
		private void LoadSinglePrefab (string fileName, Vector3i position, int rotation) {
125+
126
				// Only instantiate objects if they are required (i.e. inside this if)
127
				List<BlockChangeInfo> list = new List<BlockChangeInfo>();
128
				Prefab prefab = new Prefab();
129
130
				prefab.Load(directory,fileName);
131-
				prefab.Load(prefabsPath,fileName);
131+
				for (int a = 0; a < rot; a++)
132-
				for (int a = 0; a < rotation; a++)
132+
133
					prefab.RotateY(false);
134
				}
135
				m_Console.SendResult(string.Concat(
136
					"Prefab ",
137
					fileName,
138
					" is Loading. Please wait...")
139
				);
140
				Random rand = new Random(DateTime.Now.Millisecond);
141
142
// Var names again ;)
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);
148+
							cl0008 block = prefab.GetBlock(prefabPos.x, prefabPos.y, prefabPos.z);
149-
							// Use vector arithmetics (Prefab class does not support GetBlock with Vector3i)
149+
// Use vector arithmetics (don't know why Prefab class does not support GetBlock with Vector3i :( )
150-
							blockPos = position + prefabPos;
150+
							blockPos = location + prefabPos;
151-
							c011AA block2 = world.GetBlock(prefabPos);
151+
							cl0008 block2 = world.GetBlock(prefabPos);
152-
							if (block.c821AA != block2.c821AA && (block.k431AA != 0 || prefab.bCopyAirBlocks)) {
152+
							if (block.fd0031 != block2.fd0031 && (block.fd0031 != 0 || prefab.bCopyAirBlocks)) {
153-
								uint int1 = LootContainer.lootPlaceholderMap.Replace(block, rand).c821AA;
153+
								uint uint1 = LootContainer.lootPlaceholderMap.Replace(block, rand).fd0031;
154-
								BlockChangeInfo item = new BlockChangeInfo(blockPos, int1, true);
154+
								BlockChangeInfo item = new BlockChangeInfo(blockPos, uint1, 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
// More appropriate message
165
					m_Console.SendResult(string.Concat("Prefab ", fileName, " contains no blocks..."));
166
				}
167
			} else {
168
				m_Console.SendResult(string.Concat("Prefab ", fileName , " not found..."));
169
			}
170
		}
171
172
173
	}
174
}