· 6 years ago · Sep 04, 2019, 07:28 PM
1/*
2
3Has same methods as IData
4 - put
5 - get
6 - has
7 - getKeys
8 - clear
9
10Own methods:
11 - create
12
13Own properties:
14 - file
15
16Because it has same method names as the methods in IData its easy to implement in existing script
17*/
18var File = Java.type("java.io.File");
19var Files = Java.type("java.nio.file.Files");
20var Paths = Java.type("java.nio.file.Paths");
21var CHARSET_UTF_8 = Java.type("java.nio.charset.StandardCharsets").UTF_8;
22var API = Java.type('noppes.npcs.api.NpcAPI').Instance();
23
24
25var IData_look_alike = new CSTData("storage/data.json")
26IData_look_alike.create();
27
28
29
30
31
32/*
33jsonfile can be non-existend json file
34
35use CSTData.create() to create it
36or use mkPath()
37*/
38function CSTData(jsonfile) {
39
40
41 this.file = jsonfile;
42
43 this.exists = function() {
44 return new File(this.file).exists();
45 };
46 this.create = function(){
47 if(!this.exists()) {
48 new File(this.file).createNewFile();
49 writeToFile(this.file, "{}");
50 }
51 return this;
52 };
53
54 this.clear = function() {
55 writeToFile(this.file, "{}");
56 }
57
58 this.put = function(key, value) {
59 try {
60 var d = JSON.parse(this.exists() ? readFileAsString(this.file).replace(/\n/gm, "") : "{}");
61 d[key] = value;
62 writeToFile(this.file, JSON.stringify(d));
63 return true;
64 } catch(exc) {
65 handleError(exc);
66 return false;
67 }
68 }
69
70 this.has = function(key) {
71 try {
72 return Object.keys(cson_parse(readFileAsString(this.file))).indexOf(key) > -1;
73 } catch(exc) {
74 handleError(exc);
75 return false;
76 }
77 }
78
79 this.get = function(key) {
80 try {
81 print(this.file.toString()+" --- "+key+" --- "+readFileAsString(this.file).toString());
82 var jdata = JSON.parse(readFileAsString(this.file));
83 if(Object.keys(jdata).indexOf(key) > -1) {
84 return jdata[key];
85 }
86 } catch(exc) {
87 handleError(exc);
88 }
89 return null;
90 }
91
92 this.getKeys = function() {
93 try {
94 var jdata = JSON.parse(readFileAsString(this.file));
95 return Object.keys(jdata);
96 } catch(exc) {
97 handleError(exc);
98 }
99 return [];
100 }
101
102 this.remove = function(key) {
103 try {
104 var jdata = JSON.parse(readFileAsString(this.file));
105 if(Object.keys(jdata).indexOf(key) > -1) {
106 delete jdata[key];
107 writeToFile(this.file, JSON.stringify(jdata));
108 return true;
109 }
110 } catch(exc) {
111 handleError(exc);
112 }
113
114 return false;
115 }
116}
117
118function handleError(error) {
119 API.getIWorld(0).broadcast("Error in CustomNPCs script! Check console for more info.");
120 print("Error in "+error.fileName+":"+error.lineNumber+"\n"+error.message+"\n\n"+error.stack);
121}
122
123//Creates file, but recursively created directories also if they dont exist
124function mkPath(path) {
125 var expath = path.split("/"); //split the path
126 var curpath = "";
127 for(var ex in expath as expt) { //loop trhough path
128 curpath += (curpath == "" ? "" : "/")+expt; //current path
129 var pfile = new File(curpath);
130 if(!pfile.exists()) {
131 if(expt.match(/[\w]+\.[\w]+/) === null) { //is dir?
132 pfile.mkdir();
133 } else {//Is file
134 pfile.createNewFile();
135 }
136 }
137 }
138}
139
140
141function readDir(dirPath){
142 var res = [];
143 var files = new File(dirPath).listFiles();
144 for(var id in files as file){
145 if(file.isDirectory())
146 res = res.concat( readDir(file.toString()) );
147 else
148 res.push( Java.from( readFile(file.toString()) ).join("\n").replace(/\t/g, " ") );
149 }
150 return res;
151}
152
153function readFileAsString(filePath) {
154 return Java.from( readFile(filePath) ).join("\n").replace(/\t/g, " ");
155}
156
157
158function readFile(filePath){
159 var path = Paths.get(filePath);
160 try{
161 var lines = Files.readAllLines(path, CHARSET_UTF_8);
162 return lines;
163 } catch (e){
164 return [];
165 }
166}
167
168/*
169offset && length are optional
170*/
171function writeToFile(filePath, text, offset, length) {
172 var path = Paths.get(filePath);
173 try {
174 var writer = Files.newBufferedWriter(path, CHARSET_UTF_8);
175 writer.write(text, offset||0, length||text.length);
176 writer.close();
177 return true;
178 } catch (exc) {
179 return false
180 }
181}