1313import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
1414import org .apache .hc .client5 .http .impl .classic .HttpClients ;
1515
16- import java .io .IOException ;
1716import java .sql .*;
17+ import java .util .ArrayList ;
18+ import java .util .List ;
1819import java .util .Optional ;
1920import java .util .UUID ;
2021import java .util .concurrent .ConcurrentHashMap ;
2324import static ad1tya2 .adiauth .Bungee .utils .Uuids .*;
2425
2526public class storage {
26- private static ConcurrentHashMap <String , UserProfile > pMap = new ConcurrentHashMap <String , UserProfile >();
27- private static ConcurrentHashMap <UUID , UserProfile > pMapByPremiumUuid = new ConcurrentHashMap <UUID , UserProfile >();
28-
27+ public enum AccountType {
28+ TOTAL , CRACKED , PREMIUM
29+ }
30+ private static final ConcurrentHashMap <String , UserProfile > pMap = new ConcurrentHashMap <String , UserProfile >();
31+ private static final ConcurrentHashMap <UUID , UserProfile > pMapByPremiumUuid = new ConcurrentHashMap <UUID , UserProfile >();
32+ private static final ConcurrentHashMap <String , List <UserProfile >> profilesByIp = new ConcurrentHashMap <String , List <UserProfile >>();
33+ public static Integer getAccounts (String ip , AccountType type ){
34+ List <UserProfile > profiles = profilesByIp .get (ip );
35+ if (profiles == null ){
36+ return 0 ;
37+ }
38+ switch (type ){
39+ case TOTAL :
40+ return profiles .size ();
41+ case CRACKED :
42+ int accounts = 0 ;
43+ for (UserProfile profile : profiles ){
44+ if (!profile .isPremium ()){
45+ accounts ++;
46+ }
47+ }
48+ return accounts ;
49+ case PREMIUM :
50+ int premiums = 0 ;
51+ for (UserProfile profile : profiles ){
52+ if (profile .isPremium ()){
53+ premiums ++;
54+ }
55+ }
56+ return premiums ;
57+ default :
58+ return 0 ;
59+ }
60+ }
61+ public static void addAccountToIpList (UserProfile profile ){
62+ if (profile .lastIp == null ){
63+ return ;
64+ }
65+ List <UserProfile > profiles = profilesByIp .get (profile .lastIp );
66+ if (profiles == null ){
67+ profiles = new ArrayList <UserProfile >();
68+ }
69+ profiles .add (profile );
70+ profilesByIp .put (profile .lastIp , profiles );
71+ }
2972 public static void load (){
3073 try {
3174 mysql .load ();
@@ -48,7 +91,7 @@ public static void load(){
4891 } catch (Exception e ){
4992 user .premiumUuid = null ;
5093 }
51- addPlayerDirect (user );
94+ updatePlayerMemory (user );
5295 }
5396 records .close ();
5497 stmt .close ();
@@ -62,7 +105,10 @@ private static void logApiError(){
62105 tools .log (Level .SEVERE , "&cMojang api and/or Backup server is not responding, Please check, &eWill login the player using the last updated data" );
63106 }
64107
65- public static UserProfile getPlayerForLogin (String name , String ip ){
108+ public static Optional <UserProfile > getPlayerForLogin (String name , String ip ){
109+ if (getAccounts (ip , AccountType .TOTAL )>Config .maxTotalAccounts ){
110+ return null ;
111+ }
66112 UserProfile user = new UserProfile ();
67113 user .username = name ;
68114 user .lastIp = ip ;
@@ -82,11 +128,14 @@ public static UserProfile getPlayerForLogin(String name, String ip){
82128 }
83129 if (uuid == null ){
84130 logApiError ();
85- return oldUserByName ;
131+ return Optional . of ( oldUserByName ) ;
86132 }
87133
88134 //Premium
89135 if (uuid .isPresent ()) {
136+ if (getAccounts (ip , AccountType .PREMIUM )>Config .maxPremiumAccounts ){
137+ return null ;
138+ }
90139 user .premiumUuid = uuid .get ();
91140 user .uuid = user .premiumUuid ;
92141 UserProfile oldPremiumUser = pMapByPremiumUuid .get (user .premiumUuid );
@@ -97,34 +146,37 @@ public static UserProfile getPlayerForLogin(String name, String ip){
97146 oldUserByName .premiumUuid = user .premiumUuid ;
98147 }
99148 updatePlayer (oldUserByName );
100- return oldUserByName ;
149+ return Optional . of ( oldUserByName ) ;
101150 }
102151 updatePlayer (user );
103- return user ;
152+ return Optional . of ( user ) ;
104153 } else if (oldPremiumUser .username != user .username ) {
105154 //Username change event
106155 oldPremiumUser .username = user .username ;
107156 oldPremiumUser .lastIp = ip ;
108157 updatePlayer (oldPremiumUser );
109- return oldPremiumUser ;
158+ return Optional . of ( oldPremiumUser ) ;
110159 } else {
111160 if (oldPremiumUser .lastIp != ip ) {
112161 oldPremiumUser .lastIp = ip ;
113162 updatePlayer (oldPremiumUser );
114163 }
115- return oldPremiumUser ;
164+ return Optional . of ( oldPremiumUser ) ;
116165 }
117166 }
118167
119168 //Cracked
120169 else {
170+ if (getAccounts (ip , AccountType .CRACKED )>Config .maxCrackedAccounts ){
171+ return null ;
172+ }
121173 user .uuid = UUID .nameUUIDFromBytes (("OfflinePlayer:" + name ).getBytes (Charsets .UTF_8 ));
122174 user .premiumUuid = null ;
123175
124176 if (oldUserByName == null ) {
125177 updatePlayer (user );
126178
127- return user ;
179+ return Optional . of ( user ) ;
128180 }
129181
130182 //If the username of this player has been converted to cracked from premium
@@ -148,11 +200,11 @@ public static UserProfile getPlayerForLogin(String name, String ip){
148200 if (!oldUserByName .lastIp .equals (user .lastIp )) {
149201 user .endSession ();
150202 }
151- return user ;
203+ return Optional . of ( user ) ;
152204 }
153205 }
154206
155- public static UserProfile getPlayerDirect (String name ){
207+ public static UserProfile getPlayerMemory (String name ){
156208 return pMap .get (name );
157209 }
158210
@@ -181,15 +233,16 @@ public void run() {
181233 }
182234
183235 public static void updatePlayer (UserProfile player ){
184- addPlayerDirect (player );
236+ updatePlayerMemory (player );
185237 asyncUserProfileUpdate (player );
186238 }
187239
188- private static void addPlayerDirect (UserProfile profile ){
240+ private static void updatePlayerMemory (UserProfile profile ){
189241 pMap .put (profile .username , profile );
190242 if (profile .premiumUuid != null ) {
191243 pMapByPremiumUuid .put (profile .premiumUuid , profile );
192244 }
245+ addAccountToIpList (profile );
193246 }
194247
195248
0 commit comments