-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBindCommand.py
302 lines (293 loc) · 13.2 KB
/
BindCommand.py
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
from Helper import color
import org.bukkit as bukkit
FullString = []
ResetAllPending = []
WritingNames = []
WritingLines = []
def t():
return u'\u0009'
@hook.command('mb')
def onCommandMb(sender,args):
ops = open('build/ops.txt')
if not ''.join([sender.getName(),'\n']) in ops.readlines():
ops.close()
sender.sendMessage(''.join([color('c'),'You are not an op.']))
return False
ops.close()
if len(args) == 0:
helpMessage(sender)
return True
if args[0] == 'list': #List
b = open('build/Binds.py',"r")
n = 0
nLocal = 0
s = ''
sender.sendMessage(''.join([color('9'),'List of commands:']))
soStuff = True
for i in b.readlines():
if i[0] == '#':
s = i[1:len(i)-1]
n = n + 1
nLocal = 0
doStuff = True
if i[0].replace('\n','') == '' and doStuff:
if nLocal / 5 > 4:
sender.sendMessage(''.join([color('4'),s,color('f'),' (',str(nLocal),')']))
else:
sender.sendMessage(''.join([color('ae6c4'[(nLocal/5)]),s,color('f'),' (',str(nLocal),')']))
doStuff = False
else:
nLocal = nLocal + 1
sender.sendMessage(''.join([color('9'),'A total of ',str(n),' commands']))
return True
if args[0] == 'deleteall': #Reset
sender.sendMessage(''.join([color('4'),'Are you CERTAIN you want to delete ALL MB commands?']))
sender.sendMessage(''.join([color('4'),'Respond with /mb confirmdelete or /mb declinedelete']))
if not sender.getName() in ResetAllPending == 0:
ResetAllPending.append(sender.getName())
return True
return False
if args[0] == 'confirmdelete':
if sender.getName() in ResetAllPending:
b = open('build/Binds.py','w')
c = open('build/Template.txt')
b.writelines(c.readlines())
c.close()
b.close()
sender.sendMesage(''.join([color('a'),'Deleted']))
return True
sender.sendMesage(''.join([color('c'),'You have no pending deletion requests']))
return False
if args[0] == 'declinedelete':
if sender.getName() in ResetAllPending :
ResetAllPending.remove(sender.getName())
sender.sendMesage(''.join([color('a'),'Not deleted']))
return True
sender.sendMesage(''.join([color('c'),'You have no pending deletion requests']))
return False ### Commands before here must need no args
if len(args) < 2:
helpMessage(sender)
return True ### Commands after here must need args
if args[0] == 'write': #Start new multi thing command
args.pop(0)
if not sender.getName() in WritingNames:
WritingNames.append(sender.getName())
WritingLines.append(args)
sender.sendMesage(''.join([color('a'),'New command started, and written to']))
return True
WritingLines[WritingNames.index(sender.getName())].append(args)
sender.sendMesage(''.join([color('a'),'Command written to']))
return True
if args[0] == 'done':
if sender.getName() in WritingNames:
Written = WritingLines.pop(WritingNames.index(sender.getName()))
if complie(sender, Written = WritingLines.pop(WritingNames.index(sender.getName()))):
WritingNames.remove(sender.getName())
sender.sendMesage(''.join([color('a'),'Successfully written into a command']))
return True
WritingNames.remove(sender.getName())
return False
sender.sendMessage(''.join([color('c'),'You do not have anything being written']))
return False
if args[0] == 'new': #New single thing command
args.pop(0)
if complie(sender,args):
sender.sendMessage(''.join([color('a'),'Successfully written into a command']))
return True
return False
if args[0] == 'cancel':
if sender.getName() in WritingNames:
WritingLines.pop(WritingNames.index(sender.getName()))
WritingNames.remove(sender.getName())
sender.sendMessage(''.join([color('a'),'Successfully cancelled']))
return True
sender.sendMessage(''.join([color('c'),'Nothing to cancel']))
return False
if args[0] == 'delete': #delete
if delete(args[1]):
return True
b.close()
sender.sendMessage(''.join([color('c'),'No such command']))
return False
if args[0] == 'view': #view
b = open('build/Binds.py')
l = b.readlines()
n = 0
if ''.join(['#',args[1],'\n']) in l:
seek = l.index(''.join(['#',args[1],'\n']))
while True:
if l[seek].replace('\n','') == '':
b.close
break
n = n + 1
sender.sendMessage(''.join([str(n),': ',l.pop(seek).replace(t(),' ')]))
return True
sender.sendMessage(''.join([color('c'),'No such command']))
b.close()
return False
if args[0] == 'find':
sender.sendMessage(''.join([color('9'),'Searching for "',args[1],'"']))
n = 0
n2 = 0
b = open('build/Binds.py')
for i in b.readlines():
if i[0] == '#':
if i.lower() == ''.join(['#',args[1].lower(),'\n']):
sender.sendMessage(''.join([color('2'),i[1:len(i)-1]]))
n2 = n2 + 1
elif i.lower().find(args[1].lower()) != -1:
sender.sendMessage(''.join([color('a'),i[1:len(i)-1]]))
n = n + 1
b.close()
sender.sendMessage(''.join([color('9'),'Found ',str(n),' partial matche(s), and ',str(n2),' full matche(s)']))
return True
helpMessage(sender)
return True
def delete(Name):
b = open('build/Binds.py','r+')
if ''.join(['#',Name,'\n']) in b.readlines():
delPos = b.readlines().index(''.join(['#',Name,'\n']))
delList = b.readlines()
while True:
if delList[delPos].replace('\n','') == '':
b.write(delList)
b.close()
break
delList.pop(delPos)
return True
b.close()
return False
def helpMessage(sender):
sender.sendMessage(''.join([color('6'),'Arguments:']))
sender.sendMessage(''.join([color('a'),'List - lists all finished commands']))
sender.sendMessage(''.join([color('a'),'Find [Search] - Searches for a command']))
sender.sendMessage(''.join([color('a'),'']))
sender.sendMessage(''.join([color('a'),'ResetAll - Resets all commands (Leave it alone)']))
sender.sendMessage(''.join([color('a'),'Write [Name] [Command] - Write a new command - This command will allow you to write more to any pending command']))
sender.sendMessage(''.join([color('a'),'Done - Comfirm that the current "/mb write" command is done, and can be turned into code']))
sender.sendMessage(''.join([color('a'),'New [Name] [Command] - Writes and creates a command in one command']))
sender.sendMessage(''.join([color('a'),'View [Command Name] - View the code for a command']))
sender.sendMessage(''.join([color('a'),'Cancel - Cancels the writing of the current "/mb write" command']))
sender.sendMessage(''.join([color('6'),'The following are flags, to be used when creating a command']))
sender.sendMessage(''.join([color('a'),'/ - Start of a new line']))
sender.sendMessage(''.join([color('a'),'#s - Excecutes the current line as a comsole command']))
sender.sendMessage(''.join([color('a'),'#c - Writes the current line as a custom line of code']))
sender.sendMessage(''.join([color('a'),'#b - Broadcasts the current line to the server']))
sender.sendMessage(''.join([color('6'),'The default setting is to exceute the line as a command by the sender']))
sender.sendMessage(''.join([color('a'),'#t - (only used woth #c) creates a tab']))
sender.sendMessage(''.join([color('a'),'#p - Name of the excecuter of the command']))
sender.sendMessage(''.join([color('a'),'#n - AutoCompleted name in the first argument']))
sender.sendMessage(''.join([color('a'),'#f[hex value] - A colour or format']))
sender.sendMessage(''.join([color('a'),'#[number] - An argument of the player']))
return True
def complie(sender,args):
global FullString
FullString = []
if len(args) < 2:
sender.sendMessage(''.join([color('c'),'You must have at least four arguments']))#Argument checking
return False
if (args[1])[0] != '/':
sender.sendMessage(''.join([color('c'),'Your third argument must be a / or //']))
return False
CommandName = args.pop(0)
for i in args:
if compose(i) == False: #Split command into lines
sender.sendMessage(''.join([color('c'),'You cannot bind a command to an MB']))
CommandName = CommandName.replace('"',"") #Replace invalid characters in the name
b = open('build/Binds.py')
if ''.join(['#',CommandName,'\n']) in b.readlines():
sender.sendMessage(''.join([color('c'),'That name is already in use'])) #Checks if command name is free
b.close()
return False
b.close()
a = []
b = open('build/Binds.py','a')
b.write(''.join(['#',CommandName,'\n']))
b.write(''.join(['@hook.command("',CommandName,'",description="',CommandName,', by ',sender.getName(),'")\n']))
b.write(''.join(['def onCommand',CommandName,'(sender,args):\n'])) #Write 'introduction'
b.close()
argsNum = 0
for i in FullString:
form = 0
if i.find("#s") != -1:
form = 1
i = i.replace("#s","") #Define form of line via flags
if i.find("#b") != -1:
form = 2
i = i.replace("#b","")
if i.find("#c") != -1:
form = 3
i = i.replace("#c","")
i = i.replace("#t","{tab}")
i = i.replace("#p",'sender.getName()') #Replace returning flags for custom code lines
if i.find("#n") != -1:
i = i.replace("#n",'(bukkit.Bukkit.getPlayer(args[0])).getName()')
if argsNum == 0:
argsNum = 1
else:
i = i.replace('"',"") #Replace returning flags for normal lines, and get rid of invalid chars
i = i.replace("{","")
i = i.replace("}","")
i = i.replace("#p",'",sender.getName(),"')
if i.find("#n") != -1:
i = i.replace("#n",'",(bukkit.Bukkit.getPlayer(args[0])).getName(),"')
if argsNum == 0:
argsNum = 1
i = i.replace("#","# ")
i = i.replace("# f","#f ")
l = i.split()
while True:
if not '#f' in l:
break
if form == 3:
l.insert(l.index("#f"),''.join(['color("',l[(l.index("#f")+1)][0],'")'])) #Replaces color flags
l[(l.index("#f")+1)] = l[(l.index("#f")+1)][1:]
else:
l.insert(l.index("#f"),''.join(['",color("',l[(l.index("#f")+1)][0],'"),"']))
l[(l.index("#f")+1)] = l[(l.index("#f")+1)][1:]
l.remove("#f")
while True:
if not '#' in l:
break
else:
argsValue = int(l[(l.index("#")+1)])+1
if argsValue.isdigit():
if argsNum < argsValue:
argsNum = argsValue
if form == 3:
l.insert(l.index("#f"),''.join(['args["',l[(l.index("#f")+1)][0],']'])) #Replaces argument flags
l[(l.index("#f")+1)] = l[(l.index("#f")+1)][1:]
else:
l.insert(l.index("#"),''.join(['",args[',l[(l.index("#")+1)][0],'],"']))
l[(l.index("#")+1)] = l[(l.index("#")+1)][1:]
l.remove("#")
else:
sender.sendMessage(''.join([color('c'),'Invalid flags']))
delete(CommandName)
i = ' '.join(l)
if form == 1:
a.append(''.join([t(),'sudo("".join(["',i,'"]))\n']))
elif form == 2:
a.append(''.join([t(),'bukkit.Bukkit.broadcastMessage("".join(["',i,'"]))\n']))#Appends full argument syntax
elif form == 0:
a.append(''.join([t(),'bukkit.Bukkit.dispatchCommand(sender, "".join(["',i,'"]))\n']))
elif form == 3:
a.append(''.join([i.replace('{tab}',t()),'\n']))
a.append(''.join([t(),'return True\n']))
a.append('\n')
b = open('build/Binds.py','a')
if argsNum != 0:
b.write(''.join([t(),'if len(args) != ',str(argsNum),':\n']))
b.write(''.join([t(),t(),'sender.sendMessage("You must have ',str(argsNum),' arguments")\n']))#Writes if statement for amount of arguments
b.write(''.join([t(),t(),'return False\n']))
for i in a:
b.write(i) #Writes appends to command
b.close()
return True
def compose(argument):
global FullString
if argument[0] == '/': #Starts new lines
FullString.append('')
argument = argument[1:]
FullString[(len(FullString)-1)] = ''.join([FullString[(len(FullString)-1)],argument,' ']) #Appends argument to string of line
return True