Skip to content

Commit 56bbeca

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents f4c8901 + 90a25ff commit 56bbeca

File tree

94 files changed

+2903
-2062
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+2903
-2062
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# UserLogin
1+
# UserLogin · ![CI](https://github.com/ElCholoGamer/userlogin/actions/workflows/maven.yml/badge.svg)
22

33
This is the source code for the [UserLogin](https://www.spigotmc.org/resources/userlogin.80669/) Minecraft plugin.
44

@@ -23,8 +23,9 @@ $ mvn package
2323

2424
## Contributing
2525

26-
If you found a bug or have a suggestion, feel free
27-
to [open an issue](https://github.com/ElCholoGamer/userlogin/issues/new). If you want to contribute directly to the
28-
plugin's source code, feel free to fork the repository and open
26+
If you found the plugin useful, make sure to leave a star on the repo!
27+
If you got a bug or a suggestion, feel free to
28+
[open an issue](https://github.com/ElCholoGamer/userlogin/issues/new). If you want to
29+
contribute directly to the plugin's source code, feel free to fork the repository and open
2930
a [pull request](https://github.com/ElCholoGamer/userlogin/pulls). (Make sure to follow
3031
the [contribution guidelines](/CONTRIBUTING.md)!)

pom.xml

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,21 @@
66

77
<groupId>com.elchologamer</groupId>
88
<artifactId>UserLogin</artifactId>
9-
<version>2.11.0</version>
9+
<version>2.13.1</version>
1010
<packaging>jar</packaging>
1111

1212
<name>UserLogin</name>
1313
<description>A simple and versatile authentication system for Minecraft</description>
1414

1515
<properties>
16-
<kotlin.version>1.5.31</kotlin.version>
16+
<java.version>1.8</java.version>
1717
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1818
</properties>
1919

2020
<url>https://www.spigotmc.org/resources/userlogin.80669/</url>
2121

2222
<build>
23-
<sourceDirectory>src/main/kotlin</sourceDirectory>
2423
<plugins>
25-
<plugin>
26-
<groupId>org.jetbrains.kotlin</groupId>
27-
<artifactId>kotlin-maven-plugin</artifactId>
28-
<version>1.5.31</version>
29-
<executions>
30-
<execution>
31-
<id>compile</id>
32-
<phase>process-sources</phase>
33-
<goals>
34-
<goal>compile</goal>
35-
</goals>
36-
</execution>
37-
</executions>
38-
</plugin>
39-
4024
<plugin>
4125
<groupId>org.apache.maven.plugins</groupId>
4226
<artifactId>maven-compiler-plugin</artifactId>
@@ -111,11 +95,6 @@
11195
</repositories>
11296

11397
<dependencies>
114-
<dependency>
115-
<groupId>org.jetbrains.kotlin</groupId>
116-
<artifactId>kotlin-stdlib</artifactId>
117-
<version>1.5.31</version>
118-
</dependency>
11998
<dependency>
12099
<groupId>org.spigotmc</groupId>
121100
<artifactId>spigot-api</artifactId>
@@ -131,7 +110,7 @@
131110
<dependency>
132111
<groupId>org.apache.logging.log4j</groupId>
133112
<artifactId>log4j-core</artifactId>
134-
<version>2.13.3</version>
113+
<version>2.17.1</version>
135114
<scope>provided</scope>
136115
</dependency>
137116
<dependency>
@@ -142,7 +121,7 @@
142121
<dependency>
143122
<groupId>org.mongodb</groupId>
144123
<artifactId>mongo-java-driver</artifactId>
145-
<version>3.12.0</version>
124+
<version>3.12.10</version>
146125
<scope>provided</scope>
147126
</dependency>
148127
</dependencies>
Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
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

Comments
 (0)