Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.18 KB | None | 0 0
  1. package fr.fezzfall.api.others.sanctions;
  2.  
  3. import fr.fezzfall.api.bungee.APIBungee;
  4. import fr.fezzfall.api.bungee.events.PlayerChatBungeeEvent;
  5. import java.text.ParseException;
  6. import java.text.SimpleDateFormat;
  7. import java.util.ArrayList;
  8. import java.util.Date;
  9. import java.util.UUID;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12. import net.md_5.bungee.api.ProxyServer;
  13. import net.md_5.bungee.api.chat.ComponentBuilder;
  14. import net.md_5.bungee.api.connection.ProxiedPlayer;
  15. import org.apache.commons.lang.time.DateUtils;
  16. import redis.clients.jedis.Jedis;
  17.  
  18. public class SanctionManager
  19. {
  20.   private SanctionType type;
  21.   private String author;
  22.   private String target_name;
  23.   private String target_uuid;
  24.   private Date time;
  25.   private Date endtime;
  26.   private String endtimeString;
  27.   private String reason;
  28.   private boolean result;
  29.  
  30.   public SanctionManager(SanctionType type, String author, String target_name, String endtime, String reason, boolean targetname)
  31.   {
  32.     String UUID = null;
  33.     try
  34.     {
  35.       Jedis cache = new Jedis(APIBungee.getIP(), 3305, 0);
  36.       cache.auth(APIBungee.getMDP());
  37.      
  38.       UUID = cache.hget("players:" + target_name + ":stats", "bungee_uuid");
  39.      
  40.       cache.close();
  41.     }
  42.     catch (Exception e2)
  43.     {
  44.       e2.printStackTrace();
  45.     }
  46.     if (UUID != null) {
  47.       new SanctionManager(type, author, UUID, target_name, endtime, reason);
  48.     } else {
  49.       this.result = true;
  50.     }
  51.   }
  52.  
  53.   public SanctionManager(SanctionType type, String author, ProxiedPlayer target, String endtime, String reason)
  54.   {
  55.     this(type, author, target.getUniqueId().toString(), target.getName(), endtime, reason);
  56.   }
  57.  
  58.   public SanctionManager(SanctionType type, String author, String target_uuid, String target_name, String endtime, String reason)
  59.   {
  60.     this.type = type;
  61.     this.author = author;
  62.     this.target_uuid = target_uuid;
  63.     this.target_name = target_name;
  64.     this.reason = reason;
  65.     this.time = new Date();
  66.     if (!endtime.contains(" "))
  67.     {
  68.       Integer minutes = Integer.valueOf(convertInMinutes(endtime));
  69.       Date targetTime = new Date();
  70.       this.endtime = DateUtils.addMinutes(targetTime, minutes.intValue());
  71.       this.endtimeString = getTimeString(endtime, minutes.intValue());
  72.     }
  73.     else
  74.     {
  75.       SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
  76.       try
  77.       {
  78.         this.endtime = sdf.parse(endtime);
  79.       }
  80.       catch (ParseException e)
  81.       {
  82.         e.printStackTrace();
  83.       }
  84.     }
  85.     run();
  86.   }
  87.  
  88.   public Date getEndtime()
  89.   {
  90.     return this.endtime;
  91.   }
  92.  
  93.   public String getTarget_uuid()
  94.   {
  95.     return this.target_uuid;
  96.   }
  97.  
  98.   public String getReason()
  99.   {
  100.     return this.reason;
  101.   }
  102.  
  103.   private String getTimeString(String endtime, int min)
  104.   {
  105.     String time;
  106.     String time;
  107.     if (endtime.contains("min"))
  108.     {
  109.       time = min + " minute(s)";
  110.     }
  111.     else
  112.     {
  113.       String time;
  114.       if (endtime.contains("h"))
  115.       {
  116.         time = min / 60 + " heure(s)";
  117.       }
  118.       else
  119.       {
  120.         String time;
  121.         if (endtime.contains("j"))
  122.         {
  123.           time = min / 1440 + " jours";
  124.         }
  125.         else
  126.         {
  127.           String time;
  128.           if (endtime.contains("s"))
  129.           {
  130.             time = min / 10080 + " semaine(s)";
  131.           }
  132.           else
  133.           {
  134.             String time;
  135.             if (endtime.contains("mois")) {
  136.               time = min / 43200 + " mois";
  137.             } else {
  138.               time = min / 518400 + " an(s)";
  139.             }
  140.           }
  141.         }
  142.       }
  143.     }
  144.     return time;
  145.   }
  146.  
  147.   public void run()
  148.   {
  149.     String path = this.type.getPathredis();
  150.     try
  151.     {
  152.       Jedis cache = new Jedis(APIBungee.getIP(), 3305, 0);
  153.       cache.auth(APIBungee.getMDP());
  154.       if (cache.exists(path).booleanValue())
  155.       {
  156.         SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
  157.         String time = sdf.format(this.time);
  158.         String endtime = sdf.format(this.endtime);
  159.        
  160.         String key = this.target_uuid.toString();
  161.         String value = this.author + ";" + endtime + ";" + this.reason;
  162.        
  163.         cache.hset(path, key, value);
  164.         if (this.target_name != null) {
  165.           cache.hset("players:" + this.target_name + ":sanctions", time, this.type.getId() + "," + value);
  166.         }
  167.       }
  168.       cache.close();
  169.      
  170.       this.result = false;
  171.     }
  172.     catch (Exception e2)
  173.     {
  174.       e2.printStackTrace();
  175.     }
  176.   }
  177.  
  178.   public void mutePlayer(ProxiedPlayer pp, boolean annonce)
  179.   {
  180.     if (this.endtimeString != null)
  181.     {
  182.       if (annonce) {
  183.         ProxyServer.getInstance().broadcast(new ComponentBuilder("").append("§4§lGardenssProtect §f§l» §6" + pp.getName() + "§e est mute pendant §6" + this.endtimeString + ". §7§o(" + this.reason + ")").create());
  184.       }
  185.       pp.sendMessage(new ComponentBuilder("").create());
  186.       pp.sendMessage(new ComponentBuilder("").append("§4§lGardenssProtect §f§l» §cVous avez été mute !").create());
  187.       pp.sendMessage(new ComponentBuilder("").create());
  188.      
  189.       PlayerChatBungeeEvent.playersMute.add(this);
  190.     }
  191.   }
  192.  
  193.   public void disconnectPlayer(ProxiedPlayer pp)
  194.   {
  195.     if (this.endtimeString != null)
  196.     {
  197.       pp.disconnect(new ComponentBuilder("").append("§4§lGardenssProtect §f§l» §cVous avez été banni !").create());
  198.       ProxyServer.getInstance().broadcast(new ComponentBuilder("").append("§4§lGardenssProtect §f§l» §6" + pp.getName() + "§e est banni pendant §6" + this.endtimeString + ". §7§o(" + this.reason + ")").create());
  199.     }
  200.   }
  201.  
  202.   public boolean failed()
  203.   {
  204.     return this.result;
  205.   }
  206.  
  207.   private static int convertInMinutes(String args)
  208.   {
  209.     String patternMinutes = "[0-9]{1,3}min";
  210.     String patternHours = "[0-9]{1,3}h";
  211.     String patternDay = "[0-9]{1,3}j";
  212.     String patternWeek = "[0-9]{1,3}s";
  213.     String patternMonth = "[0-9]{1,3}mois";
  214.    
  215.     Pattern pMinutes = Pattern.compile(patternMinutes);
  216.     Pattern pHours = Pattern.compile(patternHours);
  217.     Pattern pDay = Pattern.compile(patternDay);
  218.     Pattern pWeek = Pattern.compile(patternWeek);
  219.     Pattern pMonth = Pattern.compile(patternMonth);
  220.    
  221.     Matcher mMinutes = pMinutes.matcher(args.toLowerCase());
  222.     Matcher mHours = pHours.matcher(args.toLowerCase());
  223.     Matcher mDay = pDay.matcher(args.toLowerCase());
  224.     Matcher mWeek = pWeek.matcher(args.toLowerCase());
  225.     Matcher mMonth = pMonth.matcher(args.toLowerCase());
  226.     if (mMinutes.matches()) {
  227.       return Integer.parseInt(args.split("min")[0]);
  228.     }
  229.     if (mHours.matches()) {
  230.       return Integer.parseInt(Integer.valueOf(args.split("h")[0]).intValue() * 60 + "");
  231.     }
  232.     if (mDay.matches()) {
  233.       return Integer.parseInt(Integer.valueOf(args.split("j")[0]).intValue() * 1440 + "");
  234.     }
  235.     if (mWeek.matches()) {
  236.       return Integer.parseInt(Integer.valueOf(args.split("s")[0]).intValue() * 10080 + "");
  237.     }
  238.     if (mMonth.matches()) {
  239.       return Integer.parseInt(Integer.valueOf(args.split("mois")[0]).intValue() * 43200 + "");
  240.     }
  241.     return Integer.parseInt("518400");
  242.   }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement