1+ --[[ **********************************
2+ *
3+ * Multi Theft Auto - Admin Panel
4+ *
5+ * client\widgets\admin_mute.lua
6+ *
7+ * Original File by omar-o22
8+ *
9+ **************************************]]
10+ aMute = {
11+ defaultDurations = {
12+ {" 1 minute" , 60 },
13+ {" 1 hour" , 60 * 60 },
14+ {" 1 day" , 60 * 60 * 24 },
15+ {" 1 week" , 60 * 60 * 24 * 7 },
16+ {" Permanent" , 0 }, -- HARDCODED AS SECOND LAST IN THIS TABLE, DO NOT MOVE
17+ {" Custom" , 0 } -- HARDCODED AS LAST LAST IN THIS TABLE, DO NOT MOVE
18+ },
19+ }
20+
21+ function aMute .Show (player )
22+ if (not aMuteForm ) then
23+ aMute .Create ()
24+ end
25+
26+ if (player ) then
27+ aMute .playerName = getPlayerName (player )
28+ guiSetText (aMute .Form , " Mute player " .. aMute .playerName )
29+ end
30+
31+ addEventHandler (" onClientGUIClick" , aMute .Form , aMute .onClick )
32+ addEventHandler (" onClientGUIFocus" , aMute .Form , aMute .onFocus )
33+ addEventHandler (" onClientGUIBlur" , aMute .Form , aMute .onBlur )
34+ end
35+
36+ function aMute .Close (destroy )
37+ if (destroy ) then
38+ destroyElement (aMute .Form )
39+ aMute .Form = nil
40+ else
41+ removeEventHandler (" onClientGUIClick" , aMute .Form , aMute .onClick )
42+ guiSetVisible (aMute .Form , false )
43+ aMute .Reset ()
44+ end
45+ aMute .playerName = nil
46+ end
47+
48+ function aMute .Create ()
49+ local sx , sy = guiGetScreenSize ()
50+
51+ aMute .Form = guiCreateWindow ((sx - 350 ) / 2 , (sy - 270 ) / 2 , 350 , 270 , " Mute player" , false )
52+ aMute .ReasonLabel = guiCreateLabel (25 , 40 , 300 , 20 , " Mute reason (required):" , false , aMute .Form )
53+ aMute .ReasonEditBox = guiCreateEdit (25 , 65 , 300 , 30 , " Enter mute reason..." , false , aMute .Form )
54+ aMute .ReasonEditBoxRecievedInput = false
55+ aMute .DurationLabel = guiCreateLabel (25 , 110 , 300 , 20 , " Mute duration (required):" , false , aMute .Form )
56+ aMute .DurationComboBox = guiCreateComboBox (25 , 140 , 300 , 100 , " Select mute duration..." , false , aMute .Form )
57+ for i = 1 , # aMute .defaultDurations do
58+ guiComboBoxAddItem (aMute .DurationComboBox , aMute .defaultDurations [i ][1 ])
59+ end
60+ aMute .DurationEditBox = guiCreateEdit (25 , 175 , 300 , 30 , " Duration (seconds)..." , false , aMute .Form )
61+ aMute .DurationEditBoxRecievedInput = false
62+ guiSetEnabled (aMute .DurationEditBox , false )
63+
64+ aMute .SubmitButton = guiCreateButton (70 , 222 , 100 , 30 , " Submit" , false , aMute .Form )
65+ aMute .CancelButton = guiCreateButton (180 , 222 , 100 , 30 , " Cancel" , false , aMute .Form )
66+ aRegister (" mute" , aMute .Form , aMute .Show , aMute .Close )
67+ end
68+
69+ function aMute .Reset ()
70+ guiSetText (aMute .Form , " Mute player" )
71+ guiSetText (aMute .ReasonEditBox , " Enter mute reason..." )
72+ aMute .ReasonEditBoxRecievedInput = false
73+ guiComboBoxSetSelected (aMute .DurationComboBox , - 1 )
74+ guiSetText (aMute .DurationEditBox , " Duration (seconds)..." )
75+ guiSetEnabled (aMute .DurationEditBox , false )
76+ aMute .DurationEditBoxRecievedInput = false
77+ end
78+
79+ function aMute .onClick (button , state )
80+ if not (button == " left" and state == " up" ) then
81+ return
82+ end
83+
84+ -- Handle cancel button first
85+ if source == aMute .CancelButton then
86+ aMute .Close ()
87+ return
88+ end
89+
90+ -- Autofill and enable/disable duration editbox based on choice
91+ if source == aMute .DurationComboBox then
92+ local selected = guiComboBoxGetSelected (aMute .DurationComboBox )
93+ if selected == - 1 then
94+ return
95+ elseif selected == # aMute .defaultDurations - 2 then
96+ -- Second-last option is permanent duration - clear and disable edit box
97+ guiSetText (aMute .DurationEditBox , " 0" )
98+ guiSetEnabled (aMute .DurationEditBox , false )
99+ elseif selected == # aMute .defaultDurations - 1 then
100+ -- Last option (should) be custom duration - enable duration edit box
101+ guiSetText (aMute .DurationEditBox , " Duration (seconds)..." )
102+ guiSetEnabled (aMute .DurationEditBox , true )
103+ aMute .DurationEditBoxRecievedInput = false
104+ else
105+ guiSetText (aMute .DurationEditBox , aMute .defaultDurations [selected + 1 ][2 ])
106+ guiSetEnabled (aMute .DurationEditBox , false )
107+ end
108+ return
109+ end
110+
111+ -- Handle submit button
112+ if source == aMute .SubmitButton then
113+ aMute .verifyForm ()
114+ return
115+ end
116+ end
117+
118+ function aMute .onFocus ()
119+ -- Clear reason/duration edit boxes on first click
120+ if source == aMute .ReasonEditBox then
121+ if not aMute .ReasonEditBoxRecievedInput then
122+ guiSetText (aMute .ReasonEditBox , " " )
123+ aMute .ReasonEditBoxRecievedInput = true
124+ end
125+ elseif source == aMute .DurationEditBox then
126+ if not aMute .DurationEditBoxRecievedInput then
127+ guiSetText (aMute .DurationEditBox , " " )
128+ aMute .DurationEditBoxRecievedInput = true
129+ end
130+ end
131+ end
132+
133+ function aMute .onBlur ()
134+ -- Reset default text of reason/duration edit boxes if they lose focus with no input
135+ if source == aMute .ReasonEditBox then
136+ if guiGetText (source ) == " " then
137+ guiSetText (aMute .ReasonEditBox , " Enter mute reason..." )
138+ aMute .ReasonEditBoxRecievedInput = false
139+ end
140+ elseif source == aMute .DurationEditBox then
141+ if guiGetText (source ) == " " and (guiComboBoxGetSelected (aMute .DurationComboBox ) == # aMute .defaultDurations - 1 ) then
142+ guiSetText (aMute .DurationEditBox , " Duration (seconds)..." )
143+ aMute .DurationEditBoxRecievedInput = false
144+ end
145+ end
146+ end
147+
148+ function aMute .verifyForm ()
149+ -- Verify mute reason
150+ local muteReason = guiGetText (aMute .ReasonEditBox )
151+ if muteReason == " " or (not aMute .ReasonEditBoxRecievedInput ) then
152+ messageBox (" No mute reason provided." , MB_ERROR , MB_OK )
153+ return
154+ end
155+
156+ -- Verify mute duration
157+ local muteDuration
158+ local durationSelection = guiComboBoxGetSelected (aMute .DurationComboBox )
159+ if durationSelection == - 1 then
160+ messageBox (" No mute duration provided." , MB_ERROR , MB_OK )
161+ return
162+ end
163+ durationSelection = durationSelection + 1 -- ComboBox item indices starts at 0 instead of one
164+ if durationSelection == # aMute .defaultDurations then
165+ muteDuration = guiGetText (aMute .DurationEditBox )
166+ muteDuration = tonumber (muteDuration )
167+ if not muteDuration or muteDuration <= 0 then
168+ messageBox (" Invalid mute duration provided." , MB_ERROR , MB_OK )
169+ return
170+ end
171+ else
172+ muteDuration = aMute .defaultDurations [durationSelection ][2 ]
173+ end
174+
175+ -- Build mute request "packet" and send to server
176+ local actualPlayer -- Actual player may be offline
177+ if aMute .playerName then
178+ actualPlayer = getPlayerFromName (aMute .playerName )
179+ end
180+ local data = {
181+ playerName = aMute .playerName ,
182+ reason = muteReason ,
183+ duration = muteDuration
184+ }
185+
186+ triggerServerEvent (
187+ " aPlayer" ,
188+ localPlayer ,
189+ actualPlayer ,
190+ " mute" ,
191+ data
192+ )
193+ aMute .Close ()
194+ end
0 commit comments