forked from BusterBrown1218/HTSL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
163 lines (151 loc) · 7.29 KB
/
index.js
File metadata and controls
163 lines (151 loc) · 7.29 KB
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
/// <reference types="../CTAutocomplete" />
import './gui/LoadActionGUI';
import Config from "./utils/config";
import codeWindow from './gui/codeWindow';
import { convertHE } from './compiler/convertAction';
import { preProcess } from './compiler/compile';
import { addOperation } from './gui/Queue';
import Navigator from './gui/Navigator';
import "./update/update";
import getItemFromNBT from './utils/getItemFromNBT';
import loadItemstack from './utils/loadItemstack';
register("command", ...args => {
let command;
try {
command = args[0].toLowerCase();
} catch (e) {
command = 'help';
}
if (command === 'config') return Config.openGUI();
if (command === 'gui') {
args.shift();
return codeWindow(args.join(' '));
}
if (command === 'guide') {
const guideLink = new Message(
new TextComponent("&3[HTSL] &fJust click this: &b&l[Guide]").setClick("open_url", "https://hypixel.net/threads/updated-guide-htsl.5555038/")
);
return ChatLib.chat(guideLink);
}
if (command === 'changelog') {
ChatLib.chat("&3[HTSL] &fChanges:");
let changelog = FileLib.read('./config/ChatTriggers/modules/HTSL/update/changelog.txt').split("\n");
changelog.forEach(line => {
ChatLib.chat(line.trim());
});
return;
}
if (command === 'saveitem') {
if (args.length < 2) return ChatLib.chat("&3[HTSL] &cPlease enter a filename to save it to!");
let itemHeld = Player.getHeldItem().getNBT().toString().replace(/["]/g, '\\$&');
FileLib.write(`./config/ChatTriggers/modules/HTSL/imports/${args[1]}.json`, `{"item": "${itemHeld}"}`, true);
return ChatLib.chat(`&3[HTSL] &fSaved item to ${args[1]}.json`);
}
if (command === 'convert') {
if (args.length < 3) return ChatLib.chat("&3[HTSL] &cPlease enter the action id and then the filename to save it to!");
convertHE(args[1], args[2]);
return ChatLib.chat(`&3[HTSL] &fConverting action into HTSL script saved at ${args[2]}.htsl`);
}
if (command === "addfunctions") {
if (args.length == 1) return ChatLib.chat("&3[HTSL] &cPlease add a filename!");
if (FileLib.exists(`./config/ChatTriggers/modules/HTSL/imports/${args[1]}.htsl`)) {
Navigator.isReady = true;
preProcess(FileLib.read(`./config/ChatTriggers/modules/HTSL/imports/${args[1]}.htsl`).split("\n")).filter(n => n.context == "FUNCTION").forEach((context, index) => {
if (index > 0) addOperation({ type: 'closeGui' });
if (index > 0) addOperation({ type: 'wait', time: 1500 });
addOperation({ type: 'chat', text: `/function edit ${context.contextTarget.name}`, func: context.contextTarget.name, command: true });
});
addOperation({ type: 'closeGui' });
addOperation({ type: 'done' });
return;
} else {
return ChatLib.chat("&3[HTSL] &cFile not found!");
}
}
if (command === "listscripts") {
let files;
if (args.length == 1) {
files = readDir("./config/ChatTriggers/modules/HTSL/imports/", false).filter(e => e.endsWith("htsl") || e.endsWith("\\"));
} else {
args.shift();
files = readDir(`./config/ChatTriggers/modules/HTSL/imports/${args.join(" ")}/`, false);
}
files.filter(e => e.endsWith("\\")).forEach(directory => {
ChatLib.chat(`&3Directory: &f${directory.substring(0, directory.length - 1)}`);
})
ChatLib.chat("\n&3[HTSL] &fMain Directory:\n");
return files.filter(e => e.endsWith(".htsl")).forEach(file => {
ChatLib.chat(file);
});
}
if (command === "version") {
return ChatLib.chat(`&3[HTSL] &fVersion ${JSON.parse(FileLib.read("HTSL", "./metadata.json")).version}`);
}
if (command === "giveitem") {
if (Player.asPlayerMP().player.field_71075_bZ.field_75098_d === false) {
World.playSound('mob.villager.no', 0.5, 1);
return ChatLib.chat(`&3[HTSL] &cMust be in creative mode to import an item!`);
}
args.shift();
let nbt = JSON.parse(FileLib.read('HTSL', `/imports/${args.join(" ")}.json`)).item;
let item = getItemFromNBT(nbt);
let slot = Player.getInventory().getItems().indexOf(null);
if (slot < 9) slot += 36;
loadItemstack(item.getItemStack(), slot);
return;
}
if (command === 'help') {
ChatLib.chat('&8&m-------------------------------------------------');
ChatLib.chat('&6/htsl help &7Opens the HTSL help menu!')
ChatLib.chat('&6/htsl gui <script name> &7Opens a window for editing scripts!');
ChatLib.chat('&6/htsl config &7Opens the settings menu for HTSL!');
ChatLib.chat('&6/htsl guide &7Opens a syntax guide!');
ChatLib.chat('&6/htsl changelog &7Shows you all the significant changes made in the last update!');
ChatLib.chat('&6/htsl saveitem <filename> &7Save an item to import!');
ChatLib.chat('&6/htsl convert <action id> <filename> &7Converts a HousingEditor action to HTSL!');
ChatLib.chat('&6/htsl addfunctions <filename> &7Imports all the required functions to prepare for import!');
ChatLib.chat('&6/htsl listscripts &7Lists all your scripts');
ChatLib.chat('&6/htsl version &7Returns your current HTSL version');
ChatLib.chat('&6/htsl giveitem <filename> &7Gives you an item from your imports');
ChatLib.chat('&8&m-------------------------------------------------');
} else {
ChatLib.chat('&3[HTSL] &fUnknown command! Try /htsl for help!');
}
}).setName('htsl').setAliases(['ht']);
/**
* Obtains a list of file names from a directory.
* @param {string} path The path to the directory to walk.
* @param {boolean} walk `true` if the function should walk deeper into directories.
* @returns
*/
function readDir(path, walk) {
let files = new java.io.File(path).listFiles();
let fileNames = [];
files.forEach(file => {
if (file.isDirectory()) {
if (walk) {
readDir(path + file.getName() + "/", false).forEach(newFile => {
const fileName = getMatchedFileName(path, `${file}\\${newFile}`);
if (fileName) fileNames.push(fileName);
});
} else {
const fileName = getMatchedFileName(path, file.toString());
if (fileName) fileNames.push(`${fileName}\\`);
}
} else {
const fileName = getMatchedFileName(path, file.toString());
if (fileName) fileNames.push(fileName);
}
});
return fileNames;
}
function getMatchedFileName(path, filePath) {
const formattedPath = path.replace(/\//g, "\\\\");
const fileFormattedMatchRegexp = new RegExp(`${formattedPath}(.*)`);
const formattedPathMatchArray = filePath.match(fileFormattedMatchRegexp);
if (formattedPathMatchArray) return formattedPathMatchArray[1];
const fileMatchRegexp = new RegExp(`${path}(.*)`);
const pathMatchArray = filePath.match(fileMatchRegexp);
if (pathMatchArray) return pathMatchArray[1];
return null;
}