From 334bcd43152c2650365b79813ffc4e4f4d4ae16b Mon Sep 17 00:00:00 2001 From: Petr Ilin Date: Mon, 28 Mar 2022 00:50:03 +0300 Subject: More events --- .../java/net/elytrium/limboauth/LimboAuth.java | 61 ++++++++++++++-------- .../limboauth/event/AuthPluginReloadEvent.java | 21 ++++++++ .../limboauth/event/PostAuthorizationEvent.java | 5 +- .../net/elytrium/limboauth/event/PostEvent.java | 5 +- .../limboauth/event/PostRegisterEvent.java | 5 +- .../limboauth/event/PreAuthorizationEvent.java | 5 +- .../net/elytrium/limboauth/event/PreEvent.java | 5 +- .../elytrium/limboauth/event/PreRegisterEvent.java | 5 +- .../net/elytrium/limboauth/event/TaskEvent.java | 28 +++++++++- .../limboauth/handler/AuthSessionHandler.java | 7 ++- 10 files changed, 108 insertions(+), 39 deletions(-) create mode 100644 src/main/java/net/elytrium/limboauth/event/AuthPluginReloadEvent.java (limited to 'src/main/java') diff --git a/src/main/java/net/elytrium/limboauth/LimboAuth.java b/src/main/java/net/elytrium/limboauth/LimboAuth.java index 9340bad..f499df6 100644 --- a/src/main/java/net/elytrium/limboauth/LimboAuth.java +++ b/src/main/java/net/elytrium/limboauth/LimboAuth.java @@ -63,6 +63,7 @@ import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; import java.util.regex.Pattern; import java.util.stream.Collectors; import net.elytrium.limboapi.api.Limbo; @@ -79,9 +80,11 @@ import net.elytrium.limboauth.command.LimboAuthCommand; import net.elytrium.limboauth.command.PremiumCommand; import net.elytrium.limboauth.command.TotpCommand; import net.elytrium.limboauth.command.UnregisterCommand; +import net.elytrium.limboauth.event.AuthPluginReloadEvent; import net.elytrium.limboauth.event.PreAuthorizationEvent; import net.elytrium.limboauth.event.PreEvent; import net.elytrium.limboauth.event.PreRegisterEvent; +import net.elytrium.limboauth.event.TaskEvent; import net.elytrium.limboauth.floodgate.FloodgateApiHolder; import net.elytrium.limboauth.handler.AuthSessionHandler; import net.elytrium.limboauth.listener.AuthListener; @@ -123,6 +126,8 @@ public class LimboAuth { private final LimboFactory factory; private final FloodgateApiHolder floodgateApi; + private JdbcPooledConnectionSource connectionSource; + private Dao playerDao; private Pattern nicknameValidationPattern; private Limbo authServer; @@ -183,26 +188,25 @@ public class LimboAuth { this.cachedAuthChecks.clear(); Settings.DATABASE dbConfig = Settings.IMP.DATABASE; - JdbcPooledConnectionSource connectionSource; // requireNonNull prevents the shade plugin from excluding the drivers in minimized jar. switch (dbConfig.STORAGE_TYPE.toLowerCase(Locale.ROOT)) { case "h2": { Objects.requireNonNull(org.h2.Driver.class); Objects.requireNonNull(org.h2.engine.Engine.class); - connectionSource = new JdbcPooledConnectionSource("jdbc:h2:" + this.dataDirectory.toFile().getAbsoluteFile() + "/limboauth"); + this.connectionSource = new JdbcPooledConnectionSource("jdbc:h2:" + this.dataDirectory.toFile().getAbsoluteFile() + "/limboauth"); break; } case "mysql": { Objects.requireNonNull(com.mysql.cj.jdbc.Driver.class); Objects.requireNonNull(com.mysql.cj.conf.url.SingleConnectionUrl.class); - connectionSource = new JdbcPooledConnectionSource( + this.connectionSource = new JdbcPooledConnectionSource( "jdbc:mysql://" + dbConfig.HOSTNAME + "/" + dbConfig.DATABASE + dbConfig.CONNECTION_PARAMETERS, dbConfig.USER, dbConfig.PASSWORD ); break; } case "postgresql": { Objects.requireNonNull(org.postgresql.Driver.class); - connectionSource = new JdbcPooledConnectionSource( + this.connectionSource = new JdbcPooledConnectionSource( "jdbc:postgresql://" + dbConfig.HOSTNAME + "/" + dbConfig.DATABASE + dbConfig.CONNECTION_PARAMETERS, dbConfig.USER, dbConfig.PASSWORD ); break; @@ -214,8 +218,8 @@ public class LimboAuth { } } - TableUtils.createTableIfNotExists(connectionSource, RegisteredPlayer.class); - this.playerDao = DaoManager.createDao(connectionSource, RegisteredPlayer.class); + TableUtils.createTableIfNotExists(this.connectionSource, RegisteredPlayer.class); + this.playerDao = DaoManager.createDao(this.connectionSource, RegisteredPlayer.class); this.nicknameValidationPattern = Pattern.compile(Settings.IMP.MAIN.ALLOWED_NICKNAME_REGEX); this.migrateDb(this.playerDao); @@ -287,31 +291,33 @@ public class LimboAuth { Settings.IMP.MAIN.PURGE_CACHE_MILLIS, TimeUnit.MILLISECONDS ); + + this.server.getEventManager().fireAndForget(new AuthPluginReloadEvent()); } private List filterCommands(List commands) { return commands.stream().filter(e -> e.startsWith("/")).map(e -> e.substring(1)).collect(Collectors.toList()); } - public void migrateDb(Dao playerDao) { + public void migrateDb(Dao dao) { Set tables = new HashSet<>(); - Collections.addAll(tables, playerDao.getTableInfo().getFieldTypes()); + Collections.addAll(tables, dao.getTableInfo().getFieldTypes()); String findSql; switch (Settings.IMP.DATABASE.STORAGE_TYPE) { case "h2": { findSql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" - + playerDao.getTableInfo().getTableName() + "';"; + + dao.getTableInfo().getTableName() + "';"; break; } case "postgresql": { findSql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_CATALOG = '" + Settings.IMP.DATABASE.DATABASE - + "' AND TABLE_NAME = '" + playerDao.getTableInfo().getTableName() + "';"; + + "' AND TABLE_NAME = '" + dao.getTableInfo().getTableName() + "';"; break; } case "mysql": { findSql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '" + Settings.IMP.DATABASE.DATABASE - + "' AND TABLE_NAME = '" + playerDao.getTableInfo().getTableName() + "';"; + + "' AND TABLE_NAME = '" + dao.getTableInfo().getTableName() + "';"; break; } default: { @@ -322,21 +328,21 @@ public class LimboAuth { } try { - playerDao.queryRaw(findSql).forEach(e -> tables.removeIf(q -> q.getColumnName().equalsIgnoreCase(e[0]))); + dao.queryRaw(findSql).forEach(e -> tables.removeIf(q -> q.getColumnName().equalsIgnoreCase(e[0]))); tables.forEach(t -> { try { String columnDefinition = t.getColumnDefinition(); - StringBuilder builder = new StringBuilder("ALTER TABLE \"AUTH\" ADD "); + StringBuilder builder = new StringBuilder("ALTER TABLE \"" + dao.getTableInfo().getTableName() + "\" ADD "); List dummy = new ArrayList<>(); if (columnDefinition == null) { - playerDao.getConnectionSource().getDatabaseType().appendColumnArg(t.getTableName(), builder, t, dummy, dummy, dummy, dummy); + dao.getConnectionSource().getDatabaseType().appendColumnArg(t.getTableName(), builder, t, dummy, dummy, dummy, dummy); } else { - playerDao.getConnectionSource().getDatabaseType().appendEscapedEntityName(builder, t.getColumnName()); + dao.getConnectionSource().getDatabaseType().appendEscapedEntityName(builder, t.getColumnName()); builder.append(" ").append(columnDefinition).append(" "); } - playerDao.executeRawNoArgs(builder.toString()); + dao.executeRawNoArgs(builder.toString()); } catch (SQLException e) { e.printStackTrace(); } @@ -423,16 +429,16 @@ public class LimboAuth { } if (registeredPlayer == null) { - this.server.getEventManager().fire(new PreRegisterEvent(player)).thenAcceptAsync((event) - -> this.sendPlayer(event, null)); + Consumer eventConsumer = (event) -> this.sendPlayer(event, null); + this.server.getEventManager().fire(new PreRegisterEvent(player, eventConsumer)).thenAcceptAsync(eventConsumer); } else { - this.server.getEventManager().fire(new PreAuthorizationEvent(player, registeredPlayer)).thenAcceptAsync((event) - -> this.sendPlayer(event, event.getPlayerInfo())); + Consumer eventConsumer = (event) -> this.sendPlayer(event, ((PreAuthorizationEvent) event).getPlayerInfo()); + this.server.getEventManager().fire(new PreAuthorizationEvent(player, registeredPlayer, eventConsumer)).thenAcceptAsync(eventConsumer); } } - private void sendPlayer(PreEvent event, RegisteredPlayer registeredPlayer) { - Player player = event.getPlayer(); + private void sendPlayer(TaskEvent event, RegisteredPlayer registeredPlayer) { + Player player = ((PreEvent) event).getPlayer(); switch (event.getResult()) { case BYPASS: { @@ -443,6 +449,9 @@ public class LimboAuth { player.disconnect(event.getReason()); break; } + case WAIT: { + return; + } case NORMAL: default: { try { @@ -512,6 +521,14 @@ public class LimboAuth { .forEach(userMap::remove); } + public JdbcPooledConnectionSource getConnectionSource() { + return this.connectionSource; + } + + public Dao getPlayerDao() { + return this.playerDao; + } + public Set getUnsafePasswords() { return this.unsafePasswords; } diff --git a/src/main/java/net/elytrium/limboauth/event/AuthPluginReloadEvent.java b/src/main/java/net/elytrium/limboauth/event/AuthPluginReloadEvent.java new file mode 100644 index 0000000..d7aaf15 --- /dev/null +++ b/src/main/java/net/elytrium/limboauth/event/AuthPluginReloadEvent.java @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2021 Elytrium + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package net.elytrium.limboauth.event; + +public class AuthPluginReloadEvent { +} diff --git a/src/main/java/net/elytrium/limboauth/event/PostAuthorizationEvent.java b/src/main/java/net/elytrium/limboauth/event/PostAuthorizationEvent.java index cd13c73..c28ed46 100644 --- a/src/main/java/net/elytrium/limboauth/event/PostAuthorizationEvent.java +++ b/src/main/java/net/elytrium/limboauth/event/PostAuthorizationEvent.java @@ -17,11 +17,12 @@ package net.elytrium.limboauth.event; +import java.util.function.Consumer; import net.elytrium.limboapi.api.player.LimboPlayer; import net.elytrium.limboauth.model.RegisteredPlayer; public class PostAuthorizationEvent extends PostEvent { - public PostAuthorizationEvent(LimboPlayer player, RegisteredPlayer playerInfo) { - super(player, playerInfo); + public PostAuthorizationEvent(LimboPlayer player, RegisteredPlayer playerInfo, Consumer onComplete) { + super(player, playerInfo, onComplete); } } diff --git a/src/main/java/net/elytrium/limboauth/event/PostEvent.java b/src/main/java/net/elytrium/limboauth/event/PostEvent.java index e86fbcf..68932ef 100644 --- a/src/main/java/net/elytrium/limboauth/event/PostEvent.java +++ b/src/main/java/net/elytrium/limboauth/event/PostEvent.java @@ -17,6 +17,7 @@ package net.elytrium.limboauth.event; +import java.util.function.Consumer; import net.elytrium.limboapi.api.player.LimboPlayer; import net.elytrium.limboauth.model.RegisteredPlayer; @@ -25,8 +26,8 @@ public abstract class PostEvent extends TaskEvent { private final LimboPlayer player; private final RegisteredPlayer playerInfo; - protected PostEvent(LimboPlayer player, RegisteredPlayer playerInfo) { - super(); + protected PostEvent(LimboPlayer player, RegisteredPlayer playerInfo, Consumer onComplete) { + super(onComplete); this.player = player; this.playerInfo = playerInfo; diff --git a/src/main/java/net/elytrium/limboauth/event/PostRegisterEvent.java b/src/main/java/net/elytrium/limboauth/event/PostRegisterEvent.java index df2fd2a..fd8f24c 100644 --- a/src/main/java/net/elytrium/limboauth/event/PostRegisterEvent.java +++ b/src/main/java/net/elytrium/limboauth/event/PostRegisterEvent.java @@ -17,11 +17,12 @@ package net.elytrium.limboauth.event; +import java.util.function.Consumer; import net.elytrium.limboapi.api.player.LimboPlayer; import net.elytrium.limboauth.model.RegisteredPlayer; public class PostRegisterEvent extends PostEvent { - public PostRegisterEvent(LimboPlayer player, RegisteredPlayer playerInfo) { - super(player, playerInfo); + public PostRegisterEvent(LimboPlayer player, RegisteredPlayer playerInfo, Consumer onComplete) { + super(player, playerInfo, onComplete); } } diff --git a/src/main/java/net/elytrium/limboauth/event/PreAuthorizationEvent.java b/src/main/java/net/elytrium/limboauth/event/PreAuthorizationEvent.java index d8e4dc7..207fcf6 100644 --- a/src/main/java/net/elytrium/limboauth/event/PreAuthorizationEvent.java +++ b/src/main/java/net/elytrium/limboauth/event/PreAuthorizationEvent.java @@ -18,13 +18,14 @@ package net.elytrium.limboauth.event; import com.velocitypowered.api.proxy.Player; +import java.util.function.Consumer; import net.elytrium.limboauth.model.RegisteredPlayer; public class PreAuthorizationEvent extends PreEvent { private final RegisteredPlayer playerInfo; - public PreAuthorizationEvent(Player player, RegisteredPlayer playerInfo) { - super(player); + public PreAuthorizationEvent(Player player, RegisteredPlayer playerInfo, Consumer onComplete) { + super(player, onComplete); this.playerInfo = playerInfo; } diff --git a/src/main/java/net/elytrium/limboauth/event/PreEvent.java b/src/main/java/net/elytrium/limboauth/event/PreEvent.java index 08f9b6b..45bb37d 100644 --- a/src/main/java/net/elytrium/limboauth/event/PreEvent.java +++ b/src/main/java/net/elytrium/limboauth/event/PreEvent.java @@ -18,12 +18,13 @@ package net.elytrium.limboauth.event; import com.velocitypowered.api.proxy.Player; +import java.util.function.Consumer; public abstract class PreEvent extends TaskEvent { private final Player player; - protected PreEvent(Player player) { - super(); + protected PreEvent(Player player, Consumer onComplete) { + super(onComplete); this.player = player; } diff --git a/src/main/java/net/elytrium/limboauth/event/PreRegisterEvent.java b/src/main/java/net/elytrium/limboauth/event/PreRegisterEvent.java index 8f642d3..90eff58 100644 --- a/src/main/java/net/elytrium/limboauth/event/PreRegisterEvent.java +++ b/src/main/java/net/elytrium/limboauth/event/PreRegisterEvent.java @@ -18,9 +18,10 @@ package net.elytrium.limboauth.event; import com.velocitypowered.api.proxy.Player; +import java.util.function.Consumer; public class PreRegisterEvent extends PreEvent { - public PreRegisterEvent(Player player) { - super(player); + public PreRegisterEvent(Player player, Consumer onComplete) { + super(player, onComplete); } } diff --git a/src/main/java/net/elytrium/limboauth/event/TaskEvent.java b/src/main/java/net/elytrium/limboauth/event/TaskEvent.java index 4df70c8..30e7947 100644 --- a/src/main/java/net/elytrium/limboauth/event/TaskEvent.java +++ b/src/main/java/net/elytrium/limboauth/event/TaskEvent.java @@ -17,6 +17,7 @@ package net.elytrium.limboauth.event; +import java.util.function.Consumer; import net.elytrium.limboauth.Settings; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; @@ -28,6 +29,12 @@ public abstract class TaskEvent { private Result result = Result.NORMAL; private Component reason = defaultReason; + private final Consumer onComplete; + + public TaskEvent(Consumer onComplete) { + this.onComplete = onComplete; + } + public Result getResult() { return this.result; } @@ -45,9 +52,28 @@ public abstract class TaskEvent { return this.reason; } + public void complete(Result result) { + if (this.result != Result.WAIT) { + return; + } + + this.result = result; + this.onComplete.accept(this); + } + + public void completeAndCancel(@NotNull Component c) { + if (this.result != Result.WAIT) { + return; + } + + this.cancel(c); + this.onComplete.accept(this); + } + public enum Result { CANCEL, BYPASS, - NORMAL + NORMAL, + WAIT } } diff --git a/src/main/java/net/elytrium/limboauth/handler/AuthSessionHandler.java b/src/main/java/net/elytrium/limboauth/handler/AuthSessionHandler.java index 9a6ca8b..43895f2 100644 --- a/src/main/java/net/elytrium/limboauth/handler/AuthSessionHandler.java +++ b/src/main/java/net/elytrium/limboauth/handler/AuthSessionHandler.java @@ -39,7 +39,6 @@ import net.elytrium.limboapi.api.player.LimboPlayer; import net.elytrium.limboauth.LimboAuth; import net.elytrium.limboauth.Settings; import net.elytrium.limboauth.event.PostAuthorizationEvent; -import net.elytrium.limboauth.event.PostEvent; import net.elytrium.limboauth.event.PostRegisterEvent; import net.elytrium.limboauth.event.TaskEvent; import net.elytrium.limboauth.migration.MigrationHash; @@ -316,17 +315,17 @@ public class AuthSessionHandler implements LimboSessionHandler { } this.plugin.getServer().getEventManager() - .fire(new PostAuthorizationEvent(this.player, this.playerInfo)) + .fire(new PostAuthorizationEvent(this.player, this.playerInfo, this::finishAuth)) .thenAcceptAsync(this::finishAuth); } private void finishRegister() { this.plugin.getServer().getEventManager() - .fire(new PostRegisterEvent(this.player, this.playerInfo)) + .fire(new PostRegisterEvent(this.player, this.playerInfo, this::finishAuth)) .thenAcceptAsync(this::finishAuth); } - private void finishAuth(PostEvent event) { + private void finishAuth(TaskEvent event) { if (Settings.IMP.MAIN.CRACKED_TITLE_SETTINGS.CLEAR_AFTER_LOGIN) { this.proxyPlayer.clearTitle(); } -- cgit