100% Timed Mutes and Bans (Done the correct way, with correct conventions)
Hello, and welcome to my thread.
I've created a new system, which is done flawlessly using the combinaiton of Java and Sql.
NOTE * You will need a local or external Sql server setup before you can complete this tutorial. Also, it would be a good idea to have a basic understanding of Sql before you attempt this tutorial.
Step one, create a new class called: banAssistant. Then, place this code into the file: (Note - I chose to create a new instance so you can easily check/edit this code)
Code:
import java.io.*;
import java.sql.*;
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
public class banAssistant {
public static boolean isBanned(client c) {
try {
Statement s = server.conn.createStatement();
ResultSet m = s.executeQuery("SELECT * FROM banned_users WHERE username = '"+ c.playerName +"'");
while (m.next()) {
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:m:s");
GregorianCalendar calendar = new GregorianCalendar();
Date today = calendar.getTime();
String expire = m.getString("End_date");
Date expireDate = format.parse(expire);
if (!today.after(expireDate)) {
return true;
} else {
return false;
}
}
}
catch (Exception e) {
e.printStackTrace();
return false;
}
return false;
}
public static boolean isMuted(client c) {
try {
Statement s = server.conn.createStatement();
ResultSet m = s.executeQuery("SELECT * FROM muted_users WHERE username = '"+ c.playerName +"'");
while (m.next()) {
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:m:s");
GregorianCalendar calendar = new GregorianCalendar();
Date today = calendar.getTime();
String expire = m.getString("End_date");
Date expireDate = format.parse(expire);
if (!today.after(expireDate)) {
return true;
} else {
return false;
}
}
}
catch (Exception e) {
e.printStackTrace();
return false;
}
return false;
}
}
Step two, setting up Sql on your server (skip if you've already done so, and you do NOT get errors with the above code).
Open up your server class. Add these methods/variables:
Code:
public static String MySQLURL = "jdbc:mysql://localhost/DATABASE";
public static String MySQLUser = "USERNAME";
public static String MySQLPassword = "PASSWORD";
public static Connection conn;
public static void createConnection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(MySQLURL, MySQLUser,
MySQLPassword);
misc.println("mysql connected");
} catch (Exception e) {
e.printStackTrace();
}
}
Now, in your main server method, initiate the method createConnection(), by adding this code:
Code:
createConnection();
Also, if you haven't already, import the Java.sql API:
Step three, initiating the mute/ban methods:
For muting, do this short tutorial I made: [Only registered and activated users can see links. Click Here To Register...]
Now for banning, add this (you should know where):
Code:
if (banAssistant.isBanned(this)) {
returnCode = 4;
savefile = false;
disconnected = true;
}
Step four, execute these quiries in PhpMyadmin:
Code:
CREATE TABLE IF NOT EXISTS `banned_users` (
`Username` text,
`Case_Id` text,
`Start_date` text,
`End_date` text
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `muted_users` (
`Username` text,
`Case_Id` text,
`Start_date` text,
`End_date` text
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Thank you for reading my tutorial, and I hope it helped you out :P