Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.Foryk.projects.VipChest;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.sql.Timestamp;
- import java.text.ParseException;
- import java.util.Date;
- import java.util.Vector;
- import org.bukkit.command.ConsoleCommandSender;
- import org.bukkit.plugin.java.JavaPlugin;
- public class VipChest extends JavaPlugin {
- protected VipChestLogHandler logHandler;
- protected int dbMSQLInfoInt = 4;
- protected String [] dbMSQLInfo = new String [dbMSQLInfoInt];
- long repeat = 3000L;
- public void onEnable() {
- //kontrola expirace davani chesty a unbanu
- this.dbMSQLInfo[0] = "vipSys"; //table name
- this.dbMSQLInfo[2] = "vipsys1";//"mc_29410";// + this.getServer().getPort();//"mc_29410";// + this.getServer().getPort();//"mc_29410"; //username
- this.dbMSQLInfo[1] = "jdbc:mysql://...."; //mc_29410";// mc_29410 + this.getServer().getIp() + "/" + this.dbMSQLInfo[2]; // "jdbc:mysql://mysql.sms-hosting.cz/mc_29410"; //url
- this.dbMSQLInfo[3] = "pass"; //pwd
- this.logHandler = new VipChestLogHandler(this);
- this.getServer().getScheduler().scheduleSyncRepeatingTask(VipChest.this, new Runnable() {
- public void run() {
- System.out.println("Vip controle.");
- try {
- Check();
- } catch (SQLException|ParseException e) {
- System.err.println("SQLException|ParseException: " + e.getMessage());
- }
- }
- }, 60L, repeat);
- }
- public void onDisable() {
- this.logHandler.info("Disable.");
- }
- public static long getCurrentTime(){
- Date date = new Date();
- return date.getTime();
- }
- public boolean Check() throws ParseException, SQLException{
- String query = "SELECT * FROM " + this.dbMSQLInfo[0];
- Connection con = null;
- ResultSet rs = null;
- @SuppressWarnings("unused")
- int i=0;
- Vector<String> queryes = new Vector<String>();
- try { // Load the JDBC driver
- String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
- Class.forName(driverName); // Create a connection to the database
- con = DriverManager.getConnection(this.dbMSQLInfo[1], this.dbMSQLInfo[2], this.dbMSQLInfo[3]);
- Statement stmt = con.createStatement();
- rs = stmt.executeQuery(query); // = stmt.executeQuery(query);
- while(rs.next()){ //kontroluje unb a deg
- i++;
- if(!rs.getString(8).matches(this.getConfig().getString("Sys"))){
- continue;
- }
- if(rs.getString(6).matches("new")){
- queryes.add("UPDATE " + this.dbMSQLInfo[0] + " SET tsExp = '" + (new Timestamp(((rs.getTimestamp(3).getTime() + (30L*24L*60L*60L*1000L)) ))).toString() + "' WHERE ID = '" + rs.getString(1) + "'");
- queryes.add("UPDATE " + this.dbMSQLInfo[0] + " SET data = 'pro' WHERE ID = '" + rs.getString(1) + "'");
- if(rs.getString(4).matches("50")){
- sendCmd(this.getConfig().getString("Cmds.Promote1"),rs.getString(2));
- }
- if(rs.getString(4).matches("2")){
- sendCmd(this.getConfig().getString("Cmds.Promote2"),rs.getString(2));
- }
- if(rs.getString(4).matches("99")){
- sendCmd(this.getConfig().getString("Cmds.Promote3"),rs.getString(2));
- }
- continue;
- }
- if(!rs.getString(6).matches("done")&&rs.getTimestamp(7).getTime() < getCurrentTime()){
- //demote
- sendCmd(this.getConfig().getString("Cmds.Demote"),rs.getString(2));
- queryes.add("UPDATE " + this.dbMSQLInfo[0] + " SET data = 'done' WHERE ID = '" + rs.getString(1) + "'");
- continue;
- }
- }
- } catch (ClassNotFoundException e) {
- System.out.println("ClassNotFoundException " + e.getMessage()); // Could not find the database driver
- } catch (SQLException e) {
- System.out.println("SQLException " + e.getMessage()); // Could not connect to the database
- } catch(Exception e){
- System.out.println("Exception " + e.getMessage());
- }
- query = "";
- i=0;
- VipChest.dbZapisVek(this,queryes);
- return false;
- }
- private void sendCmd(String string, String string2) {
- ConsoleCommandSender x = this.getServer().getConsoleSender();
- String string3 = new String();
- string3= string;
- if(string3.contains("<name>")){
- string3=string3.replace("<name>",string2);
- }
- this.getServer().dispatchCommand(x, string3);
- }
- public static int [] dbZapisVek(VipChest plugin, Vector<String> queryes) {
- int [] i=new int [queryes.capacity()];
- if(queryes.isEmpty()){
- return i;
- }
- Connection con = null;
- Statement stmt = null;
- try { // Load the JDBC driver
- int a = 0;
- String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
- Class.forName(driverName); // Create a connection to the database
- con = DriverManager.getConnection(plugin.dbMSQLInfo[1], plugin.dbMSQLInfo[2], plugin.dbMSQLInfo[3]);
- stmt = con.createStatement();
- while(queryes.size() > a){
- i[a] = stmt.executeUpdate(queryes.get(a));
- a++;
- }
- } catch (ClassNotFoundException|SQLException e) {
- System.out.println("ClassNotFoundException|SQLException " + e.getMessage()); // Could not connect to the database
- }finally{
- try {
- con.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- try {
- stmt.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return i;
- }
- public static ResultSet dbCteniAll(VipChest plugin, String query) throws SQLException {
- Connection con = null;
- ResultSet rs = null;
- try { // Load the JDBC driver
- String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
- Class.forName(driverName); // Create a connection to the database
- con = DriverManager.getConnection(plugin.dbMSQLInfo[1], plugin.dbMSQLInfo[2], plugin.dbMSQLInfo[3]);
- Statement stmt = con.createStatement();
- rs = stmt.executeQuery(query);
- } catch (SQLException|ClassNotFoundException e) {
- System.out.println("SQLException|ClassNotFoundException " + e.getMessage()); // Could not find the database driver
- }
- return rs;
- }
- }
- package me.Foryk.projects.VipChest;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DatabaseMetaData;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Logger;
- import org.bukkit.configuration.InvalidConfigurationException;
- import org.bukkit.plugin.PluginDescriptionFile;
- public class VipChestLogHandler implements Runnable{
- private VipChest plugin;
- private Logger logger;
- @Override
- public void run() {
- // TODO Auto-generated method stub
- }
- public VipChestLogHandler(VipChest plugin){
- this.plugin = plugin;
- this.logger = Logger.getLogger("Minecraft");
- try {
- String name = "./plugins/VipSys/config.yml";
- File file = new File(name);
- if(file.exists()){
- this.plugin.getConfig().load(name);
- }else{
- this.plugin.getConfig().options().copyDefaults(true);
- this.plugin.saveDefaultConfig();
- }
- } catch (FileNotFoundException e) {
- System.err.println("FileNotFoundException: " + e.getMessage());
- this.plugin.getPluginLoader().disablePlugin(this.plugin);
- } catch (IOException e) {
- System.err.println("IOException: " + e.getMessage());
- this.plugin.getPluginLoader().disablePlugin(this.plugin);
- } catch (InvalidConfigurationException e) {
- System.err.println("InvalidConfigurationException: " + e.getMessage());
- this.plugin.getPluginLoader().disablePlugin(this.plugin);
- }
- this.plugin.dbMSQLInfo[0] = this.plugin.getConfig().getString("Config.Table");
- this.plugin.dbMSQLInfo[1] = this.plugin.getConfig().getString("Config.Database");
- this.plugin.dbMSQLInfo[2] = this.plugin.getConfig().getString("Config.User");
- this.plugin.dbMSQLInfo[3] = this.plugin.getConfig().getString("Config.Pass");
- this.plugin.repeat = this.plugin.getConfig().getLong("Config.CheckDelay");
- try{
- String strDirectoy ="./plugins/VipSys";
- boolean success = (new File(strDirectoy)).mkdir();
- if (success) {
- System.out.println("Directory: " + strDirectoy + " created.");
- }
- String query = "CREATE TABLE " + this.plugin.dbMSQLInfo[0] + " (ID INT NOT NULL AUTO_INCREMENT,PRIMARY KEY (ID),name VARCHAR(24) NOT NULL,ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,price VARCHAR(24) NOT NULL,hash VARCHAR(24) NOT NULL,data VARCHAR(24) NOT NULL DEFAULT 'new', tsExp TIMESTAMP DEFAULT '1970-01-01 01:01:01',srv VARCHAR(24) NOT NULL)"; //-----------------------------------------!!!!!!!!!!!!!!!!!!!!!!!!!!
- Connection con = null;
- String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
- Class.forName(driverName); // Create a connection to the database
- con = DriverManager.getConnection(this.plugin.dbMSQLInfo[1], this.plugin.dbMSQLInfo[2], this.plugin.dbMSQLInfo[3]);
- if(!tableExist(con,this.plugin.dbMSQLInfo[0])){
- Statement stmt = con.createStatement();
- stmt.executeUpdate(query); // = stmt.executeQuery(query);
- }
- con.close();
- } catch (ClassNotFoundException e) {
- System.out.println("ClassNotFoundException " + e.getMessage()); // Could not find the database driver
- } catch (SQLException e) {
- System.out.println("SQLException " + e.getMessage()); // Could not connect to the database
- }
- }
- public static boolean tableExist(Connection conn, String tableName) throws SQLException {
- // select the number of rows in the table
- Statement stmt = null;
- try {
- stmt = conn.createStatement();
- DatabaseMetaData dbm = conn.getMetaData();
- ResultSet tables = dbm.getTables(null, null, tableName, null);// check if "employee" table is there
- if (tables.next()) {
- return true;
- }
- }finally {
- stmt.close();
- }
- return false;
- }
- private String buildString(String message){
- PluginDescriptionFile pdFile = plugin.getDescription();
- return pdFile.getName() + " " + pdFile.getVersion() + ": " + message;
- }
- public void info(String message){
- this.logger.info(this.buildString(message));
- }
- public void warn(String message){
- this.logger.warning(this.buildString(message));
- }
- }
- # DB connection info
- Config:
- CheckDelay: 1000
- Table: smssys
- Database: jdbc:mysql://212.80.69.108/dle
- User: dle
- Pass: dlefors
- Cmds:
- Promote1: pex user <name> group set pgold
- Promote2: pex user <name> group set pgold
- Promote3: pex user <name> group set pplatinum
- Demote: pex user <name> group set Default
- Sys: s1
- #Sys: s2
Add Comment
Please, Sign In to add comment