Guest User

Untitled

a guest
Sep 19th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.70 KB | None | 0 0
  1. package me.Foryk.projects.VipChest;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.sql.Timestamp;
  9. import java.text.ParseException;
  10. import java.util.Date;
  11. import java.util.Vector;
  12.  
  13. import org.bukkit.command.ConsoleCommandSender;
  14. import org.bukkit.plugin.java.JavaPlugin;
  15.  
  16. public class VipChest extends JavaPlugin {
  17.  
  18.     protected VipChestLogHandler logHandler;
  19.     protected int dbMSQLInfoInt = 4;
  20.     protected String [] dbMSQLInfo = new String [dbMSQLInfoInt];   
  21.     long repeat = 3000L;
  22.  
  23.     public void onEnable() {
  24.         //kontrola expirace davani chesty a unbanu
  25.         this.dbMSQLInfo[0] = "vipSys"; //table name
  26.         this.dbMSQLInfo[2] = "vipsys1";//"mc_29410";// + this.getServer().getPort();//"mc_29410";// + this.getServer().getPort();//"mc_29410"; //username
  27.         this.dbMSQLInfo[1] = "jdbc:mysql://...."; //mc_29410";// mc_29410 + this.getServer().getIp() + "/" + this.dbMSQLInfo[2]; // "jdbc:mysql://mysql.sms-hosting.cz/mc_29410"; //url
  28.         this.dbMSQLInfo[3] = "pass"; //pwd
  29.        
  30.         this.logHandler = new VipChestLogHandler(this);
  31.        
  32.         this.getServer().getScheduler().scheduleSyncRepeatingTask(VipChest.this, new Runnable() {
  33.             public void run() {
  34.                 System.out.println("Vip controle.");
  35.                 try {
  36.                     Check();
  37.                 } catch (SQLException|ParseException e) {
  38.                     System.err.println("SQLException|ParseException: " + e.getMessage());
  39.                 }
  40.             }
  41.         }, 60L, repeat);
  42.     }
  43.    
  44.     public void onDisable() {
  45.         this.logHandler.info("Disable.");
  46.        
  47.     }
  48.    
  49.     public static long getCurrentTime(){
  50.         Date date = new Date();
  51.         return date.getTime();
  52.     }  
  53.    
  54.     public boolean  Check() throws ParseException, SQLException{
  55.        
  56.         String query = "SELECT * FROM " + this.dbMSQLInfo[0];
  57.         Connection con = null;
  58.         ResultSet rs = null;
  59.         @SuppressWarnings("unused")
  60.         int i=0;
  61.         Vector<String> queryes = new Vector<String>();
  62.        
  63.         try { // Load the JDBC driver
  64.            
  65.             String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
  66.             Class.forName(driverName);  // Create a connection to the database
  67.             con =  DriverManager.getConnection(this.dbMSQLInfo[1], this.dbMSQLInfo[2], this.dbMSQLInfo[3]);
  68.             Statement stmt = con.createStatement();
  69.             rs = stmt.executeQuery(query); // = stmt.executeQuery(query);
  70.            
  71.             while(rs.next()){  //kontroluje unb a deg
  72.                 i++;
  73.                 if(!rs.getString(8).matches(this.getConfig().getString("Sys"))){
  74.                     continue;
  75.                 }
  76.                 if(rs.getString(6).matches("new")){
  77.                     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) + "'");
  78.                     queryes.add("UPDATE " + this.dbMSQLInfo[0] + " SET data = 'pro' WHERE ID = '" + rs.getString(1) + "'");
  79.                     if(rs.getString(4).matches("50")){
  80.                         sendCmd(this.getConfig().getString("Cmds.Promote1"),rs.getString(2));
  81.                     }
  82.                     if(rs.getString(4).matches("2")){
  83.                         sendCmd(this.getConfig().getString("Cmds.Promote2"),rs.getString(2));
  84.                     }
  85.                     if(rs.getString(4).matches("99")){
  86.                         sendCmd(this.getConfig().getString("Cmds.Promote3"),rs.getString(2));
  87.                     }
  88.                     continue;
  89.                 }
  90.  
  91.                 if(!rs.getString(6).matches("done")&&rs.getTimestamp(7).getTime() < getCurrentTime()){
  92.                     //demote
  93.                     sendCmd(this.getConfig().getString("Cmds.Demote"),rs.getString(2));
  94.                     queryes.add("UPDATE " + this.dbMSQLInfo[0] + " SET data = 'done' WHERE ID = '" + rs.getString(1) + "'");
  95.                     continue;
  96.                 }
  97.                
  98.             }
  99.            
  100.             } catch (ClassNotFoundException e) {
  101.                 System.out.println("ClassNotFoundException " + e.getMessage()); // Could not find the database driver
  102.             } catch (SQLException e) {
  103.                 System.out.println("SQLException " + e.getMessage()); // Could not connect to the database
  104.             } catch(Exception e){
  105.                 System.out.println("Exception " + e.getMessage());
  106.             }
  107.  
  108.         query = "";
  109.         i=0;
  110.         VipChest.dbZapisVek(this,queryes);
  111.         return false;
  112.       }
  113.    
  114.     private void sendCmd(String string, String string2) {
  115.         ConsoleCommandSender x = this.getServer().getConsoleSender();
  116.         String string3 = new String();
  117.         string3= string;
  118.         if(string3.contains("<name>")){
  119.             string3=string3.replace("<name>",string2);
  120.         }
  121.         this.getServer().dispatchCommand(x, string3);
  122.     }
  123.     public static int [] dbZapisVek(VipChest plugin, Vector<String> queryes) {
  124.        
  125.         int [] i=new int [queryes.capacity()];
  126.         if(queryes.isEmpty()){
  127.             return i;
  128.         }
  129.         Connection con = null;
  130.         Statement stmt = null;
  131.        
  132.         try { // Load the JDBC driver
  133.             int a = 0;
  134.             String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
  135.             Class.forName(driverName);  // Create a connection to the database
  136.             con =  DriverManager.getConnection(plugin.dbMSQLInfo[1], plugin.dbMSQLInfo[2], plugin.dbMSQLInfo[3]);
  137.             stmt = con.createStatement();
  138.            
  139.             while(queryes.size() > a){
  140.                 i[a] = stmt.executeUpdate(queryes.get(a));
  141.                 a++;
  142.             }
  143.            
  144.         } catch (ClassNotFoundException|SQLException e) {
  145.             System.out.println("ClassNotFoundException|SQLException " + e.getMessage()); // Could not connect to the database
  146.         }finally{
  147.             try {
  148.                 con.close();
  149.             } catch (SQLException e) {
  150.                 e.printStackTrace();
  151.             }
  152.             try {
  153.                 stmt.close();
  154.             } catch (SQLException e) {
  155.                 e.printStackTrace();
  156.             }
  157.         }
  158.         return i;
  159.     }
  160.    
  161.     public static ResultSet dbCteniAll(VipChest plugin, String query) throws SQLException {
  162.         Connection con = null;
  163.         ResultSet rs = null;
  164.         try { // Load the JDBC driver
  165.             String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
  166.             Class.forName(driverName);  // Create a connection to the database
  167.             con =  DriverManager.getConnection(plugin.dbMSQLInfo[1], plugin.dbMSQLInfo[2], plugin.dbMSQLInfo[3]);
  168.             Statement stmt = con.createStatement();
  169.             rs = stmt.executeQuery(query);
  170.         } catch (SQLException|ClassNotFoundException e) {
  171.             System.out.println("SQLException|ClassNotFoundException " + e.getMessage()); // Could not find the database driver
  172.         }
  173.         return rs;
  174.     }
  175. }
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182. package me.Foryk.projects.VipChest;
  183.  
  184. import java.io.File;
  185. import java.io.FileNotFoundException;
  186. import java.io.IOException;
  187. import java.sql.Connection;
  188. import java.sql.DatabaseMetaData;
  189. import java.sql.DriverManager;
  190. import java.sql.ResultSet;
  191. import java.sql.SQLException;
  192. import java.sql.Statement;
  193. import java.util.logging.Logger;
  194.  
  195. import org.bukkit.configuration.InvalidConfigurationException;
  196. import org.bukkit.plugin.PluginDescriptionFile;
  197.  
  198. public class VipChestLogHandler implements Runnable{
  199.    
  200.     private VipChest plugin;
  201.     private Logger logger;
  202.    
  203.     @Override
  204.     public void run() {
  205.         // TODO Auto-generated method stub
  206.     }
  207.    
  208.     public VipChestLogHandler(VipChest plugin){
  209.         this.plugin = plugin;
  210.         this.logger = Logger.getLogger("Minecraft");
  211.        
  212.         try {
  213.            
  214.             String name = "./plugins/VipSys/config.yml";
  215.             File file = new File(name);
  216.             if(file.exists()){
  217.                 this.plugin.getConfig().load(name);
  218.                 }else{
  219.                 this.plugin.getConfig().options().copyDefaults(true);
  220.                 this.plugin.saveDefaultConfig();
  221.             }
  222.            
  223.             } catch (FileNotFoundException e) {
  224.                 System.err.println("FileNotFoundException: " + e.getMessage());
  225.                 this.plugin.getPluginLoader().disablePlugin(this.plugin);
  226.             } catch (IOException e) {
  227.                 System.err.println("IOException: " + e.getMessage());
  228.                 this.plugin.getPluginLoader().disablePlugin(this.plugin);
  229.             } catch (InvalidConfigurationException e) {
  230.                 System.err.println("InvalidConfigurationException: " + e.getMessage());
  231.                 this.plugin.getPluginLoader().disablePlugin(this.plugin);
  232.             }
  233.        
  234.         this.plugin.dbMSQLInfo[0] = this.plugin.getConfig().getString("Config.Table");
  235.         this.plugin.dbMSQLInfo[1] = this.plugin.getConfig().getString("Config.Database");
  236.         this.plugin.dbMSQLInfo[2] = this.plugin.getConfig().getString("Config.User");
  237.         this.plugin.dbMSQLInfo[3] = this.plugin.getConfig().getString("Config.Pass");
  238.         this.plugin.repeat = this.plugin.getConfig().getLong("Config.CheckDelay");
  239.  
  240.        
  241.         try{
  242.               String strDirectoy ="./plugins/VipSys";
  243.  
  244.               boolean success = (new File(strDirectoy)).mkdir();
  245.               if (success) {
  246.                   System.out.println("Directory: " + strDirectoy + " created.");
  247.               }
  248.            
  249.             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)"; //-----------------------------------------!!!!!!!!!!!!!!!!!!!!!!!!!!
  250.             Connection con = null;
  251.                
  252.             String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
  253.             Class.forName(driverName);  // Create a connection to the database
  254.             con =  DriverManager.getConnection(this.plugin.dbMSQLInfo[1], this.plugin.dbMSQLInfo[2], this.plugin.dbMSQLInfo[3]);
  255.             if(!tableExist(con,this.plugin.dbMSQLInfo[0])){
  256.                 Statement stmt = con.createStatement();
  257.                 stmt.executeUpdate(query); // = stmt.executeQuery(query);
  258.             }
  259.             con.close();
  260.            
  261.         } catch (ClassNotFoundException e) {
  262.             System.out.println("ClassNotFoundException " + e.getMessage()); // Could not find the database driver
  263.         } catch (SQLException e) {
  264.             System.out.println("SQLException " + e.getMessage()); // Could not connect to the database
  265.         }
  266.     }
  267.    
  268.     public static boolean tableExist(Connection conn, String tableName) throws SQLException {
  269.             // select the number of rows in the table
  270.             Statement stmt = null;
  271.             try {
  272.                  stmt = conn.createStatement();
  273.                  DatabaseMetaData dbm = conn.getMetaData();
  274.                  ResultSet tables = dbm.getTables(null, null, tableName, null);// check if "employee" table is there
  275.                  if (tables.next()) {
  276.                      return true;
  277.                  }
  278.             }finally {
  279.                 stmt.close();
  280.             }
  281.             return false;
  282.           }
  283.    
  284.     private String buildString(String message){
  285.         PluginDescriptionFile pdFile = plugin.getDescription();
  286.        
  287.         return pdFile.getName() + " " + pdFile.getVersion() + ": " + message;
  288.     }
  289.    
  290.     public void info(String message){
  291.         this.logger.info(this.buildString(message));
  292.     }
  293.    
  294.     public void warn(String message){
  295.         this.logger.warning(this.buildString(message));
  296.     }
  297.  
  298. }
  299.  
  300.  
  301.  
  302.  
  303. # DB connection info
  304. Config:
  305.   CheckDelay: 1000
  306.   Table: smssys
  307.   Database: jdbc:mysql://212.80.69.108/dle
  308.   User: dle
  309.   Pass: dlefors
  310. Cmds:
  311.   Promote1: pex user <name> group set pgold
  312.   Promote2: pex user <name> group set pgold
  313.   Promote3: pex user <name> group set pplatinum
  314.   Demote: pex user <name> group set Default
  315. Sys: s1
  316. #Sys: s2
Add Comment
Please, Sign In to add comment