Date Format

Discussion in 'Plugin Development' started by djmaster329, Jun 6, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    djmaster329

    Hello,

    I have been working on a way to make pay2play for Minecraft easy.
    You need to pay some money and then get 30 days of access to the server.

    It writes that date to a database in the format yyyy/MM/dd and compares today's date with the date in the database. The player will be kicked if the date in the database lies in the past.

    But how can I compare the date in the database with the date of today?
    I have no idea how.

    SubscriptionManager:
    Code:
    package me.djmaster329.SubscriptionManager;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.EventHandler;
    import java.sql.*;
    import java.util.logging.Logger;
    import java.util.Calendar;
    import java.util.Date;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.io.*;
     
    /*
    * extends pugin means that it is a plugin
    * implements Listener declares this class as an event listener. It makes sense to put listeners for
    * different stuff into different classes but this is just a simple example
    */
    public class SubscriptionManager extends JavaPlugin implements Listener {
       
        Logger log = Logger.getLogger("Minecraft");
       
        @Override
        public void onEnable() {
            //this method is called once your plugin is enabled
     
            //this gets the plugin manager for our plugin
            PluginManager pm = this.getServer().getPluginManager();
     
            /*
            * this registers our listener. The first argument is the listener the second the plugin
            * if you put the listener into a different class you have to put an istance of that class
            * as the first argument
            */
            pm.registerEvents(this, this);
            CreateFolder();
           
           
        }
       
        public void CreateFolder(){
            boolean exists = (new File("plugins/SubscriptionManager/")).exists();
            if (exists) {
                // do nothing
            } else {
                boolean successmainfolder = (new File("plugins/SubscriptionManager/")).mkdir();
                        if (successmainfolder) {
                            log.info("Folder created!");
                        }
            }
        }
       
     
       
        public void onDisable() {
            //this method is called once your plugin is diabled you can save stuff or clean up here
        }
     
        /*
        * The on command method is called every time a command which is registered for your plugin is called
        * @sender: this is the sender of the command. Can be a player or the command line
        * [USER=55020]CMD[/USER]: a command object. Look into the API docs for what you can do with it
        * @commandLabel: a string of which command has been issued. Useful if you have more than one command
        * @args: an array of arguments supplied to the command
        */
        /*public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(cmd.getName().equalsIgnoreCase("basic")){ // If the player typed /basic then do the following...
                //doSomething
                return true;
            } //If this has happened the function will break and return true. if this hasn't happened the a value of false will be returned.
            //you should always return true if the command has been processed by your plugin and false if not
            return false;
        }/*
     
        /*
        * this is an event hanldler event handlers have to be in classes which implement Listener
        * the @EventHandler annotation marks this method as an event handler
        * the method name can be anything you like
        * event handler have one parameter which also defines which event they handle
        */
       
        @EventHandler
        public void loginHandler(PlayerLoginEvent event) {
            /*
            * this method is called every time a player logs in you can find out more about the event
            * via the given parameter
            * e.g. we can determine which player logged in and send him a welcome message
            */
            String username = event.getPlayer().getDisplayName().toLowerCase();
            if(username.contains(" ")){
                event.getPlayer().kickPlayer("Your username contains a space, which is not allowed for security reasons!");
            }else{
                if(username.contains("'")) {
                    event.getPlayer().kickPlayer("Your username contains a single quote, which is not allowed for security reasons!");
                }else{
                    if(username.contains(";")){
                        event.getPlayer().kickPlayer("Your username contains illegal characters!");
                    }else{
                        Date date = new Date();
                        // Keep the MM upper case
                        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
                        String formattedDate = df.format(date);
                       
                       
                        boolean exists = (new File("plugins/SubscriptionManager/" + username + ".txt")).exists();
                        if (exists) {
     
                            Connection con = null;
                            String url = "jdbc:mysql://thomasdomein.nl:3306/";
                            String db = "database";
                            String driver = "com.mysql.jdbc.Driver";
                            try{
                                Class.forName(driver);
                                con = DriverManager.getConnection(url+db,db.toString(),"pass");
                                try{
                                    log.info("Attempting to check subscription for " + event.getPlayer().getDisplayName());
                                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
                                    Statement st = con.createStatement();
                                    ResultSet val = st.executeQuery("SELECT `user`, `date` FROM `users` WHERE `user` = '" + username +"'");
                                    if(val.next()){
                                        InputStream stream = val.getBinaryStream(1);
                                        String user = val.getString("user");
                                        String subdate = val.getString("date");
                                        Date convertedDate = dateFormat.parse(subdate);
                                        Date CheckDate = convertedDate;
                                        Date datetoday = dateFormat.parse(formattedDate);
                                       
                                        log.info("Checking date " + CheckDate);
                                        if(CheckDate.after(datetoday) == true){
                                            log.info("Subscription over!");
                                            event.getPlayer().kickPlayer("Your subscription is over, go to http://bit.ly/KOc6Wy");
                                        }else{
                                            log.info("Subscription not over!");
                                            event.getPlayer().sendMessage("You are subscribed until " + subdate.toString() + ". Have Fun!");
                                        }
                                    }
                                   
     
                                }
                                catch (SQLException s){
                                    System.out.println("SQL statement is not executed!");
                                    s.printStackTrace();
                                }
                          }
                          catch (Exception e){
                              e.printStackTrace();
                          }
     
                           
                        } else {
                            System.out.println("Inserting values in Mysql database table!");
                            Connection con = null;
                            String url = "jdbc:mysql://thomasdomein.nl:3306/";
                            String db = "database";
                            String driver = "com.mysql.jdbc.Driver";
                            try{
                                Class.forName(driver);
                                con = DriverManager.getConnection(url+db,db.toString(),"password");
                                try{
                            Statement st = con.createStatement();
                            int val = st.executeUpdate("INSERT INTO `w2278368_mcpay`.`users` (`id`, `pincode`, `user`, `date`) VALUES (NULL, '', '" + date + "', '"+ date +"');");
                            System.out.println("Successfully wrote new user " + username.toString() + " to database!");
                            Writeuserfile(username);
                                }
                                catch (SQLException s){
                                    System.out.println("SQL statement is not executed!");
                                    s.printStackTrace();
                                }
                          }
                          catch (Exception e){
                              e.printStackTrace();
                          }
                        }
                    }
                }
            }
           
        }
     
        /*
        * This is another event handler with high priority. So if there are other handlers for the same
        * event this one will be called first
        */
       
         
        public void Writeuserfile(String username){
            try {
                File f;
                f=new File("plugins/SubscriptionManager/" + username + ".txt");
                  f.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
             
        }
       
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
           
           
                return true;
        }
    }
    
     
  2. Offline

    wirher

    Why don't just use System.currentTimeMillis()?
     
  3. Offline

    djmaster329

    And what would that look like in the current code?
     
  4. Offline

    dxwarlock

    IF it helps any, this it what i use reading logblocks date in SQL and compare it to todays date to figure out the days difference.

    Code:java
    1.  
    2. Date join = LBJoin(name);
    3. Date today = new Date();
    4. long diff = today.getTime() - join.getTime();
    5. long age = diff / (1000 * 60 * 60 * 24);
    6.  


    Just a matter of changing "LBJoin" to pull your date and compare them
     
  5. Offline

    djmaster329

    I will check it out, thanks :D
     
  6. Offline

    dxwarlock

    That is using the SQL "datetime" Field type, not sure if your that, or writing it as a text in the column.
     
  7. Offline

    djmaster329

    I used to have the field type on 'date', but changed it into string for now. I will change it then :)
     
Thread Status:
Not open for further replies.

Share This Page