Tutorial MySQL - Update player's old name with new name via MYSQL

Discussion in 'Resources' started by Eos, Aug 6, 2015.

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

    Eos

    I'm using Husky's MySQL for example but it should work with any MySQL library.

    MAKE SURE THAT THE PLAYER UUID IS PRIMARY AND UNIQUE

    First we need to create 2 queries one that selects from the users profile and one that updates their name.

    Code:
           String uuid = player.getUniqueId().toString();
            ResultSet query = sql.sql.querySQL("SELECT * FROM `profiles` WHERE `UUID`= '" + uuid + "';");
            PreparedStatement pquery = sql.c.prepareStatement("UPDATE `profiles` SET Name = ? WHERE UUID = ?");
    We're going to use the PlayerJoinEvent to detect the player name change

    Code:
        public void onJoin(PlayerJoinEvent e) throws Exception {
            Player player = e.getPlayer();
    
            String uuid = player.getUniqueId().toString();
    
            ResultSet query = sql.sql.querySQL("SELECT * FROM `profiles` WHERE `UUID`= '" + uuid + "';");
            PreparedStatement pquery = sql.c.prepareStatement("UPDATE `profiles` SET Name = ? WHERE UUID = ?");
    
        }
    Now we're going to actual detect if their uuid exists in the database and check if they changed their name or not and update accordingly
    Code:
            if ( query.next() )
            {
                if ( query.getString("Name").equals(player.getName()) )
                {
                    player.sendMessage("You haven't changed your name");
    
                } else {
    
                    Bukkit.broadcastMessage(ChatColor.YELLOW + query.getString("Name") + " has changed his name to " + player.getName()); // Broadcast if they changed there name
                    pquery.setString(1, player.getName());
                    pquery.setString(2, uuid);
                    pquery.executeUpdate(); // EXECUTE THE UPDATE SO THE CHANGE CAN BE APPLIED ON THE DATABASE
                    pquery.close(); // MAKE SURE YOU CLOSE THE QUERIES you don't want a shit ton of queries running
                    query.close();
    
                }
    
            }
    Final Result
    Code:
        public void onJoin(PlayerJoinEvent e) throws Exception {
            Player player = e.getPlayer();
    
            String uuid = player.getUniqueId().toString();
    
            ResultSet query = sql.sql.querySQL("SELECT * FROM `profiles` WHERE `UUID`= '" + uuid + "';");
            PreparedStatement pquery = sql.c.prepareStatement("UPDATE `profiles` SET Name = ? WHERE UUID = ?");
    
            // Name Change Detection
            if ( query.next() ) // Checks if the players uuid exists
            {
                if ( query.getString("Name").equals(player.getName()) ) // Checks if the players name is the same as the one on the database
                {
                    player.sendMessage("You haven't changed your name");
    
                } else {
    
                    Bukkit.broadcastMessage(ChatColor.YELLOW + query.getString("Name") + " has changed his name to " + player.getName());
                    pquery.setString(1, player.getName());
                    pquery.setString(2, uuid);
                    pquery.executeUpdate();
                    pquery.close();
                    query.close();
    
                }
    
            }
        }
     
    Shortninja66 likes this.
Thread Status:
Not open for further replies.

Share This Page