-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
193 lines (159 loc) · 6.96 KB
/
index.js
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
// Define the URL of the ZIP file you want to download
const modName = '' // Add name of the folder that will be created from the unzip
const pastebin = ''; // Add download link to the json format pastebin file with your direct download link.
// Function to download the ZIP file
function downloadZip(zipUrl) {
try {
const url = new java.net.URL(zipUrl); // Instantiate URL
const connection = url.openConnection();
// Set user-agent to mimic a browser
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");
connection.setRequestMethod("GET");
connection.connect(); // Make the connection
const responseCode = connection.getResponseCode();
if (responseCode !== 200) {
ChatLib.chat(`Failed to download: ${responseCode} ${connection.getResponseMessage()}`);
return;
}
const inputStream = connection.getInputStream();
const file = new java.io.File(`config/ChatTriggers/modules/file.zip`); // Adjust path as necessary
const outputStream = new java.io.FileOutputStream(file);
const buffer = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024);
let bytesRead;
// Read and write the file
while ((bytesRead = inputStream.read(buffer)) !== -1) {
outputStream.write(buffer, 0, bytesRead);
}
// Clean up
outputStream.close();
inputStream.close();
//ChatLib.chat(`Downloaded ZIP file to: ${file.getAbsolutePath()}`);
return file;
} catch (error) {
//ChatLib.chat(`Error downloading ZIP: ${error}`);
}
}
function unzip(zipFile, destDir) {
try {
const zipInputStream = new java.util.zip.ZipInputStream(new java.io.FileInputStream(zipFile));
let entry;
while ((entry = zipInputStream.getNextEntry()) !== null) {
const newFile = new java.io.File(destDir, entry.getName());
if (entry.isDirectory()) {
newFile.mkdirs(); // Create directories
} else {
const outputStream = new java.io.FileOutputStream(newFile);
const buffer = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024);
let len;
while ((len = zipInputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
outputStream.close();
}
zipInputStream.closeEntry();
}
zipInputStream.close();
//ChatLib.chat(`Unzipped to: ${destDir.getAbsolutePath()}`);
} catch (error) {
//ChatLib.chat(`Error unzipping: ${error}`);
}
}
function deleteFile(filePath) {
try {
const file = new java.io.File(filePath); // Create a File object
if (file.exists()) {
const deleted = file.delete(); // Attempt to delete the file
if (deleted) {
//ChatLib.chat(`File "${filePath}" deleted successfully.`);
} else {
//ChatLib.chat(`Failed to delete file "${filePath}".`);
}
} else {
//ChatLib.chat(`File "${filePath}" does not exist.`);
}
} catch (error) {
//ChatLib.chat(`Error deleting file: ${error}`);
}
}
// Function to delete all files and directories in a directory
function deleteAllFilesAndDirectoriesInDirectory(dirPath) {
const directory = new java.io.File(dirPath);
if (!directory.exists() || !directory.isDirectory()) {
//ChatLib.chat(`Directory "${dirPath}" does not exist or is not a directory.`);
return;
}
const files = directory.listFiles();
if (files.length === 0) {
//ChatLib.chat(`No files or directories found in "${dirPath}".`);
return;
}
let deletedItemsCount = 0;
for (let file of files) {
try {
if (file.isDirectory()) {
// Recursively delete the directory
deleteAllFilesAndDirectoriesInDirectory(file.getPath());
}
// Attempt to delete the file or empty directory
const deleted = file.delete();
if (deleted) {
deletedItemsCount++;
} else {
//ChatLib.chat(`Failed to delete "${file.getPath()}".`);
}
} catch (error) {
//ChatLib.chat(`Error deleting "${file.getPath()}": ${error}`);
}
}
//ChatLib.chat(`Deleted ${deletedItemsCount} items from "${dirPath}".`);
}
// Function to load a variable from a Pastebin JSON
function loadUrlFromPastebin(pastebinUrl){
try {
let url = new java.net.URL(pastebinUrl);
const connection = url.openConnection();
connection.setRequestMethod("GET");
connection.connect(); // Connect to the URL
const responseCode = connection.getResponseCode();
if (responseCode !== 200) {
ChatLib.chat(`Failed to load URL: ${responseCode} ${connection.getResponseMessage()}`);
return;
}
const inputStream = connection.getInputStream();
const reader = new java.io.BufferedReader(new java.io.InputStreamReader(inputStream));
const stringBuilder = new java.lang.StringBuilder();
let line;
// Read the response line by line
while ((line = reader.readLine()) !== null) {
stringBuilder.append(line);
}
// Close resources
reader.close();
inputStream.close();
// Parse JSON response
const jsonResponse = JSON.parse(stringBuilder.toString());
url = jsonResponse.url; // Assuming the JSON has a property called "url"
//ChatLib.chat(`Loaded URL: ${url}`);
return url; // Return the loaded URL
} catch (error) {
//ChatLib.chat(`Error loading URL: ${error}`);
}
}
function loadthemodule(){
if(!new java.io.File(`config/ChatTriggers/modules/${modName}`).exists()){
ChatLib.chat(`&6[&2Wasabi&4 Loader&6]&2 Loading ${modName}!`);
unzip(downloadZip(loadUrlFromPastebin(pastebin)), new java.io.File("config/ChatTriggers/modules"));
ChatTriggers.loadCT();
}
else{
deleteAllFilesAndDirectoriesInDirectory(`config/ChatTriggers/modules/${modName}`); //thank you ai for allowing me to make this single function without my brain.
deleteFile(`config/ChatTriggers/modules/${modName}`);
deleteFile(`config/ChatTriggers/modules/file.zip`);
ChatLib.chat(`&6[&2Wasabi&4 Loader&6]&2 Have fun using ${modName}!`);
}
}
// Trigger the download when the command is executed
loadthemodule();
register("command", () => {
loadthemodule();
}).setName("download");