-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_player_cosmetics.sma
237 lines (177 loc) · 6.31 KB
/
api_player_cosmetics.sma
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#pragma semicolon 1
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <hamsandwich>
#include <command_util>
#define PLUGIN "[API] Player Cosmetics"
#define VERSION "1.0.0"
#define AUTHOR "Hedgehog Fog"
#define COSMETIC_CLASSNAME "_cosmetic"
new Trie:g_itPlayerCosmetics[MAX_PLAYERS + 1];
new g_iszCosmeticClassName;
new Float:g_rgflPlayerNextRenderingUpdate[MAX_PLAYERS + 1];
public plugin_precache() {
g_iszCosmeticClassName = engfunc(EngFunc_AllocString, "info_target");
}
public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR);
RegisterHam(Ham_Think, "info_target", "HamHook_Target_Think", .Post = 0);
register_concmd("player_cosmetic_equip", "Command_Equip", ADMIN_CVAR);
register_concmd("player_cosmetic_unequip", "Command_Unequip", ADMIN_CVAR);
}
public plugin_natives() {
register_library("api_player_cosmetic");
register_native("PlayerCosmetic_Equip", "Native_Equip");
register_native("PlayerCosmetic_Unequip", "Native_Unquip");
register_native("PlayerCosmetic_IsEquiped", "Native_IsEquiped");
register_native("PlayerCosmetic_GetEntity", "Native_GetEntity");
}
public client_connect(pPlayer) {
g_itPlayerCosmetics[pPlayer] = TrieCreate();
g_rgflPlayerNextRenderingUpdate[pPlayer] = get_gametime();
}
public client_disconnected(pPlayer) {
for (new TrieIter:iIterator = TrieIterCreate(g_itPlayerCosmetics[pPlayer]); !TrieIterEnded(iIterator); TrieIterNext(iIterator)) {
static pCosmetic;
TrieIterGetCell(iIterator, pCosmetic);
@PlayerCosmetic_Destroy(pCosmetic);
}
TrieDestroy(g_itPlayerCosmetics[pPlayer]);
}
public Native_Equip(iPluginId, iArgc) {
new pPlayer = get_param(1);
new iModelIndex = get_param(2);
return @Player_EquipCosmetic(pPlayer, iModelIndex);
}
public Native_Unquip(iPluginId, iArgc) {
new pPlayer = get_param(1);
new iModelIndex = get_param(2);
return @Player_UnequipCosmetic(pPlayer, iModelIndex);
}
public Native_IsEquiped(iPluginId, iArgc) {
new pPlayer = get_param(1);
new iModelIndex = get_param(2);
return @Player_IsCosmeticEquiped(pPlayer, iModelIndex);
}
public Native_GetEntity(iPluginId, iArgc) {
new pPlayer = get_param(1);
new iModelIndex = get_param(2);
return @Player_GetCosmeticEntity(pPlayer, iModelIndex);
}
public Command_Equip(pPlayer, iLevel, iCId) {
if (!cmd_access(pPlayer, iLevel, iCId, 2)) {
return PLUGIN_HANDLED;
}
static szTarget[32];
read_argv(1, szTarget, charsmax(szTarget));
static szModel[256];
read_argv(2, szModel, charsmax(szModel));
new iTarget = CMD_RESOLVE_TARGET(szTarget);
new iModelIndex = engfunc(EngFunc_ModelIndex, szModel);
for (new pTarget = 1; pTarget <= MaxClients; ++pTarget) {
if (CMD_SHOULD_TARGET_PLAYER(pTarget, iTarget, pPlayer)) {
@Player_EquipCosmetic(pTarget, iModelIndex);
}
}
return PLUGIN_HANDLED;
}
public Command_Unequip(pPlayer, iLevel, iCId) {
if (!cmd_access(pPlayer, iLevel, iCId, 2)) {
return PLUGIN_HANDLED;
}
static szTarget[32]; read_argv(1, szTarget, charsmax(szTarget));
static szModel[256]; read_argv(2, szModel, charsmax(szModel));
new iTarget = CMD_RESOLVE_TARGET(szTarget);
new iModelIndex = engfunc(EngFunc_ModelIndex, szModel);
for (new pTarget = 1; pTarget <= MaxClients; ++pTarget) {
if (CMD_SHOULD_TARGET_PLAYER(pTarget, iTarget, pPlayer)) {
@Player_UnequipCosmetic(pTarget, iModelIndex);
}
}
return PLUGIN_HANDLED;
}
public HamHook_Target_Think(pEntity) {
static szClassName[32];
pev(pEntity, pev_classname, szClassName, charsmax(szClassName));
if (equal(szClassName, COSMETIC_CLASSNAME)) {
@PlayerCosmetic_Think(pEntity);
}
}
@Player_EquipCosmetic(this, iModelIndex) {
if (g_itPlayerCosmetics[this] == Invalid_Trie) {
return -1;
}
static szModelIndex[8];
num_to_str(iModelIndex, szModelIndex, charsmax(szModelIndex));
new pCosmetic = -1;
if (TrieKeyExists(g_itPlayerCosmetics[this], szModelIndex)) {
TrieGetCell(g_itPlayerCosmetics[this], szModelIndex, pCosmetic);
} else {
pCosmetic = @PlayerCosmetic_Create(this, iModelIndex);
TrieSetCell(g_itPlayerCosmetics[this], szModelIndex, pCosmetic);
}
return pCosmetic;
}
bool:@Player_UnequipCosmetic(this, iModelIndex) {
if (g_itPlayerCosmetics[this] == Invalid_Trie) {
return false;
}
static szModelIndex[8];
num_to_str(iModelIndex, szModelIndex, charsmax(szModelIndex));
static pCosmetic;
if (!TrieGetCell(g_itPlayerCosmetics[this], szModelIndex, pCosmetic)) {
return false;
}
@PlayerCosmetic_Destroy(pCosmetic);
TrieDeleteKey(g_itPlayerCosmetics[this], szModelIndex);
return true;
}
bool:@Player_IsCosmeticEquiped(this, iModelIndex) {
if (g_itPlayerCosmetics[this] == Invalid_Trie) {
return false;
}
static szModelIndex[8];
num_to_str(iModelIndex, szModelIndex, charsmax(szModelIndex));
return TrieKeyExists(g_itPlayerCosmetics[this], szModelIndex);
}
@Player_GetCosmeticEntity(this, iModelIndex) {
if (g_itPlayerCosmetics[this] == Invalid_Trie) {
return -1;
}
static szModelIndex[8];
num_to_str(iModelIndex, szModelIndex, charsmax(szModelIndex));
static pCosmetic;
if (!TrieGetCell(g_itPlayerCosmetics[this], szModelIndex, pCosmetic)) {
return -1;
}
return pCosmetic;
}
@PlayerCosmetic_Create(pPlayer, iModelIndex) {
new this = engfunc(EngFunc_CreateNamedEntity, g_iszCosmeticClassName);
set_pev(this, pev_classname, COSMETIC_CLASSNAME);
set_pev(this, pev_movetype, MOVETYPE_FOLLOW);
set_pev(this, pev_aiment, pPlayer);
set_pev(this, pev_owner, pPlayer);
set_pev(this, pev_modelindex, iModelIndex);
set_pev(this, pev_nextthink, get_gametime());
return this;
}
@PlayerCosmetic_Think(this) {
new pOwner = pev(this, pev_owner);
static iRenderMode; iRenderMode = pev(pOwner, pev_rendermode);
static iRenderFx; iRenderFx = pev(pOwner, pev_renderfx);
static Float:flRenderAmt; pev(pOwner, pev_renderamt, flRenderAmt);
static Float:rgflRenderColor[3]; pev(pOwner, pev_rendercolor, rgflRenderColor);
set_pev(this, pev_rendermode, iRenderMode);
set_pev(this, pev_renderamt, flRenderAmt);
set_pev(this, pev_renderfx, iRenderFx);
set_pev(this, pev_rendercolor, rgflRenderColor);
set_pev(this, pev_nextthink, get_gametime() + 0.1);
}
@PlayerCosmetic_Destroy(this) {
set_pev(this, pev_movetype, MOVETYPE_NONE);
set_pev(this, pev_aiment, 0);
set_pev(this, pev_flags, pev(this, pev_flags) | FL_KILLME);
dllfunc(DLLFunc_Think, this);
}