diff --git a/eternalcore-api/src/main/java/com/eternalcode/core/feature/afk/event/AfkSwitchEvent.java b/eternalcore-api/src/main/java/com/eternalcode/core/feature/afk/event/AfkSwitchEvent.java index f9144bfd5..0bd763231 100644 --- a/eternalcore-api/src/main/java/com/eternalcode/core/feature/afk/event/AfkSwitchEvent.java +++ b/eternalcore-api/src/main/java/com/eternalcode/core/feature/afk/event/AfkSwitchEvent.java @@ -13,17 +13,23 @@ public class AfkSwitchEvent extends Event implements Cancellable { private static final HandlerList HANDLER_LIST = new HandlerList(); private final Afk afk; + private final boolean isAfk; private boolean cancelled; - public AfkSwitchEvent(Afk afk) { + public AfkSwitchEvent(Afk afk, boolean isAfk) { super(false); this.afk = afk; + this.isAfk = isAfk; } public Afk getAfk() { return this.afk; } + public boolean isAfk() { + return this.isAfk; + } + @Override public boolean isCancelled() { return this.cancelled; diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/configuration/implementation/PluginConfiguration.java b/eternalcore-core/src/main/java/com/eternalcode/core/configuration/implementation/PluginConfiguration.java index c7ff9f84b..e7dc0bb17 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/configuration/implementation/PluginConfiguration.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/configuration/implementation/PluginConfiguration.java @@ -318,6 +318,9 @@ public static class Afk implements AfkSettings { @Description({ " ", "# The amount of time a player must be inactive to be marked as AFK" }) public Duration afkInactivityTime = Duration.ofMinutes(10); + @Description({ " ", "# Should a player be kicked from the game when marked as AFK?" }) + public boolean kickOnAfk = false; + @Override public int interactionsCountDisableAfk() { return this.interactionsCountDisableAfk; diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/afk/AfkKickController.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/afk/AfkKickController.java new file mode 100644 index 000000000..e472a0c9c --- /dev/null +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/afk/AfkKickController.java @@ -0,0 +1,68 @@ +package com.eternalcode.core.feature.afk; + +import static net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection; + +import com.eternalcode.core.configuration.implementation.PluginConfiguration; +import com.eternalcode.core.feature.afk.event.AfkSwitchEvent; +import com.eternalcode.core.injector.annotations.Inject; +import com.eternalcode.core.injector.annotations.component.Controller; +import com.eternalcode.core.translation.Translation; +import com.eternalcode.core.translation.TranslationManager; +import com.eternalcode.core.user.User; +import com.eternalcode.core.user.UserManager; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import org.bukkit.Server; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; + +import java.util.UUID; + +@Controller +class AfkKickController implements Listener { + + private final MiniMessage miniMessage; + private final Server server; + private final UserManager userManager; + private final PluginConfiguration configuration; + private final TranslationManager translationManager; + + @Inject + public AfkKickController( + MiniMessage miniMessage, + Server server, + UserManager userManager, + PluginConfiguration configuration, + TranslationManager translationManager + ) { + this.miniMessage = miniMessage; + this.server = server; + this.userManager = userManager; + this.configuration = configuration; + this.translationManager = translationManager; + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + void onAfkSwitch(AfkSwitchEvent event) { + UUID playerUUID = event.getAfk().getPlayer(); + + if (!event.isAfk() || !this.configuration.afk.kickOnAfk) { + return; + } + + Player player = this.server.getPlayer(playerUUID); + + if (player == null) { + return; + } + + User user = this.userManager.getOrCreate(playerUUID, player.getName()); + Translation translation = this.translationManager.getMessages(user); + + Component component = this.miniMessage.deserialize(translation.afk().afkKickReason()); + player.kickPlayer(legacySection().serialize(component)); + } +} diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/afk/AfkServiceImpl.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/afk/AfkServiceImpl.java index 3cc439e82..6ac5504fa 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/afk/AfkServiceImpl.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/afk/AfkServiceImpl.java @@ -48,7 +48,7 @@ public void switchAfk(UUID playerUniqueId, AfkReason reason) { @Override public Afk markAfk(UUID playerUniqueId, AfkReason reason) { Afk afk = new Afk(playerUniqueId, reason, Instant.now()); - AfkSwitchEvent event = this.eventCaller.callEvent(new AfkSwitchEvent(afk)); + AfkSwitchEvent event = this.eventCaller.callEvent(new AfkSwitchEvent(afk, true)); if (event.isCancelled()) { return afk; @@ -60,6 +60,7 @@ public Afk markAfk(UUID playerUniqueId, AfkReason reason) { return afk; } + @Override public void markInteraction(UUID playerUniqueId) { this.lastInteraction.put(playerUniqueId, Instant.now()); @@ -87,7 +88,7 @@ public void clearAfk(UUID playerUniqueId) { return; } - AfkSwitchEvent event = this.eventCaller.callEvent(new AfkSwitchEvent(afk)); + AfkSwitchEvent event = this.eventCaller.callEvent(new AfkSwitchEvent(afk, false)); if (event.isCancelled()) { return; diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/afk/AfkController.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/afk/AfkStateController.java similarity index 90% rename from eternalcore-core/src/main/java/com/eternalcode/core/feature/afk/AfkController.java rename to eternalcore-core/src/main/java/com/eternalcode/core/feature/afk/AfkStateController.java index e36da4414..cba9262a5 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/afk/AfkController.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/afk/AfkStateController.java @@ -11,12 +11,12 @@ import java.util.UUID; @Controller -class AfkController implements Listener { +class AfkStateController implements Listener { private final AfkService afkService; @Inject - AfkController(AfkService afkService) { + public AfkStateController(AfkService afkService) { this.afkService = afkService; } @@ -31,5 +31,4 @@ void onMove(PlayerMoveEvent event) { void onQuit(PlayerQuitEvent event) { this.afkService.clearAfk(event.getPlayer().getUniqueId()); } - } diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java b/eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java index 79126e861..17391e78d 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java @@ -236,6 +236,7 @@ interface AfkSection { Notice afkOn(); Notice afkOff(); Notice afkDelay(); + String afkKickReason(); } // event section diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java index 20c0f5160..e419be8c6 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java @@ -494,6 +494,9 @@ public static class ENAfkSection implements AfkSection { @Description({ " ", "# {TIME} - Time after the player can execute the command." }) public Notice afkDelay = Notice.chat("You can use this command only after {TIME}!"); + + @Description({ " " }) + public String afkKickReason = "You have been kicked due to inactivity!"; } @Description({ diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java index dd941f34b..59ff1250b 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java @@ -498,6 +498,9 @@ public static class PLAfkSection implements AfkSection { @Description({ " ", "# {TIME} - Czas po którym gracz może użyć komendy" }) public Notice afkDelay = Notice.chat("Możesz użyć tej komendy dopiero po {TIME}!"); + + @Description({ " " }) + public String afkKickReason = "Zostałeś wyrzucone z powodu braku aktywności!"; } @Description({