ItsAlexousd

DEV' PLUGIN DE BAN MySQL #07 - BanManager

Aug 16th, 2017
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.06 KB | None | 0 0
  1. package fr.itsalexousd.bansystem.bans;
  2.  
  3. import fr.itsalexousd.bansystem.Main;
  4. import fr.itsalexousd.bansystem.utils.TimeUnit;
  5. import net.md_5.bungee.BungeeCord;
  6. import net.md_5.bungee.api.connection.ProxiedPlayer;
  7.  
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.util.UUID;
  12.  
  13. public class BanManager {
  14.  
  15.     public void ban(UUID uuid, long endInSeconds, String reason){
  16.         if(isBanned(uuid)) return;
  17.  
  18.         long endToMillis = endInSeconds * 1000;
  19.         long end = endToMillis + System.currentTimeMillis();
  20.  
  21.         if(endInSeconds == -1){
  22.             end = -1;
  23.         }
  24.  
  25.         try {
  26.             PreparedStatement sts = Main.getInstance().mysql.getConnection().prepareStatement("INSERT INTO bans (player_uuid, end, reason) VALUES (?, ?, ?)");
  27.             sts.setString(1, uuid.toString());
  28.             sts.setLong(2, end);
  29.             sts.setString(3, reason);
  30.             sts.executeUpdate();
  31.         } catch (SQLException e) {
  32.             e.printStackTrace();
  33.         }
  34.  
  35.         if(BungeeCord.getInstance().getPlayer(uuid) != null){
  36.             ProxiedPlayer target = BungeeCord.getInstance().getPlayer(uuid);
  37.             target.disconnect("§cVous avez été banni !\n " +
  38.                                  "\n " +
  39.                                  "§6Raison : §f" + reason + "\n " +
  40.                                  "\n " +
  41.                                  "§aTemps restant : §f" + getTimeLeft(uuid));
  42.         }
  43.     }
  44.  
  45.     public void unban(UUID uuid){
  46.         if(!isBanned(uuid)) return;
  47.  
  48.         try {
  49.             PreparedStatement sts = Main.getInstance().mysql.getConnection().prepareStatement("DELETE FROM bans WHERE player_uuid=?");
  50.             sts.setString(1, uuid.toString());
  51.             sts.executeUpdate();
  52.         } catch (SQLException e) {
  53.             e.printStackTrace();
  54.         }
  55.     }
  56.  
  57.     public boolean isBanned(UUID uuid){
  58.         try {
  59.             PreparedStatement sts = Main.getInstance().mysql.getConnection().prepareStatement("SELECT * FROM bans WHERE player_uuid=?");
  60.             sts.setString(1, uuid.toString());
  61.             ResultSet rs = sts.executeQuery();
  62.             return rs.next();
  63.         } catch (SQLException e) {
  64.             e.printStackTrace();
  65.         }
  66.         return false;
  67.     }
  68.  
  69.     public void checkDuration(UUID uuid){
  70.         if(!isBanned(uuid)) return;
  71.  
  72.         if(getEnd(uuid) == -1) return;
  73.  
  74.         if(getEnd(uuid) < System.currentTimeMillis()){
  75.             unban(uuid);
  76.         }
  77.     }
  78.  
  79.     public long getEnd(UUID uuid){
  80.         if(!isBanned(uuid)) return 0;
  81.  
  82.         try {
  83.             PreparedStatement sts = Main.getInstance().mysql.getConnection().prepareStatement("SELECT * FROM bans WHERE player_uuid=?");
  84.             sts.setString(1, uuid.toString());
  85.             ResultSet rs = sts.executeQuery();
  86.             if(rs.next()){
  87.                 return rs.getLong("end");
  88.             }
  89.         } catch (SQLException e) {
  90.             e.printStackTrace();
  91.         }
  92.         return 0;
  93.     }
  94.  
  95.     public String getTimeLeft(UUID uuid){
  96.         if(!isBanned(uuid)) return "§cNon banni";
  97.  
  98.         if(getEnd(uuid) == -1){
  99.             return "§cPermanent";
  100.         }
  101.  
  102.         long tempsRestant = (getEnd(uuid) - System.currentTimeMillis()) / 1000;
  103.         int mois = 0;
  104.         int jours = 0;
  105.         int heures = 0;
  106.         int minutes = 0;
  107.         int secondes = 0;
  108.  
  109.         while(tempsRestant >= TimeUnit.MOIS.getToSecond()){
  110.             mois++;
  111.             tempsRestant -= TimeUnit.MOIS.getToSecond();
  112.         }
  113.  
  114.         while(tempsRestant >= TimeUnit.JOUR.getToSecond()){
  115.             jours++;
  116.             tempsRestant -= TimeUnit.JOUR.getToSecond();
  117.         }
  118.  
  119.         while(tempsRestant >= TimeUnit.HEURE.getToSecond()){
  120.             heures++;
  121.             tempsRestant -= TimeUnit.HEURE.getToSecond();
  122.         }
  123.  
  124.         while(tempsRestant >= TimeUnit.MINUTE.getToSecond()){
  125.             minutes++;
  126.             tempsRestant -= TimeUnit.MINUTE.getToSecond();
  127.         }
  128.  
  129.         while(tempsRestant >= TimeUnit.SECONDE.getToSecond()){
  130.             secondes++;
  131.             tempsRestant -= TimeUnit.SECONDE.getToSecond();
  132.         }
  133.  
  134.         // 1 Mois, 1 Jour(s), 12 Heure(s), 32 Minute(s), 12 Seconde(s)
  135.         return mois + " " + TimeUnit.MOIS.getName() + ", " + jours + " " + TimeUnit.JOUR.getName() + ", " + heures + " " + TimeUnit.HEURE.getName() + ", " + minutes + " " + TimeUnit.MINUTE.getName() + ", " + secondes + " " + TimeUnit.SECONDE.getName();
  136.     }
  137.  
  138.     public String getReason(UUID uuid){
  139.         if(!isBanned(uuid)) return "§cNon banni";
  140.  
  141.         try {
  142.             PreparedStatement sts = Main.getInstance().mysql.getConnection().prepareStatement("SELECT * FROM bans WHERE player_uuid=?");
  143.             sts.setString(1, uuid.toString());
  144.             ResultSet rs = sts.executeQuery();
  145.             if(rs.next()){
  146.                 return rs.getString("reason");
  147.             }
  148.         } catch (SQLException e) {
  149.             e.printStackTrace();
  150.         }
  151.         return "§cNon banni";
  152.     }
  153.  
  154. }
Add Comment
Please, Sign In to add comment