Fix some suboptimal behavior in invoking KickedFromServerEvent.
Previously, the event would only fire when a player was kicked from the current server they were on. Now, under certain cases, it can be fired even if the player was already connected to a server. To faciliate this, a new result (Notify) was introduced. This result will "do the right thing" if the player is kicked from the current server or is trying to connect to a different server than the one they were on.
This commit is contained in:
@@ -17,16 +17,16 @@ public final class KickedFromServerEvent implements
|
||||
private final Player player;
|
||||
private final RegisteredServer server;
|
||||
private final Component originalReason;
|
||||
private final boolean duringLogin;
|
||||
private final boolean duringServerConnect;
|
||||
private ServerKickResult result;
|
||||
|
||||
public KickedFromServerEvent(Player player, RegisteredServer server, Component originalReason,
|
||||
boolean duringLogin, Component fancyReason) {
|
||||
boolean duringServerConnect, Component fancyReason) {
|
||||
this.player = Preconditions.checkNotNull(player, "player");
|
||||
this.server = Preconditions.checkNotNull(server, "server");
|
||||
this.originalReason = Preconditions.checkNotNull(originalReason, "originalReason");
|
||||
this.duringLogin = duringLogin;
|
||||
this.result = new DisconnectPlayer(fancyReason);
|
||||
this.duringServerConnect = duringServerConnect;
|
||||
this.result = new Notify(fancyReason);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -51,8 +51,25 @@ public final class KickedFromServerEvent implements
|
||||
return originalReason;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not the player got kicked while connecting to another server.
|
||||
*
|
||||
* @return whether or not the player got kicked
|
||||
*/
|
||||
public boolean kickedDuringServerConnect() {
|
||||
return duringServerConnect;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not the player got kicked while logging in.
|
||||
*
|
||||
* @return whether or not the player got kicked
|
||||
* @deprecated {@link #kickedDuringServerConnect()} has a better name and reflects the actual
|
||||
* result
|
||||
*/
|
||||
@Deprecated
|
||||
public boolean kickedDuringLogin() {
|
||||
return duringLogin;
|
||||
return duringServerConnect;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,4 +141,37 @@ public final class KickedFromServerEvent implements
|
||||
return new RedirectPlayer(server);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifies the player with the specified message but does nothing else. This is only a valid
|
||||
* result to use if the player was trying to connect to a different server, otherwise it is
|
||||
* treated like a {@link DisconnectPlayer} result.
|
||||
*/
|
||||
public static final class Notify implements ServerKickResult {
|
||||
|
||||
private final Component message;
|
||||
|
||||
private Notify(Component message) {
|
||||
this.message = Preconditions.checkNotNull(message, "message");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAllowed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Component getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifies the player with the specified message but does nothing else.
|
||||
*
|
||||
* @param message the server to send the player to
|
||||
* @return the redirect result
|
||||
*/
|
||||
public static Notify create(Component message) {
|
||||
return new Notify(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user