From a992cfc04be8f330f669923360199b7b57a49e35 Mon Sep 17 00:00:00 2001 From: Petr Ilin Date: Fri, 25 Mar 2022 18:12:16 +0300 Subject: Events implementation --- .../java/net/elytrium/limboauth/LimboAuth.java | 12 +++++ src/main/java/net/elytrium/limboauth/Settings.java | 6 ++- .../limboauth/event/PostAuthorizationEvent.java | 27 +++++++++++ .../net/elytrium/limboauth/event/PostEvent.java | 42 +++++++++++++++++ .../limboauth/event/PostRegisterEvent.java | 27 +++++++++++ .../limboauth/event/PreAuthorizationEvent.java | 34 ++++++++++++++ .../net/elytrium/limboauth/event/PreEvent.java | 33 ++++++++++++++ .../elytrium/limboauth/event/PreRegisterEvent.java | 26 +++++++++++ .../net/elytrium/limboauth/event/TaskEvent.java | 53 ++++++++++++++++++++++ .../limboauth/handler/AuthSessionHandler.java | 23 ++++++++-- 10 files changed, 278 insertions(+), 5 deletions(-) create mode 100644 src/main/java/net/elytrium/limboauth/event/PostAuthorizationEvent.java create mode 100644 src/main/java/net/elytrium/limboauth/event/PostEvent.java create mode 100644 src/main/java/net/elytrium/limboauth/event/PostRegisterEvent.java create mode 100644 src/main/java/net/elytrium/limboauth/event/PreAuthorizationEvent.java create mode 100644 src/main/java/net/elytrium/limboauth/event/PreEvent.java create mode 100644 src/main/java/net/elytrium/limboauth/event/PreRegisterEvent.java create mode 100644 src/main/java/net/elytrium/limboauth/event/TaskEvent.java (limited to 'src/main') diff --git a/src/main/java/net/elytrium/limboauth/LimboAuth.java b/src/main/java/net/elytrium/limboauth/LimboAuth.java index 15b2222..8a471ca 100644 --- a/src/main/java/net/elytrium/limboauth/LimboAuth.java +++ b/src/main/java/net/elytrium/limboauth/LimboAuth.java @@ -80,6 +80,8 @@ 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.PreAuthorizationEvent; +import net.elytrium.limboauth.event.PreRegisterEvent; import net.elytrium.limboauth.floodgate.FloodgateApiHolder; import net.elytrium.limboauth.handler.AuthSessionHandler; import net.elytrium.limboauth.listener.AuthListener; @@ -416,6 +418,16 @@ public class LimboAuth { } } + if (registeredPlayer == null) { + this.server.getEventManager().fire(new PreRegisterEvent(player)).thenAcceptAsync((event) + -> this.sendPlayer(event.getPlayer(), null)); + } else { + this.server.getEventManager().fire(new PreAuthorizationEvent(player, registeredPlayer)).thenAcceptAsync((event) + -> this.sendPlayer(event.getPlayer(), event.getPlayerInfo())); + } + } + + private void sendPlayer(Player player, RegisteredPlayer registeredPlayer) { // Send player to auth virtual server. try { this.authServer.spawnPlayer(player, new AuthSessionHandler(this.playerDao, player, this, registeredPlayer)); diff --git a/src/main/java/net/elytrium/limboauth/Settings.java b/src/main/java/net/elytrium/limboauth/Settings.java index 5ce38ab..825ce61 100644 --- a/src/main/java/net/elytrium/limboauth/Settings.java +++ b/src/main/java/net/elytrium/limboauth/Settings.java @@ -134,7 +134,7 @@ public class Settings extends Config { public boolean CLEAR_AFTER_LOGIN = false; public Title.Times toTimes() { - return Title.Times.of(Ticks.duration(this.FADE_IN), Ticks.duration(this.STAY), Ticks.duration(this.FADE_OUT)); + return Title.Times.times(Ticks.duration(this.FADE_IN), Ticks.duration(this.STAY), Ticks.duration(this.FADE_OUT)); } } @@ -149,7 +149,7 @@ public class Settings extends Config { public int FADE_OUT = 20; public Title.Times toTimes() { - return Title.Times.of(Ticks.duration(this.FADE_IN), Ticks.duration(this.STAY), Ticks.duration(this.FADE_OUT)); + return Title.Times.times(Ticks.duration(this.FADE_IN), Ticks.duration(this.STAY), Ticks.duration(this.FADE_OUT)); } } @@ -240,6 +240,8 @@ public class Settings extends Config { public String NOT_PREMIUM = "{PRFX} &cYour account is not &6PREMIUM&c!"; public String PREMIUM_USAGE = "{PRFX} Usage: &6/premium confirm"; + public String EVENT_CANCELLED = "{PRFX} Authorization event was cancelled"; + public String FORCE_UNREGISTER_SUCCESSFUL = "{PRFX} &6{0} &asuccessfully unregistered!"; public String FORCE_UNREGISTER_KICK = "{PRFX}{NL}&aYou have been unregistered by administrator!"; public String FORCE_UNREGISTER_NOT_SUCCESSFUL = "{PRFX} &cUnable to unregister &6{0}&c. Most likely this player has never been on this server."; diff --git a/src/main/java/net/elytrium/limboauth/event/PostAuthorizationEvent.java b/src/main/java/net/elytrium/limboauth/event/PostAuthorizationEvent.java new file mode 100644 index 0000000..cd13c73 --- /dev/null +++ b/src/main/java/net/elytrium/limboauth/event/PostAuthorizationEvent.java @@ -0,0 +1,27 @@ +/* + * 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; + +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); + } +} diff --git a/src/main/java/net/elytrium/limboauth/event/PostEvent.java b/src/main/java/net/elytrium/limboauth/event/PostEvent.java new file mode 100644 index 0000000..e86fbcf --- /dev/null +++ b/src/main/java/net/elytrium/limboauth/event/PostEvent.java @@ -0,0 +1,42 @@ +/* + * 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; + +import net.elytrium.limboapi.api.player.LimboPlayer; +import net.elytrium.limboauth.model.RegisteredPlayer; + +public abstract class PostEvent extends TaskEvent { + + private final LimboPlayer player; + private final RegisteredPlayer playerInfo; + + protected PostEvent(LimboPlayer player, RegisteredPlayer playerInfo) { + super(); + + this.player = player; + this.playerInfo = playerInfo; + } + + public RegisteredPlayer getPlayerInfo() { + return this.playerInfo; + } + + public LimboPlayer getPlayer() { + return this.player; + } +} diff --git a/src/main/java/net/elytrium/limboauth/event/PostRegisterEvent.java b/src/main/java/net/elytrium/limboauth/event/PostRegisterEvent.java new file mode 100644 index 0000000..df2fd2a --- /dev/null +++ b/src/main/java/net/elytrium/limboauth/event/PostRegisterEvent.java @@ -0,0 +1,27 @@ +/* + * 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; + +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); + } +} diff --git a/src/main/java/net/elytrium/limboauth/event/PreAuthorizationEvent.java b/src/main/java/net/elytrium/limboauth/event/PreAuthorizationEvent.java new file mode 100644 index 0000000..d8e4dc7 --- /dev/null +++ b/src/main/java/net/elytrium/limboauth/event/PreAuthorizationEvent.java @@ -0,0 +1,34 @@ +/* + * 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; + +import com.velocitypowered.api.proxy.Player; +import net.elytrium.limboauth.model.RegisteredPlayer; + +public class PreAuthorizationEvent extends PreEvent { + private final RegisteredPlayer playerInfo; + + public PreAuthorizationEvent(Player player, RegisteredPlayer playerInfo) { + super(player); + this.playerInfo = playerInfo; + } + + public RegisteredPlayer getPlayerInfo() { + return this.playerInfo; + } +} diff --git a/src/main/java/net/elytrium/limboauth/event/PreEvent.java b/src/main/java/net/elytrium/limboauth/event/PreEvent.java new file mode 100644 index 0000000..08f9b6b --- /dev/null +++ b/src/main/java/net/elytrium/limboauth/event/PreEvent.java @@ -0,0 +1,33 @@ +/* + * 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; + +import com.velocitypowered.api.proxy.Player; + +public abstract class PreEvent extends TaskEvent { + private final Player player; + + protected PreEvent(Player player) { + super(); + this.player = player; + } + + public Player getPlayer() { + return this.player; + } +} diff --git a/src/main/java/net/elytrium/limboauth/event/PreRegisterEvent.java b/src/main/java/net/elytrium/limboauth/event/PreRegisterEvent.java new file mode 100644 index 0000000..8f642d3 --- /dev/null +++ b/src/main/java/net/elytrium/limboauth/event/PreRegisterEvent.java @@ -0,0 +1,26 @@ +/* + * 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; + +import com.velocitypowered.api.proxy.Player; + +public class PreRegisterEvent extends PreEvent { + public PreRegisterEvent(Player player) { + super(player); + } +} diff --git a/src/main/java/net/elytrium/limboauth/event/TaskEvent.java b/src/main/java/net/elytrium/limboauth/event/TaskEvent.java new file mode 100644 index 0000000..4df70c8 --- /dev/null +++ b/src/main/java/net/elytrium/limboauth/event/TaskEvent.java @@ -0,0 +1,53 @@ +/* + * 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; + +import net.elytrium.limboauth.Settings; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import org.jetbrains.annotations.NotNull; + +public abstract class TaskEvent { + private static final Component defaultReason + = LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.EVENT_CANCELLED); + private Result result = Result.NORMAL; + private Component reason = defaultReason; + + public Result getResult() { + return this.result; + } + + public void setResult(@NotNull Result result) { + this.result = result; + } + + public void cancel(@NotNull Component reason) { + this.result = Result.CANCEL; + this.reason = reason; + } + + public Component getReason() { + return this.reason; + } + + public enum Result { + CANCEL, + BYPASS, + NORMAL + } +} diff --git a/src/main/java/net/elytrium/limboauth/handler/AuthSessionHandler.java b/src/main/java/net/elytrium/limboauth/handler/AuthSessionHandler.java index c4c4587..03f9d5f 100644 --- a/src/main/java/net/elytrium/limboauth/handler/AuthSessionHandler.java +++ b/src/main/java/net/elytrium/limboauth/handler/AuthSessionHandler.java @@ -38,6 +38,10 @@ import net.elytrium.limboapi.api.LimboSessionHandler; 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; import net.elytrium.limboauth.model.RegisteredPlayer; import net.kyori.adventure.bossbar.BossBar; @@ -128,7 +132,7 @@ public class AuthSessionHandler implements LimboSessionHandler { ) ); } - this.finishAuth(); + this.finishRegister(); } } else { this.sendMessage(false); @@ -313,14 +317,27 @@ public class AuthSessionHandler implements LimboSessionHandler { ) ); } - this.finishAuth(); + + this.plugin.getServer().getEventManager() + .fire(new PostAuthorizationEvent(this.player, this.playerInfo)) + .thenAcceptAsync(this::finishAuth); + } + + private void finishRegister() { + this.plugin.getServer().getEventManager() + .fire(new PostRegisterEvent(this.player, this.playerInfo)) + .thenAcceptAsync(this::finishAuth); } - private void finishAuth() { + private void finishAuth(PostEvent event) { if (Settings.IMP.MAIN.CRACKED_TITLE_SETTINGS.CLEAR_AFTER_LOGIN) { this.proxyPlayer.clearTitle(); } + if (event.getResult() == TaskEvent.Result.CANCEL) { + this.proxyPlayer.disconnect(event.getReason()); + } + this.plugin.cacheAuthUser(this.proxyPlayer); this.player.disconnect(); } -- cgit