cleanup login handling & add CountdownLatch to ensure the plugin has started before logins are handled

This commit is contained in:
Luck
2017-04-21 19:10:25 +01:00
Unverified
parent c64f72e394
commit f43b9c96de
5 changed files with 72 additions and 81 deletions
@@ -50,6 +50,7 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@RequiredArgsConstructor
public class BukkitListener implements Listener {
@@ -63,6 +64,14 @@ public class BukkitListener implements Listener {
/* Called when the player first attempts a connection with the server.
Listening on LOW priority to allow plugins to modify username / UUID data here. (auth plugins) */
/* wait for the plugin to enable. because these events are fired async, they can be called before
the plugin has enabled. */
try {
plugin.getEnableLatch().await(30, TimeUnit.SECONDS);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
/* the player was denied entry to the server before this priority.
log this, so we can handle appropriately later. */
if (e.getLoginResult() != AsyncPlayerPreLoginEvent.Result.ALLOWED) {
@@ -71,7 +80,7 @@ public class BukkitListener implements Listener {
return;
}
/* either the plugin hasn't finished starting yet, or there was an issue connecting to the DB, performing file i/o, etc.
/* there was an issue connecting to the DB, performing file i/o, etc.
we don't let players join in this case, because it means they can connect to the server without their permissions data.
some server admins rely on negating perms to stop users from causing damage etc, so it's really important that
this data is loaded. */
@@ -96,7 +105,7 @@ public class BukkitListener implements Listener {
- creating a user instance in the UserManager for this connection.
- setting up cached data. */
try {
LoginHelper.loadUser(plugin, e.getUniqueId(), e.getName());
LoginHelper.loadUser(plugin, e.getUniqueId(), e.getName(), false);
} catch (Exception ex) {
ex.printStackTrace();
@@ -102,6 +102,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;
@Getter
@@ -130,6 +131,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
private CalculatorFactory calculatorFactory;
private BufferedRequest<Void> updateTaskBuffer;
private boolean started = false;
private CountDownLatch enableLatch = new CountDownLatch(1);
private VerboseHandler verboseHandler;
private BukkitSenderFactory senderFactory;
private PermissionVault permissionVault;
@@ -147,6 +149,17 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
@Override
public void onEnable() {
try {
enable();
started = true;
} finally {
// count down the latch when onEnable has been called
// we don't care about the result here
enableLatch.countDown();
}
}
private void enable() {
LuckPermsPlugin.sendStartupBanner(getConsoleSender(), this);
ignoringLogs = ConcurrentHashMap.newKeySet();
@@ -324,7 +337,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
// Load any online users (in the case of a reload)
for (Player player : getServer().getOnlinePlayers()) {
scheduler.doAsync(() -> {
LoginHelper.loadUser(this, player.getUniqueId(), player.getName());
LoginHelper.loadUser(this, player.getUniqueId(), player.getName(), false);
User user = getUserManager().get(getUuidCache().getUUID(player.getUniqueId()));
if (user != null) {
scheduler.doSync(() -> {
@@ -339,7 +352,6 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
});
}
started = true;
getLog().info("Successfully loaded.");
}