Skip to content

Commit

Permalink
GH-550 Prevent vanished players being marked as AFK (#550)
Browse files Browse the repository at this point in the history
* Prevent vanished players from being marked as AFK

* Apply review suggestions
  • Loading branch information
rchomczyk authored Oct 16, 2023
1 parent 412ad0b commit 38c5456
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.eternalcode.core.feature.afk;

import com.eternalcode.core.feature.vanish.VanishService;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Task;
import java.util.concurrent.TimeUnit;
Expand All @@ -12,11 +13,13 @@
class AfkTask implements Runnable {

private final AfkService afkService;
private final VanishService vanishService;
private final Server server;

@Inject
public AfkTask(AfkService afkService, Server server) {
public AfkTask(AfkService afkService, VanishService vanishService, Server server) {
this.afkService = afkService;
this.vanishService = vanishService;
this.server = server;
}

Expand All @@ -29,7 +32,7 @@ void markAllInactivePlayers() {
for (Player player : this.server.getOnlinePlayers()) {
UUID playerUuid = player.getUniqueId();

if (this.afkService.isAfk(playerUuid)) {
if (this.afkService.isAfk(playerUuid) || this.vanishService.isVanished(playerUuid)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.eternalcode.core.feature.vanish;

import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Service;
import java.util.UUID;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.metadata.MetadataValue;

@Service
public class VanishService {

private static final String METADATA_VANISHED_KEY = "vanished";
private final Server server;

@Inject
public VanishService(Server server) {
this.server = server;
}

public boolean isVanished(UUID playerUniqueId) {
Player player = this.server.getPlayer(playerUniqueId);
if (player == null) {
return false;
}
return this.isVanished(player);
}

private boolean isVanished(Player player) {
for (MetadataValue isVanished : player.getMetadata(METADATA_VANISHED_KEY)) {
if (isVanished.asBoolean()) {
return true;
}
}
return false;
}
}

0 comments on commit 38c5456

Please sign in to comment.