Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.rankup.fr;
- import java.sql.Connection ;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- 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))) {/*si sa ne marche pas remettre 5 a la place de 20*/
- return false;
- } else {
- return true;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- private Connection getConnection() {
- return connection;
- }
- public void createPlayerIfNotExist(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 suppPermission(Player p, String permission){
- try {
- PreparedStatement sts = getConnection().prepareStatement("DELETE FROM "+table+" WHERE " + "uuid = ? " + "and permissions = ?");
- sts.setString(1, p.getUniqueId().toString());
- sts.setString(2, permission);
- sts.executeUpdate();
- sts.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void suppPermissionsTime(){
- try {
- PreparedStatement sts = getConnection().prepareStatement("DELETE FROM "+table+" WHERE time <= NOW()");
- sts.executeUpdate();
- sts.close();
- }catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public boolean hasPermission(Player p, String permission){
- suppPermissionsTime();
- try {
- PreparedStatement sts = getConnection().prepareStatement("SELECT permissions FROM " + table + " WHERE uuid = ? AND time >= NOW()");
- sts.setString(1, p.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 addPermissionWithTimer(Player player, String permission, Integer temps){
- try {
- PreparedStatement sts = getConnection().prepareStatement("INSERT INTO "+table+" (uuid, rank, permissions, time) VALUES (?, ?, ?, DATE_ADD(NOW(), INTERVAL " + temps + " MINUTE))");
- 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 addPermission(Player p, String permission){
- try {
- PreparedStatement sts = getConnection().prepareStatement("INSERT INTO "+table+" (uuid, rank, permissions) VALUES (?, ?, ?)");
- sts.setString(1, p.getUniqueId().toString());
- sts.setInt(2, getRank(p.getUniqueId().toString()).getPower());
- sts.setString(3, permission);
- sts.executeUpdate();
- sts.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void setRank(Player p, Rank r){
- try {
- PreparedStatement sts = getConnection().prepareStatement("UPDATE "+table+" SET rank = ? WHERE uuid = ?");
- sts.setInt(1, r.getPower());
- sts.setString(2, p.getUniqueId().toString());
- sts.executeUpdate();
- sts.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment