1+ package com .elchologamer .userlogin ;
2+
3+ import com .elchologamer .userlogin .api .UserLoginAPI ;
4+ import com .elchologamer .userlogin .api .event .AuthenticationEvent ;
5+ import com .elchologamer .userlogin .api .types .AuthType ;
6+ import com .elchologamer .userlogin .util .QuickMap ;
7+ import com .elchologamer .userlogin .util .Utils ;
8+ import org .bukkit .Location ;
9+ import org .bukkit .configuration .ConfigurationSection ;
10+ import org .bukkit .configuration .file .FileConfiguration ;
11+ import org .bukkit .entity .Player ;
12+
13+ import java .net .InetSocketAddress ;
14+ import java .util .HashMap ;
15+ import java .util .List ;
16+ import java .util .Map ;
17+ import java .util .UUID ;
18+
19+ public class ULPlayer {
20+ public static Map <UUID , ULPlayer > players = new HashMap <>();
21+
22+ private final UserLogin plugin = UserLogin .getPlugin ();
23+ private final UUID uuid ;
24+ private boolean loggedIn = false ;
25+ private String ip = null ;
26+ private int loginAttempts = 0 ;
27+
28+ // Scheduler task IDs
29+ private int timeout = -1 ;
30+ private int welcomeMessage = -1 ;
31+ private int ipForgor = -1 ;
32+
33+ private ULPlayer (UUID uuid ) {
34+ this .uuid = uuid ;
35+ players .put (uuid , this );
36+ }
37+
38+ public static ULPlayer get (Player player ) {
39+ return get (player .getUniqueId ());
40+ }
41+
42+ public static ULPlayer get (UUID uuid ) {
43+ ULPlayer player = players .get (uuid );
44+ if (player == null ) player = new ULPlayer (uuid );
45+
46+ return player ;
47+ }
48+
49+ public void onJoin (boolean fromOtherServer ) {
50+ loggedIn = false ;
51+ loginAttempts = 0 ;
52+
53+ Player player = getPlayer ();
54+
55+ // Teleport to login position
56+ if (plugin .getConfig ().getBoolean ("teleports.toLogin" , plugin .getConfig ().getBoolean ("teleports.preLogin" , true ))) {
57+ player .teleport (plugin .getLocations ().getLocation ("login" , player .getWorld ().getSpawnLocation ()));
58+ }
59+
60+ if (fromOtherServer ) {
61+ onAuthenticate (AuthType .LOGIN );
62+ return ;
63+ }
64+
65+ if (ipForgor != -1 ) {
66+ plugin .getServer ().getScheduler ().cancelTask (ipForgor );
67+ ipForgor = -1 ;
68+ }
69+
70+ // Bypass if IP is registered
71+ if (ip != null ) {
72+ boolean ret = false ;
73+ InetSocketAddress addr = player .getAddress ();
74+
75+ if (addr != null && addr .getHostString ().equals (ip )) {
76+ onAuthenticate (AuthType .LOGIN );
77+ ret = true ;
78+ }
79+
80+ ip = null ;
81+ if (ret ) return ;
82+ }
83+
84+ schedulePreLoginTasks ();
85+ sendWelcomeMessage ();
86+ }
87+
88+ public void onQuit () {
89+ if (!loggedIn ) {
90+ cancelPreLoginTasks ();
91+ } else {
92+ loggedIn = false ;
93+
94+ if (plugin .getConfig ().getBoolean ("teleports.savePosition" )) {
95+ plugin .getLocations ().savePlayerLocation (getPlayer ());
96+ }
97+
98+ long rememberIp = -1 ;
99+
100+ if (plugin .getConfig ().isConfigurationSection ("ipRecords" )) {
101+ if (plugin .getConfig ().getBoolean ("ipRecords.enabled" )) {
102+ rememberIp = plugin .getConfig ().getLong ("ipRecords.delay" );
103+ }
104+ } else {
105+ rememberIp = plugin .getConfig ().getLong ("ipCache" );
106+ }
107+
108+ // Store IP address if enabled
109+ if (rememberIp >= 0 ) {
110+ // Schedule IP deletion
111+ InetSocketAddress addr = getPlayer ().getAddress ();
112+ if (addr != null ) {
113+ ip = addr .getHostString ();
114+ ipForgor = plugin .getServer ().getScheduler ().scheduleSyncDelayedTask (
115+ plugin ,
116+ () -> ip = null ,
117+ rememberIp * 20
118+ );
119+ }
120+ }
121+ }
122+ }
123+
124+ public void onAuthenticate (AuthType type ) {
125+ Player player = getPlayer ();
126+ FileConfiguration config = plugin .getConfig ();
127+
128+ ConfigurationSection teleports = config .getConfigurationSection ("teleports" );
129+ assert teleports != null ;
130+
131+ // Call event
132+ AuthenticationEvent event ;
133+
134+ boolean bungeeEnabled = config .getBoolean ("bungeeCord.enabled" );
135+ if (bungeeEnabled ) {
136+ String targetServer = config .getString ("bungeeCord.targetServer" , config .getString ("bungeeCord.spawnServer" ));
137+ event = new AuthenticationEvent (player , type , targetServer );
138+ } else {
139+ Location target = null ;
140+ Location spawn = player .getWorld ().getSpawnLocation ();
141+
142+ if (teleports .getBoolean ("savePosition" )) {
143+ target = plugin .getLocations ().getPlayerLocation (player , spawn );
144+ } else if (teleports .getBoolean ("postLogin" , teleports .getBoolean ("toSpawn" , true ))) {
145+ target = plugin .getLocations ().getLocation ("spawn" , spawn );
146+ }
147+
148+ event = new AuthenticationEvent (player , type , target );
149+ }
150+
151+ plugin .getServer ().getPluginManager ().callEvent (event );
152+
153+ if (event .isCancelled ()) return ;
154+
155+ cancelPreLoginTasks ();
156+
157+ // Save IP address
158+ if (config .getBoolean ("ipRecords.enabled" )) {
159+ InetSocketAddress addr = player .getAddress ();
160+ if (addr != null ) ip = addr .getHostString ();
161+ }
162+
163+ // Send login message
164+ if (event .getMessage () != null ) player .sendMessage (event .getMessage ());
165+
166+ // Join announcement
167+ if (event .getAnnouncement () != null ) {
168+ for (Player onlinePlayer : player .getServer ().getOnlinePlayers ()) {
169+ if (UserLoginAPI .isLoggedIn (player )) {
170+ onlinePlayer .sendMessage (event .getAnnouncement ());
171+ }
172+ }
173+ }
174+
175+ loggedIn = true ;
176+
177+ // Run login commands
178+ List <String > loginCommands = config .getStringList ("loginCommands" );
179+ for (String command : loginCommands ) {
180+ plugin .getServer ().dispatchCommand (
181+ plugin .getServer ().getConsoleSender (),
182+ command .replace ("{player}" , player .getName ()).replaceFirst ("^/" , "" )
183+ );
184+ }
185+
186+ // Teleport to destination
187+ if (bungeeEnabled && event .getTargetServer () != null ) {
188+ Utils .sendPluginMessage (player , "BungeeCord" , "Connect" , event .getTargetServer ());
189+ } else if (event .getDestination () != null ) {
190+ player .teleport (event .getDestination ());
191+ }
192+ }
193+
194+ public boolean onLoginAttempt () {
195+ int maxAttempts = plugin .getConfig ().getInt ("password.maxLoginAttempts" );
196+
197+ if (++loginAttempts >= maxAttempts ) {
198+ getPlayer ().kickPlayer (
199+ plugin .getLang ().getMessage ("messages.max_attempts_exceeded" ).replace ("{count}" , Integer .toString (maxAttempts ))
200+ );
201+ return false ;
202+ } else {
203+ return true ;
204+ }
205+ }
206+
207+ private void sendWelcomeMessage () {
208+ String path = "messages.welcome." + ((UserLoginAPI .isRegistered (getPlayer ())) ? "registered" : "unregistered" );
209+ sendMessage (path , new QuickMap <>("player" , getPlayer ().getName ()));
210+ }
211+
212+ public void schedulePreLoginTasks () {
213+ Player player = getPlayer ();
214+
215+ // Timeout
216+ long timeoutDelay = -1 ;
217+ if (plugin .getConfig ().isConfigurationSection ("timeout" )) {
218+ if (plugin .getConfig ().getBoolean ("timeout.enabled" )) {
219+ timeoutDelay = plugin .getConfig ().getLong ("timeout.time" );
220+ }
221+ } else {
222+ timeoutDelay = plugin .getConfig ().getLong ("timeout" );
223+ }
224+
225+ if (timeoutDelay >= 0 ) {
226+ timeout = player .getServer ().getScheduler ().scheduleSyncDelayedTask (
227+ plugin ,
228+ () -> player .kickPlayer (plugin .getLang ().getMessage ("messages.timeout" )),
229+ timeoutDelay * 20
230+ );
231+ }
232+
233+
234+ // Repeating welcome message
235+ long interval = plugin .getConfig ().getLong ("repeatWelcomeMessage" , plugin .getConfig ().getLong ("repeatingWelcomeMsg" , -1 ));
236+ if (interval > 0 ) {
237+ welcomeMessage = player .getServer ().getScheduler ().scheduleSyncRepeatingTask (
238+ plugin ,
239+ this ::sendWelcomeMessage ,
240+ interval * 20 , interval * 20
241+ );
242+ }
243+ }
244+
245+ public void cancelPreLoginTasks () {
246+ if (timeout != -1 ) {
247+ getPlayer ().getServer ().getScheduler ().cancelTask (timeout );
248+ timeout = -1 ;
249+ }
250+
251+ if (welcomeMessage != -1 ) {
252+ getPlayer ().getServer ().getScheduler ().cancelTask (welcomeMessage );
253+ welcomeMessage = -1 ;
254+ }
255+ }
256+
257+ public void sendMessage (String path ) {
258+ sendMessage (path , null );
259+ }
260+
261+ public void sendMessage (String path , Map <String , Object > replace ) {
262+ String message = plugin .getLang ().getMessage (path );
263+ if (message == null || message .isEmpty ()) return ;
264+
265+ if (replace != null ) {
266+ for (String k : replace .keySet ()) {
267+ message = message .replace ("{" + k + "}" , replace .get (k ).toString ());
268+ }
269+ }
270+
271+ getPlayer ().sendMessage (Utils .color (message ));
272+ }
273+
274+ public Player getPlayer () {
275+ Player player = plugin .getServer ().getPlayer (uuid );
276+ if (player == null )
277+ throw new IllegalArgumentException ("Player with UUID " + uuid + " not found" );
278+
279+ return player ;
280+ }
281+
282+ public boolean isLoggedIn () {
283+ return loggedIn ;
284+ }
285+ }
0 commit comments