Testing blocking spawning for afk players

This commit is contained in:
Teriuihi 2023-05-24 03:03:10 +02:00
parent 46e763500f
commit a482064e50
6 changed files with 67 additions and 7 deletions

View File

@ -43,5 +43,5 @@ tasks {
}
dependencies {
implementation("com.alttd:Galaxy-API:1.19-R0.1-SNAPSHOT")
implementation("com.alttd:Galaxy-API:1.19.4-R0.1-SNAPSHOT")
}

View File

@ -47,6 +47,9 @@ public class AFKCheckTimer extends BukkitRunnable {
}
long standingTime = afkPlayer.getStandingTime();
if (!afkPlayer.isAFK() && System.currentTimeMillis() - standingTime > TimeUnit.MINUTES.toMillis(Config.WARNING_TIME)) {
afkPlayer.warnPlayer(player);
}
if (!afkPlayer.isAFK() && System.currentTimeMillis() - standingTime > TimeUnit.MINUTES.toMillis(Config.TOGGLE_TIME)) {
setPlayerAFK(afkPlayer, player);
}
@ -65,6 +68,7 @@ public class AFKCheckTimer extends BukkitRunnable {
private void resetAFKPlayer(Location pastLocation, Player player, AFKPlayer afkPlayer) {
afkPlayer.setPlayerToSphereCenter(pastLocation);
afkPlayer.setStandingTime(System.currentTimeMillis());
afkPlayer.unWarnPlayer(player);
player.setSleepingIgnored(false);
plugin.AFKPlayers.removeEntry(player.getName());
afkPlayer.ResetAFK();

View File

@ -1,6 +1,7 @@
package com.alttd.afkdectector;
import com.alttd.afkdectector.afkplayer.AFKPlayer;
import com.alttd.afkdectector.afkplayer.AFKPlayers;
import com.alttd.afkdectector.command.AFKCheckCommand;
import com.alttd.afkdectector.command.AFKListCommand;
import com.alttd.afkdectector.command.ReloadCommand;
@ -18,9 +19,11 @@ import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
@ -32,8 +35,10 @@ import org.bukkit.scoreboard.Team;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
public class AFKDetector extends JavaPlugin implements Listener {
@ -50,6 +55,7 @@ public class AFKDetector extends JavaPlugin implements Listener {
* AFK players need to be added to a team.
*/
Team AFKPlayers;
private final AFKPlayers afkPlayers = new AFKPlayers();
@Override
public void onEnable() {
@ -83,7 +89,7 @@ public class AFKDetector extends JavaPlugin implements Listener {
public AFKPlayer getPlayer(Player player) {
if (!players.containsKey(player.getUniqueId())) {
players.put(player.getUniqueId(), new AFKPlayer(player, this));
players.put(player.getUniqueId(), new AFKPlayer(player, this, afkPlayers));
}
return players.get(player.getUniqueId());
}
@ -129,7 +135,7 @@ public class AFKDetector extends JavaPlugin implements Listener {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
PlayerAfkTime.put(uuid, getPlayerAFKTime(player));
players.put(uuid, new AFKPlayer(player, this));
players.put(uuid, new AFKPlayer(player, this, afkPlayers));
if (Bukkit.getOnlinePlayers().size() >= Config.PLAYER_LIMIT && !Config.SERVER_FULL) {
fullOverride = true;
}
@ -214,6 +220,17 @@ public class AFKDetector extends JavaPlugin implements Listener {
}
}*/
@EventHandler
public void onMobSpawn(CreatureSpawnEvent event) {
if (!event.getSpawnReason().equals(CreatureSpawnEvent.SpawnReason.NATURAL))
return;
List<UUID> players = event.getEntity().getLocation().getNearbyPlayers(96).stream().map(Player::getUniqueId).collect(Collectors.toList()); //assuming 6 render distance
if (afkPlayers.containsNonAfkPlayer(players)) {
return;
}
event.setCancelled(true);
}
public static AFKDetector getInstance() {
return instance;
}

View File

@ -20,14 +20,17 @@ public class AFKPlayer {
private long standingTime;
private final int afkTime;
private boolean isAFK;
private boolean isWarned;
private final AFKPlayers afkPlayers;
public AFKPlayer(Player player, AFKDetector plugin) {
public AFKPlayer(Player player, AFKDetector plugin, AFKPlayers afkPlayers) {
this.playerName = player.getName();
this.uuid = player.getUniqueId();
this.playerToSphereCenter = player.getLocation();
this.standingTime = System.currentTimeMillis();
this.afkTime = plugin.getAllowedAFKTime(player);
this.isAFK = false;
this.afkPlayers = afkPlayers;
}
public String getPlayerName() {
@ -68,6 +71,7 @@ public class AFKPlayer {
public void setAFK(boolean bool) {
isAFK = bool;
afkPlayers.addAFKPlayer(uuid);
}
public boolean isAFK() {
@ -86,6 +90,18 @@ public class AFKPlayer {
Player player = Bukkit.getPlayer(getPlayerUuid());
playerToSphereCenter = (player == null) ? null : player.getLocation();
isAFK = false;
afkPlayers.removeAFKPlayer(uuid);
}
public void warnPlayer(Player player) {
if (isWarned)
return;
player.sendMessage("You will go afk in a bit (placeholder message)");
isWarned = true;
}
public void unWarnPlayer(Player player) {
player.sendMessage("You won't be going afk for another 7 min (placeholder message)");
isWarned = false;
}
}

View File

@ -0,0 +1,21 @@
package com.alttd.afkdectector.afkplayer;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;
public class AFKPlayers {
private final HashSet<UUID> afkPlayers = new HashSet<>();
public synchronized boolean containsNonAfkPlayer(List<UUID> uuids) {
return uuids.stream().anyMatch(uuid -> !afkPlayers.contains(uuid));
}
public synchronized void addAFKPlayer(UUID uuid) {
afkPlayers.add(uuid);
}
public synchronized void removeAFKPlayer(UUID uuid) {
afkPlayers.remove(uuid);
}
}

View File

@ -35,9 +35,10 @@ public class Config extends AbstractConfiguration {
public static boolean SLEEP_IGNORE = false;
public static int RADIUS = 4;
public static int TOGGLE_TIME = 4;
public static int DEFAULT_AFK_TIME = 5;
public static int MAX_AFK_TIME = 5;
public static int WARNING_TIME = 5;
public static int TOGGLE_TIME = 7;
public static int DEFAULT_AFK_TIME = 30;
public static int MAX_AFK_TIME = 60;
public static boolean SERVER_FULL = false;
public static int PLAYER_LIMIT = Math.round(Bukkit.getMaxPlayers() * 90 / 100);
public static int COMMAND_COOL_DOWN = 60;
@ -48,6 +49,7 @@ public class Config extends AbstractConfiguration {
private static void playerSettings() {
SLEEP_IGNORE = config.getBoolean("player.sleep", SLEEP_IGNORE);
RADIUS = config.getInt("player.radius", RADIUS);
WARNING_TIME = config.getInt("player.warning-time", WARNING_TIME);
TOGGLE_TIME = config.getInt("player.toggle-time", TOGGLE_TIME);
DEFAULT_AFK_TIME = config.getInt("player.afk-time", 5);
MAX_AFK_TIME = config.getInt("player.maxafk-time", 5);