-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrpg_furniture.sp
1221 lines (1034 loc) · 42 KB
/
rpg_furniture.sp
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
T-RP
Copyright (C) 2017 Christian Ziegler
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma semicolon 1
#define PLUGIN_AUTHOR "Totenfluch"
#define PLUGIN_VERSION "1.00"
#include <sourcemod>
#include <sdktools>
#include <rpg_apartments>
#include <rpg_npc_core>
#include <rpg_inventory_core>
#include <tConomy>
#include <smlib>
#include <devzones>
#include <tStocks>
#pragma newdecls required
#define MAX_FURNITURE 1024
#define MAX_SPAWNABLE 800
#define MAX_NPCS 8
bool loaded = false;
enum LoadedFurniture {
lfId,
bool:lfActive,
String:lfName[64],
String:lfModelPath[128],
String:lfBuff[64],
Float:lfSize,
lfPrice,
String:lfFlags[8],
lfBaseDurability,
String:lfVendor[128]
}
int LoadedFurnitureItems[MAX_FURNITURE][LoadedFurniture];
int g_iLoadedFurniture;
enum spawnedFurniture {
sfId,
sfLoadedId,
sfRef,
bool:sfIsActive,
String:sfApartment[128],
String:sfOwner[20],
sfDurability
}
int SpawnedFurnitureItems[MAX_SPAWNABLE][spawnedFurniture];
int g_iSpawnedFurniture;
int g_iPlayerPrevButtons[MAXPLAYERS + 1];
char activeZone[MAXPLAYERS + 1][128];
char prevZone[MAXPLAYERS + 1][128];
int g_iLastInteractedWith[MAXPLAYERS + 1];
char dbconfig[] = "gsxh_multiroot";
Database g_DB;
char npctype[128] = "Furniture Vendor";
char g_cLastNpcType[MAXPLAYERS + 1][64];
Handle g_hOnFurnitureInteract;
int g_iDifferentNpcs;
char g_cNpcNames[MAX_NPCS][128];
enum EditItem {
eiRef,
String:eiUniqueId[64],
bool:eiEditing,
bool:eiInAdmin
}
int PlayerEditItems[MAXPLAYERS + 1][EditItem];
public Plugin myinfo =
{
name = "[T-RP] Furniture Core",
author = PLUGIN_AUTHOR,
description = "Adds Furniture to Apartments in T-RP",
version = PLUGIN_VERSION,
url = "https://totenfluch.de"
};
public void OnPluginStart() {
//RegConsoleCmd("sm_furniture", openFurnitureMenu, "Opens the Furniture Menu");
RegAdminCmd("sm_reloadfurniture", cmdReloadFurniture, ADMFLAG_ROOT, "Reload the Furniture");
RegConsoleCmd("sm_builder", cmdBuild, "Edits Furniture");
RegAdminCmd("sm_abuilder", cmdAdminBuilder, ADMFLAG_ROOT, "Opens the Admin Builder Menu");
RegAdminCmd("sm_listfurniture", cmdListFurniture, ADMFLAG_ROOT, "Lists all furniture Items");
HookEvent("round_start", onRoundStart);
char error[255];
g_DB = SQL_Connect(dbconfig, true, error, sizeof(error));
SQL_SetCharset(g_DB, "utf8");
char createTableQuery[4096];
Format(createTableQuery, sizeof(createTableQuery), "CREATE TABLE IF NOT EXISTS t_rpg_furniture ( `Id` BIGINT NOT NULL AUTO_INCREMENT , `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , `playername` VARCHAR(40) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL , `playerid` VARCHAR(20) NOT NULL, `uniqueId` VARCHAR(64) NOT NULL, `map` VARCHAR(64) NOT NULL , `name` VARCHAR(64) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL , `model` VARCHAR(128) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL , `price` INT NOT NULL , `pos_x` FLOAT NOT NULL , `pos_y` FLOAT NOT NULL , `pos_z` FLOAT NOT NULL , `angle_x` FLOAT NOT NULL , `angle_y` FLOAT NOT NULL , `angle_z` FLOAT NOT NULL , `apartmentId` varchar(128) COLLATE utf8_bin NOT NULL , `durability` int(11) NOT NULL , PRIMARY KEY (`Id`)) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_bin;");
SQL_TQuery(g_DB, SQLErrorCheckCallback, createTableQuery);
}
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max) {
/*
@Param1 -> int entity
@Param2 -> char buffs[64] (buffer)
return true on success false if not
*/
CreateNative("furniture_getBuffs", Native_getBuffs);
/*
@Param1 -> int entity
@Param2 -> char flags[8] (buffer)
return true on success false if not
*/
CreateNative("furniture_getFlags", Native_getFlags);
/*
@Param1 -> int entity
return int durability // -1 if failed
*/
CreateNative("furniture_getDurability", Native_getDurability);
/*
@Param1 -> int entity
@Param2 -> int amount
return int durability // -1 if failed
*/
CreateNative("furniture_setDurability", Native_setDurability);
/*
@Param1 -> char zone[128]
return -
*/
CreateNative("furniture_removeAllFurnitureFromApartment", Native_removeAllFurnitureFromApartment);
/*
Forward when a Player interacts with a furniture Item
@Param1 -> int entity
@Param2 -> int client
@Param3 -> char name[64]
@Param4 -> char lfBuf[64]
@Param5 -> char flags[8]
@Param6 -> char ownerId[20]
@Param7 -> int durability
*/
g_hOnFurnitureInteract = CreateGlobalForward("furniture_OnFurnitureInteract", ET_Ignore, Param_Cell, Param_Cell, Param_String, Param_String, Param_String, Param_String, Param_Cell);
}
public int Native_getBuffs(Handle plugin, int numParams) {
int entity = GetNativeCell(1);
int index;
if ((index = findSpawnedEntByRef(EntIndexToEntRef(entity))) == -1)
return 0;
SetNativeString(2, LoadedFurnitureItems[SpawnedFurnitureItems[index][sfLoadedId]][lfBuff], 64);
return 1;
}
public int Native_getFlags(Handle plugin, int numParams) {
int entity = GetNativeCell(1);
int index;
if ((index = findSpawnedEntByRef(EntIndexToEntRef(entity))) == -1)
return 0;
SetNativeString(2, LoadedFurnitureItems[SpawnedFurnitureItems[index][sfLoadedId]][lfFlags], 8);
return 1;
}
public int Native_getDurability(Handle plugin, int numParams) {
int entity = GetNativeCell(1);
int index;
if ((index = findSpawnedEntByRef(EntIndexToEntRef(entity))) == -1)
return -1;
return SpawnedFurnitureItems[index][sfDurability];
}
public int Native_setDurability(Handle plugin, int numParams) {
int entity = GetNativeCell(1);
int index;
if ((index = findSpawnedEntByRef(EntIndexToEntRef(entity))) == -1)
return -1;
return (SpawnedFurnitureItems[index][sfDurability] = GetNativeCell(2));
}
public int Native_removeAllFurnitureFromApartment(Handle plugin, int numParams) {
char zone[128];
GetNativeString(1, zone, sizeof(zone));
removeFurnituresByZone(zone);
}
public void OnMapStart() {
npc_registerNpcType(npctype);
inventory_addItemHandle("Furniture", 4);
inventory_addItemHandle("Apartment", 4);
loadFurniture();
}
public void clearAllLoadedFurniture() {
g_iDifferentNpcs = 0;
for (int i = 0; i < MAX_NPCS; i++)
strcopy(g_cNpcNames[i], 128, "");
g_iLoadedFurniture = 0;
for (int i = 0; i < MAX_FURNITURE; i++)
clearLoadedFurniture(i);
}
public void clearLoadedFurniture(int id) {
LoadedFurnitureItems[id][lfActive] = false;
strcopy(LoadedFurnitureItems[id][lfName], 64, "");
strcopy(LoadedFurnitureItems[id][lfModelPath], 128, "");
strcopy(LoadedFurnitureItems[id][lfFlags], 8, "");
strcopy(LoadedFurnitureItems[id][lfBuff], 64, "");
strcopy(LoadedFurnitureItems[id][lfVendor], 128, "");
LoadedFurnitureItems[id][lfSize] = -1.0;
LoadedFurnitureItems[id][lfPrice] = -1;
LoadedFurnitureItems[id][lfId] = -1;
LoadedFurnitureItems[id][lfBaseDurability] = -1;
}
public void clearAllSpawnedFurniture() {
g_iSpawnedFurniture = 0;
for (int i = 0; i < MAX_SPAWNABLE; i++)
clearSpawnedFurniture(i);
}
public void clearSpawnedFurniture(int id) {
SpawnedFurnitureItems[id][sfId] = -1;
SpawnedFurnitureItems[id][sfLoadedId] = -1;
SpawnedFurnitureItems[id][sfRef] = -1;
SpawnedFurnitureItems[id][sfIsActive] = false;
SpawnedFurnitureItems[id][sfDurability] = -1;
strcopy(SpawnedFurnitureItems[id][sfApartment], 64, "");
strcopy(SpawnedFurnitureItems[id][sfOwner], 20, "");
}
public int getFirstFreeSpawnedSlot() {
for (int i = 0; i < MAX_SPAWNABLE; i++)
if (SpawnedFurnitureItems[i][sfId] == -1 && !SpawnedFurnitureItems[i][sfIsActive])
return i;
return -1;
}
public int getApartmentFurnitureItemsCount(char aId[128]) {
int count = 0;
for (int i = 0; i <= g_iSpawnedFurniture; i++)
if (StrEqual(SpawnedFurnitureItems[i][sfApartment], aId))
count++;
return count;
}
public void PrintFurnitureListByZoneToClient(int client, char aId[128]) {
for (int i = 0; i <= g_iSpawnedFurniture; i++)
if (StrEqual(SpawnedFurnitureItems[i][sfApartment], aId))
printFurnitureById(client, i);
}
public bool loadFurniture() {
clearAllLoadedFurniture();
KeyValues kv = new KeyValues("rpg_furniture");
kv.ImportFromFile("addons/sourcemod/configs/rpg_furniture.txt");
if (!kv.GotoFirstSubKey())
return false;
char buffer[64];
do
{
kv.GetSectionName(buffer, sizeof(buffer));
strcopy(LoadedFurnitureItems[g_iLoadedFurniture][lfName], 64, buffer);
char tempVars[128];
kv.GetString("model", tempVars, 128, "models/props/coop_cementplant/coop_military_crate/coop_military_crate.mdl");
strcopy(LoadedFurnitureItems[g_iLoadedFurniture][lfModelPath], 128, tempVars);
PrecacheModel(tempVars, true);
char tempVars2[64];
kv.GetString("size", tempVars2, 64, "1.0");
LoadedFurnitureItems[g_iLoadedFurniture][lfSize] = StringToFloat(tempVars2);
kv.GetString("price", tempVars2, 64, "1337");
LoadedFurnitureItems[g_iLoadedFurniture][lfPrice] = StringToInt(tempVars2);
kv.GetString("flags", tempVars2, 64, "");
strcopy(LoadedFurnitureItems[g_iLoadedFurniture][lfFlags], 8, tempVars2);
kv.GetString("buff", tempVars2, 64, "");
strcopy(LoadedFurnitureItems[g_iLoadedFurniture][lfBuff], 64, tempVars2);
kv.GetString("durability", tempVars2, 64, "1337");
LoadedFurnitureItems[g_iLoadedFurniture][lfBaseDurability] = StringToInt(tempVars2);
kv.GetString("vendor", tempVars, 128, "Furniture Vendor");
if (tryToRegisterNewNpc(tempVars))
strcopy(g_cNpcNames[g_iDifferentNpcs++], 128, tempVars);
strcopy(LoadedFurnitureItems[g_iLoadedFurniture][lfVendor], 128, tempVars);
LoadedFurnitureItems[g_iLoadedFurniture][lfActive] = true;
LoadedFurnitureItems[g_iLoadedFurniture][lfId] = g_iLoadedFurniture;
g_iLoadedFurniture++;
} while (kv.GotoNextKey());
delete kv;
return true;
}
public bool tryToRegisterNewNpc(char npcName[128]) {
bool contained = false;
if (StrEqual(npcName, ""))
return false;
for (int i = 0; i < MAX_NPCS; i++) {
if (StrEqual(npcName, g_cNpcNames[i]) || StrEqual(npcName, "Furniture Vendor"))
contained = true;
}
if (!contained)
npc_registerNpcType(npcName);
return !contained;
}
public Action cmdReloadFurniture(int client, int args) {
loadFurniture();
PrintToChat(client, "Loaded %i Furnitures", g_iLoadedFurniture);
return Plugin_Handled;
}
public void openFurnitureMenu(int client, char npcType[64]) {
float playerPos[3];
float entPos[3];
if (!isValidClient(client))
return;
if (!IsValidEntity(g_iLastInteractedWith[client]))
return;
GetClientAbsOrigin(client, playerPos);
GetEntPropVector(g_iLastInteractedWith[client], Prop_Data, "m_vecOrigin", entPos);
if (GetVectorDistance(playerPos, entPos) > 100.0)
return;
Handle menu = CreateMenu(furnitureMenuHandler);
int items = 0;
for (int i = 0; i < g_iLoadedFurniture; i++) {
if (StrEqual(npcType, LoadedFurnitureItems[i][lfVendor])) {
char cId[8];
IntToString(i, cId, sizeof(cId));
AddMenuItem(menu, cId, LoadedFurnitureItems[i][lfName]);
items++;
}
}
char menuTitle[128];
Format(menuTitle, sizeof(menuTitle), "Available Furniture: %i Pieces", items);
SetMenuTitle(menu, menuTitle);
DisplayMenu(menu, client, 60);
}
public int furnitureMenuHandler(Handle menu, MenuAction action, int client, int item) {
if (action == MenuAction_Select) {
char info[8];
GetMenuItem(menu, item, info, sizeof(info));
int id = StringToInt(info);
Handle menu2 = CreateMenu(furnitureItemMenuHandler);
SetMenuTitle(menu2, LoadedFurnitureItems[id][lfName]);
char display[64];
Format(display, sizeof(display), "Model: %s", LoadedFurnitureItems[id][lfModelPath]);
AddMenuItem(menu2, "x", display, ITEMDRAW_DISABLED);
Format(display, sizeof(display), "Price: %i", LoadedFurnitureItems[id][lfPrice]);
AddMenuItem(menu2, "x", display, ITEMDRAW_DISABLED);
Format(display, sizeof(display), "Buff: %s", LoadedFurnitureItems[id][lfBuff]);
AddMenuItem(menu2, "x", display, ITEMDRAW_DISABLED);
Format(display, sizeof(display), "Durability: %i", LoadedFurnitureItems[id][lfBaseDurability]);
AddMenuItem(menu2, "x", display, ITEMDRAW_DISABLED);
AddMenuItem(menu2, info, "Buy", tConomy_getCurrency(client) >= LoadedFurnitureItems[id][lfPrice] ? ITEMDRAW_DEFAULT:ITEMDRAW_DISABLED);
DisplayMenu(menu2, client, 60);
}
if (action == MenuAction_End) {
delete menu;
}
}
public int furnitureItemMenuHandler(Handle menu, MenuAction action, int client, int item) {
if (action == MenuAction_Select) {
char info[8];
GetMenuItem(menu, item, info, sizeof(info));
int id = StringToInt(info);
char itemName[128];
strcopy(itemName, sizeof(itemName), LoadedFurnitureItems[id][lfName]);
if (tConomy_getCurrency(client) >= LoadedFurnitureItems[id][lfPrice]) {
char reason[256];
Format(reason, sizeof(reason), "Bought %s", LoadedFurnitureItems[id][lfName]);
tConomy_removeCurrency(client, LoadedFurnitureItems[id][lfPrice], reason);
inventory_givePlayerItem(client, itemName, 100, "", "Furniture", "Apartment Stuff", 0, reason);
}
if (isValidClient(client))
openFurnitureMenu(client, g_cLastNpcType[client]);
}
if (action == MenuAction_End) {
delete menu;
}
}
public void inventory_onItemUsed(int client, char itemname[128], int weight, char category[64], char category2[64], int rarity, char timestamp[64], int slot) {
if (!StrEqual(category, "Furniture"))
return;
int id;
char itemName2[64];
strcopy(itemName2, sizeof(itemName2), itemname);
if ((id = getLoadedIdByName(itemName2)) == -1) {
PrintToChat(client, "[-T-] This is an Invalid Furniture Item (No longer usable)");
return;
}
firstSpawnFurniture(client, id);
}
public void firstSpawnFurniture(int client, int id) {
char playerid[20];
GetClientAuthId(client, AuthId_Steam2, playerid, sizeof(playerid));
float pos[3];
pos = GetAimOrigin(client);
float angles[3];
angles[0] = 0.0;
angles[1] = 0.0;
angles[2] = 0.0;
float clientPos[3];
GetClientAbsOrigin(client, clientPos);
if (GetVectorDistance(clientPos, pos) > 400.0) {
PrintToChat(client, "[-T-] Too far away...");
return;
}
if (!Zone_CheckIfZoneExists(activeZone[client], true, true)) {
char rZone[64];
Zone_getMostRecentActiveZone(client, rZone);
float cpos[3];
GetClientAbsOrigin(client, cpos);
if (!Zone_isPositionInZone(rZone, cpos[0], cpos[1], cpos[2])) {
strcopy(rZone, 64, "");
return;
}
strcopy(activeZone[client], 128, rZone);
PrintToConsole(client, "changed Active Zone to: %s | bug this (ne)", rZone);
PrintToChat(client, "[-T-] (%s) Not an Apartment", activeZone[client]);
return;
}
if (!Zone_isPositionInZone(activeZone[client], pos[0], pos[1], pos[2])) {
PrintToChat(client, "[-T-] (%s) Not in an Apartment", activeZone[client]);
return;
}
if (!apartments_isClientOwner(client, activeZone[client])) {
PrintToChat(client, "[-T-] (%s) You do not own this Apartment", activeZone[client]);
return;
}
int max = getApartmentFurnitureItemsCount(activeZone[client]);
int setMax = apartments_getBuyPrice(activeZone[client]) / 12500;
setMax = setMax >= 50 ? 50 : setMax;
if (max >= setMax) {
PrintToChat(client, "[-T-] (%s) Too much Furniture in this Apartment (%i/%i) - see console for info", activeZone[client], max, setMax);
PrintFurnitureListByZoneToClient(client, activeZone[client]);
return;
}
char uniqueId[64];
Format(uniqueId, sizeof(uniqueId), "%i %s %i", id, playerid, GetTime());
char playername[MAX_NAME_LENGTH + 8];
GetClientName(client, playername, sizeof(playername));
char clean_playername[MAX_NAME_LENGTH * 2 + 16];
SQL_EscapeString(g_DB, playername, clean_playername, sizeof(clean_playername));
char mapName[128];
GetCurrentMap(mapName, sizeof(mapName));
if (!spawnFurniture(id, playerid, pos, angles, uniqueId, activeZone[client], LoadedFurnitureItems[id][lfBaseDurability])) {
PrintToChat(client, "[-T-] Server cap reached; contact an Administrator");
return;
}
char addFurnitureQuery[2048];
Format(addFurnitureQuery, sizeof(addFurnitureQuery), "INSERT IGNORE INTO `t_rpg_furniture`(`Id`, `timestamp`, `playername`, `playerid`, `uniqueId`, `map`, `name`, `model`, `price`, `pos_x`, `pos_y`, `pos_z`, `angle_x`, `angle_y`, `angle_z`, `apartmentId`, `durability`)VALUES(NULL, CURRENT_TIMESTAMP, '%s', '%s', '%s', '%s', '%s', '%s', '%i', '%.2f', '%.2f', '%.2f', '%.2f', '%.2f', '%.2f', '%s', '%i');", clean_playername, playerid, uniqueId, mapName, LoadedFurnitureItems[id][lfName], LoadedFurnitureItems[id][lfModelPath], LoadedFurnitureItems[id][lfPrice], pos[0], pos[1], pos[2], angles[0], angles[1], angles[2], activeZone[client], LoadedFurnitureItems[id][lfBaseDurability]);
SQL_TQuery(g_DB, SQLErrorCheckCallback, addFurnitureQuery);
char itemName2[128];
strcopy(itemName2, sizeof(itemName2), LoadedFurnitureItems[id][lfName]);
DataPack dp = CreateDataPack();
ResetPack(dp, true);
WritePackCell(dp, GetClientUserId(client));
WritePackString(dp, itemName2);
WritePackCell(dp, 1);
WritePackString(dp, "Placed Item");
CreateTimer(0.1, removeItemDelayed, dp);
}
public Action removeItemDelayed(Handle Timer, any data) {
int client;
char itemName[128];
int amount;
char reason[256];
ResetPack(data);
client = GetClientOfUserId(ReadPackCell(data));
ReadPackString(data, itemName, sizeof(itemName));
amount = ReadPackCell(data);
ReadPackString(data, reason, sizeof(reason));
CloseHandle(data);
inventory_removePlayerItems(client, itemName, amount, reason);
}
public bool spawnFurniture(int id, char playerid[20], float pos[3], float angles[3], char uniqueId[64], char apartmentId[128], int durability) {
int spawnedId;
if ((spawnedId = getFirstFreeSpawnedSlot()) == -1)
return false;
int furnitureEnt = CreateEntityByName("prop_dynamic_override");
if (furnitureEnt == -1)
return false;
char modelPath[128];
Format(modelPath, sizeof(modelPath), LoadedFurnitureItems[id][lfModelPath]);
SetEntityModel(furnitureEnt, modelPath);
DispatchKeyValue(furnitureEnt, "Solid", "6");
SetEntProp(furnitureEnt, Prop_Send, "m_nSolidType", 6);
if (StrContains(LoadedFurnitureItems[id][lfFlags], "p") != -1)
SetEntProp(furnitureEnt, Prop_Data, "m_CollisionGroup", COLLISION_GROUP_PUSHAWAY);
else
SetEntProp(furnitureEnt, Prop_Data, "m_CollisionGroup", COLLISION_GROUP_NONE);
SetEntPropFloat(furnitureEnt, Prop_Send, "m_flModelScale", LoadedFurnitureItems[id][lfSize]);
SetEntPropString(furnitureEnt, Prop_Data, "m_iName", uniqueId);
DispatchSpawn(furnitureEnt);
TeleportEntity(furnitureEnt, pos, angles, NULL_VECTOR);
Entity_SetGlobalName(furnitureEnt, LoadedFurnitureItems[id][lfName]);
SpawnedFurnitureItems[spawnedId][sfId] = spawnedId;
SpawnedFurnitureItems[spawnedId][sfLoadedId] = id;
SpawnedFurnitureItems[spawnedId][sfRef] = EntIndexToEntRef(furnitureEnt);
SpawnedFurnitureItems[spawnedId][sfDurability] = durability;
SpawnedFurnitureItems[spawnedId][sfIsActive] = true;
strcopy(SpawnedFurnitureItems[spawnedId][sfApartment], 64, apartmentId);
strcopy(SpawnedFurnitureItems[spawnedId][sfOwner], 20, playerid);
if (spawnedId > g_iSpawnedFurniture)
g_iSpawnedFurniture = spawnedId;
return true;
}
stock float[3] GetAimOrigin(int client) {
float vAngles[3];
float fOrigin[3];
float hOrigin[3];
GetClientEyePosition(client, fOrigin);
GetClientEyeAngles(client, vAngles);
Handle trace = TR_TraceRayFilterEx(fOrigin, vAngles, MASK_SHOT, RayType_Infinite, TraceEntityFilterPlayer);
if (TR_DidHit(trace))
{
TR_GetEndPosition(hOrigin, trace);
CloseHandle(trace);
return hOrigin;
}
CloseHandle(trace);
return hOrigin;
}
public void OnClientPostAdminCheck(int client) {
PlayerEditItems[client][eiRef] = -1;
strcopy(activeZone[client], sizeof(activeZone), "");
PlayerEditItems[client][eiInAdmin] = false;
}
public Action cmdBuild(int client, int args) {
PlayerEditItems[client][eiInAdmin] = false;
int id;
if ((id = GetClientAimTarget(client, false)) < 0) {
PrintToChat(client, "[-T-] Invalid Item");
return Plugin_Handled;
}
char entName[64];
Entity_GetGlobalName(id, entName, sizeof(entName));
char uniqueId[64];
GetEntPropString(id, Prop_Data, "m_iName", uniqueId, sizeof(uniqueId));
char playerid[20];
GetClientAuthId(client, AuthId_Steam2, playerid, sizeof(playerid));
if (StrContains(uniqueId, playerid) < 2) {
PrintToChat(client, "[-T-] You do not own that Item.");
return Plugin_Handled;
}
Menu buildMenu = CreateMenu(buildMenuHandler);
SetMenuTitle(buildMenu, "Modify Furniture (keep Aiming at the Item!)");
AddMenuItem(buildMenu, "edit", "Edit Position");
AddMenuItem(buildMenu, "stash", "Put Furniture in Inventory");
AddMenuItem(buildMenu, "delete", "Delete Furniture");
DisplayMenu(buildMenu, client, 60);
return Plugin_Handled;
}
public int buildMenuHandler(Handle menu, MenuAction action, int client, int item) {
if (action == MenuAction_Select) {
char cValue[32];
GetMenuItem(menu, item, cValue, sizeof(cValue));
int id;
if ((id = GetClientAimTarget(client, false)) < 0) {
PrintToChat(client, "[-T-] Invalid Item");
return 0;
}
char entName[64];
Entity_GetGlobalName(id, entName, sizeof(entName));
char uniqueId[64];
GetEntPropString(id, Prop_Data, "m_iName", uniqueId, sizeof(uniqueId));
char playerid[20];
GetClientAuthId(client, AuthId_Steam2, playerid, sizeof(playerid));
char globalName[128];
Entity_GetGlobalName(id, globalName, sizeof(globalName));
if (StrContains(uniqueId, playerid) < 2) {
PrintToChat(client, "[-T-] You do not own that Item.");
return 0;
}
if (StrEqual(cValue, "edit")) {
strcopy(PlayerEditItems[client][eiUniqueId], 64, uniqueId);
PlayerEditItems[client][eiRef] = EntIndexToEntRef(id);
PlayerEditItems[client][eiEditing] = true;
PlayerEditItems[client][eiInAdmin] = false;
PrintToChat(client, "[-T-] Now editing %s", entName);
PrintToChat(client, "[-T-] Hold R for Placement, W,A,S,D for Angles JUMP for up, Crouch for down and E to Exit");
SetEntityMoveType(client, MOVETYPE_NONE);
} else if (StrEqual(cValue, "stash")) {
if (resetFurnitureByRef(EntIndexToEntRef(id)))
inventory_givePlayerItem(client, globalName, 100, "", "Furniture", "Apartment Stuff", 1, "Stashed Furniture");
} else if (StrEqual(cValue, "delete")) {
if (resetFurnitureByRef(EntIndexToEntRef(id)))
PrintToChat(client, "[-T-] Deleted %s", globalName);
}
}
if (action == MenuAction_End) {
delete menu;
}
return 1;
}
public void resetSlot(int ent) {
int theId = findSpawnedEntByRef(EntIndexToEntRef(ent));
if (theId == -1)
return;
clearSpawnedFurniture(theId);
}
public Action cmdAdminBuilder(int client, int args) {
PlayerEditItems[client][eiInAdmin] = true;
int id;
if ((id = GetClientAimTarget(client, false)) < 0) {
PrintToChat(client, "[-T-] Invalid Item");
return Plugin_Handled;
}
char entName[64];
Entity_GetGlobalName(id, entName, sizeof(entName));
char uniqueId[64];
GetEntPropString(id, Prop_Data, "m_iName", uniqueId, sizeof(uniqueId));
char playerid[20];
GetClientAuthId(client, AuthId_Steam2, playerid, sizeof(playerid));
Menu buildMenu = CreateMenu(adminBuildMenuHandler);
SetMenuTitle(buildMenu, "Modify Furniture (keep Aiming at the Item!)");
AddMenuItem(buildMenu, "edit", "Edit Position");
AddMenuItem(buildMenu, "stash", "Put Furniture in Inventory");
AddMenuItem(buildMenu, "delete", "Delete Furniture");
DisplayMenu(buildMenu, client, 60);
return Plugin_Handled;
}
public int adminBuildMenuHandler(Handle menu, MenuAction action, int client, int item) {
if (action == MenuAction_Select) {
char cValue[32];
GetMenuItem(menu, item, cValue, sizeof(cValue));
int id;
if ((id = GetClientAimTarget(client, false)) < 0) {
PrintToChat(client, "[-T-] Invalid Item");
return 0;
}
char entName[64];
Entity_GetGlobalName(id, entName, sizeof(entName));
char uniqueId[64];
GetEntPropString(id, Prop_Data, "m_iName", uniqueId, sizeof(uniqueId));
char playerid[20];
GetClientAuthId(client, AuthId_Steam2, playerid, sizeof(playerid));
char globalName[128];
Entity_GetGlobalName(id, globalName, sizeof(globalName));
if (StrEqual(cValue, "edit")) {
strcopy(PlayerEditItems[client][eiUniqueId], 64, uniqueId);
PlayerEditItems[client][eiRef] = EntIndexToEntRef(id);
PlayerEditItems[client][eiEditing] = true;
PlayerEditItems[client][eiInAdmin] = true;
PrintToChat(client, "[-T-] Now editing %s", entName);
PrintToChat(client, "[-T-] Hold R for Placement, A & D for Angles JUMP for up, Crouch for down and E to Exit");
SetEntityMoveType(client, MOVETYPE_NONE);
} else if (StrEqual(cValue, "stash")) {
if (resetFurnitureByRef(EntIndexToEntRef(id)))
inventory_givePlayerItem(client, globalName, 100, "", "Furniture", "Apartment Stuff", 1, "Stashed Furniture");
} else if (StrEqual(cValue, "delete")) {
if (resetFurnitureByRef(EntIndexToEntRef(id)))
PrintToChat(client, "[-T-] Deleted %s", globalName);
}
}
if (action == MenuAction_End) {
delete menu;
}
return 1;
}
public bool resetFurnitureByRef(int ref) {
int id;
if ((id = findSpawnedEntByRef(ref)) == -1) {
return false;
}
char uniqueId[64];
if (IsValidEdict(ref)) {
int ent = EntRefToEntIndex(ref);
if (IsValidEntity(ent)) {
GetEntPropString(ent, Prop_Data, "m_iName", uniqueId, sizeof(uniqueId));
AcceptEntityInput(ent, "kill");
} else {
return false;
}
} else {
return false;
}
clearSpawnedFurniture(id);
char mapName[128];
GetCurrentMap(mapName, sizeof(mapName));
char updateFurnitureQuery[256];
Format(updateFurnitureQuery, sizeof(updateFurnitureQuery), "DELETE FROM t_rpg_furniture WHERE map = '%s' AND uniqueId = '%s';", mapName, uniqueId);
SQL_TQuery(g_DB, SQLErrorCheckCallback, updateFurnitureQuery);
return true;
}
public Action OnPlayerRunCmd(int client, int &iButtons, int &iImpulse, float fVelocity[3], float fAngles[3], int &iWeapon, int &tickcount) {
if (IsClientInGame(client) && IsPlayerAlive(client)) {
if (PlayerEditItems[client][eiEditing]) {
if (IsValidEntity(EntRefToEntIndex(PlayerEditItems[client][eiRef]))) {
if (!(g_iPlayerPrevButtons[client] & IN_USE) && iButtons & IN_USE) {
char uId[64];
strcopy(uId, sizeof(uId), PlayerEditItems[client][eiUniqueId]);
updateFurnitureToMySQL(EntRefToEntIndex(PlayerEditItems[client][eiRef]), uId);
PlayerEditItems[client][eiRef] = -1;
PlayerEditItems[client][eiEditing] = false;
strcopy(PlayerEditItems[client][eiUniqueId], 64, "");
PrintToChat(client, "[-T-] Finished Editing");
SetEntityMoveType(client, MOVETYPE_WALK);
} else if (!(g_iPlayerPrevButtons[client] & IN_JUMP) && iButtons & IN_JUMP) {
int ent = EntRefToEntIndex(PlayerEditItems[client][eiRef]);
float pos[3];
GetEntPropVector(ent, Prop_Data, "m_vecOrigin", pos);
pos[2] += 1;
float clientPos[3];
GetClientAbsOrigin(client, clientPos);
if ((GetVectorDistance(clientPos, pos) > 400.0) && !PlayerEditItems[client][eiInAdmin])
PrintToChat(client, "[-T-] Too far away...");
if (Zone_CheckIfZoneExists(activeZone[client], true, true) || PlayerEditItems[client][eiInAdmin]) {
if (Zone_isPositionInZone(activeZone[client], pos[0], pos[1], pos[2]) || PlayerEditItems[client][eiInAdmin]) {
if (apartments_isClientOwner(client, activeZone[client]) || PlayerEditItems[client][eiInAdmin]) {
TeleportEntity(ent, pos, NULL_VECTOR, NULL_VECTOR);
} else {
PrintToChat(client, "[-T-] You do not own this Apartment");
}
} else {
PrintToChat(client, "[-T-] Not in your Apartment");
}
} else {
PrintToChat(client, "[-T-] Not an Apartment");
}
iButtons ^= IN_JUMP;
return Plugin_Changed;
} else if (!(g_iPlayerPrevButtons[client] & IN_DUCK) && iButtons & IN_DUCK) {
int ent = EntRefToEntIndex(PlayerEditItems[client][eiRef]);
float pos[3];
GetEntPropVector(ent, Prop_Data, "m_vecOrigin", pos);
pos[2] -= 1;
float clientPos[3];
GetClientAbsOrigin(client, clientPos);
if ((GetVectorDistance(clientPos, pos) > 400.0) && !PlayerEditItems[client][eiInAdmin])
PrintToChat(client, "[-T-] Too far away...");
if (Zone_CheckIfZoneExists(activeZone[client], true, true) || PlayerEditItems[client][eiInAdmin]) {
if (Zone_isPositionInZone(activeZone[client], pos[0], pos[1], pos[2]) || PlayerEditItems[client][eiInAdmin]) {
if (apartments_isClientOwner(client, activeZone[client]) || PlayerEditItems[client][eiInAdmin]) {
TeleportEntity(ent, pos, NULL_VECTOR, NULL_VECTOR);
} else {
PrintToChat(client, "[-T-] You do not own this Apartment");
}
} else {
PrintToChat(client, "[-T-] Not in your Apartment");
}
} else {
PrintToChat(client, "[-T-] Not an Apartment");
}
iButtons ^= IN_DUCK;
return Plugin_Changed;
} else if (!(g_iPlayerPrevButtons[client] & IN_RELOAD) && iButtons & IN_RELOAD) {
int ent = EntRefToEntIndex(PlayerEditItems[client][eiRef]);
float pos[3];
pos = GetAimOrigin(client);
float clientPos[3];
GetClientAbsOrigin(client, clientPos);
if ((GetVectorDistance(clientPos, pos) > 400.0) && !PlayerEditItems[client][eiInAdmin])
PrintToChat(client, "[-T-] Too far away...");
if (Zone_CheckIfZoneExists(activeZone[client], true, true) || PlayerEditItems[client][eiInAdmin]) {
if (Zone_isPositionInZone(activeZone[client], pos[0], pos[1], pos[2]) || PlayerEditItems[client][eiInAdmin]) {
if (apartments_isClientOwner(client, activeZone[client]) || PlayerEditItems[client][eiInAdmin]) {
TeleportEntity(ent, pos, NULL_VECTOR, NULL_VECTOR);
} else {
PrintToChat(client, "[-T-] You do not own this Apartment");
}
} else {
PrintToChat(client, "[-T-] Not in your Apartment");
}
} else {
PrintToChat(client, "[-T-] Not an Apartment");
}
iButtons ^= IN_RELOAD;
return Plugin_Changed;
} else if (!(g_iPlayerPrevButtons[client] & IN_FORWARD) && iButtons & IN_FORWARD) {
int ent = EntRefToEntIndex(PlayerEditItems[client][eiRef]);
float angles[3];
GetEntPropVector(ent, Prop_Data, "m_angRotation", angles);
if (angles[1] >= 0.0 && angles[1] < 45.0)
angles[1] = 45.0;
else if (angles[1] >= 45.0 && angles[1] < 90.0)
angles[1] = 90.0;
else if (angles[1] >= 90.0 && angles[1] < 135.0)
angles[1] = 135.0;
else if (angles[1] >= 135.0 && angles[1] < 180.0)
angles[1] = 180.0;
else if (angles[1] >= 180.0 && angles[1] < 225.0)
angles[1] = 225.0;
else if (angles[1] >= 225.0 && angles[1] < 270.0)
angles[1] = 270.0;
else if (angles[1] >= 270.0 && angles[1] < 315.0)
angles[1] = 315.0;
else if (angles[1] >= 315.0 && angles[1] < 360.0)
angles[1] = 0.0;
else
angles[1] = 0.0;
int degrees = RoundToNearest(angles[1]);
degrees = degrees % 360;
angles[1] = float(degrees);
TeleportEntity(ent, NULL_VECTOR, angles, NULL_VECTOR);
} else if (!(g_iPlayerPrevButtons[client] & IN_BACK) && iButtons & IN_BACK) {
int ent = EntRefToEntIndex(PlayerEditItems[client][eiRef]);
float angles[3];
GetEntPropVector(ent, Prop_Data, "m_angRotation", angles);
if ((angles[1] <= 360.0 && angles[1] > 315.0) || angles[1] == 0.0)
angles[1] = 315.0;
else if (angles[1] <= 315.0 && angles[1] > 270.0)
angles[1] = 270.0;
else if (angles[1] <= 270.0 && angles[1] > 225.0)
angles[1] = 225.0;
else if (angles[1] <= 225.0 && angles[1] > 180.0)
angles[1] = 180.0;
else if (angles[1] <= 180.0 && angles[1] > 135.0)
angles[1] = 135.0;
else if (angles[1] <= 135.0 && angles[1] > 90.0)
angles[1] = 90.0;
else if (angles[1] <= 90.0 && angles[1] > 45.0)
angles[1] = 45.0;
else if (angles[1] <= 45.0 && angles[1] > 0.0)
angles[1] = 0.0;
else
angles[1] = 0.0;
int degrees = RoundToNearest(angles[1]);
degrees = degrees % 360;
angles[1] = float(degrees);
TeleportEntity(ent, NULL_VECTOR, angles, NULL_VECTOR);
} else if (!(g_iPlayerPrevButtons[client] & IN_MOVELEFT) && iButtons & IN_MOVELEFT) {
int ent = EntRefToEntIndex(PlayerEditItems[client][eiRef]);
float angles[3];
GetEntPropVector(ent, Prop_Data, "m_angRotation", angles);
angles[1] += 1;
int degrees = RoundToNearest(angles[1]);
degrees = degrees % 360;
angles[1] = float(degrees);
TeleportEntity(ent, NULL_VECTOR, angles, NULL_VECTOR);
iButtons &= ~IN_MOVELEFT;
iButtons &= ~IN_MOVERIGHT;
return Plugin_Changed;
} else if (!(g_iPlayerPrevButtons[client] & IN_MOVERIGHT) && iButtons & IN_MOVERIGHT) {
int ent = EntRefToEntIndex(PlayerEditItems[client][eiRef]);
float angles[3];
GetEntPropVector(ent, Prop_Data, "m_angRotation", angles);
angles[1] -= 1;
int degrees = RoundToNearest(angles[1]);
degrees = degrees % 360;
angles[1] = float(degrees);
TeleportEntity(ent, NULL_VECTOR, angles, NULL_VECTOR);
iButtons &= ~IN_MOVERIGHT;
iButtons &= ~IN_MOVELEFT;
return Plugin_Changed;
}
}
} else {
if (!(g_iPlayerPrevButtons[client] & IN_USE) && iButtons & IN_USE) {
int target = getClientViewObject(client);
if (!isValidClient(target)) {
if (IsValidEntity(target)) {
float pos[3];
float playerPos[3];
GetClientAbsOrigin(client, playerPos);
GetEntPropVector(target, Prop_Data, "m_vecOrigin", pos);
if (GetVectorDistance(playerPos, pos) <= 100.0) {
int index;
if ((index = findSpawnedEntByRef(EntIndexToEntRef(target))) != -1) {
PrintToConsole(client, "[-T-]: sfId: %i | sfLoadedId: %i | sfRef: %i | sfActive: %d | sfApartment: %s | sfOwner: %s | sDurability: %i",
SpawnedFurnitureItems[index][sfId],
SpawnedFurnitureItems[index][sfLoadedId],
SpawnedFurnitureItems[index][sfRef],
SpawnedFurnitureItems[index][sfIsActive],
SpawnedFurnitureItems[index][sfApartment],
SpawnedFurnitureItems[index][sfOwner],
SpawnedFurnitureItems[index][sfDurability]);
int loadedIndex = SpawnedFurnitureItems[index][sfLoadedId];
PrintToConsole(client, "[-T-]: lfActive %d | lfName: %s | lfModePath: %s | lfFlags: %s | lfBuff: %s | lfSize: %.2f | lfPrice: %i | lfId: %i",
LoadedFurnitureItems[loadedIndex][lfActive],