added afkTag

This commit is contained in:
Elias Müller 2024-11-24 21:13:25 +01:00
parent 5324749a64
commit 7e3b043c98
5 changed files with 114 additions and 3 deletions

View File

@ -0,0 +1,30 @@
package eu.mhsl.craftattack.spawn.appliances.afkTag;
import eu.mhsl.craftattack.spawn.appliance.ApplianceListener;
import io.papermc.paper.event.player.AsyncChatEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
public class AfkResetListener extends ApplianceListener<AfkTag> {
@EventHandler
public void onMove(PlayerMoveEvent event) {
getAppliance().resetTiming(event.getPlayer());
}
@EventHandler
public void onInteract(PlayerInteractEvent event) {
getAppliance().resetTiming(event.getPlayer());
}
@EventHandler
public void onChat(AsyncChatEvent event) {
getAppliance().resetTiming(event.getPlayer());
}
@EventHandler
public void onJoin(PlayerJoinEvent event) {
getAppliance().resetTiming(event.getPlayer());
}
}

View File

@ -0,0 +1,71 @@
package eu.mhsl.craftattack.spawn.appliances.afkTag;
import eu.mhsl.craftattack.spawn.Main;
import eu.mhsl.craftattack.spawn.appliance.Appliance;
import eu.mhsl.craftattack.spawn.appliances.displayName.DisplayName;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.util.Ticks;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.WeakHashMap;
public class AfkTag extends Appliance implements DisplayName.DisplayNamed {
private final WeakHashMap<Player, Long> afkTimings = new WeakHashMap<>();
private static final int updateIntervalSeconds = 30;
private static final int afkWhenMillis = 5 * 60 * 1000;
@Override
public void onEnable() {
Bukkit.getScheduler().runTaskTimerAsynchronously(
Main.instance(),
this::checkAfkPlayers,
Ticks.TICKS_PER_SECOND,
Ticks.TICKS_PER_SECOND * updateIntervalSeconds
);
}
public void resetTiming(Player player) {
boolean wasAfk = isAfk(player);
this.afkTimings.put(player, System.currentTimeMillis());
if (wasAfk) updateAfkPrefix(player);
}
private void checkAfkPlayers() {
this.afkTimings.keySet().forEach((player) -> {
if(!isAfk(player)) return;
updateAfkPrefix(player);
});
}
private boolean isAfk(Player player) {
if(player.isSleeping()) return false;
long lastTimeActive = this.afkTimings.getOrDefault(player, 0L);
long timeSinceLastActive = System.currentTimeMillis() - lastTimeActive;
return timeSinceLastActive >= afkWhenMillis;
}
private void updateAfkPrefix(Player player) {
Main.instance().getAppliance(DisplayName.class).update(player);
}
@Override
public @Nullable Component getNamePrefix(Player p) {
if(isAfk(p)) return Component.text("[\uD83D\uDECC]", NamedTextColor.GRAY)
.hoverEvent(HoverEvent.showText(Component.text("Der Spieler ist AFK")));
return null;
}
@Override
protected @NotNull List<Listener> listeners() {
return List.of(new AfkResetListener());
}
}

View File

@ -4,6 +4,7 @@ import eu.mhsl.craftattack.spawn.Main;
import eu.mhsl.craftattack.spawn.appliance.Appliance;
import eu.mhsl.craftattack.spawn.appliances.adminMarker.AdminMarker;
import eu.mhsl.craftattack.spawn.appliances.adminMarker.AdminMarkerListener;
import eu.mhsl.craftattack.spawn.appliances.afkTag.AfkTag;
import eu.mhsl.craftattack.spawn.appliances.outlawed.Outlawed;
import eu.mhsl.craftattack.spawn.appliances.yearRank.YearRank;
import net.kyori.adventure.text.Component;
@ -13,17 +14,23 @@ import net.kyori.adventure.text.format.TextColor;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.function.Supplier;
import java.util.logging.Level;
public class DisplayName extends Appliance {
public interface DisplayNamed {
@Nullable Component getNamePrefix(Player p);
}
public void update(Player player) {
TextColor playerColor = queryAppliance(AdminMarker.class).getPlayerColor(player);
List<Supplier<Component>> prefixes = List.of(
() -> queryAppliance(Outlawed.class).getNamePrefix(player),
() -> queryAppliance(YearRank.class).getNamePrefix(player)
() -> queryAppliance(YearRank.class).getNamePrefix(player),
() -> queryAppliance(AfkTag.class).getNamePrefix(player)
);
ComponentBuilder<TextComponent, TextComponent.Builder> playerName = Component.text();

View File

@ -17,7 +17,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.*;
public class Outlawed extends Appliance {
public class Outlawed extends Appliance implements DisplayName.DisplayNamed {
public final int timeoutInMs = 1000 * 60 * 60 * 6;
private final Map<UUID, Long> timeouts = new HashMap<>();
@ -110,6 +110,7 @@ public class Outlawed extends Appliance {
};
}
@Override
public Component getNamePrefix(Player player) {
if(isOutlawed(player)) {
return Component.text("[☠]", NamedTextColor.RED)

View File

@ -3,6 +3,7 @@ package eu.mhsl.craftattack.spawn.appliances.yearRank;
import eu.mhsl.craftattack.spawn.Main;
import eu.mhsl.craftattack.spawn.appliance.Appliance;
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
import eu.mhsl.craftattack.spawn.appliances.displayName.DisplayName;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
@ -15,7 +16,7 @@ import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.*;
public class YearRank extends Appliance {
public class YearRank extends Appliance implements DisplayName.DisplayNamed {
record CraftAttackYear(String name) {}
private final Map<UUID, List<CraftAttackYear>> rankMap = new HashMap<>();
@ -44,6 +45,7 @@ public class YearRank extends Appliance {
});
}
@Override
public @Nullable Component getNamePrefix(Player player) {
if(!rankMap.containsKey(player.getUniqueId())) return null;
int yearCount = rankMap.get(player.getUniqueId()).size();