Advertisement
imBEheAR

Logowanie

Jan 19th, 2022
942
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Singleton
  2. public class UserRepository implements Listener {
  3.  
  4.     private final Set<UUID> premiumAccount = Sets.newHashSet();
  5.     private final ConcurrentMap<UUID, String> firstPassword = Maps.newConcurrentMap();
  6.     private final ConcurrentMap<UUID, RegisteredUser> userCache = Maps.newConcurrentMap();
  7.     private final Set<UUID> locked = Sets.newHashSet();
  8.     private final Set<UUID> registering = Sets.newHashSet();
  9.  
  10.     private final RegisteredUserDatabase userDatabase;
  11.     private final SpigotMessenger spigotMessenger;
  12.  
  13.     @Inject
  14.     public UserRepository(RegisteredUserDatabase userDatabase, SpigotMessenger spigotMessenger) {
  15.         this.userDatabase = userDatabase;
  16.         this.spigotMessenger = spigotMessenger;
  17.     }
  18.  
  19.     @EventHandler
  20.     public void handlePlayerJoin(PlayerJoinEvent event) {
  21.  
  22.         Player player = event.getPlayer();
  23.  
  24.         locked.add(player.getUniqueId());
  25.         player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 999999, 10));
  26.         player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 999999, 10));
  27.  
  28.         userDatabase.loadUser(player.getUniqueId(), user -> {
  29.             if (user == null) {
  30.                 registering.add(player.getUniqueId());
  31.  
  32.                 player.sendMessage("§6Prosimy o wprowadzenie hasła na czacie, bez żadnych komend.");
  33.                 player.sendMessage("§eWybierz silne hasło! Nie odpowiadamy za włamania.");
  34.  
  35.                 return;
  36.             }
  37.  
  38.             player.sendMessage("§aWitaj ponownie na naszym serwerze!");
  39.  
  40.             player.sendMessage("§7Prosimy o wprowadzenie hasła do konta na czacie.");
  41.             userCache.put(player.getUniqueId(), user);
  42.         });
  43.  
  44.     }
  45.  
  46.     @EventHandler
  47.     public void handlePlayerQuit(PlayerQuitEvent event) {
  48.         removeAll(event.getPlayer());
  49.     }
  50.  
  51.     @EventHandler
  52.     public void handlePlayerMove(PlayerMoveEvent event) {
  53.         Player player = event.getPlayer();
  54.         if (!locked.contains(player.getUniqueId())) {
  55.             return;
  56.         }
  57.         player.teleport(event.getFrom());
  58.     }
  59.  
  60.     @EventHandler
  61.     public void handleAsyncPlayerChat(AsyncPlayerChatEvent event) {
  62.         Player player = event.getPlayer();
  63.         String message = event.getMessage().replace("%", "%%");
  64.  
  65.         if (!locked.contains(player.getUniqueId())) {
  66.             return;
  67.         }
  68.  
  69.         event.setCancelled(true);
  70.  
  71.         if (firstPassword.containsKey(player.getUniqueId())) {
  72.             String password = firstPassword.get(player.getUniqueId());
  73.  
  74.             if (!password.equals(message)) {
  75.                 ServerMainThread.RunParallel.run(() -> {
  76.                     player.kickPlayer("§cWprowadziłeś dwa różne hasła. Spróbuj ponownie.");
  77.                 });
  78.                 return;
  79.             }
  80.  
  81.             player.sendMessage("§aPomyślnie utworzyłeś konto! Miłej gry :)");
  82.  
  83.             String salt = BCrypt.gensalt();
  84.  
  85.             userDatabase.registerUser(new RegisteredUser(
  86.                     player.getUniqueId(), player.getName(),
  87.                     premiumAccount.contains(player.getUniqueId()),
  88.                     salt,
  89.                     BCrypt.hashpw(password, salt)));
  90.  
  91.             removeAll(player);
  92.             spigotMessenger.movePlayerToLobby(player);
  93.         } else if (registering.contains(player.getUniqueId())) {
  94.             firstPassword.put(player.getUniqueId(), message);
  95.             player.sendMessage("§7Prosimy o powtórzenie hasła.");
  96.         } else {
  97.             RegisteredUser user = userCache.get(player.getUniqueId());
  98.             if (BCrypt.checkpw(message, user.getHashedPassword())) {
  99.                 player.sendMessage("§aHasło przyjęte! Przenoszenie na ostatnią lokalizacje sektorów...");
  100.                 removeAll(player);
  101.                 spigotMessenger.movePlayerToLobby(player);
  102.                 return;
  103.             }
  104.  
  105.             ServerMainThread.RunParallel.run(() -> {
  106.                 player.kickPlayer("§cWprowadzono błędne hasło. Spróbuj ponownie.");
  107.             });
  108.         }
  109.     }
  110.  
  111.     private void removeAll(Player player) {
  112.         ServerMainThread.RunParallel.run(() -> {
  113.             for (PotionEffect potionEffect : player.getActivePotionEffects()) {
  114.                 player.removePotionEffect(potionEffect.getType());
  115.             }
  116.         });
  117.         premiumAccount.remove(player.getUniqueId());
  118.         userCache.remove(player.getUniqueId());
  119.         firstPassword.remove(player.getUniqueId());
  120.         registering.remove(player.getUniqueId());
  121.         locked.remove(player.getUniqueId());
  122.     }
  123.  
  124. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement