Advertisement
gt22

Untitled

Sep 29th, 2016
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. public static UserWrapper tryGetUser(String from)
  2.     {
  3.         return tryGetUser(from, null);
  4.     }
  5.    
  6.     public static UserWrapper tryGetUser(String from, JDA jda)
  7.     {
  8.         if(jda == null)
  9.         {
  10.             jda = Core.bot;
  11.         }
  12.         User usr = jda.getUserById(from);
  13.         if (usr == null)
  14.         {
  15.             if (from.contains("#") && !from.startsWith("#"))
  16.             {
  17.                 String[] str = from.split("#");
  18.                 usr = getUser(str[0], str[1]);
  19.                 if (usr != null)
  20.                 {
  21.                     return new UserWrapper(usr);
  22.                 }
  23.             }
  24.             else
  25.             {
  26.                 if (from.startsWith("@"))
  27.                 {
  28.                     from = from.substring(1);
  29.                 }
  30.                 String f = from;
  31.                 if(f.startsWith("#"))
  32.                 {
  33.                     return new UserWrapper(jda.getUsers().parallelStream().filter(user -> user.getDiscriminator().equals(f.substring(1))).toArray(User[]::new));
  34.                 }
  35.                 else
  36.                 {
  37.                     return new UserWrapper(jda.getUsers().parallelStream().filter(user -> user.getUsername().equals(f)).toArray(User[]::new));
  38.                 }
  39.             }
  40.         }
  41.         else
  42.         {
  43.             return new UserWrapper(usr);
  44.         }
  45.         return new UserWrapper();
  46.     }
  47.  
  48.     public static class UserWrapper
  49.     {
  50.  
  51.         public static enum WrapperState
  52.         {
  53.             SINGLE, MULTI, EMPTY;
  54.         }
  55.  
  56.         public Optional<User> single;
  57.         public Optional<User[]> multipile;
  58.         public WrapperState state;
  59.  
  60.         public UserWrapper(User single)
  61.         {
  62.             single(single);
  63.         }
  64.  
  65.         public UserWrapper(User[] multipile)
  66.         {
  67.             if (multipile.length == 0)
  68.             {
  69.                 empty();
  70.             }
  71.             else if (multipile.length == 1)
  72.             {
  73.                 single(multipile[0]);
  74.             }
  75.             else
  76.             {
  77.                 multi(multipile);
  78.             }
  79.         }
  80.  
  81.         public UserWrapper()
  82.         {
  83.             empty();
  84.         }
  85.        
  86.         private void single(User usr)
  87.         {
  88.             this.single = Optional.of(usr);
  89.             this.multipile = Optional.empty();
  90.             this.state = WrapperState.SINGLE;
  91.         }
  92.        
  93.         private void multi(User[] usr)
  94.         {
  95.             this.single = Optional.empty();
  96.             this.multipile = Optional.of(usr);
  97.             this.state = WrapperState.MULTI;
  98.         }
  99.        
  100.         private void empty()
  101.         {
  102.             this.single = Optional.empty();
  103.             this.multipile = Optional.empty();
  104.             state = WrapperState.EMPTY;
  105.         }
  106.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement