Code:
/*
This was made by filth jr, kthx respect.
Please read the comments so you learn something.
*/
import java.io.File;
import java.io.IOException;
import jxl.*;
import jxl.write.*;
import jxl.read.biff.BiffException;
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class BanHandler {
public String Date = "None";
public boolean muted = false;
public int Punishment = 0;
private String getDate(int days) {
SimpleDateFormat sdf = new SimpleDateFormat("M/d/yyyy");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, days);
return sdf.format(cal.getTime());
}
public boolean bannedHost(String host) {
if (Punishment == 5) {
return (host.equals(getHost(host))) ? true : false;
}
return false;
}
public boolean bannedName(String name, String host) {
if (name.equals(getName(name))) {
if (Punishment == 4) {
return true;
} else if (Punishment == 1) {
muted = true;
return false;
} else if (Punishment == 2 || Punishment == 3) {
String sep[] = Date.split("/");
int day = Integer.parseInt(sep[0]);
int month = Integer.parseInt(sep[1]);
int year = Integer.parseInt(sep[2]);
Calendar cal = Calendar.getInstance();
cal.set(year, month, day);
Calendar cal2 = Calendar.getInstance();
int month2 = cal2.get(Calendar.MONTH);
int day2 = cal2.get(Calendar.DATE);
int year2 = cal2.get(Calendar.YEAR);
cal2.set(year2, month2 + 1, day2); // Calendar is different -.- I have to add a month.
if (cal.before(cal2)) { // If the date the ban is lifted on is before
// the current date, you will be unbanned.
writeSheet(name, host, 0);
return false;
} else {
return true;
}
} else if (Punishment == 0) {
return false;
} else if (Punishment == 5) {
writeSheet("", host, -1); // This fucker came back with a different IP.
// Too bad they didn't change accounts :3 BAN THEIR IP AGAIN!
return false;
}
}
return false;
}
public String getName(String name) {
return readSheet(0, name, null);
}
public String getHost(String host) {
return readSheet(1, null, host);
}
/*
int banType
0 = No ban
1 = Mute
2 = Temp Ban 3 Days
3 = Temp Ban 7 Days
4 = Permanent Ban
5 = Host Ban
*/
public void ban(String playerName, String host, int banType) {
if (!playerName.equals(null) || !host.equals(null) || banType > -1) {
writeSheet(playerName, host, banType);
} else {
System.out.println("Null error");
}
}
public String readSheet(int read, String playerName, String host) { // This method will return a value from a spreadsheet based on the int 'read'
try {
try {
Workbook workbook = Workbook.getWorkbook(new File("config\\ban.xls")); // Accesses your spread sheet
// NOTE: If you are going to do any manual editing to your sheet, make sure that you save it as a
// Windows 97-2003 worksheet (in Save As) if you have Excel 2007. It has the extension '.xls'
Sheet sheet = workbook.getSheet(0); // Now we are accessing the sheets in the excel file. We are accessing
// Sheet '0', which is the first sheet.
String valueFound = null; // the value we will be returning at end of String
// Now using a for each loop to, in the end, get the contents of each cell
for (int row = 0; row < sheet.getRows(); row++) {
boolean vars = false;
// Now, the int read comes in. NOTE: The Date and Punishment are ALWAYS read
if (read == 0) { // If read == 0, this method will return the player name
if (sheet.getCell(0, row).getContents().trim().equals(playerName)) {
// If we found the row the player's data is on...
valueFound = sheet.getCell(0, row).getContents().trim();
vars = true;
// DO NOT RETURN HERE. You want to close the workbook first!
}
} else if (read == 1) { // If read == 1, this method will return the player's host
if (sheet.getCell(2, row).getContents().trim().equals(host)) {
// If we found the row the player's data is on...
valueFound = sheet.getCell(2, row).getContents().trim();
vars = true;
}
}
if (vars) { // If we set these values on the wrong row, we will get an error
// so this boolean is just a catch.
NumberCell nc = (NumberCell) sheet.getCell(1, row);
Punishment = (int) nc.getValue();
Date = sheet.getCell(3, row).getContents().trim();
}
}
workbook.close(); // Don't forget to close the workbook to free some memory.
return valueFound;
} catch(BiffException ex) {
ex.printStackTrace();
return null;
}
}catch(IOException ex2) {
ex2.printStackTrace();
return null;
}
}
public void writeSheet(String playerName, String host, int offense) {
try {
try {
try {
boolean WriteOffense = true;
if (offense < 0) {
WriteOffense = false;
}
Workbook workbook = Workbook.getWorkbook(new File("config\\ban.xls"));
// The reason we are loading the 'reading' workbook is so that we can copy it.
WritableWorkbook workbookWrite = Workbook.createWorkbook(new File("config\\ban.xls"), workbook);
// This is the 'writing' workbook. It has copied all the values of the reading workbook.
Sheet sheetRead = workbook.getSheet(0); // Sheet used for reading
WritableSheet sheet = workbookWrite.getSheet(0); // Sheet used for writing
Label label; // This will later become the value of a cell as a STRING
jxl.write.Number number; // This will later become the value of a cell as a NUMBER (holds ints, longs, doubles, etc)
int row = sheetRead.getRows(); // Finds the first cell without a value
// So that you can place data in it.
int permrow = -1; // IF PLAYER ALREADY LOGGED, DON'T MAKE NEW ENTRY FOR THEM :)
for (int x = 0; x < row; x++) { // Gets first free spot incase you fuck up your file, and it skips empty spaces (LOLIDIDNTDOTHAT)
if (sheetRead.getCell(0, x).getContents().trim().equals(playerName) && playerName.length() > 0) {
permrow = x;
}
if (sheetRead.getCell(0, x).getContents().length() < 1) {
row = x;
break;
}
}
if (permrow > -1) { // if player has already been logged..
String loadhost = sheetRead.getCell(2, permrow).getContents().trim();
// Loading that player's host...
if (host != loadhost && host != null && !WriteOffense) { // If they have a new IP...
label = new Label(2, row, host); // BAN IT! HAHAHHAA.
sheet.addCell(label);
}
}
// Writes all new data to empty cells unless a player data is being overwritten
label = new Label(2, (permrow > -1) ? permrow : row, host);
sheet.addCell(label);
if (WriteOffense) { // if you're writing more than a host...
if (offense == 2 || offense == 3) { // Adds time to see when ban will be lifed, tempban
label = new Label(3, (permrow > -1) ? permrow : row, getDate((offense == 2) ? 3 : 7).trim());
sheet.addCell(label);
} else { // Does not add time, permaban, mute
label = new Label(3, (permrow > -1) ? permrow : row, getDate(0).trim());
sheet.addCell(label);
}
label = new Label(0, (permrow > -1) ? permrow : row, playerName); // Make new label
sheet.addCell(label); // Add label
number = new jxl.write.Number(1, (permrow > -1) ? permrow : row, offense);
sheet.addCell(number);
}
workbookWrite.write(); // Write stuff to workbook.
workbook.close();
workbookWrite.close(); // Don't forget to close the workbook to free some memory.
} catch(BiffException ex) { // All the exceptions that need to be caught LOL.
ex.printStackTrace();
return;
}
} catch(WriteException ex2) {
ex2.printStackTrace();
return;
}
}catch(IOException ex3) {
ex3.printStackTrace();
}
}
}