-
Notifications
You must be signed in to change notification settings - Fork 0
/
structure_manager.py
172 lines (159 loc) · 7.72 KB
/
structure_manager.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
# -*- coding: utf-8 -*-
import os
import re
import base64
import urllib
import urllib2
import traceback
helpmsg ='''------------
§7!!struct get <folder> <name> <url> (-o) (-b64)§r -下载结构文件到本地的文件夹中
请指定文件夹,以免覆盖同名结构文件
文件夹和文件名只兼容英文小写字母、数字、短划线和下划线
使用 §7-o§r 在重名时覆盖
使用 §7-b64§r 打开base64模式,将会作为base64编码的文件被下载和解码
§7!!struct list(:<page>) (<key>)§r -列出服务器的结构文件,最多一页20个
指定关键字 §7<key>§r 进一步定位
使用如 §7-list:2§r 的格式来翻页
§7!!struct del <folder> <name> -删除结构文件
在文件名处使用星号 §7*§r 时删除整个文件夹,§c谨慎操作!§r
§7!!struct paste <folder> <name> (-d:<N|10M|1H|1D|1W|2W|1M|6M|1Y>)§r -将结构文件以base64编码上传到pastebin
采用base64编码,下载时记得使用 §7-b64§r
使用 §7<-d>§r 设置保存时长;不使用 §7<-d>§r 时默认无限保存
冒号后分别是:无限、十分钟、一小时、一天、一周、两周、一个月、两个月和一年
------------'''
def onServerInfo(server, info):
try:
if info.isPlayer == 1:
if info.content.startswith('!!struct'):
if info.content == '!!struct':
for line in helpmsg.splitlines():
server.tell(info.player, line)
elif re.match('^!!struct list(:\d+)?( [0-9a-z\*\.\-_/:]+)?$', info.content):
args = info.content.split(' ')
args[1] = args[1].split(':')
if len(args) == 2:
args.append('')
if len(args[1]) == 1:
args[1].append('1')
listStruct(server, info.player, int(args[1][1]), args[2])
elif re.match('^!!struct del [0-9a-z\*\.\-_/]+ [0-9a-z\*\.\-_/]+$', info.content):
args = info.content.split(' ')
delStruct(server, info.player, args[2], args[3])
elif re.match('^!!struct paste [0-9a-z\*\.\-_/]+ [0-9a-z\*\.\-_/]+( -d:((N)|(10M)|(1H)|(1D)|(1W)|(2W)|(1M)|(6M)|(1Y)))?$', info.content):
args = info.content.split(' ')
if len(args) == 4:
args.append(['-d','N'])
else:
args[4] = args[4].split(':')
pasteStruct(server, info.player, args[2], args[3], args[4][1])
elif re.match('^!!struct get [0-9a-z\*\.\-_/]+ [0-9a-z\*\.\-_/]+ \S+( -o)?( -b64)?( -o)?$', info.content):
args = info.content.split(' ')
can_overwrite = info.content.find(' -o') > -1
is_b64mode = info.content.find(' -b64') > -1
getStruct(server, args[2], args[3], args[4], is_b64mode, can_overwrite)
except:
lines = traceback.format_exc().splitlines()
for l in lines:
server.say(l)
def getStruct(server, foldername, filename, url, is_b64mode, can_overwrite, do_reload=True):
if not can_overwrite:
if os.path.exists('server/world/generated/'+foldername+'/structures/'+filename+'.nbt'):
server.say('§a'+foldername+':'+filename+'§r§c exists, use §r§7-o§r§c to overwrite§r')
return
server.say('trying downloading as §a'+foldername+':'+filename+'§r...')
try:
os.makedirs('server/world/generated/'+foldername+'/structures')
except:
pass
try:
data = urllib2.urlopen(url).read()
with open('server/world/generated/'+foldername+'/structures/'+filename+'.nbt','wb') as nbtfile:
if is_b64mode:
nbtfile.write(base64.b64decode(data.replace('data:application/octet-stream;base64,','')))
server.say('§a'+foldername+':'+filename+'§r has been downloaded and decoded successfully')
else:
nbtfile.write(data)
server.say('§a'+foldername+':'+filename+'§r has been downloaded successfully')
if do_reload:
server.execute('reload')
except:
lines = traceback.format_exc().splitlines()
for l in lines:
server.say(l)
server.say('§cfailed to download§r')
def listStruct(server, player, page, kwrd=''):
counter = 1
start = (page-1)*20
end = page*20
for i in os.listdir('server/world/generated/'):
for j in os.listdir('server/world/generated/'+i+'/structures'):
strout = i+':'+j
if strout.find(kwrd) > -1:
if counter > start:
server.tell(player, '§a'+strout[0:len(strout)-4]+'§r')
counter += 1
if counter > end:
return
if counter == 1:
server.tell(player, '§cNo item to list!§r')
def delStruct(server, player, foldername, filename, do_reload=True):
if filename == '*':
if not os.path.exists('server/world/generated/'+foldername):
server.tell(player, '§cfolder §r§7'+foldername+'§r§c does not exist§r')
return
else:
if not os.path.exists('server/world/generated/'+foldername+'/structures/'+filename+'.nbt'):
server.tell(player, '§cstructure §r§7'+foldername+':'+filename+'§r§c does not exist§r')
return
try:
if filename == '*':
for i in os.listdir('server/world/generated/'+foldername+'/structures/'):
delStruct(server, player, foldername, i[0:len(i)-4], do_reload=False)
os.rmdir('server/world/generated/'+foldername+'/structures')
os.rmdir('server/world/generated/'+foldername)
else:
os.remove('server/world/generated/'+foldername+'/structures/'+filename+'.nbt')
server.say(player+' has deleted §c'+foldername+':'+filename+'§r')
if do_reload:
server.execute('reload')
except:
lines = traceback.format_exc().splitlines()
for l in lines:
server.say(l)
server.tell(player, '§cfailed to delete§r§7 '+foldername+':'+filename+'§r')
parse_time={
'N':'a really long time',
'10M':'10 minutes',
'1H':'1 hour',
'1D':'1 day',
'1W':'1 week',
'2W':'2 weeks',
'1M':'1 month',
'6M':'6 months',
'1Y':'a year'}
def pasteStruct(server, player, foldername, filename, expire_date='N'):
filepath = 'server/world/generated/'+foldername+'/structures/'+filename+'.nbt' #检验文件
if not os.path.exists(filepath):
server.tell(player, '§cstructure §r§7'+foldername+':'+filename+'§r§c does not exist§r')
return
with open(filepath,'rb') as nbtfile: #转码
b64data = base64.b64encode(nbtfile.read())
paste_param = urllib.urlencode({'api_option': 'paste', #丢给 pastebin
'api_dev_key': "04941ffafb9e4a8ab6e9da99fc162e54", #如果这个key不能用了,你得自己注册个pastebin账号搞个新的
'api_option': 'paste',
'api_paste_code': b64data,
'api_paste_expire_date': expire_date
})
try:
response = urllib.urlopen('https://pastebin.com' + '/api/api_post.php',paste_param) #返回网址
response_url = response.read()
except:
lines = traceback.format_exc().splitlines()
for l in lines:
server.say(l)
server.tell(player, '§cfailed to upload to pastebin§r§7 '+foldername+':'+filename+'§r')
finally:
response.close()
response_url = response_url.split('/')
response_url = 'https://pastebin.com/raw/'+response_url[len(response_url)-1]
server.execute('tellraw @a ['+'{"text": "§a'+foldername+':'+filename+'§r is uploaded to §7'+response_url+'§r for §a'+parse_time[expire_date]+'§r\\nclick "}, '+'{"text": "§nhere§r", "clickEvent": {"action": "suggest_command", "value": "'+response_url+'"}}, '+'{"text": " and copy to use otherwhere"}]')