-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathHomePlayer.cs
140 lines (135 loc) · 5.43 KB
/
HomePlayer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using Rocket.Core;
using Rocket.Core.Logging;
using Rocket.Unturned.Chat;
using Rocket.Unturned.Player;
using UnityEngine;
using Rocket.API.Serialisation;
namespace ZaupHomeCommand
{
public class HomePlayer : UnturnedPlayerComponent
{
private bool GoingHome;
private DateTime LastCalledHomeCommand;
private Vector3 LastCalledHomePos;
private bool waitrestricted;
private byte waittime;
private bool movementrestricted;
private bool cangohome;
private Vector3 bedPos;
private byte bedRot;
private UnturnedPlayer p;
public void Awake()
{
Rocket.Core.Logging.Logger.Log("Homeplayer is awake");
Console.Write("Homeplayer is awake.");
}
protected override void Load()
{
this.GoingHome = false;
this.cangohome = false;
}
public void GoHome(Vector3 bedPos, byte bedRot, UnturnedPlayer player)
{
Rocket.Core.Logging.Logger.Log("starting gohome");
this.waitrestricted = HomeCommand.Instance.Configuration.Instance.TeleportWait;
this.movementrestricted = HomeCommand.Instance.Configuration.Instance.MovementRestriction;
this.p = player;
this.bedPos = Vector3.up + bedPos;
this.bedRot = bedRot;
if (this.waitrestricted)
{
// We have to wait to teleport now find out how long
this.LastCalledHomeCommand = DateTime.Now;
if (HomeCommand.Instance.WaitGroups.ContainsKey("all"))
{
HomeCommand.Instance.WaitGroups.TryGetValue("all", out this.waittime);
}
else
{
if (player.IsAdmin && HomeCommand.Instance.WaitGroups.ContainsKey("admin"))
{
HomeCommand.Instance.WaitGroups.TryGetValue("admin", out this.waittime);
}
else
{
// Either not an admin or they don't get special wait restrictions.
List<RocketPermissionsGroup> hg = R.Permissions.GetGroups(player, true);
if (hg.Count <= 0)
{
Rocket.Core.Logging.Logger.Log("There was an error as a player has no groups!");
}
byte[] time2 = new byte[hg.Count];
for (byte g=0;g<hg.Count;g++)
{
RocketPermissionsGroup hgr = hg[g];
HomeCommand.Instance.WaitGroups.TryGetValue(hgr.Id, out time2[g]);
if (time2[g] <= 0)
{
time2[g] = 60;
}
}
Array.Sort(time2);
// Take the lowest time.
this.waittime = time2[0];
}
}
if (this.movementrestricted)
{
this.LastCalledHomePos = this.transform.position;
UnturnedChat.Say(player, String.Format(HomeCommand.Instance.Configuration.Instance.FoundBedWaitNoMoveMsg, player.CharacterName, this.waittime));
}
else
{
UnturnedChat.Say(player, String.Format(HomeCommand.Instance.Configuration.Instance.FoundBedNowWaitMsg, player.CharacterName, this.waittime));
}
}
else
{
this.cangohome = true;
}
this.GoingHome = true;
this.DoGoHome();
}
private void DoGoHome()
{
Rocket.Core.Logging.Logger.Log("starting dogohome");
if (!this.cangohome) return;
UnturnedChat.Say(this.p, String.Format(HomeCommand.Instance.Configuration.Instance.TeleportMsg, this.p.CharacterName));
this.p.Teleport(this.bedPos, this.bedRot);
this.cangohome = false;
this.GoingHome = false;
}
public void FixedUpdate()
{
if (!this.GoingHome) return;
if (this.p.Dead)
{
// Abort teleport, they died.
UnturnedChat.Say(this.p, String.Format(HomeCommand.Instance.Configuration.Instance.NoTeleportDiedMsg, this.p.CharacterName));
this.GoingHome = false;
this.cangohome = false;
return;
}
if (this.movementrestricted)
{
if (Vector3.Distance(this.p.Position, this.LastCalledHomePos) > 0.1)
{
// Abort teleport, they moved.
UnturnedChat.Say(this.p, String.Format(HomeCommand.Instance.Configuration.Instance.UnableMoveSinceMoveMsg, this.p.CharacterName));
this.GoingHome = false;
this.cangohome = false;
return;
}
}
if (this.waitrestricted)
{
if ((DateTime.Now - this.LastCalledHomeCommand).TotalSeconds < this.waittime) return;
}
// We made it this far, we can go home.
this.cangohome = true;
this.DoGoHome();
}
}
}