-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathPlayerLoginEvent.java
More file actions
254 lines (227 loc) · 8.24 KB
/
PlayerLoginEvent.java
File metadata and controls
254 lines (227 loc) · 8.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package org.bukkit.event.player;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import io.papermc.paper.event.connection.PlayerConnectionValidateLoginEvent;
import org.bukkit.Warning;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
/**
* Stores details for players attempting to log in.
* <br>
* Note that this event is called <i>early</i> in the player initialization
* process. It is recommended that most options involving the Player
* <i>entity</i> be postponed to the {@link PlayerJoinEvent} instead.
*
* @deprecated Use {@link PlayerConnectionValidateLoginEvent} to handle pre-login logic
* (e.g. authentication or ban checks), or {@link io.papermc.paper.event.player.PlayerServerFullCheckEvent} to allow
* players to bypass the server's maximum player limit.
* Minecraft triggers this twice internally, using this event skips one of the validation checks done by the server.
* Additionally, this event causes the full player entity to be created much earlier than it would be in Vanilla,
* leaving it with mostly dysfunctional methods and state.
*/
@Warning(reason = "Listening to this event causes the player to be created early.")
@Deprecated(since = "1.21.6")
public class PlayerLoginEvent extends PlayerEvent {
private static final HandlerList HANDLER_LIST = new HandlerList();
private final String hostname;
private final InetAddress address;
private final SocketAddress realAddress;
private Result result = Result.ALLOWED;
private Component message = Component.empty();
@ApiStatus.Internal
public PlayerLoginEvent(@NotNull final Player player, @NotNull final String hostname, @NotNull final InetAddress address, final @NotNull SocketAddress realAddress) {
super(player);
this.hostname = hostname;
this.address = address;
this.realAddress = realAddress;
}
@ApiStatus.Internal
public PlayerLoginEvent(@NotNull final Player player, @NotNull final String hostname, @NotNull final InetAddress address, final @NotNull InetAddress realAddress) {
this(player, hostname, address, new InetSocketAddress(address, 0));
}
@ApiStatus.Internal
public PlayerLoginEvent(@NotNull final Player player, @NotNull final String hostname, @NotNull final InetAddress address) {
this(player, hostname, address, address);
}
@ApiStatus.Internal
@Deprecated(forRemoval = true)
public PlayerLoginEvent(@NotNull final Player player, @NotNull String hostname, @NotNull final InetAddress address, @NotNull final Result result, @NotNull final String message, @NotNull final InetAddress realAddress) {
this(player, hostname, address, realAddress);
this.result = result;
this.message = LegacyComponentSerializer.legacySection().deserialize(message);
}
@ApiStatus.Internal
public PlayerLoginEvent(@NotNull final Player player, @NotNull String hostname, @NotNull final InetAddress address, @NotNull final Result result, @NotNull final net.kyori.adventure.text.Component message, @NotNull final InetAddress realAddress) {
this(player, hostname, address, realAddress);
this.result = result;
this.message = message;
}
/**
* Gets the hostname that the player used to connect to the server, or
* blank if unknown
*
* @return The hostname
*/
@NotNull
public String getHostname() {
return this.hostname;
}
/**
* Gets the {@link InetAddress} for the Player associated with this event.
* This method is provided as a workaround for player.getAddress()
* returning {@code null} during PlayerLoginEvent.
*
* @return The address for this player. For legacy compatibility, this may
* be {@code null}.
*/
@NotNull
public InetAddress getAddress() {
return this.address;
}
/**
* Gets the connection socket address of this player, regardless of whether
* it has been spoofed or not.
*
* @return the player's connection socket address
*/
@NotNull
public SocketAddress getRealSocketAddress() {
return this.realAddress;
}
/**
* Gets the connection address of this player, regardless of whether it has
* been spoofed or not.
*
* @return the player's connection address, or the loopback address if the
* player is connecting through a Unix socket
* @see #getAddress()
*/
@NotNull
public InetAddress getRealAddress() {
if (this.realAddress instanceof InetSocketAddress inet) return inet.getAddress();
return InetAddress.getLoopbackAddress();
}
/**
* Gets the current result of the login, as an enum
*
* @return Current Result of the login
*/
@NotNull
public Result getResult() {
return this.result;
}
/**
* Sets the new result of the login, as an enum
*
* @param result New result to set
*/
public void setResult(@NotNull final Result result) {
this.result = result;
}
/**
* Gets the current kick message that will be used when the outcome is not allowed
*
* @return Current kick message
*/
public @NotNull Component kickMessage() {
return this.message;
}
/**
* Sets the kick message to display when the outcome is not allowed
*
* @param message New kick message
*/
public void kickMessage(@NotNull Component message) {
this.message = message;
}
/**
* Gets the current kick message that will be used when the outcome is not allowed
*
* @return Current kick message
* @deprecated in favour of {@link #kickMessage()}
*/
@NotNull
@Deprecated // Paper
public String getKickMessage() {
return LegacyComponentSerializer.legacySection().serialize(this.message);
}
/**
* Sets the kick message to display when the outcome is not allowed
*
* @param message New kick message
* @deprecated in favour of {@link #kickMessage(Component)}
*/
@Deprecated
public void setKickMessage(@NotNull final String message) {
this.message = LegacyComponentSerializer.legacySection().deserialize(message);
}
/**
* Allows the player to log in
*/
public void allow() {
this.result = Result.ALLOWED;
this.message = Component.empty();
}
/**
* Disallows the player from logging in, with the given reason
*
* @param result New result for disallowing the player
* @param message Kick message to display to the user
* @deprecated in favour of {@link #disallow(Result, Component)}
*/
@Deprecated
public void disallow(@NotNull final Result result, @NotNull final String message) {
this.result = result;
this.message = LegacyComponentSerializer.legacySection().deserialize(message);
}
/**
* Disallows the player from logging in, with the given reason
*
* @param result New result for disallowing the player
* @param message Kick message to display to the user
*/
public void disallow(@NotNull final Result result, @NotNull final Component message) {
this.result = result;
this.message = message;
}
@NotNull
@Override
public HandlerList getHandlers() {
return HANDLER_LIST;
}
@NotNull
public static HandlerList getHandlerList() {
return HANDLER_LIST;
}
/**
* Basic kick reasons for communicating to plugins
*/
public enum Result {
/**
* The player is allowed to log in
*/
ALLOWED,
/**
* The player is not allowed to log in, due to the server being full
*/
KICK_FULL,
/**
* The player is not allowed to log in, due to them being banned
*/
KICK_BANNED,
/**
* The player is not allowed to log in, due to them not being on the
* white list
*/
KICK_WHITELIST,
/**
* The player is not allowed to log in, for reasons undefined
*/
KICK_OTHER
}
}