Last active
December 15, 2015 22:38
-
-
Save KeepJ96/5333927 to your computer and use it in GitHub Desktop.
Flat File Backend Manager for IP-Check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.risenphoenix.jnk.ipcheck; | |
import java.io.BufferedReader; | |
import java.io.DataInputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
import java.util.logging.Logger; | |
import org.bukkit.Bukkit; | |
public class FlatFile implements Backend{ | |
// BACKEND MANAGER FOR FLAT-FILE | |
private static ArrayList<String> playerInfo = new ArrayList<String>(); | |
private static Logger logger = Bukkit.getLogger(); | |
// Messages | |
public static final String PLUG_NAME = "[IP-Check] "; | |
public static final String INIT_BACKEND = "Initizalizing Flat-File Backend..."; | |
public static final String BAN_LIST_READ_ERR = "Error occurred while attempting to read banned-ips.txt!"; | |
private static final String FLAT_FILE_WRITE_ERR = "An error occurred while attempting to write to the database document."; | |
private static final String FLAT_FILE_READ_ERR = "An error occurred while attempting to read the database document."; | |
private static final String FLAT_FILE_GEN_ERR = "An error occurred while attempting to generate a new database document!"; | |
// Files | |
public static File bannedIPs = new File("banned-ips.txt"); | |
private static File path = new File("plugins/IP-check/database.db"); | |
@Override | |
public void onLoad() { | |
double initTime = System.currentTimeMillis(); | |
logger.info(PLUG_NAME + INIT_BACKEND); | |
generateFile(); | |
loadFile(); | |
double haltTime = System.currentTimeMillis(); | |
logger.info(PLUG_NAME + "Initialization complete! Time taken: " + ((haltTime - initTime) / 1000) + " seconds."); | |
} | |
@Override | |
public void onDisable() { | |
double initTime = System.currentTimeMillis(); | |
logger.info(PLUG_NAME + INIT_BACKEND); | |
saveFile(); | |
double haltTime = System.currentTimeMillis(); | |
logger.info(PLUG_NAME + "Backend Manager successfully shutdown! Time taken: " + ((haltTime - initTime) / 1000) + " seconds."); | |
} | |
@Override | |
public void loadFile() { | |
BufferedReader br = null; | |
try { | |
FileInputStream fstream = new FileInputStream(path); | |
DataInputStream in = new DataInputStream(fstream); | |
br = new BufferedReader(new InputStreamReader(in)); | |
String strLine; | |
while ((strLine = br.readLine()) != null) { | |
playerInfo.add(strLine); | |
} | |
} catch (Exception e) { | |
logger.severe(FLAT_FILE_READ_ERR); | |
} finally { | |
try { | |
if (br != null) { | |
br.close(); | |
} | |
} catch (Exception e) { | |
logger.severe(e.getMessage()); | |
} | |
} | |
} | |
@Override | |
public void saveFile() { | |
FileWriter f = null; | |
try { | |
if (path.exists()) { | |
path.delete(); | |
} | |
f = new FileWriter(path, true); | |
for(String s:playerInfo) { | |
f.write(s + "\r\n"); | |
} | |
f.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
logger.severe(FLAT_FILE_WRITE_ERR); | |
} finally { | |
try { | |
if (f != null) { | |
f.close(); | |
} | |
} catch (Exception e){ | |
logger.severe(e.getMessage()); | |
} | |
} | |
} | |
@Override | |
public void log(String player, String ip) { | |
StringBuilder sb = new StringBuilder(); | |
int index = 0; | |
sb.append(player); | |
sb.append("|"); | |
sb.append(ip); | |
// Check if player is already in the list. | |
for (String s:playerInfo) { | |
if (s.contains(player.toLowerCase())) { | |
playerInfo.remove(index); | |
playerInfo.add(sb.toString()); | |
return; | |
} | |
index++; | |
} | |
playerInfo.add(sb.toString()); | |
} | |
@Override | |
public void generateFile() { | |
if (!path.exists()) { | |
FileWriter f = null; | |
try { | |
f = new FileWriter(path, true); | |
} catch (IOException e) { | |
logger.severe(FLAT_FILE_GEN_ERR); | |
} finally { | |
try { | |
if (f != null) { | |
f.close(); | |
} | |
} catch (IOException e) { | |
logger.severe(e.getMessage()); | |
} | |
} | |
} | |
} | |
@Override | |
public ArrayList<String> getAlts(String ip) { | |
ArrayList<String> players = new ArrayList<String>(); | |
for (String s:playerInfo) { | |
if (s.contains(ip)) { | |
StringBuilder sb = new StringBuilder(); | |
int index = 0; | |
while (s.charAt(index) != '|') { | |
sb.append(s.charAt(index)); | |
index++; | |
} | |
players.add(sb.toString()); | |
} | |
} | |
return players; | |
} | |
// CHECK FOR USAGE AFTER PLUGIN IS COMPLETE | |
@Override | |
public ArrayList<String> getBannedList() { | |
ArrayList<String> ips = new ArrayList<String>(); | |
try { | |
FileInputStream fstream = new FileInputStream(bannedIPs); | |
DataInputStream in = new DataInputStream(fstream); | |
BufferedReader br = new BufferedReader(new InputStreamReader(in)); | |
String strLine; | |
// Skip the first three lines of the file | |
for (int lineSkip = 0; lineSkip < 3; lineSkip++) br.readLine(); | |
while ((strLine = br.readLine()) != null) { | |
char start = strLine.charAt(0); | |
String ip = ""; | |
int charCount = 0; | |
if (start >= '0' && start <= '9') { | |
while (strLine.charAt(charCount) != '|') { | |
ip = ip + strLine.charAt(charCount); | |
charCount++; | |
} | |
ips.add(ip); | |
} | |
} | |
br.close(); | |
} catch (Exception e) { | |
// Console Output in the event we fail to read the configuration file. | |
logger.severe(BAN_LIST_READ_ERR); | |
} | |
return ips; | |
} | |
@Override | |
public boolean isBannedIP(String ip) { | |
BufferedReader br = null; | |
try { | |
FileInputStream fstream = new FileInputStream(bannedIPs); | |
DataInputStream in = new DataInputStream(fstream); | |
br = new BufferedReader(new InputStreamReader(in)); | |
String strLine; | |
/* Skip the first three lines of the file to prevent the while statement | |
* from terminating prematurely. */ | |
for (int lineSkip = 0; lineSkip < 3; lineSkip++) br.readLine(); | |
while ((strLine = br.readLine()) != null) { | |
if (strLine.contains(ip)) { | |
return true; | |
} | |
} | |
} catch (Exception e) { | |
logger.severe(BAN_LIST_READ_ERR); | |
} finally { | |
try { | |
if (br != null) { | |
br.close(); | |
} | |
} catch (Exception e) { | |
logger.severe(e.getMessage()); | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment