Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public void log(String player, String ip) {
- // Generate the File Path for the player log
- File playerFile = new File("plugins/IP-Check/DATABASE/PLAYERS/" + player + ".log");
- // Convert IP from XXX.XXX.XXX.XXX to XXX_XXX_XXX_XXX
- String convertedIP = convertIPFormat(ip);
- // Generate the File Path for the ip log
- File ipFile = new File("plugins/IP-Check/DATABASE/IPS/" + convertedIP + ".log");
- //### IP-LOG SECTOR ###//
- if (!ipFile.exists()) {
- // FILE WRITER
- FileWriter f = null;
- try {
- f = new FileWriter(ipFile, true);
- f.write(player + "\r\n"); //Store the player name passed to the log file.
- } catch (Exception e) {
- ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
- EL.execute(e);
- logger.severe(FLAT_FILE_GEN_ERR);
- } finally {
- try {
- if (f != null) {
- f.close(); // Close the file writer
- }
- } catch (Exception e){
- ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
- EL.execute(e);
- logger.severe(e.getMessage());
- }
- }
- // END FILE WRITER
- // If the file does exist, read it into memory
- } else {
- ArrayList<String> list = new ArrayList<String>();
- BufferedReader br = null;
- boolean shouldWrite = true;
- // FILE READER
- try {
- // Open a file stream to read the file
- FileInputStream fstream = new FileInputStream(ipFile);
- DataInputStream in = new DataInputStream(fstream);
- br = new BufferedReader(new InputStreamReader(in));
- String strLine;
- while ((strLine = br.readLine()) != null) {
- list.add(strLine.toLowerCase()); // Add each line of the file to memory
- }
- } catch (Exception e) {
- ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
- EL.execute(e);
- logger.severe(FLAT_FILE_READ_ERR);
- list = null; // If we encountered an error reading the file, set the memory location to NULL so that we can handle the error.
- } finally {
- try {
- if (br != null) {
- br.close();
- }
- } catch (Exception e) {
- ErrorLogger EL = new ErrorLogger(); // Catch the error, pass to handler
- EL.execute(e);
- logger.severe(e.getMessage());
- }
- }
- // END FILE READER
- for (String s:list) {
- if (s.equals(player)) {
- shouldWrite = false; // If the file already exists and the IP is already logged, do not write the log file
- }
- }
- // If we did not find the IP
- if (shouldWrite) {
- list.add(ip); // Add the IP to the file's memory block
- // Write the log file to disk
- // FILE WRITER
- FileWriter f = null;
- try {
- if (playerFile.exists()) playerFile.delete(); // Delete the old log file
- f = new FileWriter(playerFile, true);
- for (String s:list) {
- f.write(s + "\r\n"); //Write each line in memory to the file, plus a line break and carriage return.
- }
- } catch (Exception e) {
- ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
- EL.execute(e);
- logger.severe(FLAT_FILE_WRITE_ERR);
- } finally {
- try {
- if (f != null) {
- f.close(); // Close the file writer
- }
- } catch (Exception e){
- ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
- EL.execute(e);
- logger.severe(e.getMessage());
- }
- }
- // END FILE WRITER
- }
- }
- //### PLAYER-LOG SECTOR ###/
- // If the player log path does not exist, generate it
- if (!playerFile.exists()) {
- // FILE WRITER
- FileWriter f = null;
- try {
- f = new FileWriter(playerFile, true);
- f.write(ip + "\r\n"); //Store the IP-Address passed to the log file.
- } catch (Exception e) {
- ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
- EL.execute(e);
- logger.severe(FLAT_FILE_GEN_ERR);
- } finally {
- try {
- if (f != null) {
- f.close(); // Close the file writer
- }
- } catch (Exception e){
- ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
- EL.execute(e);
- logger.severe(e.getMessage());
- }
- }
- // END FILE WRITER
- // If the file does exist, read it into memory
- } else {
- ArrayList<String> list = new ArrayList<String>();
- BufferedReader br = null;
- // FILE READER
- try {
- // Open a file stream to read the file
- FileInputStream fstream = new FileInputStream(playerFile);
- DataInputStream in = new DataInputStream(fstream);
- br = new BufferedReader(new InputStreamReader(in));
- String strLine;
- while ((strLine = br.readLine()) != null) {
- list.add(strLine.toLowerCase()); // Add each line of the file to memory
- }
- } catch (Exception e) {
- ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
- EL.execute(e);
- logger.severe(FLAT_FILE_READ_ERR);
- list = null; // If we encountered an error reading the file, set the memory location to NULL so that we can handle the error.
- } finally {
- try {
- if (br != null) {
- br.close();
- }
- } catch (Exception e) {
- ErrorLogger EL = new ErrorLogger(); // Catch the error, pass to handler
- EL.execute(e);
- logger.severe(e.getMessage());
- }
- }
- // END FILE READER
- for (String s:list) {
- if (s.equals(ip)) {
- return; // If the file already exists and the IP is already logged, we're done.
- }
- }
- // If we did not find the IP
- list.add(ip); // Add the IP to the file's memory block
- // Write the log file to disk
- // FILE WRITER
- FileWriter f = null;
- try {
- if (playerFile.exists()) playerFile.delete(); // Delete the old log file
- f = new FileWriter(playerFile, true);
- for (String s:list) {
- f.write(s + "\r\n"); //Write each line in memory to the file, plus a line break and carriage return.
- }
- } catch (Exception e) {
- ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
- EL.execute(e);
- logger.severe(FLAT_FILE_WRITE_ERR);
- } finally {
- try {
- if (f != null) {
- f.close(); // Close the file writer
- }
- } catch (Exception e){
- ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
- EL.execute(e);
- logger.severe(e.getMessage());
- }
- }
- // END FILE WRITER
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement