Skip to content

Commit 24afe97

Browse files
authored
Merge pull request #25
v2.12.0
2 parents 6ac6f57 + 8a660a6 commit 24afe97

File tree

87 files changed

+2612
-2022
lines changed

Some content is hidden

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

87 files changed

+2612
-2022
lines changed

README.md

Lines changed: 1 addition & 1 deletion
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

pom.xml

Lines changed: 2 additions & 23 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.12.0</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>
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
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

Comments
 (0)