source: binary-improvements/7dtd-server-fixes/src/CustomCommands/TeleportPlayer.cs@ 218

Last change on this file since 218 was 203, checked in by alloc, 10 years ago

fixes

File size: 2.9 KB
RevLine 
[163]1using AllocsFixes.PersistentData;
2using System;
3using System.Collections.Generic;
[203]4using System.Reflection;
[189]5using UnityEngine;
[163]6
7namespace AllocsFixes.CustomCommands
8{
9 public class TeleportPlayer : ConsoleCommand
10 {
[203]11 private MethodInfo NetConnection_SendPackage = null;
12
[163]13 public TeleportPlayer (ConsoleSdtd cons) : base(cons)
14 {
[203]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 }
[163]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 {
[189]41 if (_params.Length != 4 && _params.Length != 2) {
[163]42 m_Console.SendResult ("Usage: teleportplayer <entityid|playername|steamid> <x> <y> <z>");
[189]43 m_Console.SendResult (" or: teleportplayer <entityid|playername|steamid> <target entityid|playername|steamid>");
[163]44 } else {
[189]45 Player p1 = PersistentContainer.Instance.Players.GetPlayerByNameOrId (_params [0], true);
46 if (p1 == null) {
[163]47 m_Console.SendResult ("Playername or entity/steamid id not found.");
48 return;
49 }
[189]50 if (!p1.IsOnline) {
[163]51 m_Console.SendResult ("Player not online.");
52 return;
53 }
54
[189]55 if (_params.Length == 4) {
56 int x = int.MinValue;
57 int y = int.MinValue;
58 int z = int.MinValue;
[163]59
[189]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();
[163]84 }
85
[189]86 NetPackage_EntityPosAndRot pkg = new NetPackage_EntityPosAndRot (p1.Entity);
[163]87
[203]88 if (NetConnection_SendPackage != null) {
89 NetConnection_SendPackage.Invoke (p1.ClientInfo.netConnection [0], new object[] {pkg});
90 }
[163]91 }
92 } catch (Exception e) {
93 Log.Out ("Error in TeleportPlayer.Run: " + e);
94 }
95 }
[174]96
[163]97 }
98}
Note: See TracBrowser for help on using the repository browser.