Skip to content

Commit 1c7309f

Browse files
committed
rewrite to java again
1 parent eb7d714 commit 1c7309f

File tree

85 files changed

+2602
-1994
lines changed

Some content is hidden

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

85 files changed

+2602
-1994
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@
4343
</goals>
4444
</execution>
4545
</executions>
46+
<configuration>
47+
<filters>
48+
<filter>
49+
<artifact>*:*</artifact>
50+
<excludes>
51+
<exclude>META-INF/*.SF</exclude>
52+
<exclude>META-INF/*.DSA</exclude>
53+
<exclude>META-INF/*.RSA</exclude>
54+
</excludes>
55+
</filter>
56+
</filters>
57+
</configuration>
4658
</plugin>
4759

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

0 commit comments

Comments
 (0)