Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.Ajeux.MyRank;
- import java.sql.*;
- import org.bukkit.entity.Player;
- public class RankSQL {
- private String url_base, host, name, username, password, table;
- private Connection connection;
- public RankSQL(String url_base, String host, String name, String username, String password, String table){
- this.url_base = url_base;
- this.host = host;
- this.name = name;
- this.username = username;
- this.password = password;
- this.table = table;
- }
- public void connection(){
- if(!isConnected()){
- try{
- connection = DriverManager.getConnection(url_base + host + "/" + name, username, password);
- }catch(SQLException e){
- e.printStackTrace();
- }
- }
- }
- public void deconnection(){
- if(isConnected()){
- try{
- connection.close();
- }catch(SQLException e){
- e.printStackTrace();
- }
- }
- }
- private boolean isConnected(){
- try{
- if((connection == null) || (connection.isClosed() || (connection.isValid(5)))){
- return false;
- }else{
- return true;
- }
- }catch(SQLException e){
- e.printStackTrace();
- }
- return false;
- }
- private Connection getConnection(){
- return connection;
- }
- public void createAccount(Player player){
- try{
- PreparedStatement sts = getConnection().prepareStatement("SELECT rank FROM " + table + " WHERE uuid = ?");
- sts.setString(1, player.getUniqueId().toString());
- ResultSet rs = sts.executeQuery();
- if(!rs.next()){
- sts.close();
- sts = getConnection().prepareStatement("INSERT INTO " + table + " (uuid, rank) VALUES (?, ?)");
- sts.setString(1, player.getUniqueId().toString());
- sts.setInt(2, 10);
- sts.executeUpdate();
- sts.close();
- }
- }catch(SQLException e){
- e.printStackTrace();
- }
- }
- public Rank getRank(String uuid){
- Rank rank = null;
- try{
- PreparedStatement sts = getConnection().prepareStatement("SELECT rank FROM " + table + " WHERE uuid = ?");
- sts.setString(1, uuid);
- ResultSet rs = sts.executeQuery();
- if(!rs.next()){
- return null;
- }
- rank = Rank.getFromPower(rs.getInt("rank"));
- sts.close();
- }catch(SQLException e){
- e.printStackTrace();
- }
- return rank;
- }
- public void setRank(Player player, Rank rank){
- try{
- PreparedStatement sts = getConnection().prepareStatement("UPDATE " + table + " SET rank = ? WHERE uuid = ?");
- sts.setInt(1, rank.getPower());
- sts.setString(2, player.getUniqueId().toString());
- sts.executeUpdate();
- sts.close();
- }catch(SQLException e){
- e.printStackTrace();
- }
- }
- public boolean hasPermission(Player player, String permission){
- try{
- PreparedStatement sts = getConnection().prepareStatement("SELECT permissions FROM " + table + " WHERE uuid = ?");
- sts.setString(1, player.getUniqueId().toString());
- ResultSet rs = sts.executeQuery();
- while(rs.next()){
- if(permission.equalsIgnoreCase(rs.getString("permissions"))){
- return true;
- }
- }
- sts.close();
- }catch(SQLException e){
- e.printStackTrace();
- }
- return false;
- }
- public void addPermission(Player player, String permission){
- try{
- PreparedStatement sts = getConnection().prepareStatement("INSERT INTO " + table + " (uuid, rank, permissions) VALUES (?, ?, ?)");
- sts.setString(1, player.getUniqueId().toString());
- sts.setInt(2, getRank(player.getUniqueId().toString()).getPower());
- sts.setString(3, permission);
- sts.executeUpdate();
- sts.close();
- }catch(SQLException e){
- e.printStackTrace();
- }
- }
- public void removePermission(Player player, String permission){
- try{
- PreparedStatement sts = getConnection().prepareStatement("DELETE FROM " + table + " WHERE "+"uuid = ?"+"and permissions = ?");
- sts.setString(1, player.getUniqueId().toString());
- sts.setString(2, permission);
- sts.executeUpdate();
- sts.close();
- }catch(SQLException e){
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment