Skip to content

Commit

Permalink
GH-681 Add option to kick a player for being AFK (#694)
Browse files Browse the repository at this point in the history
* Init commit

* Reverse if

* Change PL translation

* Update eternalcore-core/src/main/java/com/eternalcode/core/feature/afk/AfkController.java

Co-authored-by: Martin Sulikowski <vLuckyyy.biznes@gmail.com>

* Revert changes

* Follow suggestions

* Make classes private

* Replace Legacy class with built-in LegacyComponentSerializer using static import.

Signed-off-by: Martin Sulikowski <vLuckyyy.biznes@gmail.com>

---------

Signed-off-by: Martin Sulikowski <vLuckyyy.biznes@gmail.com>
Co-authored-by: Martin Sulikowski <vLuckyyy.biznes@gmail.com>
  • Loading branch information
failutee and vLuckyyy authored Feb 19, 2024
1 parent 21afc0c commit 2178a1f
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -31,5 +31,4 @@ void onMove(PlayerMoveEvent event) {
void onQuit(PlayerQuitEvent event) {
this.afkService.clearAfk(event.getPlayer().getUniqueId());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ interface AfkSection {
Notice afkOn();
Notice afkOff();
Notice afkDelay();
String afkKickReason();
}

// event section
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("<red>► <dark_red>You can use this command only after <red>{TIME}!");

@Description({ " " })
public String afkKickReason = "<red>You have been kicked due to inactivity!";
}

@Description({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("<red>► <dark_red>Możesz użyć tej komendy dopiero po <dark_red>{TIME}!");

@Description({ " " })
public String afkKickReason = "<red>Zostałeś wyrzucone z powodu braku aktywności!";
}

@Description({
Expand Down

0 comments on commit 2178a1f

Please sign in to comment.