forked from Lulzboat-Mods/Enhanced-emotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmotes.cs
185 lines (156 loc) · 5.67 KB
/
Emotes.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
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
183
184
185
using CitizenFX.Core;
using CitizenFX.Core.Native;
using CitizenFX.Core.UI;
using NativeUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TinyJson;
namespace Emotes
{
public class Emotes : BaseScript
{
string emotesText = string.Empty;
List<Emote> emotes = new List<Emote>();
List<string> errors = new List<string>();
Config config = new Config();
bool isEmotePlaying = false;
MenuPool menuPool;
UIMenu mainMenu;
public Emotes()
{
// FiveM related things
if (Game.IsLoading)
EventHandlers["onPlayerJoining"] += new Action<dynamic, dynamic>(OnPlayerJoining);
else
OnPlayerJoining(null, null);
Tick += CancelEmoteTick;
}
async Task CancelEmoteTick()
{
//if (menuLoaded)
menuPool.ProcessMenus();
// Cancel emote when player is moving
if ((Game.IsControlJustReleased(0, Control.MoveUpOnly) ||
Game.IsControlJustReleased(0, Control.MoveDownOnly) ||
Game.IsControlJustReleased(0, Control.MoveLeftOnly) ||
Game.IsControlJustReleased(0, Control.MoveRightOnly)) && isEmotePlaying)
{
CancelEmote();
}
else if (Game.IsControlJustPressed(0, Control.ReplayShowhotkey))
mainMenu.Visible = !mainMenu.Visible;
}
#region Methods
void LoadConfig()
{
string configFile = Function.Call<string>(Hash.LOAD_RESOURCE_FILE, "emotes", "config.ini");
Debug.WriteLine("{0}", configFile);
string[] lines = configFile.Split('\n');
foreach (string line in lines)
{
string[] content = line.Split('=');
if (content[0].ToLower() == "jsonfile")
{
config.JsonFile = content[1];
config.JsonFile = config.JsonFile.Replace("\n", "");
}
}
}
void LoadJson()
{
string jsonString = string.Empty;
string fileName = string.Empty;
foreach (char c in config.JsonFile)
{
if ((int)c != 13)
fileName += c;
}
jsonString = Function.Call<string>(Hash.LOAD_RESOURCE_FILE, "emotes", fileName);
var jsonObject = (Dictionary<string, object>)jsonString.FromJson<object>();
var emoteList = (List<object>)jsonObject["emotes"];
foreach (Dictionary<string, object> emote in emoteList)
{
emotes.Add(new Emote()
{
EmoteType = (string)emote["hash"],
Command = (string)emote["title"],
Description = (string)emote["description"],
Delay = (int)emote["delay"],
PlayEnterAnim = (bool)emote["playEnterAnim"]
});
}
errors.Add((string)jsonObject["errorVehicle"]);
errors.Add((string)jsonObject["errorUnexpected"]);
errors.Add((string)jsonObject["errorPlayerID"]);
foreach (Emote emote in emotes)
{
if (emotesText == string.Empty)
emotesText += emote.Command;
else
emotesText += ", " + emote.Command;
}
}
void PrintEmoteList()
{
TriggerEvent("chatMessage", "ALERT", new int[] { 255, 0, 0 } , emotesText);
}
void CancelEmote()
{
Function.Call(Hash.CLEAR_PED_TASKS, Game.PlayerPed);
isEmotePlaying = false;
}
void PlayEmote(Emote emote)
{
if (Game.PlayerPed != null)
{
bool isInVehicle = Function.Call<bool>(Hash.IS_PED_IN_ANY_VEHICLE, Game.PlayerPed, true);
if (!isInVehicle)
{
Function.Call(Hash.TASK_START_SCENARIO_IN_PLACE, Game.PlayerPed, emote.EmoteType, emote.Delay, emote.PlayEnterAnim);
isEmotePlaying = true;
Screen.ShowNotification(emote.Description);
}
else
Screen.ShowNotification(errors.ElementAt(0));
}
else
Screen.ShowNotification(errors.ElementAt(2));
}
#endregion
#region Events
void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
{
Emote emote = emotes.FirstOrDefault(o => o.Description == item.Text);
if (emote != null)
PlayEmote(emote);
else
Screen.ShowNotification(errors.ElementAt(1));
mainMenu.Visible = false;
}
// TODO: Should check if player already loaded
void OnPlayerJoining(dynamic arg1, dynamic arg2)
{
if (emotes.Count != 0)
return;
// Récupère les configs et la liste d'emote
LoadConfig();
LoadJson();
// Crée le menu
menuPool = new MenuPool();
mainMenu = new UIMenu("Emotes", "Select an emote")
{
MouseControlsEnabled = false
};
menuPool.Add(mainMenu);
mainMenu.OnItemSelect += OnItemSelect;
foreach (Emote emote in emotes)
{
mainMenu.AddItem(new UIMenuItem(emote.Description));
}
menuPool.RefreshIndex();
}
#endregion
}
}