forked from Zamirathe/ZaupHomeCommand
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHomePlayer.cs
125 lines (114 loc) · 4.94 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
using System;
using System.Collections;
using System.Collections.Generic;
using Rocket.Core;
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 bool _waitRestricted;
private byte _waitTime;
public bool movementRestricted;
public bool canGoHome;
private Vector3 _bedPos;
private byte _bedRot;
private UnturnedPlayer _player;
public static readonly Dictionary<UnturnedPlayer, HomePlayer> CurrentHomePlayers = new Dictionary<UnturnedPlayer, HomePlayer>();
public void Awake()
{
Rocket.Core.Logging.Logger.Log("Homeplayer is awake");
}
protected override void Load()
{
_goingHome = false;
canGoHome = false;
}
public void GoHome(Vector3 bedPos, byte bedRot, UnturnedPlayer player)
{
Rocket.Core.Logging.Logger.Log("starting gohome");
_waitRestricted = ZaupHomeCommand.Instance.Configuration.Instance.TeleportWait;
movementRestricted = ZaupHomeCommand.Instance.Configuration.Instance.MovementRestriction;
_player = player;
_bedPos = Vector3.up + bedPos + new Vector3(0f, 0.5f, 0f);
_bedRot = bedRot;
if (_waitRestricted)
{
// We have to wait to teleport now find out how long
_lastCalledHomeCommand = DateTime.Now;
if (ZaupHomeCommand.Instance.waitGroups.ContainsKey("all"))
ZaupHomeCommand.Instance.waitGroups.TryGetValue("all", out _waitTime);
else
{
if (player.IsAdmin && ZaupHomeCommand.Instance.waitGroups.ContainsKey("admin"))
ZaupHomeCommand.Instance.waitGroups.TryGetValue("admin", out _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];
ZaupHomeCommand.Instance.waitGroups.TryGetValue(hgr.Id, out time2[g]);
if (time2[g] <= 0)
{
time2[g] = 60;
}
}
Array.Sort(time2);
// Take the lowest time.
_waitTime = time2[0];
}
}
UnturnedChat.Say(player,
movementRestricted
? string.Format(ZaupHomeCommand.Instance.Configuration.Instance.FoundBedWaitNoMoveMsg,
player.CharacterName, _waitTime)
: string.Format(ZaupHomeCommand.Instance.Configuration.Instance.FoundBedNowWaitMsg,
player.CharacterName, _waitTime));
}
else
canGoHome = true;
_goingHome = true;
StartCoroutine(DoGoHome());
}
private IEnumerator DoGoHome()
{
if (_player.Dead)
{
// Abort teleport, they died.
UnturnedChat.Say(_player, string.Format(ZaupHomeCommand.Instance.Configuration.Instance.NoTeleportDiedMsg, _player.CharacterName));
_goingHome = false;
canGoHome = false;
CurrentHomePlayers.Remove(_player);
yield break;
}
Rocket.Core.Logging.Logger.Log("starting dogohome");
if (!canGoHome)
{
CurrentHomePlayers.Remove(_player);
yield break;
}
UnturnedChat.Say(_player, string.Format(ZaupHomeCommand.Instance.Configuration.Instance.TeleportMsg, _player.CharacterName));
_player.Teleport(_bedPos, _bedRot);
canGoHome = false;
_goingHome = false;
CurrentHomePlayers.Remove(_player);
}
public void FixedUpdate()
{
if (_waitRestricted && (DateTime.Now - _lastCalledHomeCommand).TotalSeconds < _waitTime || !_goingHome) return;
// We made it this far, we can go home.
canGoHome = true;
StartCoroutine(DoGoHome());
}
}
}