Advertisement
Guest User

Untitled

a guest
Sep 27th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.48 KB | None | 0 0
  1. package net.mcduels.authentication;
  2.  
  3. import net.mcduels.authentication.exceptions.LackOfPermissionException;
  4. import net.mcduels.authentication.exceptions.PasswordBoundsException;
  5. import net.md_5.bungee.api.ChatColor;
  6. import net.md_5.bungee.api.chat.BaseComponent;
  7. import net.md_5.bungee.api.chat.ClickEvent;
  8. import net.md_5.bungee.api.chat.ComponentBuilder;
  9. import org.bukkit.command.CommandSender;
  10. import org.bukkit.entity.Player;
  11. import org.json.simple.JSONObject;
  12. import org.json.simple.parser.JSONParser;
  13. import org.json.simple.parser.ParseException;
  14.  
  15. import java.io.BufferedWriter;
  16. import java.io.FileReader;
  17. import java.io.FileWriter;
  18. import java.io.IOException;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Date;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import java.util.UUID;
  24.  
  25. public class AuthenticationRegistration<T , V> {
  26.  
  27.     private UUID identification;
  28.  
  29.     private T username;
  30.     private V password;
  31.  
  32.     private int autehntication_reset = 0; //default password reset integer
  33.  
  34.     public AuthenticationRegistration(final UUID id, T user, V pass, int reset) {
  35.         this.identification = id;
  36.         this.password = pass;
  37.         this.username = user;
  38.         this.autehntication_reset = reset;
  39.     }
  40.  
  41.     @SuppressWarnings("unchecked")
  42.     public void initialize() throws IOException, PasswordBoundsException {
  43.         JSONObject object = new JSONObject();
  44.  
  45.         object.put("identification", this.identification);
  46.         object.put("username", this.username);
  47.  
  48.         int[] passArray = (int[]) password;
  49.  
  50.         if (passArray.length > 10) {
  51.  
  52.             for (Player all : Authentication.getInstance().getServer().getOnlinePlayers()) {
  53.                 if (all.getUniqueId().equals(identification)) {
  54.  
  55.                     throw new PasswordBoundsException(); // TODO: customize it
  56.                 }
  57.             }
  58.         } else {
  59.  
  60.             object.put("password", this.password);
  61.             object.put("authentication_reset", this.autehntication_reset);
  62.             object.put("timestamp", getTimeStamp());
  63.         }
  64.  
  65.         try (BufferedWriter writer = new BufferedWriter(new FileWriter(Authentication.getInstance().getAuthenticationFile()))) {
  66.  
  67.             writer.write(object.toJSONString());
  68.             writer.flush();
  69.  
  70.         } finally {
  71.             Authentication.getInstance().getLogger().info(this.toString());
  72.         }
  73.     }
  74.  
  75.     @Override
  76.     public String toString() {
  77.         return getTimeStamp() + ":" + identification + ":" + username + ":" + password;
  78.     }
  79.  
  80.     private String getTimeStamp() {
  81.         return new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
  82.     }
  83.  
  84.     public static class AuthenticationManager {
  85.  
  86.         /**
  87.          * GLOBAL AUTHENTICATION GETTER
  88.          */
  89.  
  90.         public static String getAuthenticationPlayer(Player player, CommandSender sender) throws LackOfPermissionException {
  91.             if (!sender.hasPermission("authentication.check")) { throw new LackOfPermissionException(); } //TODO: customize it
  92.  
  93.             JSONParser parser = new JSONParser();
  94.  
  95.             try {
  96.  
  97.                 Object obj = parser.parse(new FileReader(Authentication.getInstance().getAuthenticationFile()));
  98.  
  99.                 JSONObject object = (JSONObject) obj;
  100.  
  101.                 sender.sendMessage("&a&lFetching Authentication data (GLOBAL)...".replace('&', '§'));
  102.  
  103.                 if (object.containsKey(player.getUniqueId())) {
  104.  
  105.                     String id = (String) object.get("identification");
  106.                     String user = (String) object.get("username");
  107.                     String pass = (String) object.get("password");
  108.                     String time = (String) object.get("timestamp");
  109.                     int reset = (int) object.get("authentication_reset");
  110.  
  111.                     sender.sendMessage("&eIdentification: &f".replace('&', '§') + id);
  112.                     sender.sendMessage("&eUsername: &f".replace('&', '§') + user);
  113.                     sender.sendMessage("&ePassword: &f".replace('&', '§') + pass);
  114.                     sender.sendMessage("&eReset: &f".replace('&', '§') + reset);
  115.                     sender.sendMessage("&eTimeStamp: &f".replace('&', '§') + time);
  116.                 } else {
  117.  
  118.                     sender.sendMessage(player.getName() + " wasn't found in the database! :(");
  119.  
  120.                 }
  121.             } catch (IOException | ParseException e) {
  122.                 e.printStackTrace();
  123.             }
  124.  
  125.             return null;
  126.         }
  127.        
  128.         private static Long registerResetRunnable(Object obj) {
  129.             final Map<Object, Long> runnable = new HashMap<>();
  130.            
  131.             if (runnable.containsKey(obj)) {
  132.                
  133.                 long elapsedTimeLong = runnable.get(obj) - System.currentTimeMillis();
  134.                
  135.                 return elapsedTimeLong/1000;
  136.             } else {
  137.                 runnable.put(obj, System.currentTimeMillis() + (10 * 1000));
  138.             }
  139.            
  140.             return null;
  141.         }
  142.  
  143.         private static BaseComponent[] clickable(Player player) {
  144.            
  145.             return new ComponentBuilder("are you sure you want to reset?").color(ChatColor.AQUA).append("[CANCEL]").color(ChatColor.RED).event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/authentication admin cancel " + player)).append("[CONFIRM]").color(ChatColor.GREEN).event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/authentication admin reset " + player)).create();
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement