1 | using AllocsFixes.PersistentData;
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Reflection;
|
---|
5 | using UnityEngine;
|
---|
6 |
|
---|
7 | namespace AllocsFixes.CustomCommands
|
---|
8 | {
|
---|
9 | public class TeleportPlayer : ConsoleCommand
|
---|
10 | {
|
---|
11 | private MethodInfo NetConnection_SendPackage = null;
|
---|
12 |
|
---|
13 | public TeleportPlayer (ConsoleSdtd cons) : base(cons)
|
---|
14 | {
|
---|
15 | Type typeClientInfo = Assembly.GetAssembly (typeof(ClientInfo)).GetType ("ClientInfo");
|
---|
16 | Type typeNetConnection = typeClientInfo.GetField ("netConnection").FieldType.GetElementType ();
|
---|
17 | MethodInfo[] mis = typeNetConnection.GetMethods ();
|
---|
18 | foreach (MethodInfo mi in mis) {
|
---|
19 | ParameterInfo[] pis = mi.GetParameters ();
|
---|
20 | if (pis.Length == 1) {
|
---|
21 | if (typeof (Package).Equals (pis[0].ParameterType)) {
|
---|
22 | NetConnection_SendPackage = mi;
|
---|
23 | }
|
---|
24 | }
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 | public override string Description ()
|
---|
29 | {
|
---|
30 | return "teleport a player to a given location";
|
---|
31 | }
|
---|
32 |
|
---|
33 | public override string[] Names ()
|
---|
34 | {
|
---|
35 | return new string[] { "teleportplayer", "tele" };
|
---|
36 | }
|
---|
37 |
|
---|
38 | public override void Run (string[] _params)
|
---|
39 | {
|
---|
40 | try {
|
---|
41 | if (_params.Length != 4 && _params.Length != 2) {
|
---|
42 | m_Console.SendResult ("Usage: teleportplayer <entityid|playername|steamid> <x> <y> <z>");
|
---|
43 | m_Console.SendResult (" or: teleportplayer <entityid|playername|steamid> <target entityid|playername|steamid>");
|
---|
44 | } else {
|
---|
45 | Player p1 = PersistentContainer.Instance.Players.GetPlayerByNameOrId (_params [0], true);
|
---|
46 | if (p1 == null) {
|
---|
47 | m_Console.SendResult ("Playername or entity/steamid id not found.");
|
---|
48 | return;
|
---|
49 | }
|
---|
50 | if (!p1.IsOnline) {
|
---|
51 | m_Console.SendResult ("Player not online.");
|
---|
52 | return;
|
---|
53 | }
|
---|
54 |
|
---|
55 | if (_params.Length == 4) {
|
---|
56 | int x = int.MinValue;
|
---|
57 | int y = int.MinValue;
|
---|
58 | int z = int.MinValue;
|
---|
59 |
|
---|
60 | int.TryParse (_params [1], out x);
|
---|
61 | int.TryParse (_params [2], out y);
|
---|
62 | int.TryParse (_params [3], out z);
|
---|
63 |
|
---|
64 | if (x == int.MinValue || y == int.MinValue || z == int.MinValue) {
|
---|
65 | m_Console.SendResult ("At least one of the given coordinates is not a valid integer");
|
---|
66 | return;
|
---|
67 | }
|
---|
68 |
|
---|
69 | p1.Entity.position.x = x;
|
---|
70 | p1.Entity.position.y = y;
|
---|
71 | p1.Entity.position.z = z;
|
---|
72 | } else {
|
---|
73 | Player p2 = PersistentContainer.Instance.Players.GetPlayerByNameOrId (_params [1], true);
|
---|
74 | if (p2 == null) {
|
---|
75 | m_Console.SendResult ("Target playername or entity/steamid id not found.");
|
---|
76 | return;
|
---|
77 | }
|
---|
78 | if (!p2.IsOnline) {
|
---|
79 | m_Console.SendResult ("Target player not online.");
|
---|
80 | return;
|
---|
81 | }
|
---|
82 |
|
---|
83 | p1.Entity.position = p2.Entity.GetPosition();
|
---|
84 | }
|
---|
85 |
|
---|
86 | NetPackage_EntityPosAndRot pkg = new NetPackage_EntityPosAndRot (p1.Entity);
|
---|
87 |
|
---|
88 | if (NetConnection_SendPackage != null) {
|
---|
89 | NetConnection_SendPackage.Invoke (p1.ClientInfo.netConnection [0], new object[] {pkg});
|
---|
90 | }
|
---|
91 | }
|
---|
92 | } catch (Exception e) {
|
---|
93 | Log.Out ("Error in TeleportPlayer.Run: " + e);
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | }
|
---|
98 | }
|
---|