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 () {
50+ loggedIn = false ;
51+ loginAttempts = 0 ;
52+
53+ Player player = getPlayer ();
54+
55+ // Teleport to login position
56+ if (plugin .getConfig ().getBoolean ("teleports.toLogin" )) {
57+ player .teleport (plugin .getLocations ().getLocation ("login" , player .getWorld ().getSpawnLocation ()));
58+ }
59+
60+ // Bypass if IP is registered
61+ if (plugin .getConfig ().getBoolean ("ipRecords.enabled" )) {
62+ if (ip != null ) {
63+ InetSocketAddress addr = player .getAddress ();
64+ if (addr != null && addr .getHostString ().equals (ip )) {
65+ onAuthenticate (AuthType .LOGIN );
66+ return ;
67+ }
68+ }
69+
70+ if (ipForgor != -1 ) {
71+ plugin .getServer ().getScheduler ().cancelTask (ipForgor );
72+ ipForgor = -1 ;
73+ }
74+ }
75+
76+ schedulePreLoginTasks ();
77+ sendWelcomeMessage ();
78+ }
79+
80+ public void onQuit () {
81+ if (!loggedIn ) {
82+ cancelPreLoginTasks ();
83+ } else {
84+ loggedIn = false ;
85+
86+ if (plugin .getConfig ().getBoolean ("teleports.savePosition" )) {
87+ plugin .getLocations ().savePlayerLocation (getPlayer ());
88+ }
89+
90+ // Store IP address if enabled
91+ if (plugin .getConfig ().getBoolean ("ipRecords.enabled" )) {
92+ // Schedule IP deletion
93+ ipForgor = plugin .getServer ().getScheduler ().scheduleSyncDelayedTask (
94+ plugin ,
95+ () -> ip = null ,
96+ plugin .getConfig ().getLong ("ipRecords.delay" , 10 ) * 20
97+ );
98+ }
99+ }
100+ }
101+
102+ public void onAuthenticate (AuthType type ) {
103+ Player player = getPlayer ();
104+ FileConfiguration config = plugin .getConfig ();
105+
106+ ConfigurationSection teleports = config .getConfigurationSection ("teleports" );
107+ assert teleports != null ;
108+
109+ // Call event
110+ AuthenticationEvent event ;
111+
112+ boolean bungeeEnabled = config .getBoolean ("bungeeCord.enabled" );
113+ if (bungeeEnabled ) {
114+ String targetServer = config .getString ("bungeeCord.spawnServer" );
115+ event = new AuthenticationEvent (player , type , targetServer );
116+ } else {
117+ Location target = null ;
118+ Location spawn = player .getWorld ().getSpawnLocation ();
119+
120+ if (teleports .getBoolean ("savePosition" )) {
121+ target = plugin .getLocations ().getPlayerLocation (player , spawn );
122+ } else if (teleports .getBoolean ("toSpawn" , true )) {
123+ target = plugin .getLocations ().getLocation ("spawn" , spawn );
124+ }
125+
126+ event = new AuthenticationEvent (player , type , target );
127+ }
128+
129+ plugin .getServer ().getPluginManager ().callEvent (event );
130+
131+ if (event .isCancelled ()) return ;
132+
133+ cancelPreLoginTasks ();
134+
135+ // Save IP address
136+ if (config .getBoolean ("ipRecords.enabled" )) {
137+ InetSocketAddress addr = player .getAddress ();
138+ if (addr != null ) ip = addr .getHostString ();
139+ }
140+
141+ // Send login message
142+ if (event .getMessage () != null ) player .sendMessage (event .getMessage ());
143+
144+ // Join announcement
145+ if (event .getAnnouncement () != null ) {
146+ for (Player onlinePlayer : player .getServer ().getOnlinePlayers ()) {
147+ if (UserLoginAPI .isLoggedIn (player )) {
148+ onlinePlayer .sendMessage (event .getAnnouncement ());
149+ }
150+ }
151+ }
152+
153+ loggedIn = true ;
154+
155+ // Run login commands
156+ List <String > loginCommands = config .getStringList ("loginCommands" );
157+ for (String command : loginCommands ) {
158+ plugin .getServer ().dispatchCommand (
159+ plugin .getServer ().getConsoleSender (),
160+ command .replace ("{player}" , player .getName ()).replaceFirst ("^/" , "" )
161+ );
162+ }
163+
164+ // Teleport to destination
165+ if (bungeeEnabled && event .getTargetServer () != null ) {
166+ Utils .sendPluginMessage (player , "BungeeCord" , "Connect" , event .getTargetServer ());
167+ } else if (event .getDestination () != null ) {
168+ player .teleport (event .getDestination ());
169+ }
170+ }
171+
172+ public boolean onLoginAttempt () {
173+ int maxAttempts = plugin .getConfig ().getInt ("password.maxLoginAttempts" );
174+
175+ if (++loginAttempts >= maxAttempts ) {
176+ getPlayer ().kickPlayer (
177+ plugin .getLang ().getMessage ("messages.max_attempts_exceeded" ).replace ("{count}" , Integer .toString (maxAttempts ))
178+ );
179+ return false ;
180+ } else {
181+ return true ;
182+ }
183+ }
184+
185+ private void sendWelcomeMessage () {
186+ String path = "messages.welcome." + ((UserLoginAPI .isRegistered (getPlayer ())) ? "registered" : "unregistered" );
187+ sendMessage (path , new QuickMap <>("player" , getPlayer ().getName ()));
188+ }
189+
190+ public void schedulePreLoginTasks () {
191+ Player player = getPlayer ();
192+
193+ // Timeout
194+ if (plugin .getConfig ().getBoolean ("timeout.enabled" , true )) {
195+ long timeoutDelay = plugin .getConfig ().getLong ("timeout.time" );
196+ timeout = player .getServer ().getScheduler ().scheduleSyncDelayedTask (
197+ plugin ,
198+ () -> player .kickPlayer (plugin .getLang ().getMessage ("messages.timeout" )),
199+ timeoutDelay * 20
200+ );
201+ }
202+
203+
204+ // Repeating welcome message
205+ long interval = plugin .getConfig ().getLong ("repeatingWelcomeMsg" , -1 ) * 20 ;
206+ if (interval > 0 ) {
207+ welcomeMessage = player .getServer ().getScheduler ().scheduleSyncRepeatingTask (
208+ plugin ,
209+ this ::sendWelcomeMessage ,
210+ interval , interval
211+ );
212+ }
213+ }
214+
215+ public void cancelPreLoginTasks () {
216+ if (timeout != -1 ) {
217+ getPlayer ().getServer ().getScheduler ().cancelTask (timeout );
218+ timeout = -1 ;
219+ }
220+
221+ if (welcomeMessage != -1 ) {
222+ getPlayer ().getServer ().getScheduler ().cancelTask (welcomeMessage );
223+ welcomeMessage = -1 ;
224+ }
225+ }
226+
227+ public void sendMessage (String path ) {
228+ sendMessage (path , null );
229+ }
230+
231+ public void sendMessage (String path , Map <String , Object > replace ) {
232+ String message = plugin .getLang ().getMessage (path );
233+ if (message == null || message .isEmpty ()) return ;
234+
235+ if (replace != null ) {
236+ for (String k : replace .keySet ()) {
237+ message = message .replace ("{" + k + "}" , replace .get (k ).toString ());
238+ }
239+ }
240+
241+ getPlayer ().sendMessage (Utils .color (message ));
242+ }
243+
244+ public Player getPlayer () {
245+ Player player = plugin .getServer ().getPlayer (uuid );
246+ if (player == null )
247+ throw new IllegalArgumentException ("Player with UUID " + uuid + " not found" );
248+
249+ return player ;
250+ }
251+
252+ public boolean isLoggedIn () {
253+ return loggedIn ;
254+ }
255+ }
0 commit comments