Refactor PlayerLoginProcessEvent

This is technically a breaking change, but I'm fairly sure I'm the only person using this event. It's quite obscure ;p
This commit is contained in:
Luck 2019-01-02 13:54:55 +00:00
parent a0d04790a5
commit 6fc2321fad
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
7 changed files with 49 additions and 23 deletions

View File

@ -23,29 +23,36 @@
* SOFTWARE. * SOFTWARE.
*/ */
package me.lucko.luckperms.api.event.user; package me.lucko.luckperms.api.event.player;
import me.lucko.luckperms.api.User; import me.lucko.luckperms.api.User;
import me.lucko.luckperms.api.event.LuckPermsEvent; import me.lucko.luckperms.api.event.LuckPermsEvent;
import me.lucko.luckperms.api.event.Param; import me.lucko.luckperms.api.event.Param;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.UUID; import java.util.UUID;
/** /**
* Called when LuckPerms has finished processing a certain Player's connection. * Called when LuckPerms has finished processing a Player's initial connection.
* *
* <p>This event will always execute during the platforms async login/auth event. * <p>This event will always execute during the platforms async connection
* All handlers will be called instantly.</p> * event. The LuckPerms platform listener processing the connection will block
* while this event is posted.</p>
* *
* <p>This, among other things, allows you to wait until permission data is loaded * <p>This, among other things, allows you to wait until permission data is
* for a User during the BungeeCord 'LoginEvent', as event priorities are ignored * loaded for a User during the BungeeCord 'LoginEvent', as event priorities are
* by the current implementation.</p> * ignored by the current implementation.</p>
* *
* @since 3.4 * <p>The implementation will make an attempt to ensure this event is called
* for all connections, even if the operation to load User data was not
* successful. Note that LuckPerms will usually cancel the platform connection
* event if data could not be loaded.</p>
*
* @since 4.4
*/ */
public interface UserLoginProcessEvent extends LuckPermsEvent { public interface PlayerLoginProcessEvent extends LuckPermsEvent {
/** /**
* Gets the UUID of the connection which was processed * Gets the UUID of the connection which was processed
@ -61,11 +68,23 @@ public interface UserLoginProcessEvent extends LuckPermsEvent {
*/ */
@NonNull @Param(1) String getUsername(); @NonNull @Param(1) String getUsername();
/**
* Gets if the login was processed successfully.
*
* @return true if the login was successful
*/
default boolean wasSuccessful() {
return getUser() != null;
}
/** /**
* Gets the resultant User instance which was loaded. * Gets the resultant User instance which was loaded.
* *
* <p>Returns {@code null} if the login was not processed
* {@link #wasSuccessful() successfully.}</p>
*
* @return the user instance * @return the user instance
*/ */
@NonNull @Param(2) User getUser(); @Nullable @Param(2) User getUser();
} }

View File

@ -86,8 +86,8 @@ public class BukkitConnectionListener extends AbstractConnectionListener impleme
- setting up cached data. */ - setting up cached data. */
try { try {
User user = loadUser(e.getUniqueId(), e.getName()); User user = loadUser(e.getUniqueId(), e.getName());
this.plugin.getEventFactory().handleUserLoginProcess(e.getUniqueId(), e.getName(), user);
recordConnection(e.getUniqueId()); recordConnection(e.getUniqueId());
this.plugin.getEventFactory().handlePlayerLoginProcess(e.getUniqueId(), e.getName(), user);
} catch (Exception ex) { } catch (Exception ex) {
this.plugin.getLogger().severe("Exception occurred whilst loading data for " + e.getUniqueId() + " - " + e.getName()); this.plugin.getLogger().severe("Exception occurred whilst loading data for " + e.getUniqueId() + " - " + e.getName());
ex.printStackTrace(); ex.printStackTrace();
@ -95,6 +95,7 @@ public class BukkitConnectionListener extends AbstractConnectionListener impleme
// deny the connection // deny the connection
this.deniedAsyncLogin.add(e.getUniqueId()); this.deniedAsyncLogin.add(e.getUniqueId());
e.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, Message.LOADING_DATABASE_ERROR.asString(this.plugin.getLocaleManager())); e.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, Message.LOADING_DATABASE_ERROR.asString(this.plugin.getLocaleManager()));
this.plugin.getEventFactory().handlePlayerLoginProcess(e.getUniqueId(), e.getName(), null);
} }
} }

View File

@ -82,8 +82,8 @@ public class BungeeConnectionListener extends AbstractConnectionListener impleme
- setting up cached data. */ - setting up cached data. */
try { try {
User user = loadUser(c.getUniqueId(), c.getName()); User user = loadUser(c.getUniqueId(), c.getName());
this.plugin.getEventFactory().handleUserLoginProcess(c.getUniqueId(), c.getName(), user);
recordConnection(c.getUniqueId()); recordConnection(c.getUniqueId());
this.plugin.getEventFactory().handlePlayerLoginProcess(c.getUniqueId(), c.getName(), user);
} catch (Exception ex) { } catch (Exception ex) {
this.plugin.getLogger().severe("Exception occurred whilst loading data for " + c.getUniqueId() + " - " + c.getName()); this.plugin.getLogger().severe("Exception occurred whilst loading data for " + c.getUniqueId() + " - " + c.getName());
ex.printStackTrace(); ex.printStackTrace();
@ -94,6 +94,7 @@ public class BungeeConnectionListener extends AbstractConnectionListener impleme
e.setCancelReason(TextComponent.fromLegacyText(Message.LOADING_DATABASE_ERROR.asString(this.plugin.getLocaleManager()))); e.setCancelReason(TextComponent.fromLegacyText(Message.LOADING_DATABASE_ERROR.asString(this.plugin.getLocaleManager())));
e.setCancelled(true); e.setCancelled(true);
} }
this.plugin.getEventFactory().handlePlayerLoginProcess(c.getUniqueId(), c.getName(), null);
} }
// finally, complete our intent to modify state, so the proxy can continue handling the connection. // finally, complete our intent to modify state, so the proxy can continue handling the connection.

View File

@ -52,6 +52,7 @@ import me.lucko.luckperms.api.event.node.NodeAddEvent;
import me.lucko.luckperms.api.event.node.NodeClearEvent; import me.lucko.luckperms.api.event.node.NodeClearEvent;
import me.lucko.luckperms.api.event.node.NodeRemoveEvent; import me.lucko.luckperms.api.event.node.NodeRemoveEvent;
import me.lucko.luckperms.api.event.player.PlayerDataSaveEvent; import me.lucko.luckperms.api.event.player.PlayerDataSaveEvent;
import me.lucko.luckperms.api.event.player.PlayerLoginProcessEvent;
import me.lucko.luckperms.api.event.source.Source; import me.lucko.luckperms.api.event.source.Source;
import me.lucko.luckperms.api.event.sync.ConfigReloadEvent; import me.lucko.luckperms.api.event.sync.ConfigReloadEvent;
import me.lucko.luckperms.api.event.sync.PostSyncEvent; import me.lucko.luckperms.api.event.sync.PostSyncEvent;
@ -68,7 +69,6 @@ import me.lucko.luckperms.api.event.user.UserCacheLoadEvent;
import me.lucko.luckperms.api.event.user.UserDataRecalculateEvent; import me.lucko.luckperms.api.event.user.UserDataRecalculateEvent;
import me.lucko.luckperms.api.event.user.UserFirstLoginEvent; import me.lucko.luckperms.api.event.user.UserFirstLoginEvent;
import me.lucko.luckperms.api.event.user.UserLoadEvent; import me.lucko.luckperms.api.event.user.UserLoadEvent;
import me.lucko.luckperms.api.event.user.UserLoginProcessEvent;
import me.lucko.luckperms.api.event.user.track.UserDemoteEvent; import me.lucko.luckperms.api.event.user.track.UserDemoteEvent;
import me.lucko.luckperms.api.event.user.track.UserPromoteEvent; import me.lucko.luckperms.api.event.user.track.UserPromoteEvent;
import me.lucko.luckperms.common.api.implementation.ApiPermissionHolder; import me.lucko.luckperms.common.api.implementation.ApiPermissionHolder;
@ -121,7 +121,7 @@ public final class EventFactory {
return; return;
} }
T event = supplier.get(); T event = supplier.get();
this.eventBus.post(event); post(event);
}); });
} }
@ -280,6 +280,14 @@ public final class EventFactory {
post(UserFirstLoginEvent.class, () -> generate(UserFirstLoginEvent.class, uuid, username)); post(UserFirstLoginEvent.class, () -> generate(UserFirstLoginEvent.class, uuid, username));
} }
public void handlePlayerLoginProcess(UUID uuid, String username, User user) {
if (!shouldPost(PlayerLoginProcessEvent.class)) {
return;
}
post(generate(PlayerLoginProcessEvent.class, uuid, username, new ApiUser(user)));
}
public void handlePlayerDataSave(UUID uuid, String username, PlayerSaveResult result) { public void handlePlayerDataSave(UUID uuid, String username, PlayerSaveResult result) {
post(PlayerDataSaveEvent.class, () -> generate(PlayerDataSaveEvent.class, uuid, username, result)); post(PlayerDataSaveEvent.class, () -> generate(PlayerDataSaveEvent.class, uuid, username, result));
} }
@ -288,10 +296,6 @@ public final class EventFactory {
post(UserLoadEvent.class, () -> generate(UserLoadEvent.class, new ApiUser(user))); post(UserLoadEvent.class, () -> generate(UserLoadEvent.class, new ApiUser(user)));
} }
public void handleUserLoginProcess(UUID uuid, String username, User user) {
post(UserLoginProcessEvent.class, () -> generate(UserLoginProcessEvent.class, uuid, username, new ApiUser(user)));
}
public void handleUserDemote(User user, Track track, String from, String to, @Nullable Sender source) { public void handleUserDemote(User user, Track track, String from, String to, @Nullable Sender source) {
post(UserDemoteEvent.class, () -> { post(UserDemoteEvent.class, () -> {
Source s = source == null ? UnknownSource.INSTANCE : new EntitySourceImpl(new SenderEntity(source)); Source s = source == null ? UnknownSource.INSTANCE : new EntitySourceImpl(new SenderEntity(source));

View File

@ -77,8 +77,8 @@ public class NukkitConnectionListener extends AbstractConnectionListener impleme
- setting up cached data. */ - setting up cached data. */
try { try {
User user = loadUser(e.getUuid(), e.getName()); User user = loadUser(e.getUuid(), e.getName());
this.plugin.getEventFactory().handleUserLoginProcess(e.getUuid(), e.getName(), user);
recordConnection(e.getUuid()); recordConnection(e.getUuid());
this.plugin.getEventFactory().handlePlayerLoginProcess(e.getUuid(), e.getName(), user);
} catch (Exception ex) { } catch (Exception ex) {
this.plugin.getLogger().severe("Exception occurred whilst loading data for " + e.getUuid() + " - " + e.getName()); this.plugin.getLogger().severe("Exception occurred whilst loading data for " + e.getUuid() + " - " + e.getName());
ex.printStackTrace(); ex.printStackTrace();
@ -86,6 +86,7 @@ public class NukkitConnectionListener extends AbstractConnectionListener impleme
// deny the connection // deny the connection
this.deniedAsyncLogin.add(e.getUuid()); this.deniedAsyncLogin.add(e.getUuid());
e.disAllow(Message.LOADING_DATABASE_ERROR.asString(this.plugin.getLocaleManager())); e.disAllow(Message.LOADING_DATABASE_ERROR.asString(this.plugin.getLocaleManager()));
this.plugin.getEventFactory().handlePlayerLoginProcess(e.getUuid(), e.getName(), null);
} }
} }

View File

@ -79,8 +79,8 @@ public class SpongeConnectionListener extends AbstractConnectionListener {
- setting up cached data. */ - setting up cached data. */
try { try {
User user = loadUser(profile.getUniqueId(), username); User user = loadUser(profile.getUniqueId(), username);
this.plugin.getEventFactory().handleUserLoginProcess(profile.getUniqueId(), username, user);
recordConnection(profile.getUniqueId()); recordConnection(profile.getUniqueId());
this.plugin.getEventFactory().handlePlayerLoginProcess(profile.getUniqueId(), username, user);
} catch (Exception ex) { } catch (Exception ex) {
this.plugin.getLogger().severe("Exception occurred whilst loading data for " + profile.getUniqueId() + " - " + profile.getName()); this.plugin.getLogger().severe("Exception occurred whilst loading data for " + profile.getUniqueId() + " - " + profile.getName());
ex.printStackTrace(); ex.printStackTrace();
@ -91,6 +91,7 @@ public class SpongeConnectionListener extends AbstractConnectionListener {
e.setMessageCancelled(false); e.setMessageCancelled(false);
//noinspection deprecation //noinspection deprecation
e.setMessage(TextSerializers.LEGACY_FORMATTING_CODE.deserialize(Message.LOADING_DATABASE_ERROR.asString(this.plugin.getLocaleManager()))); e.setMessage(TextSerializers.LEGACY_FORMATTING_CODE.deserialize(Message.LOADING_DATABASE_ERROR.asString(this.plugin.getLocaleManager())));
this.plugin.getEventFactory().handlePlayerLoginProcess(profile.getUniqueId(), username, null);
} }
} }

View File

@ -86,11 +86,9 @@ public class VelocityConnectionListener extends AbstractConnectionListener {
- setting up cached data. */ - setting up cached data. */
try { try {
User user = loadUser(p.getUniqueId(), p.getUsername()); User user = loadUser(p.getUniqueId(), p.getUsername());
this.plugin.getEventFactory().handleUserLoginProcess(p.getUniqueId(), p.getUsername(), user);
recordConnection(p.getUniqueId()); recordConnection(p.getUniqueId());
// set permission provider
e.setProvider(new PlayerPermissionProvider(p, user, this.plugin.getContextManager().getCacheFor(p))); e.setProvider(new PlayerPermissionProvider(p, user, this.plugin.getContextManager().getCacheFor(p)));
this.plugin.getEventFactory().handlePlayerLoginProcess(p.getUniqueId(), p.getUsername(), user);
} catch (Exception ex) { } catch (Exception ex) {
this.plugin.getLogger().severe("Exception occurred whilst loading data for " + p.getUniqueId() + " - " + p.getUsername()); this.plugin.getLogger().severe("Exception occurred whilst loading data for " + p.getUniqueId() + " - " + p.getUsername());
ex.printStackTrace(); ex.printStackTrace();
@ -100,6 +98,7 @@ public class VelocityConnectionListener extends AbstractConnectionListener {
// cancel the login attempt // cancel the login attempt
this.deniedLogin.add(p.getUniqueId()); this.deniedLogin.add(p.getUniqueId());
} }
this.plugin.getEventFactory().handlePlayerLoginProcess(p.getUniqueId(), p.getUsername(), null);
} }
} }