Skip to content

Commit

Permalink
Reduce message dependence on PGM
Browse files Browse the repository at this point in the history
Signed-off-by: applenick <applenick@users.noreply.github.com>
  • Loading branch information
applenick committed Mar 6, 2022
1 parent 7576c67 commit 92afdd3
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions src/main/java/tc/oc/occ/idly/IdlyManager.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
package tc.oc.occ.idly;

import static net.kyori.adventure.key.Key.key;
import static net.kyori.adventure.sound.Sound.sound;
import static net.kyori.adventure.text.Component.text;

import java.time.Duration;
import java.time.Instant;
import javax.annotation.Nullable;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.entity.Player;
import tc.oc.pgm.api.PGM;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.player.MatchPlayer;
import tc.oc.pgm.util.bukkit.OnlinePlayerMapAdapter;
import tc.oc.pgm.util.text.TemporalComponent;

public class IdlyManager {

private final Idly plugin;
private final IdlyConfig config;
private final OnlinePlayerMapAdapter<Instant> playerMovementCache;
private final OnlinePlayerMapAdapter<Instant> playerAfkCache;

public IdlyManager(Idly plugin) {
this.plugin = plugin;
this.config = plugin.getIdlyConfig();
this.playerMovementCache = new OnlinePlayerMapAdapter<Instant>(plugin);
this.playerAfkCache = new OnlinePlayerMapAdapter<Instant>(plugin);
Expand Down Expand Up @@ -64,30 +71,39 @@ private Instant getAFKSince(Player player) {

private boolean checkAFK(MatchPlayer player) {
Match match = player.getMatch();
Audience viewer = plugin.getViewer(player.getBukkit());

if (!isAFK(player.getBukkit())) return false;
if (!match.isRunning()) return false;

Instant afkTime = getAFKSince(player.getBukkit());
Duration timeSinceAfk = Duration.between(afkTime, Instant.now());
long secondsLeft = (config.getKickDelay() - timeSinceAfk.getSeconds()) + 1;
Component observers = text("Observers", NamedTextColor.AQUA);

if (secondsLeft > 0) {
player.sendWarning(
sendWarning(
viewer,
text()
.append(text("You will be moved to "))
.append(match.getDefaultParty().getName())
.append(text(" in "))
.append(TemporalComponent.duration(secondsLeft, NamedTextColor.YELLOW))
.build());
.append(observers)
.append(text(" due to inactivity in "))
.append(text(secondsLeft, NamedTextColor.YELLOW))
.append(text(" second" + (secondsLeft != 1 ? "s" : "")))
.color(NamedTextColor.RED)
.build(),
COUNTDOWN);
} else {
player.sendWarning(
match.setParty(player, match.getDefaultParty());
sendWarning(
viewer,
text()
.append(text("Moved to "))
.append(match.getDefaultParty().getName())
.append(observers)
.append(text(" due to inactivity"))
.build());
match.setParty(player, match.getDefaultParty());
.color(NamedTextColor.RED)
.build(),
KICK);
this.resetMovement(player.getBukkit());
this.removeAFK(player.getBukkit());
}
Expand Down Expand Up @@ -119,6 +135,18 @@ private void checkPlayers() {
}
}

private static final Component WARNING = text(" \u26a0 ", NamedTextColor.YELLOW);
private static final Sound COUNTDOWN = sound(key("random.break"), Sound.Source.MASTER, 1f, 1.15f);
private static final Sound KICK =
sound(key("mob.zombie.woodbreak"), Sound.Source.MASTER, 1f, 1.20f);

private void sendWarning(Audience viewer, Component message, @Nullable Sound sound) {
viewer.sendMessage(text().append(WARNING).append(message));
if (sound != null) {
viewer.playSound(sound);
}
}

private Match getMatch() {
return PGM.get().getMatchManager().getMatches().hasNext()
? PGM.get().getMatchManager().getMatches().next()
Expand Down

0 comments on commit 92afdd3

Please sign in to comment.