Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.System;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.PrintWriter;
- public class SnippetGenerator
- {
- // File to generate snippets from, reads line by line, a line could be SetPlayerHealth(playerid, Float:health), only supports functions currently
- final static String RESOURCE_FILE = "resource.txt";
- final static String OUTPUT_FILE = "output.json";
- public static void main(String[] args) throws FileNotFoundException, IOException
- {
- readFile();
- }
- public static void readFile() throws FileNotFoundException, IOException
- {
- BufferedReader br = null;
- try {
- br = new BufferedReader(new FileReader(RESOURCE_FILE));
- String line = br.readLine();
- PrintWriter writer = new PrintWriter(OUTPUT_FILE, "UTF-8");
- while (line != null)
- {
- print(line);
- if (line.contains("(") && line.contains(")"))
- {
- int argStart = line.indexOf("(");
- int argEnd = line.indexOf(")");
- String name = line.substring(0, argStart);
- String args = line.substring(argStart+1, argEnd);
- String gen = "\"" + name + "\": {\n" +
- "\t\"prefix\": \"" + name + "\",\n" +
- "\t\"body\": \"" + name + "(";
- int argLen = argEnd - argStart;
- print("Name: " + name + " Argend-argstart = " + argLen);
- if (argLen > 1)
- {
- String argArray[] = args.split(",");
- int argIndex = 0;
- for (int i = 0; i < argArray.length; i++)
- {
- argIndex++;
- print(i + ": " + argArray[i]);
- gen += "${" + argIndex + ":" + argArray[i] + "}";
- if (i != argArray.length - 1)
- {
- gen += ", ";
- }
- else
- {
- gen += ")$0\"";
- print("\n");
- }
- }
- }
- else
- {
- gen += ")$0\"";
- }
- gen += "\n},";
- print(gen);
- writer.println(gen);
- }
- else
- {
- print("Couldn't generate any snippet from the following line, ( ) are missing.");
- }
- line = br.readLine();
- }
- writer.close();
- }
- catch (FileNotFoundException e)
- {
- print("Couldn't find the file specified, using path: " + RESOURCE_FILE);
- }
- catch (IOException e)
- {
- e.printStackTrace();
- print("Error: " + e.getMessage());
- }
- finally
- {
- if (br != null)
- {
- br.close();
- print("BufferedReader successfully closed.");
- }
- else
- {
- print("BufferedReader is null, will not call the close() function.");
- }
- }
- }
- public static void print(String message)
- {
- Calendar cal = Calendar.getInstance();
- SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
- System.out.println("[SnippetGen " + sdf.format(cal.getTime()) + "] " + message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement