Code:
package org.rs2;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.rs2.model.entity.players.Client;
/**
* A class for handling punishments and connections.
* @author Thock321
*
*/
public class Connection {
/**
* An enum containing the types of punishments.
* @author Thock321
*
*/
public static enum PunishmentType {
BAN(new ArrayList<String>()),
IPBAN(new ArrayList<String>()),
UUIDBAN(new ArrayList<String>()),
MUTE(new ArrayList<String>()),
IPMUTE(new ArrayList<String>());
/**
* The list that contains punishment data.
*/
private List<String> list;
/**
* Creates a new punishment type.
* @param list A new list to contain punishment data.
*/
private PunishmentType(List<String> list) {
this.list = list;
}
/**
* Gets the list containing punishment data.
* @return The list.
*/
public List<String> getList() {
return list;
}
/**
* Gets a punishment type for it's name.
* @param name The name.
* @return The punishment type.
*/
public static PunishmentType forName(String name) {
for (PunishmentType pt : PunishmentType.values()) {
if (name.equalsIgnoreCase(pt.toString()))
return pt;
}
return null;
}
}
public static Collection<String> loginLimitExceeded = new ArrayList<String> ();
private static String punishmentFilesFolder = "./Data/bans/";
private static File _punishmentFilesFolder = new File(punishmentFilesFolder);
private static File punishmentFile;
private static BufferedReader reader;
private static BufferedWriter writer;
/**
* Punishes a player.
* @param c The player.
* @param type The type of punishment.
*/
public static void punish(Client c, PunishmentType type) {
punishmentFile = new File(punishmentFilesFolder + type.toString().toLowerCase() + ".txt");
String toAdd = type.toString().contains("IP") ? c.playerName + ":" + c.connectedFrom : (type.toString().contains("UID") ? c.playerName + ":" + c.UUID : c.playerName);
type.getList().add(toAdd);
try {
writer = new BufferedWriter(new FileWriter(punishmentFile));
try {
writer.newLine();
writer.write(toAdd);
} finally {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Removes a punishment.
* @param toRemove The string to remove from the file.
* @param type The type of punishment.
*/
public static void removePunishment(String toRemove, PunishmentType type) {
punishmentFile = new File(punishmentFilesFolder + type.toString().toLowerCase() + ".txt");
type.getList().remove(toRemove);
try {
writer = new BufferedWriter(new FileWriter(punishmentFile));
reader = new BufferedReader(new FileReader(punishmentFile));
String line;
try {
while ((line = reader.readLine()) != null) {
line = line.trim();
if (!line.equalsIgnoreCase(toRemove)) {
writer.write(line);
}
}
} finally {
writer.close();
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Loads all punishments.
*/
public static void loadPunishments() {
for (File file : _punishmentFilesFolder.listFiles()) {
PunishmentType type = PunishmentType.forName(file.getName().replace(".txt", ""));
if (type == null)
continue;
try {
reader = new BufferedReader(new FileReader(file));
String line;
try {
while ((line = reader.readLine()) != null) {
line = line.trim();
type.getList().add(line);
}
} finally {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Checks if a player is punished for a certain punishment type.
* @param playerName The player.
* @param type The punishment type.
* @return <code>true</code> if the player is punished for the type, otherwise <code>false</code>.
*/
public static boolean isPunishedForType(String playerName, PunishmentType type) {
try {
punishmentFile = new File(punishmentFilesFolder + type.toString().toLowerCase() + ".txt");
reader = new BufferedReader(new FileReader(punishmentFile));
try {
String line;
while ((line = reader.readLine()) != null) {
if (line.toLowerCase().contains(playerName.toLowerCase()))
return true;
}
} finally {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* Checks if an ip has reached its login limit.
* @param IP The ip.
* @return <code>true</code> if the ip has reached its login limit, otherwise <code>false</code>.
*/
public static boolean checkLoginList(String IP) {
loginLimitExceeded.add(IP);
int num = 0;
for(String ips : loginLimitExceeded) {
if(IP.equals(ips)) {
num++;
}
}
return num > 5;
}
}