Skip to content

Commit cbb2760

Browse files
committed
Add max accounts and secondAttempt features
1 parent 4de1158 commit cbb2760

File tree

11 files changed

+146
-38
lines changed

11 files changed

+146
-38
lines changed

src/main/java/ad1tya2/adiauth/Bungee/Config.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ public class Config {
2424
public static List<String> whitelistedCommands;
2525

2626
public static String pluginMsgConf;
27-
public static int serverCheckInterval = 30;
28-
public static String backupServer;
29-
public static boolean forceBackupServer, convertOldCrackedToPremium, backupServerEnabled;
27+
public static int serverCheckInterval = 30,
28+
maxPremiumAccounts, maxCrackedAccounts, maxTotalAccounts;
29+
public static String backupServer, secondAttempt;
30+
public static boolean forceBackupServer, convertOldCrackedToPremium, backupServerEnabled, secondAttemptEnabled;
3031
public static class Messages {
3132
public static String registerError, loginAndRegisterSuccess, alreadyLoggedIn, loginNotRegistered,
3233
loginWrongPass, loginError, alreadyRegistered, noServersAvailable, registerMessage,
33-
loginMessage, logoutMessage, changePassError, genericPremiumError, successfulChangePass;
34+
loginMessage, logoutMessage, changePassError, genericPremiumError, successfulChangePass,
35+
tooManyAccounts;
3436
public static Title loginAndRegisterSuccessTitle, registerTitle, loginTitle;
3537
}
3638
public enum MYSQL {
@@ -87,6 +89,12 @@ public static void load() {
8789
backupServer = config.getString("forceBackupServer");
8890
backupServerEnabled = !backupServer.equals("");
8991
convertOldCrackedToPremium = config.getBoolean("convertOldCrackedToPremium");
92+
maxPremiumAccounts = config.getInt("maxPremiumAccounts");
93+
maxCrackedAccounts = config.getInt("maxCrackedAccounts");
94+
maxTotalAccounts = config.getInt("maxTotalAccounts");
95+
Messages.tooManyAccounts = getMsgString("tooManyAccounts");
96+
secondAttempt = tools.getColoured(config.getString("secondAttempt"));
97+
Config.secondAttemptEnabled = !secondAttempt.equals("");
9098

9199
}
92100
catch (Exception e){

src/main/java/ad1tya2/adiauth/Bungee/UserProfile.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class UserProfile {
99
public String password;
1010
public String lastIp;
1111
public long sessionEnd = 1L;
12+
boolean loggingIn = false;
1213
public boolean isPremium(){
1314
return premiumUuid != null;
1415
}
@@ -30,6 +31,18 @@ public boolean isRegistered(){
3031
return password != null;
3132
}
3233

34+
public void startLoginProcess(){
35+
loggingIn = true;
36+
}
37+
38+
public boolean isLoginBeingProcessed(){
39+
return loggingIn;
40+
}
41+
42+
public void loginProcessCompleted(){
43+
loggingIn = false;
44+
}
45+
3346

3447

3548

src/main/java/ad1tya2/adiauth/Bungee/commands/changepass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void execute(CommandSender sender, String[] args) {
2121
return;
2222
}
2323
ProxiedPlayer p = (ProxiedPlayer) sender;
24-
UserProfile profile = storage.getPlayerDirect(p.getName());
24+
UserProfile profile = storage.getPlayerMemory(p.getName());
2525
if(profile.isPremium()){
2626
p.sendMessage(Config.Messages.genericPremiumError);
2727
}

src/main/java/ad1tya2/adiauth/Bungee/commands/forcechangepass.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import net.md_5.bungee.api.CommandSender;
77
import net.md_5.bungee.api.plugin.Command;
88

9-
import javax.swing.*;
10-
119
public class forcechangepass extends Command {
1210
public forcechangepass() {
1311
super("forcechangepass", "adiauth.admin", "adminchangepass");
@@ -21,7 +19,7 @@ public void execute(CommandSender sender, String[] args) {
2119
}
2220
String username = args[0];
2321
String newPass = args[1];
24-
UserProfile profile = storage.getPlayerDirect(username);
22+
UserProfile profile = storage.getPlayerMemory(username);
2523
if(profile == null){
2624
sender.sendMessage("&ePlayer dosent exist");
2725
} else {

src/main/java/ad1tya2/adiauth/Bungee/commands/login.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
import net.md_5.bungee.api.connection.Server;
1414
import net.md_5.bungee.api.plugin.Command;
1515

16-
17-
import java.nio.charset.StandardCharsets;
18-
1916
public class login extends Command {
2017
public login() {
2118
super("login", null, "l");
@@ -28,7 +25,7 @@ public void execute(CommandSender sender, String[] args) {
2825
return;
2926
}
3027
ProxiedPlayer p = (ProxiedPlayer) sender;
31-
UserProfile profile = storage.getPlayerDirect(p.getName());
28+
UserProfile profile = storage.getPlayerMemory(p.getName());
3229
String pass = tools.getSha256(args[0]);
3330
if(profile.isLogged()){
3431
sender.sendMessage(Config.Messages.alreadyLoggedIn);

src/main/java/ad1tya2/adiauth/Bungee/commands/logout.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void execute(CommandSender sender, String[] args) {
1919
return;
2020
}
2121
ProxiedPlayer p = (ProxiedPlayer) sender;
22-
UserProfile profile = storage.getPlayerDirect(p.getName());
22+
UserProfile profile = storage.getPlayerMemory(p.getName());
2323
profile.endSession();
2424
p.disconnect(Config.Messages.logoutMessage);
2525
}

src/main/java/ad1tya2/adiauth/Bungee/commands/register.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void execute(CommandSender sender, String[] args) {
2525
return;
2626
}
2727
ProxiedPlayer p = (ProxiedPlayer) sender;
28-
UserProfile profile = storage.getPlayerDirect(p.getName());
28+
UserProfile profile = storage.getPlayerMemory(p.getName());
2929
String pass = tools.getSha256(args[0]);
3030
if(profile.isPremium()){
3131
sender.sendMessage(Config.Messages.alreadyRegistered);

src/main/java/ad1tya2/adiauth/Bungee/commands/unregister.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void execute(CommandSender sender, String[] args) {
1818
return;
1919
}
2020
String username = args[0];
21-
UserProfile profile = storage.getPlayerDirect(username);
21+
UserProfile profile = storage.getPlayerMemory(username);
2222
if(profile == null){
2323
sender.sendMessage("&ePlayer dosent exist");
2424
} else {

src/main/java/ad1tya2/adiauth/Bungee/data/storage.java

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
1414
import org.apache.hc.client5.http.impl.classic.HttpClients;
1515

16-
import java.io.IOException;
1716
import java.sql.*;
17+
import java.util.ArrayList;
18+
import java.util.List;
1819
import java.util.Optional;
1920
import java.util.UUID;
2021
import java.util.concurrent.ConcurrentHashMap;
@@ -23,9 +24,51 @@
2324
import static ad1tya2.adiauth.Bungee.utils.Uuids.*;
2425

2526
public 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

src/main/java/ad1tya2/adiauth/Bungee/events/Handler.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import ad1tya2.adiauth.Bungee.AdiAuth;
44
import ad1tya2.adiauth.Bungee.Config;
5-
import ad1tya2.adiauth.Bungee.commands.login;
65
import ad1tya2.adiauth.Bungee.data.servers;
76
import ad1tya2.adiauth.Bungee.data.storage;
87
import ad1tya2.adiauth.Bungee.utils.pluginMessaging;
@@ -20,6 +19,7 @@
2019

2120
import java.lang.reflect.Field;
2221

22+
import java.util.Optional;
2323
import java.util.logging.Level;
2424

2525
public class Handler implements Listener {
@@ -34,14 +34,27 @@ public void onPreLogin(PreLoginEvent event){
3434
@Override
3535
public void run() {
3636
PendingConnection conn = event.getConnection();
37-
UserProfile profile = storage.getPlayerForLogin(conn.getName(), tools.getIp(conn.getSocketAddress()));
38-
if(profile == null){
37+
Optional<UserProfile> optional = storage.getPlayerForLogin(conn.getName(), tools.getIp(conn.getSocketAddress()));
38+
if(optional == null){
39+
event.setCancelReason(Config.Messages.tooManyAccounts);
40+
event.setCancelled(true);
41+
return;
42+
}
43+
if(!optional.isPresent()){
3944
event.setCancelReason(tools.getColoured(
4045
"&cConnection has been cancelled due to internal server error."
4146
));
4247
event.setCancelled(true);
4348
return;
4449
}
50+
UserProfile profile = optional.get();
51+
if(profile.isLoginBeingProcessed()){
52+
event.setCancelReason(Config.secondAttempt);
53+
event.setCancelled(true);
54+
profile.loginProcessCompleted();
55+
return;
56+
}
57+
profile.startLoginProcess();
4558
conn.setUniqueId(profile.uuid);
4659
conn.setOnlineMode(profile.isPremium());
4760
event.completeIntent(AdiAuth.instance);
@@ -54,7 +67,7 @@ public void run() {
5467
public void onLogin(LoginEvent event){
5568
try {
5669
InitialHandler handler = (InitialHandler) event.getConnection();
57-
UserProfile profile = storage.getPlayerDirect(handler.getName());
70+
UserProfile profile = storage.getPlayerMemory(handler.getName());
5871
Class handle = handler.getClass();
5972
Field uniqueId = handle.getDeclaredField("uniqueId");
6073
uniqueId.setAccessible(true);
@@ -65,12 +78,20 @@ public void onLogin(LoginEvent event){
6578
}
6679
}
6780

81+
@EventHandler(priority = 127)
82+
public void postLogin(PostLoginEvent event){
83+
UserProfile profile = storage.getPlayerMemory(event.getPlayer().getName());
84+
if(profile != null){
85+
profile.loginProcessCompleted();
86+
}
87+
}
88+
6889

6990

7091
@EventHandler
7192
public void serverConnect(ServerConnectEvent event){
7293
ProxiedPlayer p = event.getPlayer();
73-
UserProfile user = storage.getPlayerDirect(p.getName());
94+
UserProfile user = storage.getPlayerMemory(p.getName());
7495
if(user.isLogged()){
7596
event.setTarget(servers.getHubServer());
7697
}
@@ -98,7 +119,7 @@ public void chatEvent(ChatEvent event){
98119
return;
99120
}
100121
ProxiedPlayer p = (ProxiedPlayer) event.getSender();
101-
UserProfile profile = storage.getPlayerDirect(p.getName());
122+
UserProfile profile = storage.getPlayerMemory(p.getName());
102123
if(profile.isLogged()){
103124
return;
104125
}

0 commit comments

Comments
 (0)