-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEventHandler.cs
More file actions
182 lines (164 loc) · 8.49 KB
/
EventHandler.cs
File metadata and controls
182 lines (164 loc) · 8.49 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections.Generic;
using System.Linq;
using LabApi.Events.Arguments.PlayerEvents;
using LabApi.Events.CustomHandlers;
using SSMenuSystem.Features;
using SSMenuSystem.Features.Wrappers;
using UserSettings.ServerSpecific;
using MEC;
using Exiled.Events.EventArgs.Player;
using Log = SSMenuSystem.Features.Log;
namespace SSMenuSystem
{
// ReSharper disable once ClassNeverInstantiated.Global
internal class EventHandler : CustomEventsHandler
{
public override void OnPlayerJoined(PlayerJoinedEventArgs ev) => Timing.RunCoroutine(Parameters.SyncAll(ev.Player.ReferenceHub));
public override void OnPlayerLeft(PlayerLeftEventArgs ev) => Menu.DeletePlayer(ev.Player.ReferenceHub);
public override void OnPlayerGroupChanged(PlayerGroupChangedEventArgs ev) =>
SyncChangedGroup(ev.Player.ReferenceHub);
private static void SyncChangedGroup(ReferenceHub hub)
{
Timing.CallDelayed(0.1f, () =>
{
if (Parameters.SyncCache.ContainsKey(hub))
return;
Menu menu = Menu.GetCurrentPlayerMenu(hub);
menu?.Reload(hub);
if (menu == null)
Menu.LoadForPlayer(hub, null);
});
}
public static void OnReceivingInput(ReferenceHub hub, ServerSpecificSettingBase ss)
{
Log.Debug("Received input for hub " + hub.nicknameSync.MyNick + ": " + ss.Label + " - " + ss.SettingId + "(" + ss.GetType().Name + ")");
try
{
if (Parameters.SyncCache.TryGetValue(hub, out List<ServerSpecificSettingBase> value))
{
value.Add(ss);
Log.Debug("received value that been flagged as \"SyncCached\". Redirected values to Cache.");
return;
}
// return to menu
if (ss.SettingId == -999)
{
Menu.LoadForPlayer(hub, null);
return;
}
if (ss.OriginalDefinition != null)
{
ss.Label = ss.OriginalDefinition.Label;
ss.HintDescription = ss.OriginalDefinition.HintDescription;
ss.SettingId = ss.OriginalDefinition.SettingId;
}
else // is a pin or header
ss.SettingId -= Menu.GetCurrentPlayerMenu(hub)?.Hash ?? 0;
Log.Debug("Values after Originial definition: " + ss.Label + " - " + ss.SettingId + "(" + ss.GetType().Name + ")");
// check permissions
Menu menu = Menu.GetCurrentPlayerMenu(hub);
if (!menu?.CheckAccess(hub) ?? false)
{
Log.Warn($"{hub.nicknameSync.MyNick} tried to interact with menu {menu.Name} which is disabled for him.");
Menu.LoadForPlayer(hub, null);
return;
}
// global/local keybinds
if (ss.SettingId > Keybind.Increment && ss is SSKeybindSetting setting)
{
Keybind loadedKeybind = Menu.TryGetKeybinding(hub, ss, menu);
if (loadedKeybind != null)
{
loadedKeybind.Action?.Invoke(hub, setting.SyncIsPressed);
return;
}
}
// load main menu
if (ss.SettingId == 0 && menu != null)
{
// return to upper menu (or main menu)
Menu m = Menu.GetMenu(menu.MenuRelated);
Menu.LoadForPlayer(hub, m);
}
// load method when input is used on specific menu.
else if (menu != null)
{
//if (ss.SettingId < 0)
if (menu.TryGetSubMenu(ss.SettingId, out Menu subMenu))
Menu.LoadForPlayer(hub, subMenu);
else
{
if (menu.InternalSettingsSync[hub].Any(x => x.SettingId == ss.SettingId))
menu.InternalSettingsSync[hub][menu.InternalSettingsSync[hub].FindIndex(x => x.SettingId == ss.SettingId)] = ss;
else
menu.InternalSettingsSync[hub].Add(ss);
ServerSpecificSettingBase s = menu.SentSettings.TryGetValue(hub, out ServerSpecificSettingBase[] customSettings)
? customSettings.FirstOrDefault(b => b.SettingId == ss.SettingId)
: null;
s ??= customSettings?.FirstOrDefault(b => b.SettingId == ss.SettingId - menu.Hash);
s ??= customSettings?.First(b => b.SettingId - menu.Hash == ss.SettingId);
if (customSettings != null)
{
foreach (ServerSpecificSettingBase tkt in customSettings)
{
Log.Debug("Values found in sent settings for target hub: " + tkt.Label + " - " + tkt.SettingId + "(removed hash: " + (tkt.SettingId - menu.Hash) + ")" + "(" + tkt.GetType().Name + ")");
}
}
else
Log.Error("No sent settings found.");
if (s == null)
throw new Exception("Failed to find the sent setting.");
Log.Debug("Value found in sent settings " + s.Label + " - " + s.SettingId + "(" + s.GetType().Name + ")");
switch (s)
{
case Button wBtn:
wBtn.Action?.Invoke(hub, (SSButton)ss);
break;
case Dropdown wDropdown:
wDropdown.Action?.Invoke(hub, wDropdown.Options[((SSDropdownSetting)ss).SyncSelectionIndexRaw], ((SSDropdownSetting)ss).SyncSelectionIndexRaw, (SSDropdownSetting)ss);
break;
case Plaintext wPlaintext:
wPlaintext.OnChanged?.Invoke(hub, ((SSPlaintextSetting)ss).SyncInputText, (SSPlaintextSetting)ss);
break;
case Slider wSlider:
wSlider.Action?.Invoke(hub, ((SSSliderSetting)ss).SyncFloatValue, (SSSliderSetting)ss);
break;
case YesNoButton wYesNo:
wYesNo.Action?.Invoke(hub, ((SSTwoButtonsSetting)ss).SyncIsA, (SSTwoButtonsSetting)ss);
break;
}
if (ss.SettingId > menu.Hash)
ss.SettingId -= menu.Hash;
menu.OnInput(hub, ss);
}
}
// load selected menu.
else
{
if (!Menu.Menus.Any(x => x.Id == ss.SettingId))
throw new KeyNotFoundException($"invalid loaded id ({ss.SettingId}). please report this bug to developers.");
Menu m = Menu.Menus.FirstOrDefault(x => x.Id == ss.SettingId);
Menu.LoadForPlayer(hub, m);
}
}
catch (Exception e)
{
Log.Error($"there is a error while receiving input {ss.SettingId} ({ss.Label}): {e.Message}\nActivate Debugger to show full details.");
#if DEBUG
Log.Error(e.ToString());
#else
Log.Debug(e.ToString());
#endif
if (Plugin.Instance.Config.ShowErrorToClient)
{
Features.Utils.SendToPlayer(hub, null, new ServerSpecificSettingBase[]
{
new SSTextArea(-5, $"<color=red><b>{Plugin.Instance.Translation.ServerError}\n{((hub.serverRoles.RemoteAdmin || Plugin.Instance.Config.ShowFullErrorToClient) && Plugin.Instance.Config.ShowFullErrorToModerators ? e.ToString() : Plugin.Instance.Translation.NoPermission)}</b></color>", SSTextArea.FoldoutMode.CollapsedByDefault, Plugin.Instance.Translation.ServerError),
new SSButton(-999, Plugin.Instance.Translation.ReloadButton.Label, Plugin.Instance.Translation.ReloadButton.ButtonText)
});
}
}
}
}
}